forked from HaxeFoundation/hxcpp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThread.cpp
More file actions
422 lines (355 loc) · 8.8 KB
/
Copy pathThread.cpp
File metadata and controls
422 lines (355 loc) · 8.8 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include <hxcpp.h>
#if (HXCPP_API_LEVEL<500)
#include <hx/Thread.h>
#include <time.h>
#include <hx/thread/ConditionVariable.hpp>
#include <hx/thread/RecursiveMutex.hpp>
#include <hx/thread/CountingSemaphore.hpp>
#include "thread/ThreadImpl.hpp"
#include <atomic>
// --- Deque ----------------------------------------------------------
struct Deque : public Array_obj<Dynamic>
{
Deque() : Array_obj<Dynamic>(0,0) { }
static Deque *Create()
{
Deque *result = new Deque();
result->mFinalizer = new hx::InternalFinalizer(result,clean);
return result;
}
static void clean(hx::Object *inObj)
{
Deque *d = dynamic_cast<Deque *>(inObj);
if (d) d->Clean();
}
void Clean()
{
#ifdef HX_WINDOWS
mMutex.Clean();
#endif
mSemaphore.Clean();
}
#ifdef HXCPP_VISIT_ALLOCS
void __Visit(hx::VisitContext *__inCtx)
{
Array_obj<Dynamic>::__Visit(__inCtx);
mFinalizer->Visit(__inCtx);
}
#endif
#ifndef HX_THREAD_SEMAPHORE_LOCKABLE
HxMutex mMutex;
void PushBack(Dynamic inValue)
{
hx::EnterGCFreeZone();
AutoLock lock(mMutex);
hx::ExitGCFreeZone();
push(inValue);
mSemaphore.Set();
}
void PushFront(Dynamic inValue)
{
hx::EnterGCFreeZone();
AutoLock lock(mMutex);
hx::ExitGCFreeZone();
unshift(inValue);
mSemaphore.Set();
}
Dynamic PopFront(bool inBlock)
{
hx::EnterGCFreeZone();
AutoLock lock(mMutex);
if (!inBlock)
{
hx::ExitGCFreeZone();
return shift();
}
// Ok - wait for something on stack...
while(!length)
{
mSemaphore.Reset();
lock.Unlock();
mSemaphore.Wait();
lock.Lock();
}
hx::ExitGCFreeZone();
if (length==1)
mSemaphore.Reset();
return shift();
}
#else
void PushBack(Dynamic inValue)
{
hx::EnterGCFreeZone();
AutoLock lock(mSemaphore);
hx::ExitGCFreeZone();
push(inValue);
mSemaphore.QSet();
}
void PushFront(Dynamic inValue)
{
hx::EnterGCFreeZone();
AutoLock lock(mSemaphore);
hx::ExitGCFreeZone();
unshift(inValue);
mSemaphore.QSet();
}
Dynamic PopFront(bool inBlock)
{
hx::EnterGCFreeZone();
AutoLock lock(mSemaphore);
while(inBlock && !length)
mSemaphore.QWait();
hx::ExitGCFreeZone();
Dynamic result = shift();
if (length)
mSemaphore.QSet();
return result;
}
#endif
hx::InternalFinalizer *mFinalizer;
HxSemaphore mSemaphore;
};
Dynamic __hxcpp_deque_create()
{
return Deque::Create();
}
void __hxcpp_deque_add(Dynamic q,Dynamic inVal)
{
Deque *d = dynamic_cast<Deque *>(q.mPtr);
if (!d)
throw HX_INVALID_OBJECT;
d->PushBack(inVal);
}
void __hxcpp_deque_push(Dynamic q,Dynamic inVal)
{
Deque *d = dynamic_cast<Deque *>(q.mPtr);
if (!d)
throw HX_INVALID_OBJECT;
d->PushFront(inVal);
}
Dynamic __hxcpp_deque_pop(Dynamic q,bool block)
{
Deque *d = dynamic_cast<Deque *>(q.mPtr);
if (!d)
throw HX_INVALID_OBJECT;
return d->PopFront(block);
}
// --- Thread ----------------------------------------------------------
Dynamic __hxcpp_thread_create(Dynamic inStart)
{
return hx::thread::Thread_obj::create(inStart);
}
Dynamic __hxcpp_thread_current()
{
return hx::thread::Thread_obj::current();
}
void __hxcpp_thread_send(Dynamic inThread, Dynamic inMessage)
{
hx::Throw(HX_CSTRING("Not Implemented"));
}
Dynamic __hxcpp_thread_read_message(bool inBlocked)
{
return hx::Throw(HX_CSTRING("Not Implemented"));
}
bool __hxcpp_is_current_thread(hx::Object *inThread)
{
return inThread == hx::thread::Thread_obj::current();
}
// --- TLS ------------------------------------------------------------
Dynamic __hxcpp_tls_get(int inID)
{
return reinterpret_cast<hx::thread::ThreadImpl_obj*>(hx::thread::Thread_obj::current().GetPtr())->getSlot(inID);
}
void __hxcpp_tls_set(int inID,Dynamic inVal)
{
reinterpret_cast<hx::thread::ThreadImpl_obj*>(hx::thread::Thread_obj::current().GetPtr())->setSlot(inID, inVal);
}
// --- Mutex ------------------------------------------------------------
Dynamic __hxcpp_mutex_create()
{
return new hx::thread::RecursiveMutex_obj();
}
void __hxcpp_mutex_acquire(Dynamic inMutex)
{
auto mutex = inMutex.Cast<hx::thread::RecursiveMutex>();
mutex->acquire();
}
bool __hxcpp_mutex_try(Dynamic inMutex)
{
auto mutex = inMutex.Cast<hx::thread::RecursiveMutex>();
return mutex->tryAcquire();
}
void __hxcpp_mutex_release(Dynamic inMutex)
{
auto mutex = inMutex.Cast<hx::thread::RecursiveMutex>();
mutex->release();
}
// --- Semaphore ------------------------------------------------------------
Dynamic __hxcpp_semaphore_create(int value) {
return new hx::thread::CountingSemaphore_obj(value);
}
void __hxcpp_semaphore_acquire(Dynamic inSemaphore) {
auto semaphore = inSemaphore.Cast<hx::thread::CountingSemaphore>();
semaphore->acquire();
}
bool __hxcpp_semaphore_try_acquire(Dynamic inSemaphore, double timeout) {
auto semaphore = inSemaphore.Cast<hx::thread::CountingSemaphore>();
return semaphore->tryAcquire(timeout);
}
void __hxcpp_semaphore_release(Dynamic inSemaphore) {
auto semaphore = inSemaphore.Cast<hx::thread::CountingSemaphore>();
semaphore->release();
}
// --- Condition ------------------------------------------------------------
Dynamic __hxcpp_condition_create(void)
{
return new hx::thread::ConditionVariable_obj();
}
void __hxcpp_condition_acquire(Dynamic inCond)
{
auto condition = inCond.Cast<hx::thread::ConditionVariable>();
condition->acquire();
}
bool __hxcpp_condition_try_acquire(Dynamic inCond)
{
auto condition = inCond.Cast<hx::thread::ConditionVariable>();
return condition->tryAcquire();
}
void __hxcpp_condition_release(Dynamic inCond)
{
auto condition = inCond.Cast<hx::thread::ConditionVariable>();
condition->release();
}
void __hxcpp_condition_wait(Dynamic inCond)
{
auto condition = inCond.Cast<hx::thread::ConditionVariable>();
condition->wait();
}
bool __hxcpp_condition_timed_wait(Dynamic inCond, double timeout)
{
return hx::Throw(HX_CSTRING("Not Implemented"));
}
void __hxcpp_condition_signal(Dynamic inCond)
{
auto condition = inCond.Cast<hx::thread::ConditionVariable>();
condition->signal();
}
void __hxcpp_condition_broadcast(Dynamic inCond)
{
auto condition = inCond.Cast<hx::thread::ConditionVariable>();
condition->broadcast();
}
// --- Lock ------------------------------------------------------------
class hxLock : public hx::Object
{
public:
hxLock()
{
mFinalizer = new hx::InternalFinalizer(this);
mFinalizer->mFinalizer = clean;
}
HX_IS_INSTANCE_OF enum { _hx_ClassId = hx::clsIdLock };
#ifdef HXCPP_VISIT_ALLOCS
void __Visit(hx::VisitContext *__inCtx) { mFinalizer->Visit(__inCtx); }
#endif
hx::InternalFinalizer *mFinalizer;
#if defined(HX_WINDOWS) || defined(__SNC__)
double Now()
{
return (double)clock()/CLOCKS_PER_SEC;
}
#else
double Now()
{
struct timeval tv;
gettimeofday(&tv,0);
return tv.tv_sec + tv.tv_usec*0.000001;
}
#endif
static void clean(hx::Object *inObj)
{
hxLock *l = dynamic_cast<hxLock *>(inObj);
if (l)
{
l->mNotEmpty.Clean();
l->mAvailableLock.Clean();
}
}
bool Wait(double inTimeout)
{
double stop = 0;
if (inTimeout>=0)
stop = Now() + inTimeout;
while(1)
{
mAvailableLock.Lock();
if (mAvailable)
{
--mAvailable;
if (mAvailable>0)
mNotEmpty.Set();
mAvailableLock.Unlock();
return true;
}
mAvailableLock.Unlock();
double wait = 0;
if (inTimeout>=0)
{
wait = stop-Now();
if (wait<=0)
return false;
}
hx::EnterGCFreeZone();
if (inTimeout<0)
mNotEmpty.Wait( );
else
mNotEmpty.WaitSeconds(wait);
hx::ExitGCFreeZone();
}
}
void Release()
{
AutoLock lock(mAvailableLock);
mAvailable++;
mNotEmpty.Set();
}
HxSemaphore mNotEmpty;
HxMutex mAvailableLock;
int mAvailable;
};
Dynamic __hxcpp_lock_create()
{
return new hxLock;
}
bool __hxcpp_lock_wait(Dynamic inlock,double inTime)
{
hxLock *lock = dynamic_cast<hxLock *>(inlock.mPtr);
if (!lock)
throw HX_INVALID_OBJECT;
return lock->Wait(inTime);
}
void __hxcpp_lock_release(Dynamic inlock)
{
hxLock *lock = dynamic_cast<hxLock *>(inlock.mPtr);
if (!lock)
throw HX_INVALID_OBJECT;
lock->Release();
}
int __hxcpp_GetCurrentThreadNumber()
{
return hx::thread::Thread_obj::id();
}
#endif
// --- Atomic ---
bool _hx_atomic_exchange_if(::cpp::Pointer<cpp::AtomicInt> inPtr, int test, int newVal )
{
return _hx_atomic_compare_exchange(inPtr, test, newVal) == test;
}
int _hx_atomic_inc(::cpp::Pointer<cpp::AtomicInt> inPtr )
{
return _hx_atomic_add(inPtr, 1);
}
int _hx_atomic_dec(::cpp::Pointer<cpp::AtomicInt> inPtr )
{
return _hx_atomic_sub(inPtr, 1);
}