gwnavruntime/basesystem/threadlocalstorage.h Source File

threadlocalstorage.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 
8 #ifndef Navigation_ThreadLocalStorage_H
9 #define Navigation_ThreadLocalStorage_H
10 
12 
13 #if defined (KY_OS_XBOX360)
14 #define KY_THREAD_LOCAL __declspec(thread)
15 #elif defined (KY_OS_WII)
16 #define KY_THREAD_LOCAL
17 #elif defined (KY_OS_MAC) || defined (KY_OS_IPHONE)
18 #define KY_THREAD_LOCAL
19 #include <pthread.h>
20 #else
21 #define KY_THREAD_LOCAL __thread
22 #endif
23 
24 
25 namespace Kaim
26 {
27 
28 
29 //-----------------------------------------------------------------------------------
30 // KY_OS_WIN32
31 //-----------------------------------------------------------------------------------
32 #if defined (KY_OS_WIN32)
33 
34 class WinTls
35 {
36 public:
37  static unsigned long KyTlsAlloc();
38  static void* KyTlsGetValue(unsigned long dwTlsIndex);
39  static int KyTlsSetValue(unsigned long dwTlsIndex, void* lpTlsValue);
40  static int KyTlsFree(unsigned long dwTlsIndex);
41 };
42 
43 
44 template<typename ptrT> class ThreadLocalStorageWrapper
45 {
46  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
47 public:
48  ThreadLocalStorageWrapper() { m_tlsIdx = (KyUInt32)WinTls::KyTlsAlloc(); }
49  ~ThreadLocalStorageWrapper() { WinTls::KyTlsFree(m_tlsIdx); }
50  inline ptrT Get() { return (ptrT)WinTls::KyTlsGetValue(m_tlsIdx); }
51  inline void Set(ptrT ptr) { WinTls::KyTlsSetValue(m_tlsIdx, (void*)ptr); }
52 private:
53  KyUInt32 m_tlsIdx;
54 };
55 
56 //-----------------------------------------------------------------------------------
57 // KY_OS_MAC
58 //-----------------------------------------------------------------------------------
59 #elif defined (KY_OS_MAC) || defined (KY_OS_IPHONE)
60 
61 template<typename ptrT> class ThreadLocalStorageWrapper
62 {
63  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
64 public:
65  ThreadLocalStorageWrapper() { pthread_key_create(&m_key, KY_NULL); }
66  ~ThreadLocalStorageWrapper() { pthread_key_delete(m_key); }
67  inline ptrT Get() { return (ptrT) pthread_getspecific(m_key); }
68  inline void Set(ptrT ptr) { pthread_setspecific(m_key, (void*)ptr ); }
69 private:
70  pthread_key_t m_key;
71 };
72 
73 
74 //-----------------------------------------------------------------------------------
75 // !KY_OS_WIN32 !KY_OS_MAC
76 //-----------------------------------------------------------------------------------
77 #else
78 
79 template<typename ptrT> class ThreadLocalStorageWrapper
80 {
81  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
82 public:
83  ThreadLocalStorageWrapper() {}
84  ~ThreadLocalStorageWrapper() {}
85  inline ptrT Get() { return TlsInstance(); }
86  inline void Set(ptrT ptr) { TlsInstance() = ptr; }
87 private:
88  inline ptrT& TlsInstance() { static KY_THREAD_LOCAL ptrT s_ptr; return s_ptr; }
89 };
90 
91 #endif
92 
93 
94 } // namespace Kaim
95 
96 
97 #endif
#define KY_NULL
Null value.
Definition: types.h:247
Definition: gamekitcrowddispersion.h:20
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36