OLD | NEW |
| (Empty) |
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 | |
22 #include "ThreadSpecific.h" | |
23 | |
24 #if OS(WIN) | |
25 | |
26 #include "StdLibExtras.h" | |
27 #include "ThreadingPrimitives.h" | |
28 #include "wtf/Allocator.h" | |
29 #include "wtf/DoublyLinkedList.h" | |
30 | |
31 namespace WTF { | |
32 | |
33 static DoublyLinkedList<PlatformThreadSpecificKey>& destructorsList() { | |
34 DEFINE_STATIC_LOCAL(DoublyLinkedList<PlatformThreadSpecificKey>, staticList, | |
35 ()); | |
36 return staticList; | |
37 } | |
38 | |
39 static Mutex& destructorsMutex() { | |
40 DEFINE_STATIC_LOCAL(Mutex, staticMutex, ()); | |
41 return staticMutex; | |
42 } | |
43 | |
44 class PlatformThreadSpecificKey | |
45 : public DoublyLinkedListNode<PlatformThreadSpecificKey> { | |
46 USING_FAST_MALLOC(PlatformThreadSpecificKey); | |
47 WTF_MAKE_NONCOPYABLE(PlatformThreadSpecificKey); | |
48 | |
49 public: | |
50 friend class DoublyLinkedListNode<PlatformThreadSpecificKey>; | |
51 | |
52 PlatformThreadSpecificKey(void (*destructor)(void*)) | |
53 : m_destructor(destructor) { | |
54 m_tlsKey = TlsAlloc(); | |
55 if (m_tlsKey == TLS_OUT_OF_INDEXES) | |
56 CRASH(); | |
57 } | |
58 | |
59 ~PlatformThreadSpecificKey() { TlsFree(m_tlsKey); } | |
60 | |
61 void setValue(void* data) { TlsSetValue(m_tlsKey, data); } | |
62 void* value() { return TlsGetValue(m_tlsKey); } | |
63 | |
64 void callDestructor() { | |
65 if (void* data = value()) | |
66 m_destructor(data); | |
67 } | |
68 | |
69 private: | |
70 void (*m_destructor)(void*); | |
71 DWORD m_tlsKey; | |
72 PlatformThreadSpecificKey* m_prev; | |
73 PlatformThreadSpecificKey* m_next; | |
74 }; | |
75 | |
76 long& tlsKeyCount() { | |
77 static long count; | |
78 return count; | |
79 } | |
80 | |
81 DWORD* tlsKeys() { | |
82 static DWORD keys[kMaxTlsKeySize]; | |
83 return keys; | |
84 } | |
85 | |
86 void threadSpecificKeyCreate(ThreadSpecificKey* key, | |
87 void (*destructor)(void*)) { | |
88 *key = new PlatformThreadSpecificKey(destructor); | |
89 | |
90 MutexLocker locker(destructorsMutex()); | |
91 destructorsList().push(*key); | |
92 } | |
93 | |
94 void threadSpecificKeyDelete(ThreadSpecificKey key) { | |
95 MutexLocker locker(destructorsMutex()); | |
96 destructorsList().remove(key); | |
97 delete key; | |
98 } | |
99 | |
100 void threadSpecificSet(ThreadSpecificKey key, void* data) { | |
101 key->setValue(data); | |
102 } | |
103 | |
104 void* threadSpecificGet(ThreadSpecificKey key) { | |
105 return key->value(); | |
106 } | |
107 | |
108 void ThreadSpecificThreadExit() { | |
109 for (long i = 0; i < tlsKeyCount(); i++) { | |
110 // The layout of ThreadSpecific<T>::Data does not depend on T. So we are | |
111 // safe to do the static cast to ThreadSpecific<int> in order to access its | |
112 // data member. | |
113 ThreadSpecific<int>::Data* data = | |
114 static_cast<ThreadSpecific<int>::Data*>(TlsGetValue(tlsKeys()[i])); | |
115 if (data) | |
116 data->destructor(data); | |
117 } | |
118 | |
119 MutexLocker locker(destructorsMutex()); | |
120 PlatformThreadSpecificKey* key = destructorsList().head(); | |
121 while (key) { | |
122 PlatformThreadSpecificKey* nextKey = key->next(); | |
123 key->callDestructor(); | |
124 key = nextKey; | |
125 } | |
126 } | |
127 | |
128 } // namespace WTF | |
129 | |
130 #endif // OS(WIN) | |
OLD | NEW |