ThreadSpecificWin.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) 2009 Jian Li <jianli@chromium.org>
  3. * Copyright (C) 2012 Patrick Gansterer <paroga@paroga.com>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public License
  16. * along with this library; see the file COPYING.LIB. If not, write to
  17. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301, USA.
  19. *
  20. */
  21. #include "config.h"
  22. #include "ThreadSpecific.h"
  23. #if OS(WINDOWS)
  24. #include "StdLibExtras.h"
  25. #include "ThreadingPrimitives.h"
  26. #include <wtf/DoublyLinkedList.h>
  27. #if !USE(PTHREADS)
  28. namespace WTF {
  29. static DoublyLinkedList<PlatformThreadSpecificKey>& destructorsList()
  30. {
  31. static DoublyLinkedList<PlatformThreadSpecificKey> staticList;
  32. return staticList;
  33. }
  34. static Mutex& destructorsMutex()
  35. {
  36. static Mutex staticMutex;
  37. return staticMutex;
  38. }
  39. class PlatformThreadSpecificKey : public DoublyLinkedListNode<PlatformThreadSpecificKey> {
  40. public:
  41. friend class DoublyLinkedListNode<PlatformThreadSpecificKey>;
  42. PlatformThreadSpecificKey(void (*destructor)(void *))
  43. : m_destructor(destructor)
  44. {
  45. m_tlsKey = TlsAlloc();
  46. if (m_tlsKey == TLS_OUT_OF_INDEXES)
  47. CRASH();
  48. }
  49. ~PlatformThreadSpecificKey()
  50. {
  51. TlsFree(m_tlsKey);
  52. }
  53. void setValue(void* data) { TlsSetValue(m_tlsKey, data); }
  54. void* value() { return TlsGetValue(m_tlsKey); }
  55. void callDestructor()
  56. {
  57. if (void* data = value())
  58. m_destructor(data);
  59. }
  60. private:
  61. void (*m_destructor)(void *);
  62. DWORD m_tlsKey;
  63. PlatformThreadSpecificKey* m_prev;
  64. PlatformThreadSpecificKey* m_next;
  65. };
  66. long& tlsKeyCount()
  67. {
  68. static long count;
  69. return count;
  70. }
  71. DWORD* tlsKeys()
  72. {
  73. static DWORD keys[kMaxTlsKeySize];
  74. return keys;
  75. }
  76. void threadSpecificKeyCreate(ThreadSpecificKey* key, void (*destructor)(void *))
  77. {
  78. // Use the original malloc() instead of fastMalloc() to use this function in FastMalloc code.
  79. *key = static_cast<PlatformThreadSpecificKey*>(::malloc(sizeof(PlatformThreadSpecificKey)));
  80. new (*key) PlatformThreadSpecificKey(destructor);
  81. MutexLocker locker(destructorsMutex());
  82. destructorsList().push(*key);
  83. }
  84. void threadSpecificKeyDelete(ThreadSpecificKey key)
  85. {
  86. MutexLocker locker(destructorsMutex());
  87. destructorsList().remove(key);
  88. key->~PlatformThreadSpecificKey();
  89. ::free(key);
  90. }
  91. void threadSpecificSet(ThreadSpecificKey key, void* data)
  92. {
  93. key->setValue(data);
  94. }
  95. void* threadSpecificGet(ThreadSpecificKey key)
  96. {
  97. return key->value();
  98. }
  99. void ThreadSpecificThreadExit()
  100. {
  101. for (long i = 0; i < tlsKeyCount(); i++) {
  102. // The layout of ThreadSpecific<T>::Data does not depend on T. So we are safe to do the static cast to ThreadSpecific<int> in order to access its data member.
  103. ThreadSpecific<int>::Data* data = static_cast<ThreadSpecific<int>::Data*>(TlsGetValue(tlsKeys()[i]));
  104. if (data)
  105. data->destructor(data);
  106. }
  107. MutexLocker locker(destructorsMutex());
  108. PlatformThreadSpecificKey* key = destructorsList().head();
  109. while (key) {
  110. PlatformThreadSpecificKey* nextKey = key->next();
  111. key->callDestructor();
  112. key = nextKey;
  113. }
  114. }
  115. } // namespace WTF
  116. #endif // !USE(PTHREADS)
  117. #endif // OS(WINDOWS)