Baselib_ThreadLocalStorage.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #pragma once
  2. // Baselib_ThreadLocalStorage
  3. // Thread-local storage (TLS) is a computer programming method that uses static or global memory local to a thread.
  4. //
  5. // TLS is used in some places where ordinary, single-threaded programs would use global variables, but where this would be inappropriate
  6. // in multithreaded cases. An example of such situations is where functions use a global variable to set an error condition
  7. // (for example the global variable errno used by many functions of the C library). If errno were a global variable,
  8. // a call of a system function on one thread may overwrite the value previously set by a call of a system function on a different thread,
  9. // possibly before following code on that different thread could check for the error condition. The solution is to have errno be a variable
  10. // that looks like it is global, but in fact exists once per thread—i.e., it lives in thread-local storage. A second use case would be
  11. // multiple threads accumulating information into a global variable. To avoid a race condition, every access to this global variable would
  12. // have to be protected by a mutex. Alternatively, each thread might accumulate into a thread-local variable (that, by definition,
  13. // cannot be read from or written to from other threads, implying that there can be no race conditions). Threads then only have to synchronise
  14. // a final accumulation from their own thread-local variable into a single, truly global variable.
  15. //
  16. // Many systems impose restrictions on the size of the thread-local memory block, in fact often rather tight limits.
  17. // On the other hand, if a system can provide at least a memory address (pointer) sized variable thread-local, then this allows the use of
  18. // arbitrarily sized memory blocks in a thread-local manner, by allocating such a memory block dynamically and storing the memory address of
  19. // that block in the thread-local variable.
  20. //
  21. // "Thread-local storage", Wikipedia: The Free Encyclopedia
  22. // https://en.wikipedia.org/w/index.php?title=Thread-local_storage&oldid=860347814
  23. #ifdef __cplusplus
  24. BASELIB_C_INTERFACE
  25. {
  26. #endif
  27. // It's guaranteed that we can allocate at least Baselib_TLS_MinimumGuaranteedSlots values on all platforms.
  28. static const uint32_t Baselib_TLS_MinimumGuaranteedSlots = 100;
  29. // Thread Local Storage slot handle.
  30. typedef uintptr_t Baselib_TLS_Handle;
  31. // Allocates a new Thread Local Storage slot. In case of an error, abort with Baselib_ErrorCode_OutOfSystemResources will be triggered.
  32. // On some platforms this might be fiber local storage.
  33. //
  34. // The value of a newly create Thread Local Storage slot is guaranteed to be zero on all threads.
  35. BASELIB_API Baselib_TLS_Handle Baselib_TLS_Alloc(void);
  36. // Frees provided Thread Local Storage slot.
  37. BASELIB_API void Baselib_TLS_Free(Baselib_TLS_Handle handle);
  38. // Sets value to Thread Local Storage slot.
  39. BASELIB_FORCEINLINE_API void Baselib_TLS_Set(Baselib_TLS_Handle handle, uintptr_t value);
  40. // Gets value from Thread Local Storage slot.
  41. //
  42. // If called on just initialized variable, guaranteed to return 0.
  43. BASELIB_FORCEINLINE_API uintptr_t Baselib_TLS_Get(Baselib_TLS_Handle handle);
  44. #ifdef __cplusplus
  45. } // BASELIB_C_INTERFACE
  46. #endif
  47. #include <C/Baselib_ThreadLocalStorage.inl.h>