[email protected] | dd99f539 | 2009-01-08 16:47:14 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 Jian Li <[email protected]> |
[email protected] | 58d30e4 | 2012-08-07 00:29:14 | [diff] [blame] | 3 | * Copyright (C) 2012 Patrick Gansterer <[email protected]> |
[email protected] | dd99f539 | 2009-01-08 16:47:14 | [diff] [blame] | 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 | |
[email protected] | dd99f539 | 2009-01-08 16:47:14 | [diff] [blame] | 22 | #include "ThreadSpecific.h" |
[email protected] | dd99f539 | 2009-01-08 16:47:14 | [diff] [blame] | 23 | |
[email protected] | ac08cdb | 2013-09-08 22:51:53 | [diff] [blame] | 24 | #if OS(WIN) |
[email protected] | 7b983e8 | 2013-01-17 10:04:45 | [diff] [blame] | 25 | |
[email protected] | 58d30e4 | 2012-08-07 00:29:14 | [diff] [blame] | 26 | #include "StdLibExtras.h" |
| 27 | #include "ThreadingPrimitives.h" |
tasak | 4faca4c4 | 2016-01-19 08:38:55 | [diff] [blame] | 28 | #include "wtf/Allocator.h" |
[email protected] | a71b313 | 2013-06-27 17:54:15 | [diff] [blame] | 29 | #include "wtf/DoublyLinkedList.h" |
[email protected] | 58d30e4 | 2012-08-07 00:29:14 | [diff] [blame] | 30 | |
[email protected] | dd99f539 | 2009-01-08 16:47:14 | [diff] [blame] | 31 | namespace WTF { |
| 32 | |
[email protected] | 58d30e4 | 2012-08-07 00:29:14 | [diff] [blame] | 33 | static DoublyLinkedList<PlatformThreadSpecificKey>& destructorsList() |
| 34 | { |
| 35 | DEFINE_STATIC_LOCAL(DoublyLinkedList<PlatformThreadSpecificKey>, staticList, ()); |
| 36 | return staticList; |
| 37 | } |
| 38 | |
| 39 | static Mutex& destructorsMutex() |
| 40 | { |
| 41 | DEFINE_STATIC_LOCAL(Mutex, staticMutex, ()); |
| 42 | return staticMutex; |
| 43 | } |
| 44 | |
| 45 | class PlatformThreadSpecificKey : public DoublyLinkedListNode<PlatformThreadSpecificKey> { |
tasak | 4faca4c4 | 2016-01-19 08:38:55 | [diff] [blame] | 46 | USING_FAST_MALLOC(PlatformThreadSpecificKey); |
| 47 | WTF_MAKE_NONCOPYABLE(PlatformThreadSpecificKey); |
[email protected] | 58d30e4 | 2012-08-07 00:29:14 | [diff] [blame] | 48 | public: |
| 49 | friend class DoublyLinkedListNode<PlatformThreadSpecificKey>; |
| 50 | |
| 51 | PlatformThreadSpecificKey(void (*destructor)(void *)) |
| 52 | : m_destructor(destructor) |
| 53 | { |
| 54 | m_tlsKey = TlsAlloc(); |
| 55 | if (m_tlsKey == TLS_OUT_OF_INDEXES) |
| 56 | CRASH(); |
| 57 | } |
| 58 | |
| 59 | ~PlatformThreadSpecificKey() |
| 60 | { |
| 61 | TlsFree(m_tlsKey); |
| 62 | } |
| 63 | |
| 64 | void setValue(void* data) { TlsSetValue(m_tlsKey, data); } |
| 65 | void* value() { return TlsGetValue(m_tlsKey); } |
| 66 | |
| 67 | void callDestructor() |
| 68 | { |
tkent | bee5120 | 2015-09-30 07:59:25 | [diff] [blame] | 69 | if (void* data = value()) |
[email protected] | 58d30e4 | 2012-08-07 00:29:14 | [diff] [blame] | 70 | m_destructor(data); |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | void (*m_destructor)(void *); |
| 75 | DWORD m_tlsKey; |
| 76 | PlatformThreadSpecificKey* m_prev; |
| 77 | PlatformThreadSpecificKey* m_next; |
| 78 | }; |
| 79 | |
[email protected] | 8ad682d | 2009-03-11 20:39:21 | [diff] [blame] | 80 | long& tlsKeyCount() |
| 81 | { |
| 82 | static long count; |
| 83 | return count; |
| 84 | } |
| 85 | |
| 86 | DWORD* tlsKeys() |
| 87 | { |
| 88 | static DWORD keys[kMaxTlsKeySize]; |
| 89 | return keys; |
| 90 | } |
[email protected] | dd99f539 | 2009-01-08 16:47:14 | [diff] [blame] | 91 | |
[email protected] | 58d30e4 | 2012-08-07 00:29:14 | [diff] [blame] | 92 | void threadSpecificKeyCreate(ThreadSpecificKey* key, void (*destructor)(void *)) |
| 93 | { |
| 94 | *key = new PlatformThreadSpecificKey(destructor); |
| 95 | |
| 96 | MutexLocker locker(destructorsMutex()); |
| 97 | destructorsList().push(*key); |
| 98 | } |
| 99 | |
| 100 | void threadSpecificKeyDelete(ThreadSpecificKey key) |
| 101 | { |
| 102 | MutexLocker locker(destructorsMutex()); |
| 103 | destructorsList().remove(key); |
| 104 | delete key; |
| 105 | } |
| 106 | |
| 107 | void threadSpecificSet(ThreadSpecificKey key, void* data) |
| 108 | { |
| 109 | key->setValue(data); |
| 110 | } |
| 111 | |
| 112 | void* threadSpecificGet(ThreadSpecificKey key) |
| 113 | { |
| 114 | return key->value(); |
| 115 | } |
| 116 | |
[email protected] | dd99f539 | 2009-01-08 16:47:14 | [diff] [blame] | 117 | void ThreadSpecificThreadExit() |
| 118 | { |
[email protected] | 8ad682d | 2009-03-11 20:39:21 | [diff] [blame] | 119 | for (long i = 0; i < tlsKeyCount(); i++) { |
[email protected] | dd99f539 | 2009-01-08 16:47:14 | [diff] [blame] | 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 member. |
[email protected] | 8ad682d | 2009-03-11 20:39:21 | [diff] [blame] | 121 | ThreadSpecific<int>::Data* data = static_cast<ThreadSpecific<int>::Data*>(TlsGetValue(tlsKeys()[i])); |
[email protected] | dd99f539 | 2009-01-08 16:47:14 | [diff] [blame] | 122 | if (data) |
| 123 | data->destructor(data); |
| 124 | } |
[email protected] | 58d30e4 | 2012-08-07 00:29:14 | [diff] [blame] | 125 | |
| 126 | MutexLocker locker(destructorsMutex()); |
| 127 | PlatformThreadSpecificKey* key = destructorsList().head(); |
| 128 | while (key) { |
| 129 | PlatformThreadSpecificKey* nextKey = key->next(); |
| 130 | key->callDestructor(); |
| 131 | key = nextKey; |
| 132 | } |
[email protected] | dd99f539 | 2009-01-08 16:47:14 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | } // namespace WTF |
[email protected] | 58d30e4 | 2012-08-07 00:29:14 | [diff] [blame] | 136 | |
[email protected] | ac08cdb | 2013-09-08 22:51:53 | [diff] [blame] | 137 | #endif // OS(WIN) |