summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/sys/windows/Thread.cpp
blob: 23b0033be45e3bae7d473bc0c06a108456c4e6c6 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

// Ensure definition of OpenThread in mingw
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif

#include "qpid/sys/Thread.h"
#include "qpid/sys/Runnable.h"
#include "qpid/sys/windows/check.h"

#include <process.h>
#include <windows.h>

/*
 * This implementation distinguishes between two types of thread: Qpid
 * threads (based on qpid::sys::Runnable) and the rest.  It provides a
 * join() that will not deadlock against the Windows loader lock for
 * Qpid threads.
 *
 * System thread identifiers are unique per Windows thread; thread
 * handles are not.  Thread identifiers can be recycled, but keeping a
 * handle open against the thread prevents recycling as long as
 * shared_ptr references to a ThreadPrivate structure remain.
 *
 * There is a 1-1 relationship between Qpid threads and their
 * ThreadPrivate structure.  Non-Qpid threads do not need to find the
 * qpidThreadDone handle, so there may be a 1-many relationship for
 * them.
 *
 * TLS storage is used for a lockless solution for static library
 * builds.  The special case of LoadLibrary/FreeLibrary requires
 * additional synchronization variables and resource cleanup in
 * DllMain.  _DLL marks the dynamic case.
 */

namespace qpid {
namespace sys {

class ThreadPrivate {
public:
    friend class Thread;
    friend unsigned __stdcall runThreadPrivate(void*);
    typedef boost::shared_ptr<ThreadPrivate> shared_ptr;
    ~ThreadPrivate();

private:
    unsigned threadId;
    HANDLE threadHandle;
    HANDLE initCompleted;
    HANDLE qpidThreadDone;
    Runnable* runnable;
    shared_ptr keepAlive;

    ThreadPrivate() : threadId(GetCurrentThreadId()), initCompleted(NULL),
                      qpidThreadDone(NULL), runnable(NULL) {
        threadHandle =  OpenThread (SYNCHRONIZE, FALSE, threadId);
        QPID_WINDOWS_CHECK_CRT_NZ(threadHandle);
    }

    ThreadPrivate(Runnable* r) : threadHandle(NULL), initCompleted(NULL),
                                 qpidThreadDone(NULL), runnable(r) {}

    void start(shared_ptr& p);
    static shared_ptr createThread(Runnable* r);
};

}}  // namespace qpid::sys 


namespace {
using namespace qpid::sys;

#ifdef _DLL
class ScopedCriticalSection
{
  public:
    ScopedCriticalSection(CRITICAL_SECTION& cs) : criticalSection(cs) { EnterCriticalSection(&criticalSection); }
    ~ScopedCriticalSection() { LeaveCriticalSection(&criticalSection); }
  private:
    CRITICAL_SECTION& criticalSection;
};

CRITICAL_SECTION threadLock;
long runningThreads = 0;
HANDLE threadsDone;
bool terminating = false;
#endif


DWORD volatile tlsIndex = TLS_OUT_OF_INDEXES;

DWORD getTlsIndex() {
    if (tlsIndex != TLS_OUT_OF_INDEXES)
        return tlsIndex;        // already set

    DWORD trialIndex = TlsAlloc();
    QPID_WINDOWS_CHECK_NOT(trialIndex, TLS_OUT_OF_INDEXES); // No OS resource
    
    // only one thread gets to set the value
    DWORD actualIndex = (DWORD) InterlockedCompareExchange((LONG volatile *) &tlsIndex, (LONG) trialIndex, (LONG) TLS_OUT_OF_INDEXES);
    if (actualIndex == TLS_OUT_OF_INDEXES)
        return trialIndex;      // we won the race
    else {
        TlsFree(trialIndex);
        return actualIndex;
    }
}

} // namespace

