-
Notifications
You must be signed in to change notification settings - Fork 1
/
thread.cpp
203 lines (177 loc) · 3.01 KB
/
thread.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
/*
* Licensed under CLIFE license. See LICENCE.TXT
*
* Produced by: Jeff Lait
*
* CLIFE Development
*
* NAME: thread.cpp ( CLIFE, C++ )
*
* COMMENTS:
* Attempt at a threading class.
*/
#ifndef _3DS
#include "thread.h"
#ifdef LINUX
#include "thread_linux.h"
#ifndef iPOWDER
#include <sys/sysinfo.h>
#endif
#else
#include "thread_win.h"
#endif
THREAD *
THREAD::alloc()
{
#ifdef LINUX
return new THREAD_LINUX();
#else
return new THREAD_WIN();
#endif
}
int
THREAD::numProcessors()
{
static int nproc = -1;
if (nproc < 0)
{
#ifdef LINUX
#ifdef iPOWDER
nproc = 1;
#else
nproc = get_nprocs_conf();
#endif
#else
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
nproc = sysinfo.dwNumberOfProcessors;
#endif
// Zany users have managed to muck with thier registery to
// get 0 procs returned...
if (nproc < 1)
nproc = 1;
}
return nproc;
}
void *
THREAD::wrapper(void *data)
{
THREAD *thread = (THREAD *) data;
while (1)
{
// Await the thread to be ready.
thread->waittillimready();
thread->setActive(true);
// Hurray! Trigger callback.
if (thread->myCB)
thread->myCB(thread->myCBData);
thread->iamdonenow();
thread->setActive(false);
}
return (void *)1;
}
LOCK::LOCK()
{
#ifdef LINUX
pthread_mutexattr_init(&myLockAttr);
#ifdef iPOWDER
pthread_mutexattr_settype(&myLockAttr, PTHREAD_MUTEX_RECURSIVE);
#else
pthread_mutexattr_settype(&myLockAttr, PTHREAD_MUTEX_RECURSIVE_NP);
#endif
pthread_mutex_init(&myLock, &myLockAttr);
#else
myLock = new CRITICAL_SECTION;
::InitializeCriticalSection(myLock);
#endif
}
LOCK::~LOCK()
{
#ifdef LINUX
pthread_mutex_destroy(&myLock);
pthread_mutexattr_destroy(&myLockAttr);
#else
DeleteCriticalSection(myLock);
delete myLock;
#endif
}
bool
LOCK::tryToLock()
{
#ifdef LINUX
return !pthread_mutex_trylock(&myLock);
#else
return ::TryEnterCriticalSection(myLock) ? true : false;
#endif
}
void
LOCK::lock()
{
#ifdef LINUX
pthread_mutex_lock(&myLock);
#else
::EnterCriticalSection(myLock);
#endif
}
void
LOCK::unlock()
{
#ifdef LINUX
pthread_mutex_unlock(&myLock);
#else
::LeaveCriticalSection(myLock);
#endif
}
bool
LOCK::isLocked()
{
if (tryToLock())
{
unlock();
return false;
}
return true;
}
CONDITION::CONDITION()
{
#ifdef LINUX
pthread_cond_init(&myCond, 0);
#else
myEvent = CreateEvent(0, false, false, 0);
myNumWaiting.set(0);
#endif
}
CONDITION::~CONDITION()
{
#ifdef LINUX
pthread_cond_destroy(&myCond);
#else
CloseHandle(myEvent);
#endif
}
void
CONDITION::wait(LOCK &lock)
{
#ifdef LINUX
pthread_cond_wait(&myCond, &lock.myLock);
#else
myNumWaiting.add(1);
lock.unlock();
WaitForSingleObject(myEvent, INFINITE);
myNumWaiting.add(-1);
lock.lock();
#endif
}
void
CONDITION::trigger()
{
#ifdef LINUX
pthread_cond_signal(&myCond);
#else
// This requires the caller has the lock active so no one
// will start a wait during our test.
if (myNumWaiting > 0)
SetEvent(myEvent);
#endif
}
#endif