nlib
Tls.h
[詳解]
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)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:179
errno_t Reset(T *p=0) noexcept
設定したスレッド固有のポインタを置き換えます。
Definition: Tls.h:70
T & operator*() const noexcept
ポインタの参照外しを行います。
Definition: Tls.h:64
Definition: Base64.h:25
T * Release() noexcept
設定したスレッド固有のポインタを解放します。
Definition: Tls.h:65
T * operator->() const noexcept
ポインタを返します。
Definition: Tls.h:63
errno_t nlib_tls_setvalue(nlib_tls tls, const void *value)
TLSスロットに値を格納します。
T * Get() const noexcept
設定したスレッド固有のポインタを取得します。
Definition: Tls.h:57
errno_t Init() noexcept
スレッドローカルストレージを初期化します。
Definition: Tls.h:30
pthread_key_t nlib_tls
TLSスロットのIDを示す型です。
errno_t nlib_tls_alloc(nlib_tls *tls, nlib_tls_destructor destr)
TLSスロットに対する新しいIDを確保します。
空の構造体で、関数の引数をムーブすべきことを示すために利用されます。
Definition: Config.h:265
スレッドローカルストレージを扱うためのクラスです。
Definition: Tls.h:26
constexpr Tls() noexcept
デフォルトコンストラクタです。
Definition: Tls.h:28
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:105
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
Definition: Config.h:107
~Tls() noexcept
デストラクタです。ポインタはdeleteされます。
Definition: Tls.h:34
開発環境別の設定が書かれるファイルです。
#define NLIB_TLS_INVALID
無効なTLSスロットのIDを指し示す値
Definition: Platform.h:563
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:245
errno_t nlib_tls_getvalue(nlib_tls tls, void **value)
TLSスロットから値を取り出します。
errno_t nlib_tls_free(nlib_tls tls)
TLSスロットに対応するIDを解放します。
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37