BitShares-Core  6.1.0
BitShares blockchain implementation and command-line interface 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
void set_task_specific_data(unsigned slot, void *new_value, void(*cleanup)(void *))
unsigned get_next_unused_task_storage_slot()
void * get_task_specific_data(unsigned slot)
fc::context * current
Definition: thread_d.hpp:106
boost::atomic< unsigned > thread_specific_slot_counter
unsigned next_unused_task_storage_slot
Definition: thread_d.hpp:120
static thread & current()
Definition: thread.cpp:125
void * get_thread_specific_data(unsigned slot)
task_base * cur_task
Definition: context.hpp:200
void set_specific_data(std::vector< detail::specific_data_info > *specific_data, unsigned slot, void *new_value, void(*cleanup)(void *))
void * get_specific_data(std::vector< detail::specific_data_info > *specific_data, unsigned slot)
std::vector< detail::specific_data_info > * _task_specific_data
Definition: task.hpp:68
unsigned get_next_unused_thread_storage_slot()
Definition: api.hpp:15
void set_thread_specific_data(unsigned slot, void *new_value, void(*cleanup)(void *))