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:
30  if (idx_ != NLIB_TLS_INVALID) return EALREADY;
31  return nlib_tls_alloc(&idx_, destr_func);
32  }
34  if (idx_ != NLIB_TLS_INVALID) nlib_tls_free(idx_);
35  }
36 #ifdef __cpp_rvalue_references
37  Tls(Tls&& rhs) NLIB_NOEXCEPT : idx_(rhs.idx_) { rhs.idx_ = NLIB_TLS_INVALID; }
38  Tls& operator=(Tls&& rhs) NLIB_NOEXCEPT {
39  idx_ = rhs.idx_;
40  rhs.idx_ = NLIB_TLS_INVALID;
41  return *this;
42  }
43 #endif
44  Tls(Tls& rhs, move_tag) NLIB_NOEXCEPT {
45  idx_ = rhs.idx_;
46  rhs.idx_ = NLIB_TLS_INVALID;
47  }
48  Tls& assign(Tls& rhs, move_tag) NLIB_NOEXCEPT {
49  idx_ = rhs.idx_;
50  rhs.idx_ = NLIB_TLS_INVALID;
51  return *this;
52  }
53  T* Get() const NLIB_NOEXCEPT {
54  void* p;
55  errno_t e = nlib_tls_getvalue(idx_, &p);
56  if (e != 0) return nullptr;
57  return reinterpret_cast<T*>(p);
58  }
59  T* operator->() const NLIB_NOEXCEPT { return this->Get(); }
60  T& operator*() const NLIB_NOEXCEPT { return *this->Get(); }
62  T* obj = this->Get();
63  nlib_tls_setvalue(idx_, nullptr);
64  return obj;
65  }
67  T* obj = this->Get();
68  if (obj) {
69  delete obj;
70  }
71  return nlib_tls_setvalue(idx_, p);
72  }
73 
74  private:
75  static void destr_func(void* p) NLIB_NOEXCEPT { delete reinterpret_cast<T*>(p); }
76  nlib_tls idx_;
78 };
79 
80 } // namespace threading
81 NLIB_NAMESPACE_END
82 
83 NLIB_DEFINE_STD_SWAP_T_BEGIN3(nn, nlib, threading)
84 NLIB_DEFINE_STD_SWAP_T1(T, NLIB_NS::threading::Tls)
85 NLIB_DEFINE_STD_SWAP_T_END3(nn, nlib, threading)
86 
87 #endif // INCLUDE_NN_NLIB_THREADING_TLS_H_
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:183
errno_t Reset(T *p=0) noexcept
設定したスレッド固有のポインタを置き換えます。
Definition: Tls.h:66
T & operator*() const noexcept
ポインタの参照外しを行います。
Definition: Tls.h:60
Definition: Base64.h:25
T * Release() noexcept
設定したスレッド固有のポインタを解放します。
Definition: Tls.h:61
T * operator->() const noexcept
ポインタを返します。
Definition: Tls.h:59
errno_t nlib_tls_setvalue(nlib_tls tls, const void *value)
TLSスロットに値を格納します。
T * Get() const noexcept
設定したスレッド固有のポインタを取得します。
Definition: Tls.h:53
errno_t Init() noexcept
スレッドローカルストレージを初期化します。
Definition: Tls.h:29
pthread_key_t nlib_tls
TLSスロットのIDを示す型です。
errno_t nlib_tls_alloc(nlib_tls *tls, nlib_tls_destructor destr)
TLSスロットに対する新しいIDを確保します。
スレッドローカルストレージを扱うためのクラスです。
Definition: Tls.h:26
constexpr Tls() noexcept
デフォルトコンストラクタです。
Definition: Tls.h:28
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:109
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
Definition: Config.h:111
~Tls() noexcept
デストラクタです。ポインタはdeleteされます。
Definition: Tls.h:33
開発環境別の設定が書かれるファイルです。
#define NLIB_TLS_INVALID
無効なTLSスロットのIDを指し示す値
Definition: Platform.h:559
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:250
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