TlsSlot

Files CCore/inc/TlsSlot.h CCore/src/TlsSlot.cpp

TlsSlot is designed to provide a thread local variables. It is integrated with the PlanInit subsystem.


class TlsSlot
 {
   .... 

  public:
  
   explicit TlsSlot(const char *tag);
   
   ~TlsSlot();
  
   void set(void *value);
  
   void * get();
 };

Don't use objects of this class directly! Here is the pattern:


struct MySlot : TlsSlot
 {
  MySlot() : TlsSlot("MySlot") {}
 };
 
MySlot MySlotObject CCORE_INITPRI_1 ;

PlanInitNode * GetPlanInitNode_MySlot() { return &MySlotObject; }

void SomeFunc()
 {
  void *tls=MySlotObject->get();
 }

TlsSlot value is a thread-specific, i.e. depends on the calling thread. Each thread has own private copy of this value. The initial value is 0. If the TlsSlot constructor is unable to allocate a TLS slot, it aborts execution.

You should avoid using TlsSlot, leave it to the CCore internals. There is a better way to define a thread-specific variable: when you start a thread, using the Task object and functions, you always can find the current task by the Task::GetCurrent() function. So just add a required thread context to the Task object. You can also use the PerTask tool.