summaryrefslogtreecommitdiff
path: root/Source/WTF/wtf/ThreadSpecificWin.cpp
blob: 61a594251b10e0ff7fb2b326902869d7b8132d68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (C) 2009 Jian Li <jianli@chromium.org>
 * Copyright (C) 2012 Patrick Gansterer <paroga@paroga.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"
#include "ThreadSpecific.h"

#include "StdLibExtras.h"
#include "ThreadingPrimitives.h"

#if !USE(PTHREADS)

namespace WTF {

static Mutex& destructorsMutex()
{
    DEFINE_STATIC_LOCAL(Mutex, staticMutex, ());
    return staticMutex;
}

class ThreadSpecificKeyValue {
public:
    ThreadSpecificKeyValue(void (*destructor)(void *))
        : m_destructor(destructor)
    {
        m_tlsKey = TlsAlloc();
        if (m_tlsKey == TLS_OUT_OF_INDEXES)
            CRASH();

        MutexLocker locker(destructorsMutex());
        m_next = m_first;
        m_first = this;
    }

    ~ThreadSpecificKeyValue()
    {
        MutexLocker locker(destructorsMutex());
        ThreadSpecificKeyValue** next = &m_first;
        while (*next != this) {
            ASSERT(*next);
            next = &(*next)->m_next;
        }
        *next = (*next)->m_next;

        TlsFree(m_tlsKey);
    }

    void setValue(void* data) { TlsSetValue(m_tlsKey, data); }
    void* value() { return TlsGetValue(m_tlsKey); }

    static void callDestructors()
    {
        MutexLocker locker(destructorsMutex());
        ThreadSpecificKeyValue* next = m_first;
        while (next) {
            if (void* data = next->value())
                next->m_destructor(data);
            next = next->m_next;
        }
    }

private:
    void (*m_destructor)(void *);
    DWORD m_tlsKey;
    ThreadSpecificKeyValue* m_next;

    static ThreadSpecificKeyValue* m_first;
};

ThreadSpecificKeyValue* ThreadSpecificKeyValue::m_first = 0;

long& tlsKeyCount()
{
    static long count;
    return count;
}

DWORD* tlsKeys()
{
    static DWORD keys[kMaxTlsKeySize];
    return keys;
}

void ThreadSpecificKeyCreate(ThreadSpecificKey* key, void (*destructor)(void *))
{
    *key = new ThreadSpecificKeyValue(destructor);
}

void ThreadSpecificKeyDelete(ThreadSpecificKey key)
{
    delete key;
}

void ThreadSpecificSet(ThreadSpecificKey key, void* data)
{
    key->setValue(data);
}

void* ThreadSpecificGet(ThreadSpecificKey key)
{
    return key->value();
}

void ThreadSpecificThreadExit()
{
    for (long i = 0; i < tlsKeyCount(); i++) {
        // 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.
        ThreadSpecific<int>::Data* data = static_cast<ThreadSpecific<int>::Data*>(TlsGetValue(tlsKeys()[i]));
        if (data)
            data->destructor(data);
    }

    ThreadSpecificKeyValue::callDestructors();
}

} // namespace WTF

#endif // !USE(PTHREADS)