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 
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
設定したスレッド固有のポインタを置き換えます。
Definition: Tls.h:53
T & operator*() const noexcept
ポインタの参照外しを行います。
Definition: Tls.h:47
T * Release() noexcept
設定したスレッド固有のポインタを解放します。
Definition: Tls.h:48
T * operator->() const noexcept
ポインタを返します。
Definition: Tls.h:46
errno_t nlib_tls_setvalue(nlib_tls tls, const void *value)
TLSスロットに値を格納します。
T * Get() const noexcept
設定したスレッド固有のポインタを取得します。
Definition: Tls.h:40
errno_t Init() noexcept
スレッドローカルストレージを初期化します。
Definition: Tls.h:28
pthread_key_t nlib_tls
TLSスロットのIDを示す型です。
NLIB_CHECK_RESULT errno_t nlib_tls_alloc(nlib_tls *tls, nlib_tls_destructor destr)
TLSスロットに対する新しいIDを確保します。
スレッドローカルストレージを扱うためのクラスです。
Definition: Tls.h:25
constexpr Tls() noexcept
デフォルトコンストラクタです。
Definition: Tls.h:27
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:99
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
Definition: Config.h:93
~Tls() noexcept
デストラクタです。ポインタはdeleteされます。
Definition: Tls.h:34
開発環境別の設定が書かれるファイルです。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:229
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