namespace qpid {
namespace sys {

unsigned __stdcall runThreadPrivate(void* p)
{
    ThreadPrivate* threadPrivate = static_cast<ThreadPrivate*>(p);
    TlsSetValue(getTlsIndex(), threadPrivate);

    WaitForSingleObject (threadPrivate->initCompleted, INFINITE);
    CloseHandle (threadPrivate->initCompleted);
    threadPrivate->initCompleted = NULL;

    try {
        threadPrivate->runnable->run();
    } catch (...) {
        // not our concern
    }

    SetEvent (threadPrivate->qpidThreadDone); // allow join()
    threadPrivate->keepAlive.reset();         // may run ThreadPrivate destructor

#ifdef _DLL
    {
        ScopedCriticalSection l(threadLock);
        if (--runningThreads == 0)
            SetEvent(threadsDone);
    }
#endif
    return 0;
}


ThreadPrivate::shared_ptr ThreadPrivate::createThread(Runnable* runnable) {
    ThreadPrivate::shared_ptr tp(new ThreadPrivate(runnable));
    tp->start(tp);
    return tp;
}

void ThreadPrivate::start(ThreadPrivate::shared_ptr& tp) {
    getTlsIndex();              // fail here if OS problem, not in new thread

    initCompleted = CreateEvent (NULL, TRUE, FALSE, NULL);
    QPID_WINDOWS_CHECK_CRT_NZ(initCompleted);
    qpidThreadDone = CreateEvent (NULL, TRUE, FALSE, NULL);
    QPID_WINDOWS_CHECK_CRT_NZ(qpidThreadDone);

#ifdef _DLL
    {
        ScopedCriticalSection l(threadLock);
        if (terminating)
            throw qpid::Exception(QPID_MSG("creating thread after exit/FreeLibrary"));
        runningThreads++;
    }
#endif

    uintptr_t h =  _beginthreadex(0,
                                  0,
                                  runThreadPrivate,
                                  (void *)this,
                                  0,
                                  &threadId);

#ifdef _DLL
    if (h == NULL) {
        ScopedCriticalSection l(threadLock);
        if (--runningThreads == 0)
            SetEvent(threadsDone);
    }
#endif

    QPID_WINDOWS_CHECK_CRT_NZ(h);

    // Success
    keepAlive = tp;
    threadHandle = reinterpret_cast<HANDLE>(h);
    SetEvent (initCompleted);
}

ThreadPrivate::~ThreadPrivate() {
    if (threadHandle)
        CloseHandle (threadHandle);
    if (initCompleted)
        CloseHandle (initCompleted);
    if (qpidThreadDone)
        CloseHandle (qpidThreadDone);
}


Thread::Thread() {}

Thread::Thread(Runnable* runnable) : impl(ThreadPrivate::createThread(runnable)) {}

Thread::Thread(Runnable& runnable) : impl(ThreadPrivate::createThread(&runnable)) {}

Thread::operator bool() {
    return impl;
}

bool Thread::operator==(const Thread& t) const {
    if (!impl || !t.impl)
        return false;
    return impl->threadId == t.impl->threadId;
}

bool Thread::operator!=(const Thread& t) const {
    return !(*this==t);
}

void Thread::join() {
    if (impl) {
        DWORD status;
        if (impl->runnable) {
            HANDLE handles[2] = {impl->qpidThreadDone, impl->threadHandle};
            // wait for either.  threadHandle not signalled if loader
            // lock held (FreeLibrary).  qpidThreadDone not signalled
            // if thread terminated by exit().
            status = WaitForMultipleObjects (2, handles, false, INFINITE);
        }
        else
            status = WaitForSingleObject (impl->threadHandle, INFINITE);
        QPID_WINDOWS_CHECK_NOT(status, WAIT_FAILED);
    }
}

unsigned long Thread::logId() {
    return GetCurrentThreadId();
}

/* static */
Thread Thread::current() {
    ThreadPrivate* tlsValue = (ThreadPrivate *) TlsGetValue(getTlsIndex());
    Thread t;
    if (tlsValue != NULL) {
        // called from within Runnable->run(), so keepAlive has positive use count
        t.impl = tlsValue->keepAlive;
    }
    else
        t.impl.reset(new ThreadPrivate());
    return t;
}

}}  // namespace qpid::sys


#ifdef _DLL

// DllMain: called possibly many times in a process lifetime if dll
// loaded and freed repeatedly .  Be mindful of Windows loader lock
// and other DllMain restrictions.

BOOL APIENTRY DllMain(HMODULE hm, DWORD reason, LPVOID reserved) {
    switch (reason) {
    case DLL_PROCESS_ATTACH:
        InitializeCriticalSection(&threadLock);
        threadsDone = CreateEvent(NULL, TRUE, FALSE, NULL);
        break;

    case DLL_PROCESS_DETACH:
        terminating = true;
        if (reserved != NULL) {
            // process exit(): threads are stopped arbitrarily and
            // possibly in an inconsistent state.  Not even threadLock
            // can be trusted.  All static destructors have been
            // called at this point and any resources this unit knows
            // about will be released as part of process tear down by
            // the OS.  Accordingly, do nothing.
            return TRUE;
        }
        else {
            // FreeLibrary(): threads are still running and we are
            // encouraged to clean up to avoid leaks.  Mostly we just
            // want any straggler threads to finish and notify
            // threadsDone as the last thing they do.
            while (1) {
                {
                    ScopedCriticalSection l(threadLock);
                    if (runningThreads == 0)
                        break;
                    ResetEvent(threadsDone);
                }
                WaitForSingleObject(threadsDone, INFINITE);
            }
            if (tlsIndex != TLS_OUT_OF_INDEXES)
                TlsFree(getTlsIndex());
            CloseHandle(threadsDone);
            DeleteCriticalSection(&threadLock);
        }
        break;

    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
        break;
    }
    return TRUE;
}

#endif