| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Jian Li <jianli@chromium.org> | 2 * Copyright (C) 2009 Jian Li <jianli@chromium.org> |
| 3 * Copyright (C) 2012 Patrick Gansterer <paroga@paroga.com> | 3 * Copyright (C) 2012 Patrick Gansterer <paroga@paroga.com> |
| 4 * | 4 * |
| 5 * This library is free software; you can redistribute it and/or | 5 * This library is free software; you can redistribute it and/or |
| 6 * modify it under the terms of the GNU Library General Public | 6 * modify it under the terms of the GNU Library General Public |
| 7 * License as published by the Free Software Foundation; either | 7 * License as published by the Free Software Foundation; either |
| 8 * version 2 of the License, or (at your option) any later version. | 8 * version 2 of the License, or (at your option) any later version. |
| 9 * | 9 * |
| 10 * This library is distributed in the hope that it will be useful, | 10 * This library is distributed in the hope that it will be useful, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "ThreadSpecific.h" | 23 #include "ThreadSpecific.h" |
| 24 | 24 |
| 25 #if OS(WIN) | 25 #if OS(WIN) |
| 26 | 26 |
| 27 #include "StdLibExtras.h" | 27 #include "StdLibExtras.h" |
| 28 #include "ThreadingPrimitives.h" | 28 #include "ThreadingPrimitives.h" |
| 29 #include "wtf/DoublyLinkedList.h" | 29 #include "wtf/DoublyLinkedList.h" |
| 30 | 30 |
| 31 namespace WTF { | 31 namespace WTF { |
| 32 | 32 |
| 33 static DoublyLinkedList<PlatformThreadSpecificKey>& destructorsList() | 33 static DoublyLinkedList<PlatformThreadSpecificKey>& destructorsList() { |
| 34 { | 34 DEFINE_STATIC_LOCAL(DoublyLinkedList<PlatformThreadSpecificKey>, staticList, (
)); |
| 35 DEFINE_STATIC_LOCAL(DoublyLinkedList<PlatformThreadSpecificKey>, staticList,
()); | 35 return staticList; |
| 36 return staticList; | |
| 37 } | 36 } |
| 38 | 37 |
| 39 static Mutex& destructorsMutex() | 38 static Mutex& destructorsMutex() { |
| 40 { | 39 DEFINE_STATIC_LOCAL(Mutex, staticMutex, ()); |
| 41 DEFINE_STATIC_LOCAL(Mutex, staticMutex, ()); | 40 return staticMutex; |
| 42 return staticMutex; | |
| 43 } | 41 } |
| 44 | 42 |
| 45 class PlatformThreadSpecificKey : public DoublyLinkedListNode<PlatformThreadSpec
ificKey> { | 43 class PlatformThreadSpecificKey : public DoublyLinkedListNode<PlatformThreadSpec
ificKey> { |
| 46 public: | 44 public: |
| 47 friend class DoublyLinkedListNode<PlatformThreadSpecificKey>; | 45 friend class DoublyLinkedListNode<PlatformThreadSpecificKey>; |
| 48 | 46 |
| 49 PlatformThreadSpecificKey(void (*destructor)(void *)) | 47 PlatformThreadSpecificKey(void (*destructor)(void*)) |
| 50 : m_destructor(destructor) | 48 : m_destructor(destructor) { |
| 51 { | 49 m_tlsKey = TlsAlloc(); |
| 52 m_tlsKey = TlsAlloc(); | 50 if (m_tlsKey == TLS_OUT_OF_INDEXES) |
| 53 if (m_tlsKey == TLS_OUT_OF_INDEXES) | 51 CRASH(); |
| 54 CRASH(); | 52 } |
| 55 } | |
| 56 | 53 |
| 57 ~PlatformThreadSpecificKey() | 54 ~PlatformThreadSpecificKey() { |
| 58 { | 55 TlsFree(m_tlsKey); |
| 59 TlsFree(m_tlsKey); | 56 } |
| 60 } | |
| 61 | 57 |
| 62 void setValue(void* data) { TlsSetValue(m_tlsKey, data); } | 58 void setValue(void* data) { TlsSetValue(m_tlsKey, data); } |
| 63 void* value() { return TlsGetValue(m_tlsKey); } | 59 void* value() { return TlsGetValue(m_tlsKey); } |
| 64 | 60 |
| 65 void callDestructor() | 61 void callDestructor() { |
| 66 { | 62 if (void* data = value()) |
| 67 if (void* data = value()) | 63 m_destructor(data); |
| 68 m_destructor(data); | 64 } |
| 69 } | |
| 70 | 65 |
| 71 private: | 66 private: |
| 72 void (*m_destructor)(void *); | 67 void (*m_destructor)(void*); |
| 73 DWORD m_tlsKey; | 68 DWORD m_tlsKey; |
| 74 PlatformThreadSpecificKey* m_prev; | 69 PlatformThreadSpecificKey* m_prev; |
| 75 PlatformThreadSpecificKey* m_next; | 70 PlatformThreadSpecificKey* m_next; |
| 76 }; | 71 }; |
| 77 | 72 |
| 78 long& tlsKeyCount() | 73 long& tlsKeyCount() { |
| 79 { | 74 static long count; |
| 80 static long count; | 75 return count; |
| 81 return count; | |
| 82 } | 76 } |
| 83 | 77 |
| 84 DWORD* tlsKeys() | 78 DWORD* tlsKeys() { |
| 85 { | 79 static DWORD keys[kMaxTlsKeySize]; |
| 86 static DWORD keys[kMaxTlsKeySize]; | 80 return keys; |
| 87 return keys; | |
| 88 } | 81 } |
| 89 | 82 |
| 90 void threadSpecificKeyCreate(ThreadSpecificKey* key, void (*destructor)(void *)) | 83 void threadSpecificKeyCreate(ThreadSpecificKey* key, void (*destructor)(void*))
{ |
| 91 { | 84 *key = new PlatformThreadSpecificKey(destructor); |
| 92 *key = new PlatformThreadSpecificKey(destructor); | |
| 93 | 85 |
| 94 MutexLocker locker(destructorsMutex()); | 86 MutexLocker locker(destructorsMutex()); |
| 95 destructorsList().push(*key); | 87 destructorsList().push(*key); |
| 96 } | 88 } |
| 97 | 89 |
| 98 void threadSpecificKeyDelete(ThreadSpecificKey key) | 90 void threadSpecificKeyDelete(ThreadSpecificKey key) { |
| 99 { | 91 MutexLocker locker(destructorsMutex()); |
| 100 MutexLocker locker(destructorsMutex()); | 92 destructorsList().remove(key); |
| 101 destructorsList().remove(key); | 93 delete key; |
| 102 delete key; | |
| 103 } | 94 } |
| 104 | 95 |
| 105 void threadSpecificSet(ThreadSpecificKey key, void* data) | 96 void threadSpecificSet(ThreadSpecificKey key, void* data) { |
| 106 { | 97 key->setValue(data); |
| 107 key->setValue(data); | |
| 108 } | 98 } |
| 109 | 99 |
| 110 void* threadSpecificGet(ThreadSpecificKey key) | 100 void* threadSpecificGet(ThreadSpecificKey key) { |
| 111 { | 101 return key->value(); |
| 112 return key->value(); | |
| 113 } | 102 } |
| 114 | 103 |
| 115 void ThreadSpecificThreadExit() | 104 void ThreadSpecificThreadExit() { |
| 116 { | 105 for (long i = 0; i < tlsKeyCount(); i++) { |
| 117 for (long i = 0; i < tlsKeyCount(); i++) { | 106 // The layout of ThreadSpecific<T>::Data does not depend on T. So we are saf
e to do the static cast to ThreadSpecific<int> in order to access its data membe
r. |
| 118 // 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 m
ember. | 107 ThreadSpecific<int>::Data* data = static_cast<ThreadSpecific<int>::Data*>(Tl
sGetValue(tlsKeys()[i])); |
| 119 ThreadSpecific<int>::Data* data = static_cast<ThreadSpecific<int>::Data*
>(TlsGetValue(tlsKeys()[i])); | 108 if (data) |
| 120 if (data) | 109 data->destructor(data); |
| 121 data->destructor(data); | 110 } |
| 122 } | |
| 123 | 111 |
| 124 MutexLocker locker(destructorsMutex()); | 112 MutexLocker locker(destructorsMutex()); |
| 125 PlatformThreadSpecificKey* key = destructorsList().head(); | 113 PlatformThreadSpecificKey* key = destructorsList().head(); |
| 126 while (key) { | 114 while (key) { |
| 127 PlatformThreadSpecificKey* nextKey = key->next(); | 115 PlatformThreadSpecificKey* nextKey = key->next(); |
| 128 key->callDestructor(); | 116 key->callDestructor(); |
| 129 key = nextKey; | 117 key = nextKey; |
| 130 } | 118 } |
| 131 } | 119 } |
| 132 | 120 |
| 133 } // namespace WTF | 121 } // namespace WTF |
| 134 | 122 |
| 135 #endif // OS(WIN) | 123 #endif // OS(WIN) |
| OLD | NEW |