nlib
Tls.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_THREADING_TLS_H_
4 #define INCLUDE_NN_NLIB_THREADING_TLS_H_
5 
6 #include "nn/nlib/Config.h"
7 
8 NLIB_NAMESPACE_BEGIN
9 namespace threading {
10 
11 template <class T>
12 class Tls NLIB_FINAL {
13  public:
14  NLIB_CEXPR Tls() NLIB_NOEXCEPT : m_IdxValid(false) {}
16  if (m_IdxValid) return EALREADY;
17  errno_t e = nlib_tls_alloc(&m_Idx, destr_func);
18  if (e == 0) m_IdxValid = true;
19  return e;
20  }
22  if (m_IdxValid) {
23  nlib_tls_free(m_Idx);
24  m_IdxValid = false;
25  }
26  }
27  T* Get() const NLIB_NOEXCEPT {
28  void* p;
29  errno_t e = nlib_tls_getvalue(m_Idx, &p);
30  if (e != 0) return NULL;
31  return reinterpret_cast<T*>(p);
32  }
33  T* operator->() const NLIB_NOEXCEPT { return this->Get(); }
34  T& operator*() const NLIB_NOEXCEPT { return *this->Get(); }
36  T* obj = this->Get();
37  nlib_tls_setvalue(m_Idx, NULL);
38  return obj;
39  }
41  T* obj = this->Get();
42  if (obj) {
43  delete obj;
44  }
45  return nlib_tls_setvalue(m_Idx, p);
46  }
47 
48  private:
49  static void destr_func(void* p) NLIB_NOEXCEPT {
50  delete reinterpret_cast<T*>(p);
51  }
52  nlib_tls m_Idx;
53  bool m_IdxValid;
54 };
55 
56 } // namespace threading
57 NLIB_NAMESPACE_END
58 
59 #endif // INCLUDE_NN_NLIB_THREADING_TLS_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
errno_t Reset(T *p=0) noexcept
Releases the pointer to the specified thread.
Definition: Tls.h:40
T * Release() noexcept
Releases the pointer to the specified thread.
Definition: Tls.h:35
T * Get() const noexcept
Gets a pointer to the specified thread.
Definition: Tls.h:27
errno_t nlib_tls_setvalue(nlib_tls tls, const void *value)
Stores a value in a TLS slot.
errno_t Init() noexcept
Initializes thread local storage.
Definition: Tls.h:15
pthread_key_t nlib_tls
The type for TLS slot IDs.
Definition: Platform.h:571
NLIB_CHECK_RESULT errno_t nlib_tls_alloc(nlib_tls *tls, nlib_tls_destructor destr)
Allocates a new ID for the specified TLS slot.
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Class for handling thread local storage.
Definition: Tls.h:12
constexpr Tls() noexcept
Instantiates the object with default parameters (default constructor).
Definition: Tls.h:14
~Tls() noexcept
Destructor. The pointer will be deleted.
Definition: Tls.h:21
A file that contains the configuration information for each development environment.
T & operator*() const noexcept
Dereferences the pointer.
Definition: Tls.h:34
T * operator->() const noexcept
Returns the pointer.
Definition: Tls.h:33
errno_t nlib_tls_getvalue(nlib_tls tls, void **value)
Gets the value from a TLS slot.
errno_t nlib_tls_free(nlib_tls tls)
Frees the ID corresponding to the TLS slot.
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24