BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
thread_specific.cpp
Go to the documentation of this file.
1 #include <fc/log/logger.hpp>
3 #include "thread_d.hpp"
4 #include <boost/atomic.hpp>
5 
6 namespace fc
7 {
8  namespace detail
9  {
10  boost::atomic<unsigned> thread_specific_slot_counter;
12  {
13  return thread_specific_slot_counter.fetch_add(1);
14  }
15 
16  void* get_specific_data(std::vector<detail::specific_data_info> *specific_data, unsigned slot)
17  {
18  return slot < specific_data->size() ?
19  (*specific_data)[slot].value : nullptr;
20  }
21  void set_specific_data(std::vector<detail::specific_data_info> *specific_data, unsigned slot, void* new_value, void(*cleanup)(void*))
22  {
23  if (slot + 1 > specific_data->size())
24  specific_data->resize(slot + 1);
25  (*specific_data)[slot] = detail::specific_data_info(new_value, cleanup);
26  }
27 
28  void* get_thread_specific_data(unsigned slot)
29  {
30  return get_specific_data(&thread::current().my->thread_specific_data, slot);
31  }
32  void set_thread_specific_data(unsigned slot, void* new_value, void(*cleanup)(void*))
33  {
34  return set_specific_data(&thread::current().my->thread_specific_data, slot, new_value, cleanup);
35  }
36 
38  {
40  }
41  void* get_task_specific_data(unsigned slot)
42  {
43  context* current_context = thread::current().my->current;
44  if (!current_context ||
45  !current_context->cur_task)
46  return get_specific_data(&thread::current().my->non_task_specific_data, slot);
47  if (current_context->cur_task->_task_specific_data)
48  return get_specific_data(current_context->cur_task->_task_specific_data, slot);
49  return nullptr;
50  }
51  void set_task_specific_data(unsigned slot, void* new_value, void(*cleanup)(void*))
52  {
53  context* current_context = thread::current().my->current;
54  if (!current_context ||
55  !current_context->cur_task)
56  set_specific_data(&thread::current().my->non_task_specific_data, slot, new_value, cleanup);
57  else
58  {
59  if (!current_context->cur_task->_task_specific_data)
60  current_context->cur_task->_task_specific_data = new std::vector<detail::specific_data_info>;
61  set_specific_data(current_context->cur_task->_task_specific_data, slot, new_value, cleanup);
62  }
63  }
64  }
65 } // end namespace fc
fc::context::cur_task
task_base * cur_task
Definition: context.hpp:200
fc::thread::current
static thread & current()
Definition: thread.cpp:125
fc::detail::get_task_specific_data
void * get_task_specific_data(unsigned slot)
Definition: thread_specific.cpp:41
thread_specific.hpp
fc::detail::set_task_specific_data
void set_task_specific_data(unsigned slot, void *new_value, void(*cleanup)(void *))
Definition: thread_specific.cpp:51
fc::detail::get_next_unused_task_storage_slot
unsigned get_next_unused_task_storage_slot()
Definition: thread_specific.cpp:37
fc
Definition: api.hpp:15
fc::thread_d::current
fc::context * current
Definition: thread_d.hpp:106
thread_d.hpp
fc::context
Definition: context.hpp:47
fc::detail::get_thread_specific_data
void * get_thread_specific_data(unsigned slot)
Definition: thread_specific.cpp:28
fc::thread_d::next_unused_task_storage_slot
unsigned next_unused_task_storage_slot
Definition: thread_d.hpp:120
fc::detail::specific_data_info
Definition: task.hpp:15
fc::detail::thread_specific_slot_counter
boost::atomic< unsigned > thread_specific_slot_counter
Definition: thread_specific.cpp:10
fc::detail::get_specific_data
void * get_specific_data(std::vector< detail::specific_data_info > *specific_data, unsigned slot)
Definition: thread_specific.cpp:16
fc::detail::get_next_unused_thread_storage_slot
unsigned get_next_unused_thread_storage_slot()
Definition: thread_specific.cpp:11
fc::detail::set_specific_data
void set_specific_data(std::vector< detail::specific_data_info > *specific_data, unsigned slot, void *new_value, void(*cleanup)(void *))
Definition: thread_specific.cpp:21
logger.hpp
fc::detail::set_thread_specific_data
void set_thread_specific_data(unsigned slot, void *new_value, void(*cleanup)(void *))
Definition: thread_specific.cpp:32
fc::task_base::_task_specific_data
std::vector< detail::specific_data_info > * _task_specific_data
Definition: task.hpp:68