threadStatic.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _TORQUETHREADSTATIC_H_
  23. #define _TORQUETHREADSTATIC_H_
  24. #include "core/util/tVector.h"
  25. //-----------------------------------------------------------------------------
  26. // TorqueThreadStatic Base Class
  27. class _TorqueThreadStatic
  28. {
  29. friend class _TorqueThreadStaticReg;
  30. private:
  31. #ifdef TORQUE_ENABLE_THREAD_STATIC_METRICS
  32. U32 mHitCount;
  33. #endif
  34. protected:
  35. static U32 mListIndex;
  36. virtual _TorqueThreadStatic *_createInstance() const = 0;
  37. public:
  38. _TorqueThreadStatic()
  39. #ifdef TORQUE_ENABLE_THREAD_STATIC_METRICS
  40. : mHitCount( 0 )
  41. #endif
  42. { };
  43. static const U32 getListIndex(){ return mListIndex; }
  44. virtual void *getMemInstPtr() = 0;
  45. virtual const dsize_t getMemInstSize() const = 0;
  46. #ifdef TORQUE_ENABLE_THREAD_STATIC_METRICS
  47. _TorqueThreadStatic *_chainHit() { mHitCount++; return this; }
  48. const U32 &trackHit() { return ++mHitCount; }
  49. const U32 &getHitCount() const { return mHitCount; }
  50. #endif
  51. };
  52. // Typedef
  53. typedef VectorPtr<_TorqueThreadStatic *> TorqueThreadStaticList;
  54. typedef TorqueThreadStaticList * TorqueThreadStaticListHandle;
  55. //-----------------------------------------------------------------------------
  56. // Auto-registration class and manager of the instances
  57. class _TorqueThreadStaticReg
  58. {
  59. // This will manage all of the thread static registrations
  60. static _TorqueThreadStaticReg *smFirst;
  61. _TorqueThreadStaticReg *mNext;
  62. // This is a vector of vectors which will store instances of thread static
  63. // variables. mThreadStaticInsances[0] will be the list of the initial values
  64. // of the statics, and then indexing for instanced versions will start at 1
  65. //
  66. // Note that the list of instances in mThreadStaticInstances[0] does not, and
  67. // must not get 'delete' called on it, because all members of the list are
  68. // pointers to statically allocated memory. All other lists will be contain
  69. // pointers to dynamically allocated memory, and will need to be freed upon
  70. // termination.
  71. //
  72. // So this was originally a static data member, however that caused problems because
  73. // I was relying on static initialization order to make sure the vector got initialized
  74. // *before* any static instance of this class was created via macro. By wrapping the
  75. // static in a function, I can be assured that the static memory will get initialized
  76. // before it is modified.
  77. static Vector<TorqueThreadStaticList> &getThreadStaticListVector();
  78. public:
  79. /// Constructor
  80. _TorqueThreadStaticReg( _TorqueThreadStatic *ttsInitial )
  81. {
  82. // Link this entry into the list
  83. mNext = smFirst;
  84. smFirst = this;
  85. // Create list 0 (initial values) if it doesn't exist
  86. if( getThreadStaticListVector().empty() )
  87. getThreadStaticListVector().increment();
  88. // Set the index of the thread static for lookup
  89. ttsInitial->mListIndex = getThreadStaticListVector()[0].size();
  90. // Add the static to the initial value list
  91. getThreadStaticListVector()[0].push_back( ttsInitial );
  92. }
  93. virtual ~_TorqueThreadStaticReg();
  94. // Accessors
  95. static const TorqueThreadStaticList &getStaticList( const U32 idx = 0 )
  96. {
  97. AssertFatal( getThreadStaticListVector().size() > idx, "Out of range static list" );
  98. return getThreadStaticListVector()[idx];
  99. }
  100. static void destroyInstances();
  101. static void destroyInstance( TorqueThreadStaticList *instanceList );
  102. static const _TorqueThreadStaticReg *getFirst() { return smFirst; }
  103. const _TorqueThreadStaticReg *getNext() const { return mNext; }
  104. /// Spawn another copy of the ThreadStatics and pass back the id
  105. static TorqueThreadStaticListHandle spawnThreadStaticsInstance();
  106. };
  107. //-----------------------------------------------------------------------------
  108. // Template class that will get used as a base for the thread statics
  109. template<class T>
  110. class TorqueThreadStatic : public _TorqueThreadStatic
  111. {
  112. // The reg object will want access to mInstance
  113. friend class _TorqueThreadStaticReg;
  114. private:
  115. T mInstance;
  116. public:
  117. TorqueThreadStatic( T instanceVal ) : mInstance( instanceVal ) {}
  118. virtual void *getMemInstPtr() { return &mInstance; }
  119. // I am not sure these are needed, and I don't want to create confusing-to-debug code
  120. #if 0
  121. // Operator overloads
  122. operator T*() { return &mInstance; }
  123. operator T*() const { return &mInstance; }
  124. operator const T*() const { return &mInstance; }
  125. bool operator ==( const T &l ) const { return mInstance == l; }
  126. bool operator !=( const T &l ) const { return mInstance != l; }
  127. T &operator =( const T &l ) { mInstance = l; return mInstance; }
  128. #endif // if0
  129. };
  130. //-----------------------------------------------------------------------------
  131. // If ThreadStatic behavior is not enabled, than the macros will resolve
  132. // to regular, static memory
  133. #ifndef TORQUE_ENABLE_THREAD_STATICS
  134. #define DITTS( type, name, initialvalue ) static type name = initialvalue
  135. #define ATTS( name ) name
  136. #else // TORQUE_ENABLE_THREAD_STATICS is defined
  137. //-----------------------------------------------------------------------------
  138. // Declare TorqueThreadStatic, and initialize it's value
  139. //
  140. // This macro would be used in a .cpp file to declare a ThreadStatic
  141. #define DITTS(type, name, initalvalue) \
  142. class _##name##TorqueThreadStatic : public TorqueThreadStatic<type> \
  143. { \
  144. protected:\
  145. virtual _TorqueThreadStatic *_createInstance() const { return new _##name##TorqueThreadStatic; } \
  146. public: \
  147. _##name##TorqueThreadStatic() : TorqueThreadStatic<type>( initalvalue ) {} \
  148. virtual const dsize_t getMemInstSize() const { return sizeof( type ); } \
  149. type &_cast() { return *reinterpret_cast<type *>( getMemInstPtr() ); } \
  150. const type &_const_cast() const { return *reinterpret_cast<const type *>( getMemInstPtr() ); } \
  151. }; \
  152. static _##name##TorqueThreadStatic name##TorqueThreadStatic; \
  153. static _TorqueThreadStaticReg _##name##TTSReg( reinterpret_cast<_TorqueThreadStatic *>( & name##TorqueThreadStatic ) )
  154. //-----------------------------------------------------------------------------
  155. // Access TorqueThreadStatic
  156. // NOTE: TEMPDEF is there as a temporary place holder for however we want to get the index of the currently running
  157. // thread or whatever.
  158. #define TEMPDEF 0
  159. #ifdef TORQUE_ENABLE_THREAD_STATIC_METRICS
  160. // Access counting macro
  161. # define ATTS_(name, idx) \
  162. (reinterpret_cast< _##name##TorqueThreadStatic *>( _TorqueThreadStaticReg::getStaticList( idx )[ _##name##TorqueThreadStatic::getListIndex() ]->_chainHit() )->_cast() )
  163. // Const access counting macro
  164. # define CATTS_(name, idx) \
  165. (reinterpret_cast< _##name##TorqueThreadStatic *>( _TorqueThreadStaticReg::getStaticList( idx )[ _##name##TorqueThreadStatic::getListIndex() ]->_chainHit() )->_const_cast() )
  166. #else
  167. // Regular access macro
  168. # define ATTS_(name, idx) \
  169. (reinterpret_cast< _##name##TorqueThreadStatic *>( _TorqueThreadStaticReg::getStaticList( idx )[ _##name##TorqueThreadStatic::getListIndex() ] )->_cast() )
  170. // Const access macro
  171. # define CATTS_(name, idx) \
  172. (reinterpret_cast< _##name##TorqueThreadStatic *>( _TorqueThreadStaticReg::getStaticList( idx )[ _##name##TorqueThreadStatic::getListIndex() ] )->_const_cast() )
  173. #endif // TORQUE_ENABLE_THREAD_STATIC_METRICS
  174. #define ATTS(name) ATTS_(name, TEMPDEF)
  175. #define CATTS(name) CATTS_(name, TEMPDEF)
  176. #endif // TORQUE_ENABLE_THREAD_STATICS
  177. #endif