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