-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy paththreadapi.c
More file actions
2718 lines (2400 loc) · 72.3 KB
/
threadapi.c
File metadata and controls
2718 lines (2400 loc) · 72.3 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** This file is part of GAP, a system for computational discrete algebra.
**
** Copyright of GAP belongs to its developers, whose names are too numerous
** to list here. Please refer to the COPYRIGHT file for details.
**
** SPDX-License-Identifier: GPL-2.0-or-later
**
** This file contains the GAP interface for thread primitives.
*/
#include "threadapi.h"
#include "../bool.h"
#include "../calls.h"
#include "../code.h"
#include "../error.h"
#include "../funcs.h"
#include "../gvars.h"
#include "../io.h"
#include "../lists.h"
#include "../modules.h"
#include "../objects.h"
#include "../plist.h"
#include "../precord.h"
#include "../read.h"
#include "../records.h"
#include "../set.h"
#include "../stats.h"
#include "../stringobj.h"
#include "../trycatch.h"
#include "../vars.h"
#include "guards.h"
#include "misc.h"
#include "region.h"
#include "thread.h"
#include "tls.h"
#include "traverse.h"
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <pthread.h>
#define RequireThread(funcname, op, argname) \
RequireArgumentConditionEx(funcname, op, "<" argname ">", \
TNUM_OBJ(op) == T_THREAD, \
"must be a thread object")
#define RequireChannel(funcname, op) \
RequireArgumentCondition(funcname, op, IsChannel(op), "must be a channel")
#define RequireSemaphore(funcname, op) \
RequireArgumentCondition(funcname, op, TNUM_OBJ(op) == T_SEMAPHORE, \
"must be a semaphore")
#define RequireBarrier(funcname, op) \
RequireArgumentCondition(funcname, op, IsBarrier(op), "must be a barrier")
#define RequireSyncVar(funcname, op) \
RequireArgumentCondition(funcname, op, IsSyncVar(op), \
"must be a synchronization variable")
struct WaitList {
struct WaitList * prev;
struct WaitList * next;
ThreadLocalStorage * thread;
};
typedef struct {
pthread_mutex_t lock;
struct WaitList *head, *tail;
} Monitor;
typedef struct Channel {
Obj monitor;
Obj queue;
int waiting;
int dynamic;
UInt head, tail;
UInt size, capacity;
} Channel;
typedef struct Semaphore {
Obj monitor;
UInt count;
int waiting;
} Semaphore;
typedef struct Barrier {
Obj monitor;
UInt count;
UInt phase;
int waiting;
} Barrier;
typedef struct SyncVar {
Obj monitor;
Obj value;
int written;
} SyncVar;
static void AddWaitList(Monitor * monitor, struct WaitList * node)
{
if (monitor->tail) {
monitor->tail->next = node;
node->prev = monitor->tail;
node->next = NULL;
monitor->tail = node;
}
else {
monitor->head = monitor->tail = node;
node->next = node->prev = NULL;
}
}
static void RemoveWaitList(Monitor * monitor, struct WaitList * node)
{
if (monitor->head) {
if (node->prev)
node->prev->next = node->next;
else
monitor->head = node->next;
if (node->next)
node->next->prev = node->prev;
else
monitor->tail = node->prev;
}
}
#ifndef WARD_ENABLED
static inline void * ObjPtr(Obj obj)
{
return PTR_BAG(obj);
}
#endif
Obj NewThreadObject(UInt id)
{
Obj result = NewBag(T_THREAD, sizeof(ThreadObject));
ThreadObject *thread = (ThreadObject *)ADDR_OBJ(result);
thread->id = id;
return result;
}
static inline Int ThreadID(Obj obj)
{
GAP_ASSERT(TNUM_OBJ(obj) == T_THREAD);
const ThreadObject *thread = (const ThreadObject *)CONST_ADDR_OBJ(obj);
return thread->id;
}
Obj NewMonitor(void)
{
Bag monitorBag;
Monitor * monitor;
monitorBag = NewBag(T_MONITOR, sizeof(Monitor));
monitor = ObjPtr(monitorBag);
pthread_mutex_init(&monitor->lock, 0);
monitor->head = monitor->tail = NULL;
return monitorBag;
}
static void LockThread(ThreadLocalStorage * thread)
{
pthread_mutex_lock(thread->threadLock);
}
static void UnlockThread(ThreadLocalStorage * thread)
{
pthread_mutex_unlock(thread->threadLock);
}
static void SignalThread(ThreadLocalStorage * thread)
{
pthread_cond_signal(thread->threadSignal);
}
static void WaitThreadSignal(void)
{
int id = TLS(threadID);
if (!UpdateThreadState(id, TSTATE_RUNNING, TSTATE_BLOCKED))
HandleInterrupts(1, 0);
pthread_cond_wait(TLS(threadSignal), TLS(threadLock));
if (!UpdateThreadState(id, TSTATE_BLOCKED, TSTATE_RUNNING) &&
GetThreadState(id) != TSTATE_RUNNING)
HandleInterrupts(1, 0);
}
void LockMonitor(Monitor * monitor)
{
pthread_mutex_lock(&monitor->lock);
}
int TryLockMonitor(Monitor * monitor)
{
return !pthread_mutex_trylock(&monitor->lock);
}
static void UnlockMonitor(Monitor * monitor)
{
pthread_mutex_unlock(&monitor->lock);
}
/****************************************************************************
**
*F WaitForMonitor(monitor) . . . . . .. . wait for a monitor to be ready
**
** 'WaitForMonitor' waits for the monitor to be signaled by another
** thread. The monitor must be locked upon entry and will be locked
** again upon exit.
*/
static void WaitForMonitor(Monitor * monitor)
{
struct WaitList node;
node.thread = GetTLS();
AddWaitList(monitor, &node);
UnlockMonitor(monitor);
LockThread(GetTLS());
while (!TLS(acquiredMonitor))
WaitThreadSignal();
if (!TryLockMonitor(monitor)) {
UnlockThread(GetTLS());
LockMonitor(monitor);
LockThread(GetTLS());
}
TLS(acquiredMonitor) = NULL;
RemoveWaitList(monitor, &node);
UnlockThread(GetTLS());
}
static int ChannelOrder(const void * c1, const void * c2)
{
const char * p1 = (const char *)ObjPtr((*(Channel **)c1)->monitor);
const char * p2 = (const char *)ObjPtr((*(Channel **)c2)->monitor);
return p1 < p2;
}
static void SortChannels(UInt count, Channel ** channels)
{
MergeSort(channels, count, sizeof(Channel *), ChannelOrder);
}
static int MonitorsAreSorted(UInt count, Monitor ** monitors)
{
UInt i;
for (i = 1; i < count; i++)
if ((char *)(monitors[i - 1]) > (char *)(monitors[i]))
return 0;
return 1;
}
void LockMonitors(UInt count, Monitor ** monitors)
{
UInt i;
assert(MonitorsAreSorted(count, monitors));
for (i = 0; i < count; i++)
LockMonitor(monitors[i]);
}
void UnlockMonitors(UInt count, Monitor ** monitors)
{
UInt i;
for (i = 0; i < count; i++)
UnlockMonitor(monitors[i]);
}
/****************************************************************************
**
*F WaitForAnyMonitor(count, monitors) . . wait for a monitor to be ready
**
** 'WaitForAnyMonitor' waits for any one of the monitors in the list to
** be signaled. The function returns when any of them is signaled via
** 'SignalMonitor'. The first argument is the number of monitors in the
** list, the second argument is an array of monitor pointers.
**
** The list must be sorted by 'MonitorOrder' before passing it to the
** function; all monitors must also be locked before calling the function
** by calling 'LockMonitors'.
**
** Upon return, all monitors but the one that was signaled will be
** unlocked.
*/
UInt WaitForAnyMonitor(UInt count, Monitor ** monitors)
{
struct WaitList * nodes;
Monitor * monitor;
UInt i;
Int result;
assert(MonitorsAreSorted(count, monitors));
nodes = alloca(sizeof(struct WaitList) * count);
for (i = 0; i < count; i++)
nodes[i].thread = GetTLS();
for (i = 0; i < count; i++)
AddWaitList(monitors[i], &nodes[i]);
for (i = 0; i < count; i++)
UnlockMonitor(monitors[i]);
LockThread(GetTLS());
while (!TLS(acquiredMonitor))
WaitThreadSignal();
monitor = TLS(acquiredMonitor);
UnlockThread(GetTLS());
// The following loops will initialize <result>, but the compiler
// cannot know this; to avoid warnings, we set result to an
// initial nonsense value.
result = -1;
for (i = 0; i < count; i++) {
LockMonitor(monitors[i]);
if (monitors[i] == monitor) {
RemoveWaitList(monitors[i], &nodes[i]);
result = i;
// keep it locked for further processing by caller
}
else {
RemoveWaitList(monitors[i], &nodes[i]);
UnlockMonitor(monitors[i]);
}
}
LockThread(GetTLS());
TLS(acquiredMonitor) = NULL;
UnlockThread(GetTLS());
return result;
}
/****************************************************************************
**
*F SignalMonitor(monitor) . . . . . . . . . . send a signal to a monitor
**
** Sends a signal to a monitor that is being waited for by another thread.
** The monitor must be locked upon entry and will be locked again upon
** exit. If no thread is waiting for the monitor, no operation will occur.
*/
void SignalMonitor(Monitor * monitor)
{
struct WaitList * queue = monitor->head;
while (queue != NULL) {
ThreadLocalStorage * thread = queue->thread;
LockThread(thread);
if (!thread->acquiredMonitor) {
thread->acquiredMonitor = monitor;
SignalThread(thread);
UnlockThread(thread);
break;
}
UnlockThread(thread);
queue = queue->next;
}
}
static Obj ArgumentError(const char * message)
{
ErrorQuit(message, 0, 0);
return 0;
}
static int GetThreadID(const char * funcname, Obj thread)
{
if (IS_INTOBJ(thread)) {
Int id = INT_INTOBJ(thread);
if (0 <= id && id < MAX_THREADS)
return id;
}
else if (TNUM_OBJ(thread) == T_THREAD) {
return ThreadID(thread);
}
RequireArgumentEx(funcname, thread, NICE_ARGNAME(thread),
"must be a thread object or an integer between 0 and "
"MAX_THREADS - 1");
}
// TODO: register globals
static Obj FirstKeepAlive;
static Obj LastKeepAlive;
static pthread_mutex_t KeepAliveLock;
#define KEPTALIVE(obj) (ADDR_OBJ(obj)[1])
#define PREV_KEPT(obj) (ADDR_OBJ(obj)[2])
#define NEXT_KEPT(obj) (ADDR_OBJ(obj)[3])
Obj KeepAlive(Obj obj)
{
Obj newKeepAlive = NEW_PLIST(T_PLIST, 4);
SET_REGION(newKeepAlive, NULL); // public region
pthread_mutex_lock(&KeepAliveLock);
SET_LEN_PLIST(newKeepAlive, 3);
KEPTALIVE(newKeepAlive) = obj;
PREV_KEPT(newKeepAlive) = LastKeepAlive;
NEXT_KEPT(newKeepAlive) = (Obj)0;
if (LastKeepAlive)
NEXT_KEPT(LastKeepAlive) = newKeepAlive;
else
FirstKeepAlive = LastKeepAlive = newKeepAlive;
pthread_mutex_unlock(&KeepAliveLock);
return newKeepAlive;
}
void StopKeepAlive(Obj node)
{
#ifndef WARD_ENABLED
Obj pred, succ;
pthread_mutex_lock(&KeepAliveLock);
pred = PREV_KEPT(node);
succ = NEXT_KEPT(node);
if (pred)
NEXT_KEPT(pred) = succ;
else
FirstKeepAlive = succ;
if (succ)
PREV_KEPT(succ) = pred;
else
LastKeepAlive = pred;
pthread_mutex_unlock(&KeepAliveLock);
#endif
}
static GVarDescriptor GVarTHREAD_INIT;
static GVarDescriptor GVarTHREAD_EXIT;
static void ThreadedInterpreter(void * funcargs)
{
Obj tmp, func;
int i;
// initialize everything and begin a fresh execution context
tmp = KEPTALIVE(funcargs);
StopKeepAlive(funcargs);
func = ELM_PLIST(tmp, 1);
for (i = 2; i <= LEN_PLIST(tmp); i++) {
Obj item = ELM_PLIST(tmp, i);
SET_ELM_PLIST(tmp, i - 1, item);
}
SET_LEN_PLIST(tmp, LEN_PLIST(tmp) - 1);
GAP_TRY
{
Obj init, exit;
if (setjmp(TLS(threadExit)))
return;
init = GVarOptFunction(&GVarTHREAD_INIT);
if (init)
CALL_0ARGS(init);
CallFuncList(func, tmp);
exit = GVarOptFunction(&GVarTHREAD_EXIT);
if (exit)
CALL_0ARGS(exit);
}
GAP_CATCH
{
}
}
/****************************************************************************
**
*F FuncCreateThread ... create a new thread
**
** The function creates a new thread with a new interpreter and executes
** the function passed as an argument in it. It returns an integer that
** is a unique identifier for the thread.
*/
static Obj FuncCreateThread(Obj self, Obj funcargs)
{
Int i, n;
Obj thread;
Obj templist;
n = LEN_PLIST(funcargs);
if (n == 0 || !IS_FUNC(ELM_PLIST(funcargs, 1)))
return ArgumentError(
"CreateThread: Needs at least one function argument");
Obj func = ELM_PLIST(funcargs, 1);
if (NARG_FUNC(func) != n - 1) {
if (NARG_FUNC(func) == 1) {
ErrorMayQuit("CreateThread: <func> expects 1 argument, but got %d", n - 1, 0);
} else {
ErrorMayQuit("CreateThread: <func> expects %d arguments, but got %d", NARG_FUNC(func), n - 1);
}
}
templist = NEW_PLIST(T_PLIST, n);
SET_LEN_PLIST(templist, n);
SET_REGION(templist, NULL); // make it public
for (i = 1; i <= n; i++)
SET_ELM_PLIST(templist, i, ELM_PLIST(funcargs, i));
thread = RunThread(ThreadedInterpreter, KeepAlive(templist));
if (!thread)
return Fail;
return thread;
}
/****************************************************************************
**
*F FuncWaitThread ... wait for a created thread to finish.
**
** The function waits for an existing thread to finish.
*/
static Obj FuncWaitThread(Obj self, Obj obj)
{
const char * error = NULL;
RequireThread(SELF_NAME, obj, "thread");
LockThreadControl(1);
ThreadObject *thread = (ThreadObject *)ADDR_OBJ(obj);
if (thread->status & THREAD_JOINED)
error = "ThreadObject is already being waited for";
thread->status |= THREAD_JOINED;
UnlockThreadControl();
if (error)
ErrorQuit("WaitThread: %s", (UInt)error, 0);
if (!JoinThread(thread->id))
ErrorQuit("WaitThread: Invalid thread id", 0, 0);
return (Obj)0;
}
/****************************************************************************
**
*F FuncCurrentThread ... return thread object of current thread.
**
*/
static Obj FuncCurrentThread(Obj self)
{
return TLS(threadObject);
}
/****************************************************************************
**
*F FuncThreadID ... return numerical thread id of thread.
**
*/
static Obj FuncThreadID(Obj self, Obj thread)
{
RequireThread(SELF_NAME, thread, "thread");
return INTOBJ_INT(ThreadID(thread));
}
/****************************************************************************
**
*F FuncKillThread ... kill a given thread
**
*/
static Obj FuncKillThread(Obj self, Obj thread)
{
int id = GetThreadID("KillThread", thread);
KillThread(id);
return (Obj)0;
}
/****************************************************************************
**
*F FuncInterruptThread ... interrupt a given thread
**
*/
static Obj FuncInterruptThread(Obj self, Obj thread, Obj handler)
{
int id = GetThreadID("InterruptThread", thread);
RequireBoundedInt(SELF_NAME, handler, 0, MAX_INTERRUPT);
InterruptThread(id, (int)(INT_INTOBJ(handler)));
return (Obj)0;
}
/****************************************************************************
**
*F FuncSetInterruptHandler ... set interrupt handler for current thread
**
*/
static Obj FuncSetInterruptHandler(Obj self, Obj handler, Obj func)
{
RequireBoundedInt(SELF_NAME, handler, 0, MAX_INTERRUPT);
if (func == Fail) {
SetInterruptHandler((int)(INT_INTOBJ(handler)), (Obj)0);
return (Obj)0;
}
if (TNUM_OBJ(func) != T_FUNCTION || NARG_FUNC(func) != 0 ||
!BODY_FUNC(func))
RequireArgument(SELF_NAME, func,
"must be a parameterless function or 'fail'");
SetInterruptHandler((int)(INT_INTOBJ(handler)), func);
return (Obj)0;
}
/****************************************************************************
**
*F FuncPauseThread ... pause a given thread
**
*/
static Obj FuncPauseThread(Obj self, Obj thread)
{
int id = GetThreadID("PauseThread", thread);
PauseThread(id);
return (Obj)0;
}
/****************************************************************************
**
*F FuncResumeThread ... resume a given thread
**
*/
static Obj FuncResumeThread(Obj self, Obj thread)
{
int id = GetThreadID("ResumeThread", thread);
ResumeThread(id);
return (Obj)0;
}
/****************************************************************************
**
*F FuncRegionOf ... return region of an object
**
*/
static Obj PublicRegion;
static Obj FuncRegionOf(Obj self, Obj obj)
{
Region * region = GetRegionOf(obj);
return region == NULL ? PublicRegion : region->obj;
}
/****************************************************************************
**
*F FuncSetRegionName ... set the name of an object's region
*F FuncClearRegionName ... clear the name of an object's region
*F FuncRegionName ... get the name of an object's region
**
*/
static Obj FuncSetRegionName(Obj self, Obj obj, Obj name)
{
Region * region = GetRegionOf(obj);
if (!region)
ArgumentError(
"SetRegionName: Cannot change name of the public region");
RequireStringRep(SELF_NAME, name);
SetRegionName(region, name);
return (Obj)0;
}
static Obj FuncClearRegionName(Obj self, Obj obj)
{
Region * region = GetRegionOf(obj);
if (!region)
ArgumentError(
"ClearRegionName: Cannot change name of the public region");
SetRegionName(region, (Obj)0);
return (Obj)0;
}
static Obj FuncRegionName(Obj self, Obj obj)
{
Obj result;
Region * region = GetRegionOf(obj);
result = GetRegionName(region);
if (!result)
result = Fail;
return result;
}
/****************************************************************************
**
*F FuncIsShared ... return whether a region is shared
**
*/
static Obj FuncIsShared(Obj self, Obj obj)
{
Region * region = GetRegionOf(obj);
return (region && !region->fixed_owner) ? True : False;
}
/****************************************************************************
**
*F FuncIsPublic ... return whether a region is public
**
*/
static Obj FuncIsPublic(Obj self, Obj obj)
{
Region * region = GetRegionOf(obj);
return region == NULL ? True : False;
}
/****************************************************************************
**
*F FuncIsThreadLocal ... return whether a region is thread-local
**
*/
static Obj FuncIsThreadLocal(Obj self, Obj obj)
{
Region * region = GetRegionOf(obj);
return (region && region->fixed_owner && region->owner == GetTLS())
? True
: False;
}
/****************************************************************************
**
*F FuncHaveWriteAccess ... return if we have a write lock on the region
**
*/
static Obj FuncHaveWriteAccess(Obj self, Obj obj)
{
Region * region = GetRegionOf(obj);
if (region != NULL &&
(region->owner == GetTLS() || region->alt_owner == GetTLS()))
return True;
else
return False;
}
/****************************************************************************
**
*F FuncHaveReadAccess ... return if we have a read lock on the region
**
*/
static Obj FuncHaveReadAccess(Obj self, Obj obj)
{
Region * region = GetRegionOf(obj);
if (region != NULL && CheckReadAccess(obj))
return True;
else
return False;
}
/****************************************************************************
**
*F FuncHASH_LOCK ........... acquire write lock on an object.
*F FuncHASH_UNLOCK ......... release write lock on an object.
*F FuncHASH_LOCK_SHARED ..... acquire read lock on an object.
*F FuncHASH_UNLOCK_SHARED ... release read lock on an object.
**
*/
static Obj FuncHASH_LOCK(Obj self, Obj target)
{
HashLock(target);
return (Obj)0;
}
static Obj FuncHASH_UNLOCK(Obj self, Obj target)
{
HashUnlock(target);
return (Obj)0;
}
static Obj FuncHASH_LOCK_SHARED(Obj self, Obj target)
{
HashLockShared(target);
return (Obj)0;
}
static Obj FuncHASH_UNLOCK_SHARED(Obj self, Obj target)
{
HashUnlockShared(target);
return (Obj)0;
}
/****************************************************************************
**
*F FuncHASH_SYNCHRONIZED ......... execute a function while holding a write
*lock.
*F FuncHASH_SYNCHRONIZED_SHARED ... execute a function while holding a read
*lock.
**
*/
static Obj FuncHASH_SYNCHRONIZED(Obj self, Obj target, Obj function)
{
HashLock(target);
Call0ArgsInNewReader(function);
HashUnlock(target);
return (Obj)0;
}
static Obj FuncHASH_SYNCHRONIZED_SHARED(Obj self, Obj target, Obj function)
{
HashLockShared(target);
Call0ArgsInNewReader(function);
HashUnlockShared(target);
return (Obj)0;
}
/****************************************************************************
**
*F FuncCREATOR_OF ... return function that created an object
**
*/
static Obj FuncCREATOR_OF(Obj self, Obj obj)
{
#ifdef TRACK_CREATOR
Obj result = NEW_PLIST_IMM(T_PLIST, 2);
SET_LEN_PLIST(result, 2);
if (!IS_BAG_REF(obj)) {
SET_ELM_PLIST(result, 1, Fail);
SET_ELM_PLIST(result, 2, Fail);
return result;
}
if (obj[2])
SET_ELM_PLIST(result, 1, (Obj)(obj[2]));
else
SET_ELM_PLIST(result, 1, Fail);
if (obj[3])
SET_ELM_PLIST(result, 2, (Obj)(obj[3]));
else
SET_ELM_PLIST(result, 2, Fail);
return result;
#else
return Fail;
#endif
}
static Obj FuncDISABLE_GUARDS(Obj self, Obj flag)
{
if (flag == False)
TLS(DisableGuards) = 0;
else if (flag == True)
TLS(DisableGuards) = 1;
else if (IS_INTOBJ(flag))
TLS(DisableGuards) = (int)(INT_INTOBJ(flag));
else
RequireArgument(SELF_NAME, flag,
"must be a boolean or a small integer");
return (Obj)0;
}
static Obj FuncWITH_TARGET_REGION(Obj self, Obj obj, Obj func)
{
Region * volatile oldRegion = TLS(currentRegion);
Region * volatile region = GetRegionOf(obj);
RequireFunction(SELF_NAME, func);
if (!region || !CheckExclusiveWriteAccess(obj))
return ArgumentError(
"WITH_TARGET_REGION: Requires write access to target region");
GAP_TRY
{
TLS(currentRegion) = region;
CALL_0ARGS(func);
TLS(currentRegion) = oldRegion;
}
GAP_CATCH
{
TLS(currentRegion) = oldRegion;
GAP_THROW();
}
return (Obj)0;
}
static Obj TYPE_THREAD;
static Obj TYPE_SEMAPHORE;
static Obj TYPE_CHANNEL;
static Obj TYPE_BARRIER;
static Obj TYPE_SYNCVAR;
static Obj TYPE_REGION;
static Obj TypeThread(Obj obj)
{
return TYPE_THREAD;
}
static Obj TypeSemaphore(Obj obj)
{
return TYPE_SEMAPHORE;
}
static Obj TypeChannel(Obj obj)
{
return TYPE_CHANNEL;
}
static Obj TypeBarrier(Obj obj)
{
return TYPE_BARRIER;
}
static Obj TypeSyncVar(Obj obj)
{
return TYPE_SYNCVAR;
}
static Obj TypeRegion(Obj obj)
{
return TYPE_REGION;
}
#ifdef USE_GASMAN
static void MarkSemaphoreBag(Bag);
static void MarkChannelBag(Bag);
static void MarkBarrierBag(Bag);
static void MarkSyncVarBag(Bag);
#endif
static void FinalizeMonitor(Bag);
static void PrintThread(Obj);
static void PrintSemaphore(Obj);
static void PrintChannel(Obj);
static void PrintBarrier(Obj);
static void PrintSyncVar(Obj);
static void PrintRegion(Obj);
GVarDescriptor LastInaccessibleGVar;
static GVarDescriptor MAX_INTERRUPTGVar;
static UInt RNAM_SIGINT;
static UInt RNAM_SIGCHLD;
static UInt RNAM_SIGVTALRM;
#ifdef SIGWINCH
static UInt RNAM_SIGWINCH;
#endif
#ifndef WARD_ENABLED
#ifdef USE_GASMAN
static void MarkSemaphoreBag(Bag bag)
{
Semaphore * sem = (Semaphore *)(PTR_BAG(bag));
MarkBag(sem->monitor);
}
static void MarkChannelBag(Bag bag)
{
Channel * channel = (Channel *)(PTR_BAG(bag));
MarkBag(channel->queue);
MarkBag(channel->monitor);
}
static void MarkBarrierBag(Bag bag)
{
Barrier * barrier = (Barrier *)(PTR_BAG(bag));
MarkBag(barrier->monitor);
}
static void MarkSyncVarBag(Bag bag)
{
SyncVar * syncvar = (SyncVar *)(PTR_BAG(bag));
MarkBag(syncvar->queue);
MarkBag(syncvar->monitor);
}
#endif
static void FinalizeMonitor(Bag bag)
{
Monitor * monitor = (Monitor *)(PTR_BAG(bag));
pthread_mutex_destroy(&monitor->lock);
}
static void LockChannel(Channel * channel)
{
LockMonitor(ObjPtr(channel->monitor));
}
static void UnlockChannel(Channel * channel)
{
UnlockMonitor(ObjPtr(channel->monitor));
}
static void SignalChannel(Channel * channel)
{
if (channel->waiting)
SignalMonitor(ObjPtr(channel->monitor));
}
static void WaitChannel(Channel * channel)
{
channel->waiting++;
WaitForMonitor(ObjPtr(channel->monitor));
channel->waiting--;