nlib
Tls.h
Go to the documentation of this file.
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_THREADING_TLS_H_
17 #define INCLUDE_NN_NLIB_THREADING_TLS_H_
18 
19 #include "nn/nlib/Config.h"
20 #include "nn/nlib/Swap.h"
21 
22 NLIB_NAMESPACE_BEGIN
23 namespace threading {
24 
25 template <class T>
26 class Tls NLIB_FINAL {
27  public:
29  : idx_(NLIB_TLS_INVALID) {}
31  if (idx_ != NLIB_TLS_INVALID) return EALREADY;
32  return nlib_tls_alloc(&idx_, destr_func);
33  }
35  if (idx_ != NLIB_TLS_INVALID) nlib_tls_free(idx_);
36  }
37 #ifdef __cpp_rvalue_references
38  Tls(Tls&& rhs) NLIB_NOEXCEPT
39  : idx_(rhs.idx_) {
40  rhs.idx_ = NLIB_TLS_INVALID;
41  }
42  Tls& operator=(Tls&& rhs) NLIB_NOEXCEPT {
43  idx_ = rhs.idx_;
44  rhs.idx_ = NLIB_TLS_INVALID;
45  return *this;
46  }
47 #endif
48  Tls(Tls& rhs, move_tag) NLIB_NOEXCEPT { // NOLINT
49  idx_ = rhs.idx_;
50  rhs.idx_ = NLIB_TLS_INVALID;
51  }
52  Tls& assign(Tls& rhs, move_tag) NLIB_NOEXCEPT { // NOLINT
53  idx_ = rhs.idx_;
54  rhs.idx_ = NLIB_TLS_INVALID;
55  return *this;
56  }
57  T* Get() const NLIB_NOEXCEPT {
58  void* p;
59  errno_t e = nlib_tls_getvalue(idx_, &p);
60  if (e != 0) return nullptr;
61  return reinterpret_cast<T*>(p);
62  }
63  T* operator->() const NLIB_NOEXCEPT { return this->Get(); }
64  T& operator*() const NLIB_NOEXCEPT { return *this->Get(); }
66  T* obj = this->Get();
67  nlib_tls_setvalue(idx_, nullptr);
68  return obj;
69  }
71  T* obj = this->Get();
72  if (obj) {
73  delete obj;
74  }
75  return nlib_tls_setvalue(idx_, p);
76  }
77 
78  private:
79  static void destr_func(void* p) NLIB_NOEXCEPT {
80  delete reinterpret_cast<T*>(p);
81  }
82  nlib_tls idx_;
84 };
85 
86 } // namespace threading
87 NLIB_NAMESPACE_END
88 
89 NLIB_DEFINE_STD_SWAP_T_BEGIN3(nn, nlib, threading) // NOLINT
90 NLIB_DEFINE_STD_SWAP_T1(T, NLIB_NS::threading::Tls) // NOLINT
91 NLIB_DEFINE_STD_SWAP_T_END3(nn, nlib, threading) // NOLINT
92 
93 #endif // INCLUDE_NN_NLIB_THREADING_TLS_H_
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:179
errno_t Reset(T *p=0) noexcept
Releases the pointer to the specified thread.
Definition: Tls.h:70
T & operator*() const noexcept
Dereferences the pointer.
Definition: Tls.h:64
Definition: Base64.h:25
T * Release() noexcept
Releases the pointer to the specified thread.
Definition: Tls.h:65
T * operator->() const noexcept
Returns the pointer.
Definition: Tls.h:63
errno_t nlib_tls_setvalue(nlib_tls tls, const void *value)
Stores a value in a TLS slot.
T * Get() const noexcept
Gets a pointer to the specified thread.
Definition: Tls.h:57
errno_t Init() noexcept
Initializes thread local storage.
Definition: Tls.h:30
pthread_key_t nlib_tls
The type for TLS slot IDs.
errno_t nlib_tls_alloc(nlib_tls *tls, nlib_tls_destructor destr)
Allocates a new ID for the specified TLS slot.
An empty structure indicating that an argument to a function needs to be moved.
Definition: Config.h:265
Class for handling thread local storage.
Definition: Tls.h:26
constexpr Tls() noexcept
Instantiates the object with default parameters (default constructor).
Definition: Tls.h:28
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:105
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Definition: Config.h:107
~Tls() noexcept
Destructor. The pointer will be deleted.
Definition: Tls.h:34
A file that contains the configuration information for each development environment.
#define NLIB_TLS_INVALID
Value indicating the ID of an invalid TLS slot.
Definition: Platform.h:563
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:245
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:37