/
concurrency.cpp
247 lines (215 loc) · 5.59 KB
/
concurrency.cpp
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
/**
* @file concurrency.cpp
* Concurrency: threads, mutexes, semaphores. @ingroup system
*
* @authors Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2012 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2006 Jamie Jones <jamie_jones_au@yahoo.com.au>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "de/concurrency.h"
#include "de/garbage.h"
#include <QMutex>
#include <QApplication>
#include <QDebug>
#include <de/Time>
#include <de/Log>
#include <assert.h>
static uint mainThreadId = 0; ///< ID of the main thread.
CallbackThread::CallbackThread(systhreadfunc_t func, void* param)
: _callback(func), _parm(param), _returnValue(0),
_exitStatus(DENG_THREAD_STOPPED_NORMALLY),
_terminationFunc(0)
{
//qDebug() << "CallbackThread:" << this << "created.";
// Only used if the thread needs to be shut down forcibly.
setTerminationEnabled(true);
// Cleanup at app exit time for threads whose exit value hasn't been checked.
connect(qApp, SIGNAL(destroyed()), this, SLOT(deleteNow()));
}
CallbackThread::~CallbackThread()
{
if(isRunning())
{
//qDebug() << "CallbackThread:" << this << "forcibly stopping, deleting.";
terminate();
wait(1000);
}
else
{
//qDebug() << "CallbackThread:" << this << "deleted.";
}
}
void CallbackThread::deleteNow()
{
delete this;
}
void CallbackThread::run()
{
_exitStatus = DENG_THREAD_STOPPED_WITH_FORCE;
try
{
if(_callback)
{
_returnValue = _callback(_parm);
}
_exitStatus = DENG_THREAD_STOPPED_NORMALLY;
}
catch(const std::exception& error)
{
LOG_AS("CallbackThread");
LOG_ERROR(QString("Uncaught exception: ") + error.what());
_returnValue = -1;
_exitStatus = DENG_THREAD_STOPPED_WITH_EXCEPTION;
}
if(_terminationFunc)
{
_terminationFunc(_exitStatus);
}
Garbage_ClearForThread();
}
int CallbackThread::exitValue() const
{
return _returnValue;
}
systhreadexitstatus_t CallbackThread::exitStatus() const
{
return _exitStatus;
}
void CallbackThread::setTerminationFunc(void (*func)(systhreadexitstatus_t))
{
_terminationFunc = func;
}
void Sys_MarkAsMainThread(void)
{
// This is the main thread.
mainThreadId = Sys_CurrentThreadId();
}
boolean Sys_InMainThread(void)
{
return mainThreadId == Sys_CurrentThreadId();
}
void Thread_Sleep(int milliseconds)
{
de::Time::Delta::fromMilliSeconds(milliseconds).sleep();
}
thread_t Sys_StartThread(systhreadfunc_t startpos, void *parm)
{
CallbackThread* t = new CallbackThread(startpos, parm);
t->start();
return t;
}
void Thread_KillAbnormally(thread_t handle)
{
QThread* t = reinterpret_cast<QThread*>(handle);
if(!handle)
{
t = QThread::currentThread();
}
assert(t);
t->terminate();
}
void Thread_SetCallback(thread_t thread, void (*terminationFunc)(systhreadexitstatus_t))
{
CallbackThread* t = reinterpret_cast<CallbackThread*>(thread);
DENG_ASSERT(t);
if(!t) return;
t->setTerminationFunc(terminationFunc);
}
int Sys_WaitThread(thread_t handle, int timeoutMs, systhreadexitstatus_t* exitStatus)
{
CallbackThread* t = reinterpret_cast<CallbackThread*>(handle);
assert(static_cast<QThread*>(t) != QThread::currentThread());
t->wait(timeoutMs);
if(!t->isFinished())
{
LOG_WARNING("Thread did not stop in time, forcibly killing it.");
if(exitStatus) *exitStatus = DENG_THREAD_STOPPED_WITH_FORCE;
}
else
{
if(exitStatus) *exitStatus = t->exitStatus();
}
t->deleteLater(); // get rid of it
return t->exitValue();
}
uint Sys_ThreadId(thread_t handle)
{
QThread* t = reinterpret_cast<QThread*>(handle);
if(!t) t = QThread::currentThread();
return uint(PTR2INT(t));
}
uint Sys_CurrentThreadId(void)
{
return Sys_ThreadId(NULL /*this thread*/);
}
/// @todo remove the name parameter
mutex_t Sys_CreateMutex(const char*)
{
return new QMutex(QMutex::Recursive);
}
void Sys_DestroyMutex(mutex_t handle)
{
if(handle)
{
delete reinterpret_cast<QMutex*>(handle);
}
}
void Sys_Lock(mutex_t handle)
{
QMutex* m = reinterpret_cast<QMutex*>(handle);
assert(m != 0);
if(m)
{
m->lock();
}
}
void Sys_Unlock(mutex_t handle)
{
QMutex* m = reinterpret_cast<QMutex*>(handle);
assert(m != 0);
if(m)
{
m->unlock();
}
}
#if 0
sem_t Sem_Create(uint32_t initialValue)
{
return (sem_t) SDL_CreateSemaphore(initialValue);
}
void Sem_Destroy(sem_t semaphore)
{
if(semaphore)
{
SDL_DestroySemaphore((SDL_sem *) semaphore);
}
}
void Sem_P(sem_t semaphore)
{
if(semaphore)
{
SDL_SemWait((SDL_sem *) semaphore);
}
}
void Sem_V(sem_t semaphore)
{
if(semaphore)
{
SDL_SemPost((SDL_sem *) semaphore);
}
}
#endif