-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathThread.clcl
202 lines (154 loc) · 4.96 KB
/
Thread.clcl
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
class pthread_mutex_t
{
include MObjectBase;
def initialize() {}
def allocSize(): static native size_t;
def clone():pthread_mutex_t {
result := pthread_mutex_t();
memcpy(result.headOfMemory(), self.headOfMemory(), allocSize());
return result;
}
}
class pthread_cond_t
{
include MObjectBase;
def initialize() {}
def allocSize(): static native size_t;
def clone():pthread_cond_t {
result := pthread_cond_t();
memcpy(result.headOfMemory(), self.headOfMemory(), allocSize());
return result;
}
}
class timespec
{
include MObjectBase;
tv_sec: time_t;
tv_nsec: long;
def initialize() {
self.tv_sec = 0;
self.tv_nsec = 0.to_long;
}
def initialize(tv_sec: time_t, tv_nsec:long) {
self.tv_sec = tv_sec;
self.tv_nsec = tv_nsec;
}
def clone():timespec {
result := timespec();
result.tv_sec = self.tv_sec;
result.tv_nsec = self.tv_nsec;
return result;
}
def minus(right:timespec):timespec {
result:timespec = timespec();
result.tv_sec = self.tv_sec - right.tv_sec;
result.tv_nsec = self.tv_nsec - right.tv_nsec;
return result;
}
}
inherit System
{
typedef pid_t int;
def gettid():static native pid_t;
typedef pthread_mutexattr_t uint;
typedef pthread_condattr_t uint;
def initialize_thread_system(): static native;
def initialize(): static {
initialize_thread_system();
}
EINVAL: static int;
EDEADLK: static int;
EBUSY: static int;
EPERM: static int;
EBUSY: static int;
ETIMEDOUT: static int;
EINTR: static int;
PTHREAD_MUTEX_RECURSIVE: static uint;
PTHREAD_MUTEX_ERRORCHECK: static uint;
def pthread_mutex_init(mutex:pthread_mutex_t, attr:pthread_mutexattr_t): static native;
def pthread_mutex_destroy(mutex:pthread_mutex_t): static native int throws Exception;
def pthread_mutex_lock(mutex:pthread_mutex_t): static native int throws Exception;
def pthread_mutex_unlock(mutex:pthread_mutex_t): static native int throws Exception;
def pthread_mutex_trylock(mutex:pthread_mutex_t): static native int;
def pthread_mutexattr_init(attr:pthread_mutexattr_t): static native;
def pthread_mutexattr_settype(attr:pthread_mutexattr_t, kind:int): static native throws Exception;
def pthread_mutexattr_destroy(attr:pthread_mutexattr_t): static native;
def pthread_mutexattr_gettype(attr:pthread_mutexattr_t, kind:pointer@int): static native;
def pthread_cond_init(cond:pthread_cond_t, cond_attr:pthread_condattr_t): static native;
def pthread_cond_signal(cond:pthread_cond_t): static native;
def pthread_cond_broadcast(cond:pthread_cond_t): static native;
def pthread_cond_wait(cond:pthread_cond_t, mutex:pthread_mutex_t): static native;
def pthread_cond_timedwait(cond:pthread_cond_t, mutex:pthread_mutex_t, abtime:timespec): static native int throws Exception;
def pthread_cond_destroy(cond:pthread_cond_t): static native int throws Exception;
}
class Thread
{
include MObjectBase;
typedef pthread_t ulong;
thread_id:pthread_t;
block:lambda()?;
def initialize_thread(thread:Thread, block:lambda()): static native throws Exception;
def pthread_join(thread_id:pthread_t): static native throws Exception;
def initialize() {
self.thread_id = 0;
block = null;
}
def initialize(block:lambda()) throws Exception {
Thread.initialize_thread(self, block);
}
def join() throws Exception {
Thread.pthread_join(thread_id);
}
def clone():Thread {
result := Thread();
result.thread_id = self.thread_id;
result.block = self.block;
return result;
}
}
class Mutex
{
include MObjectBase;
mutex: pthread_mutex_t;
attr: pthread_mutexattr_t;
def initialize() {
mutex = pthread_mutex_t();
pthread_mutexattr_init(attr);
pthread_mutex_init(mutex, attr);
}
def initialize(recursive:bool, error_check:bool) {
mutex = pthread_mutex_t();
pthread_mutexattr_init(attr);
if(recursive) {
pthread_mutexattr_settype(attr, PTHREAD_MUTEX_RECURSIVE);
}
elif(error_check) {
pthread_mutexattr_settype(attr, PTHREAD_MUTEX_ERRORCHECK);
}
pthread_mutex_init(mutex, attr);
}
def finalize() {
if(mutex.identifyWith(null).negative()) {
pthread_mutex_destroy(mutex);
}
}
def clone():Mutex {
result := Mutex();
if(self.mutex.identifyWith(null).negative()) {
result.mutex = self.mutex.clone();
}
result.attr = self.attr;
return result;
}
def lock() {
pthread_mutex_lock(mutex);
}
def unlock() {
pthread_mutex_unlock(mutex);
}
def run(block:lambda()) {
pthread_mutex_lock(mutex);
block();
pthread_mutex_unlock(mutex);
}
}