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 
21 NLIB_NAMESPACE_BEGIN
22 namespace threading {
23 
24 template <class T>
25 class Tls NLIB_FINAL {
26  public:
27  NLIB_CEXPR Tls() NLIB_NOEXCEPT : idx_(), is_idx_valid_(false) {}
29  if (is_idx_valid_) return EALREADY;
30  errno_t e = nlib_tls_alloc(&idx_, destr_func);
31  if (e == 0) is_idx_valid_ = true;
32  return e;
33  }
35  if (is_idx_valid_) {
36  nlib_tls_free(idx_);
37  is_idx_valid_ = false;
38  }
39  }
40  T* Get() const NLIB_NOEXCEPT {
41  void* p;
42  errno_t e = nlib_tls_getvalue(idx_, &p);
43  if (e != 0) return NULL;
44  return reinterpret_cast<T*>(p);
45  }
46  T* operator->() const NLIB_NOEXCEPT { return this->Get(); }
47  T& operator*() const NLIB_NOEXCEPT { return *this->Get(); }
49  T* obj = this->Get();
50  nlib_tls_setvalue(idx_, NULL);
51  return obj;
52  }
54  T* obj = this->Get();
55  if (obj) {
56  delete obj;
57  }
58  return nlib_tls_setvalue(idx_, p);
59  }
60 
61  private:
62  static void destr_func(void* p) NLIB_NOEXCEPT {
63  delete reinterpret_cast<T*>(p);
64  }
65  nlib_tls idx_;
66  bool is_idx_valid_;
67 };
68 
69 } // namespace threading
70 NLIB_NAMESPACE_END
71 
72 #endif // INCLUDE_NN_NLIB_THREADING_TLS_H_
errno_t Reset(T *p=0) noexcept
Releases the pointer to the specified thread.
Definition: Tls.h:53
T & operator*() const noexcept
Dereferences the pointer.
Definition: Tls.h:47
T * Release() noexcept
Releases the pointer to the specified thread.
Definition: Tls.h:48
T * operator->() const noexcept
Returns the pointer.
Definition: Tls.h:46
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:40
errno_t Init() noexcept
Initializes thread local storage.
Definition: Tls.h:28
pthread_key_t nlib_tls
The type for TLS slot IDs.
NLIB_CHECK_RESULT errno_t nlib_tls_alloc(nlib_tls *tls, nlib_tls_destructor destr)
Allocates a new ID for the specified TLS slot.
Class for handling thread local storage.
Definition: Tls.h:25
constexpr Tls() noexcept
Instantiates the object with default parameters (default constructor).
Definition: Tls.h:27
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:99
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Definition: Config.h:93
~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_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:229
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