ThreadLocal.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // ThreadLocal.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/ThreadLocal.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Threading
  8. // Module: Thread
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/ThreadLocal.h"
  16. #include "Poco/SingletonHolder.h"
  17. #include "Poco/Thread.h"
  18. namespace Poco {
  19. TLSAbstractSlot::TLSAbstractSlot()
  20. {
  21. }
  22. TLSAbstractSlot::~TLSAbstractSlot()
  23. {
  24. }
  25. ThreadLocalStorage::ThreadLocalStorage()
  26. {
  27. }
  28. ThreadLocalStorage::~ThreadLocalStorage()
  29. {
  30. for (TLSMap::iterator it = _map.begin(); it != _map.end(); ++it)
  31. {
  32. delete it->second;
  33. }
  34. }
  35. TLSAbstractSlot*& ThreadLocalStorage::get(const void* key)
  36. {
  37. TLSMap::iterator it = _map.find(key);
  38. if (it == _map.end())
  39. return _map.insert(TLSMap::value_type(key, reinterpret_cast<Poco::TLSAbstractSlot*>(0))).first->second;
  40. else
  41. return it->second;
  42. }
  43. namespace
  44. {
  45. static SingletonHolder<ThreadLocalStorage> sh;
  46. }
  47. ThreadLocalStorage& ThreadLocalStorage::current()
  48. {
  49. Thread* pThread = Thread::current();
  50. if (pThread)
  51. {
  52. return pThread->tls();
  53. }
  54. else
  55. {
  56. return *sh.get();
  57. }
  58. }
  59. void ThreadLocalStorage::clear()
  60. {
  61. Thread* pThread = Thread::current();
  62. if (pThread)
  63. pThread->clearTLS();
  64. }
  65. } // namespace Poco