threadStatic.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include "core/threadStatic.h"
  23. //-----------------------------------------------------------------------------
  24. // Statics
  25. U32 _TorqueThreadStatic::mListIndex = 0;
  26. _TorqueThreadStaticReg *_TorqueThreadStaticReg::smFirst = NULL;
  27. //-----------------------------------------------------------------------------
  28. inline Vector<TorqueThreadStaticList> &_TorqueThreadStaticReg::getThreadStaticListVector()
  29. {
  30. // This function assures that the static vector of ThreadStatics will get initialized
  31. // before first use.
  32. static Vector<TorqueThreadStaticList> sTorqueThreadStaticVec( __FILE__, __LINE__ );
  33. return sTorqueThreadStaticVec;
  34. }
  35. //-----------------------------------------------------------------------------
  36. // Destructor, size should == 1 otherwise someone didn't clean up, or someone
  37. // did horrible things to list index 0
  38. _TorqueThreadStaticReg::~_TorqueThreadStaticReg()
  39. {
  40. AssertFatal( getThreadStaticListVector().size() == 1, "Destruction of static list was not performed on program exit" );
  41. }
  42. //-----------------------------------------------------------------------------
  43. void _TorqueThreadStaticReg::destroyInstances()
  44. {
  45. // mThreadStaticInstances[0] does *not* need to be deallocated
  46. // because all members of the list are pointers to static memory
  47. while( getThreadStaticListVector().size() > 1 )
  48. {
  49. // Delete the members of this list
  50. while( getThreadStaticListVector().last().size() )
  51. {
  52. _TorqueThreadStatic *biscuit = getThreadStaticListVector().last().first();
  53. // Erase the vector entry
  54. getThreadStaticListVector().last().pop_front();
  55. // And finally the memory
  56. delete biscuit;
  57. }
  58. // Remove the entry from the list of lists
  59. getThreadStaticListVector().pop_back();
  60. }
  61. }
  62. //-----------------------------------------------------------------------------
  63. void _TorqueThreadStaticReg::destroyInstance( TorqueThreadStaticList *instanceList )
  64. {
  65. AssertFatal( instanceList != &getThreadStaticListVector().first(), "Cannot delete static instance list index 0" );
  66. while( instanceList->size() )
  67. {
  68. _TorqueThreadStatic *biscuit = getThreadStaticListVector().last().first();
  69. // Erase the vector entry
  70. getThreadStaticListVector().last().pop_front();
  71. // And finally the memory
  72. delete biscuit;
  73. }
  74. getThreadStaticListVector().erase( instanceList );
  75. }
  76. //-----------------------------------------------------------------------------
  77. TorqueThreadStaticListHandle _TorqueThreadStaticReg::spawnThreadStaticsInstance()
  78. {
  79. AssertFatal( getThreadStaticListVector().size() > 0, "List is not initialized somehow" );
  80. // Add a new list of static instances
  81. getThreadStaticListVector().increment();
  82. // Copy mThreadStaticInstances[0] (master copy) into new memory, and
  83. // pass it back.
  84. for( S32 i = 0; i < getThreadStaticListVector()[0].size(); i++ )
  85. {
  86. getThreadStaticListVector().last().push_back( getThreadStaticListVector()[0][i]->_createInstance() );
  87. }
  88. // Return list index of newly allocated static instance list
  89. return &getThreadStaticListVector().last();
  90. }