summaryrefslogtreecommitdiff
path: root/windows/JackWinThread.cpp
blob: 2704fa88d9027ada8b67089e8f9c7eb9400f9bd2 (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
/*
Copyright (C) 2001 Paul Davis
Copyright (C) 2004-2006 Grame

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

#include "JackWinThread.h"
#include "JackError.h"
#include "JackTime.h"
#include <assert.h>

namespace Jack
{

DWORD WINAPI JackWinThread::ThreadHandler(void* arg)
{
    JackWinThread* obj = (JackWinThread*)arg;
    JackRunnableInterface* runnable = obj->fRunnable;
   
    // Signal creation thread when started with StartSync
    jack_log("ThreadHandler: start");
    obj->fStatus = kIniting;
    
    // Call Init method
    if (!runnable->Init()) {
        jack_error("Thread init fails: thread quits");
        return 0;
    }
    
    obj->fStatus = kRunning;
    
    // If Init succeed, start the thread loop
    bool res = true;
    while (obj->fStatus == kRunning && res) {
        res = runnable->Execute();
    }

    SetEvent(obj->fEvent);
    jack_log("ThreadHandler: exit");
    return 0;
}

JackWinThread::JackWinThread(JackRunnableInterface* runnable)
        : JackThread(runnable, 0, false, 0)
{
    fEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    fThread = NULL;
    assert(fEvent);
}

JackWinThread::~JackWinThread()
{
    CloseHandle(fEvent);
    CloseHandle(fThread);
}

int JackWinThread::Start()
{
    fStatus = kStarting;

    // Check if the thread was correctly started
    if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
        fStatus = kIdle;
        return -1;
    } else {
        return 0;
    }
}
    
int JackWinThread::StartSync()
{
    fStatus = kStarting;
    
    if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
        fStatus = kIdle;
        return -1;
    } else {
        int count = 0;
        while (fStatus == kStarting && ++count < 1000) {
            JackSleep(1000);
        }
        return (count == 1000) ? -1 : 0;
    }
}

int JackWinThread::StartImp(pthread_t* thread, int priority, int realtime, ThreadCallback start_routine, void* arg)
{
    DWORD id;
    *thread = CreateThread(NULL, 0, start_routine, arg, 0, &id);

    if (*thread == NULL) {
        jack_error("Cannot create thread error = %d", GetLastError());
        return -1;
    }

    if (realtime) {
        
        jack_log("Create RT thread");
        if (!SetThreadPriority(*thread, THREAD_PRIORITY_TIME_CRITICAL)) {
            jack_error("Cannot set priority class = %d", GetLastError());
            return -1;
        }

    } else {
        jack_log("Create non RT thread");
    }
    
    return 0;
}

// voir http://www.microsoft.com/belux/msdn/nl/community/columns/ldoc/multithread1.mspx

int JackWinThread::Kill()
{
    if (fThread) { // If thread has been started
        TerminateThread(fThread, 0);
        WaitForSingleObject(fThread, INFINITE);
        CloseHandle(fThread);
        jack_log("JackWinThread::Kill");
        fThread = NULL;
        fStatus = kIdle;
        return 0;
    } else {
        return -1;
    }
}

int JackWinThread::Stop()
{
    if (fThread) { // If thread has been started
        jack_log("JackWinThread::Stop");
        fStatus = kIdle; // Request for the thread to stop
        WaitForSingleObject(fEvent, INFINITE);
        CloseHandle(fThread);
        fThread = NULL;
        return 0;
    } else {
        return -1;
    }
}

int JackWinThread::KillImp(pthread_t thread)
{
    if (thread) { // If thread has been started
        TerminateThread(thread, 0);
        WaitForSingleObject(thread, INFINITE);
        CloseHandle(thread);
        return 0;
    } else {
        return -1;
    }
}

int JackWinThread::StopImp(pthread_t thread)
{
    if (thread) { // If thread has been started
        WaitForSingleObject(thread, INFINITE);
        CloseHandle(thread);
        return 0;
    } else {
        return -1;
    }
}

int JackWinThread::AcquireRealTime()
{
    return (fThread) ? AcquireRealTimeImp(fThread, fPriority) : -1;
}

int JackWinThread::AcquireRealTime(int priority)
{
    fPriority = priority;
    return AcquireRealTime();
}

int JackWinThread::AcquireRealTimeImp(pthread_t thread, int priority)
{
    jack_log("JackWinThread::AcquireRealTime");

    if (SetThreadPriority(thread, THREAD_PRIORITY_TIME_CRITICAL)) {
        return 0;
    } else {
        jack_error("Cannot set thread priority = %d", GetLastError());
        return -1;
    }
}
int JackWinThread::DropRealTime()
{
    return DropRealTimeImp(fThread);
}

int JackWinThread::DropRealTimeImp(pthread_t thread)
{
    if (SetThreadPriority(thread, THREAD_PRIORITY_NORMAL)) {
        return 0;
    } else {
        jack_error("Cannot set thread priority = %d", GetLastError());
        return -1;
    }
}

pthread_t JackWinThread::GetThreadID()
{
    return fThread;
}

void JackWinThread::Terminate()
{
    ExitThread(0);
}

} // end of namespace

EXPORT bool jack_tls_allocate_key(jack_tls_key *key_ptr)
{
    DWORD key;
    
    key = TlsAlloc();
    if (key == TLS_OUT_OF_INDEXES)
    {
        jack_error("TlsAlloc() failed. Error is %d", (unsigned int)GetLastError());
        return false;
    }
    
    *key_ptr = key;
    return true;
}

EXPORT bool jack_tls_free_key(jack_tls_key key)
{
    if (!TlsFree(key))
    {
        jack_error("TlsFree() failed. Error is %d", (unsigned int)GetLastError());
        return false;
    }
    
    return true;
}

EXPORT bool jack_tls_set(jack_tls_key key, void *data_ptr)
{
    if (!TlsSetValue(key, data_ptr))
    {
        jack_error("TlsSetValue() failed. Error is %d", (unsigned int)GetLastError());
        return false;
    }
    
    return true;
}

EXPORT void *jack_tls_get(jack_tls_key key)
{
    return TlsGetValue(key);
}