ThreadLocalStorage.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "ThreadLocalStorage.h"
  2. BfpTLS* BfTLSManager::sInternalThreadKey = 0;
  3. BfTLSManager gBfTLSManager;
  4. uint32 BfTLSManager::Alloc()
  5. {
  6. Beefy::AutoCrit autoCrit(mCritSect);
  7. int idx = 0;
  8. if (mFreeIndices.size() != 0)
  9. {
  10. idx = mFreeIndices.back();
  11. mFreeIndices.pop_back();
  12. }
  13. else
  14. {
  15. idx = mAllocIdx++;
  16. if (mAllocIdx >= mAllocSize)
  17. {
  18. int newSize = std::max(mAllocSize, 4);
  19. BfpTLS** newArr = new BfpTLS*[newSize];
  20. BfTLSDatumRoot** newDatumArr = new BfTLSDatumRoot*[newSize];
  21. if (mAllocSize > 0)
  22. {
  23. memcpy(newArr, mAllocatedKeys, mAllocSize*sizeof(BfpTLS*));
  24. memcpy(newDatumArr, mAssociatedTLSDatums, mAllocSize*sizeof(BfTLSDatumRoot*));
  25. }
  26. mAllocSize = newSize;
  27. BF_FULL_MEMORY_FENCE();
  28. mAllocatedKeys = newArr;
  29. mAssociatedTLSDatums = newDatumArr;
  30. }
  31. }
  32. mAllocatedKeys[idx] = BfpTLS_Create(NULL);
  33. mAssociatedTLSDatums[idx] = NULL;
  34. return idx;
  35. }
  36. void BfTLSManager::RegisterTlsDatum(uint32 key, BfTLSDatumRoot* tlsDatum)
  37. {
  38. mAssociatedTLSDatums[key] = tlsDatum;
  39. }
  40. void BfTLSManager::Free(uint32 idx)
  41. {
  42. BfTLSDatumRoot* tlsDatumRoot = (BfTLSDatumRoot*) mAssociatedTLSDatums[idx];
  43. if (tlsDatumRoot != NULL)
  44. tlsDatumRoot->Finalize();
  45. Beefy::AutoCrit autoCrit(mCritSect);
  46. mFreeIndices.push_back(idx);
  47. BfpTLS_Release(mAllocatedKeys[idx]);
  48. }