2
0

threadSafeFreeList.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 _THREADSAFEFREELIST_H_
  23. #define _THREADSAFEFREELIST_H_
  24. #ifndef _THREADSAFEREFCOUNT_H_
  25. # include "platform/threads/threadSafeRefCount.h"
  26. #endif
  27. #ifndef _PLATFORMINTRINSICS_H_
  28. # include "platform/platformIntrinsics.h"
  29. #endif
  30. #include "platform/tmm_off.h"
  31. /// @file
  32. /// Lock-free freelists for concurrent access.
  33. /// Freelist for re-using allocations in a concurrent setting.
  34. ///
  35. /// @note Make sure that there are no more allocations in use
  36. /// when the free list is destructed.
  37. /// @note Allocated instances come with a reference already counted
  38. /// on the instance.
  39. ///
  40. /// @param T Type of elements to allocate; must be derived from
  41. /// ThreadSafeRefCount and have at least define one additional
  42. /// pointer-sized field.
  43. template< class T >
  44. class ThreadSafeFreeList
  45. {
  46. protected:
  47. T* mFreeList;
  48. #ifdef TORQUE_DEBUG
  49. S32 mNumNodesTotal;
  50. S32 mNumNodesFree;
  51. #endif
  52. T*& getNext( T* ptr )
  53. {
  54. return *( ( T** ) &( ( U8* ) ptr )[ sizeof( T ) - sizeof( T* ) ] );
  55. }
  56. public:
  57. /// Create the freelist.
  58. ///
  59. /// @param numPreAlloc Number of instances to pre-allocate.
  60. ThreadSafeFreeList( U32 numPreAlloc = 0 )
  61. : mFreeList( 0 )
  62. {
  63. #ifdef TORQUE_DEBUG
  64. mNumNodesTotal = 0;
  65. mNumNodesFree = 0;
  66. #endif
  67. for( U32 i = 0; i < numPreAlloc; ++ i )
  68. free( alloc() );
  69. }
  70. ~ThreadSafeFreeList()
  71. {
  72. #ifdef TORQUE_DEBUG
  73. AssertWarn( mNumNodesTotal == mNumNodesFree,
  74. "ThreadSafeFreeList::~ThreadSafeFreeList() - still got live instances" );
  75. #endif
  76. // Destroy remaining nodes. Not synchronized. We assume all
  77. // concurrent processing to have finished.
  78. while( mFreeList )
  79. {
  80. T* next = getNext( mFreeList );
  81. dFree( mFreeList );
  82. mFreeList = next;
  83. }
  84. }
  85. /// Return memory for a new instance.
  86. void* alloc()
  87. {
  88. T* ptr;
  89. while( 1 )
  90. {
  91. ptr = ThreadSafeRef< T >::safeRead( mFreeList );
  92. if( !ptr )
  93. {
  94. ptr = ( T* ) dMalloc( sizeof( T ) );
  95. dMemset( ptr, 0, sizeof( T ) );
  96. #ifdef TORQUE_DEBUG
  97. dFetchAndAdd( mNumNodesTotal, 1 );
  98. #endif
  99. ptr->addRef();
  100. break;
  101. }
  102. else if( dCompareAndSwap( mFreeList, ptr, getNext( ptr ) ) )
  103. {
  104. #ifdef TORQUE_DEBUG
  105. dFetchAndAdd( mNumNodesFree, -1 );
  106. #endif
  107. ptr->clearLowestBit();
  108. break;
  109. }
  110. else
  111. ptr->release();
  112. }
  113. return ptr;
  114. }
  115. /// Return the memory allocated to the given instance to the freelist.
  116. void free( void* ptr )
  117. {
  118. AssertFatal( ptr, "ThreadSafeFreeList::free() - got a NULL pointer" );
  119. T* node = ( T* ) ptr;
  120. while( 1 )
  121. {
  122. T* list = mFreeList;
  123. getNext( node ) = list;
  124. if( dCompareAndSwap( mFreeList, list, node ) )
  125. break;
  126. }
  127. #ifdef TORQUE_DEBUG
  128. dFetchAndAdd( mNumNodesFree, 1 );
  129. #endif
  130. }
  131. void dumpDebug()
  132. {
  133. #ifdef TORQUE_DEBUG
  134. Platform::outputDebugString( "[ThreadSafeFreeList] total=%i, free=%i",
  135. mNumNodesTotal, mNumNodesFree );
  136. #endif
  137. }
  138. };
  139. /// Baseclass for objects allocated from ThreadSafeFreeLists.
  140. template< class T, class DeletePolicy = DeleteSingle >
  141. class ThreadSafeFreeListNode : public ThreadSafeRefCount< T, DeletePolicy >
  142. {
  143. public:
  144. typedef ThreadSafeRefCount< T, DeletePolicy > Parent;
  145. ThreadSafeFreeListNode()
  146. : Parent( false ) {}
  147. static void* operator new( size_t size, ThreadSafeFreeList< T >& freeList )
  148. {
  149. AssertFatal( size <= sizeof( T ),
  150. "ThreadSafeFreeListNode::new() - size exceeds limit of freelist" );
  151. TORQUE_UNUSED( size );
  152. return freeList.alloc();
  153. }
  154. static void operator delete( void* ptr, ThreadSafeFreeList< T >& freeList )
  155. {
  156. freeList.free( ptr );
  157. }
  158. };
  159. #include "platform/tmm_on.h"
  160. #endif // _THREADSAFEFREELIST_H_