threadStatic.cpp
Engine/source/core/threadStatic.cpp
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "core/threadStatic.h" 25 26//----------------------------------------------------------------------------- 27// Statics 28U32 _TorqueThreadStatic::mListIndex = 0; 29 30_TorqueThreadStaticReg *_TorqueThreadStaticReg::smFirst = NULL; 31//----------------------------------------------------------------------------- 32 33inline Vector<TorqueThreadStaticList> &_TorqueThreadStaticReg::getThreadStaticListVector() 34{ 35 // This function assures that the static vector of ThreadStatics will get initialized 36 // before first use. 37 static Vector<TorqueThreadStaticList> sTorqueThreadStaticVec( __FILE__, __LINE__ ); 38 39 return sTorqueThreadStaticVec; 40} 41 42//----------------------------------------------------------------------------- 43 44// Destructor, size should == 1 otherwise someone didn't clean up, or someone 45// did horrible things to list index 0 46_TorqueThreadStaticReg::~_TorqueThreadStaticReg() 47{ 48 AssertFatal( getThreadStaticListVector().size() == 1, "Destruction of static list was not performed on program exit" ); 49} 50 51//----------------------------------------------------------------------------- 52 53void _TorqueThreadStaticReg::destroyInstances() 54{ 55 // mThreadStaticInstances[0] does *not* need to be deallocated 56 // because all members of the list are pointers to static memory 57 while( getThreadStaticListVector().size() > 1 ) 58 { 59 // Delete the members of this list 60 while( getThreadStaticListVector().last().size() ) 61 { 62 _TorqueThreadStatic *biscuit = getThreadStaticListVector().last().first(); 63 64 // Erase the vector entry 65 getThreadStaticListVector().last().pop_front(); 66 67 // And finally the memory 68 delete biscuit; 69 } 70 71 // Remove the entry from the list of lists 72 getThreadStaticListVector().pop_back(); 73 } 74} 75 76//----------------------------------------------------------------------------- 77 78void _TorqueThreadStaticReg::destroyInstance( TorqueThreadStaticList *instanceList ) 79{ 80 AssertFatal( instanceList != &getThreadStaticListVector().first(), "Cannot delete static instance list index 0" ); 81 82 while( instanceList->size() ) 83 { 84 _TorqueThreadStatic *biscuit = getThreadStaticListVector().last().first(); 85 86 // Erase the vector entry 87 getThreadStaticListVector().last().pop_front(); 88 89 // And finally the memory 90 delete biscuit; 91 } 92 93 getThreadStaticListVector().erase( instanceList ); 94} 95 96//----------------------------------------------------------------------------- 97 98TorqueThreadStaticListHandle _TorqueThreadStaticReg::spawnThreadStaticsInstance() 99{ 100 AssertFatal( getThreadStaticListVector().size() > 0, "List is not initialized somehow" ); 101 102 // Add a new list of static instances 103 getThreadStaticListVector().increment(); 104 105 // Copy mThreadStaticInstances[0] (master copy) into new memory, and 106 // pass it back. 107 for( S32 i = 0; i < getThreadStaticListVector()[0].size(); i++ ) 108 { 109 getThreadStaticListVector().last().push_back( getThreadStaticListVector()[0][i]->_createInstance() ); 110 } 111 112 // Return list index of newly allocated static instance list 113 return &getThreadStaticListVector().last(); 114} 115