HCore SysTlsSlot

Files CCore/inc/sys/SysTlsSlot.h CCore/src/sys/SysTlsSlot.cpp

This part provides the OS backed thread local storage support.


#ifndef CCore_inc_sys_SysTlsSlot_h
#define CCore_inc_sys_SysTlsSlot_h

#include <CCore/inc/sys/SysError.h>
 
namespace CCore {
namespace Sys {

/* classes */ 

struct TlsSlot;

/* struct TlsSlot */ 

struct TlsSlot
 {
  // private

  ....

  // public
  
  ErrorType init() noexcept;
  
  void exit() noexcept;
  
  void * get() noexcept;
  
  void set(void *value) noexcept;
 };
 
} // namespace Sys
} // namespace CCore
 
#endif

To allocate a system TLS slot you create an instance of the structure TlsSlot and initialize it. Then you can use methods set() and get() to access a thread variable in this slot of the type void *. The initial value of this variable is null. Each thread of execution has its own independent copy of this variable. Finally, you uninitialize the instance. Copying of the instances is prohibited. The private content of the structure is target-dependent. TLS methods, except init(), do not produce errors. They must be successful or call abort.

init() initialize the TLS slot. The error code is returned. If the object is successfully initialized, it must be uninitialized by the method exit().

exit() uninitialize the TLS slot.

get() gets the value of the thread variable.

set() sets the value of the thread variable.

Internally, TlsSlot may contain only some "TLS index" and use global methods to deal with it:


struct TlsSlot
 {
  // private data
  
  using Type = .... ;
 
  Type index;
  
  // private
  
  struct AllocType
   {
    Type index;
    ErrorType error;
   };
   
  static AllocType Alloc() noexcept;
  
  static void Free(Type index) noexcept;
  
  static void * Get(Type index) noexcept;
  
  static void Set(Type index,void *value) noexcept;
  
  // public
  
  ErrorType init()
   {
    AllocType result=Alloc();
    
    index=result.index;
    
    return result.error;
   }
  
  void exit() { Free(index); }
  
  void * get() { return Get(index); }
  
  void set(void *value) { Set(index,value); }
 };