1 /****************************************************************************
2 **
3 **  This file is part of GAP, a system for computational discrete algebra.
4 **
5 **  Copyright of GAP belongs to its developers, whose names are too numerous
6 **  to list here. Please refer to the COPYRIGHT file for details.
7 **
8 **  SPDX-License-Identifier: GPL-2.0-or-later
9 */
10 
11 #ifndef GAP_THREAD_H
12 #define GAP_THREAD_H
13 
14 #include "system.h"
15 
16 #if !defined(HPCGAP)
17 
18 /*
19  * HPC-GAP stubs.
20  */
21 
22 #define HashLock(obj)         do { } while(0)
23 #define HashLockShared(obj)   do { } while(0)
24 #define HashUnlock(obj)       do { } while(0)
25 #define HashUnlockShared(obj) do { } while(0)
26 
27 #else
28 
29 #include "hpc/tlsconfig.h"
30 #include "hpc/region.h"
31 
32 /* Maximum number of threads excluding the main thread */
33 #define MAX_THREADS 1023
34 
35 extern int PreThreadCreation;
36 
37 #ifndef HAVE_NATIVE_TLS
38 void *AllocateTLS(void);
39 void FreeTLS(void *address);
40 #endif
41 
42 
43 void AddGCRoots(void);
44 void RemoveGCroots(void);
45 
46 void RunThreadedMain(
47         int (*mainFunction)(int, char **),
48         int argc,
49         char **argv );
50 
51 void CreateMainRegion(void);
52 Obj RunThread(void (*start)(void *), void *arg);
53 int JoinThread(int id);
54 
55 void RegionWriteLock(Region *region);
56 int RegionTryWriteLock(Region *region);
57 void RegionWriteUnlock(Region *region);
58 void RegionReadLock(Region *region);
59 int RegionTryReadLock(Region *region);
60 void RegionReadUnlock(Region *region);
61 void RegionUnlock(Region *region);
62 Region *CurrentRegion(void);
63 Region *GetRegionOf(Obj obj);
64 extern Region *LimboRegion, *ReadOnlyRegion;
65 extern Obj PublicRegionName;
66 
67 void SetRegionName(Region *region, Obj name);
68 Obj GetRegionName(Region *region);
69 Obj GetRegionLockCounters(Region *region);
70 void ResetRegionLockCounters(Region *region);
71 
72 void LockThreadControl(int modify);
73 void UnlockThreadControl(void);
74 
75 void GCThreadHandler(void);
76 
77 void InitMainThread(void);
78 
79 typedef enum LockQual {
80     LOCK_QUAL_NONE = 0,
81     LOCK_QUAL_READONLY = 1,
82     LOCK_QUAL_READWRITE = 2,
83 } LockQual;
84 
85 typedef enum LockStatus {
86     LOCK_STATUS_UNLOCKED = 0,
87     LOCK_STATUS_READWRITE_LOCKED = 1,
88     LOCK_STATUS_READONLY_LOCKED = 2,
89 } LockStatus;
90 
91 LockStatus IsLocked(Region *region);
92 void GetLockStatus(int count, Obj *objects, LockStatus *status);
93 
94 
95 typedef enum LockMode {
96     LOCK_MODE_READONLY = 0,
97     LOCK_MODE_READWRITE = 1,
98     LOCK_MODE_DEFAULT = LOCK_MODE_READWRITE,
99 } LockMode;
100 
101 int LockObject(Obj obj, LockMode mode);
102 int LockObjects(int count, Obj * objects, const LockMode * mode);
103 int TryLockObjects(int count, Obj * objects, const LockMode * mode);
104 
105 void PushRegionLock(Region *region);
106 void PopRegionLocks(int newSP);
107 int RegionLockSP(void);
108 
109 void HashLock(void *obj);
110 void HashLockShared(void *obj);
111 void HashUnlock(void *obj);
112 void HashUnlockShared(void *obj);
113 
114 /* Thread state constants and functions */
115 
116 #define TSTATE_SHIFT 3
117 #define TSTATE_MASK ((1 << TSTATE_SHIFT) - 1)
118 #define TSTATE_RUNNING 0
119 #define TSTATE_TERMINATED 1
120 #define TSTATE_BLOCKED 2
121 #define TSTATE_SYSCALL 3
122 
123 #define TSTATE_INTERRUPT 4
124 
125 #define TSTATE_PAUSED 4
126 #define TSTATE_INTERRUPTED 5
127 #define TSTATE_KILLED 6
128 
129 
130 int GetThreadState(int threadID);
131 int UpdateThreadState(int threadID, int oldState, int newState);
132 void KillThread(int threadID);
133 void PauseThread(int threadID);
134 void InterruptThread(int threadID, int handler);
135 void ResumeThread(int threadID);
136 void HandleInterrupts(int locked, Stat stat);
137 int PauseAllThreads(void);
138 void ResumeAllThreads(void);
139 void SetInterruptHandler(int handler, Obj func);
140 
141 #define MAX_INTERRUPT 100
142 
143 #endif // HPCGAP
144 
145 #endif // GAP_THREAD_H
146