-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathThreading.alusus
More file actions
159 lines (129 loc) · 4.19 KB
/
Threading.alusus
File metadata and controls
159 lines (129 loc) · 4.19 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
/*
* Copyright (C) 2026 Sarmad Abdullah
*
* This file is part of Alusus Threading library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <https://www.gnu.org/licenses/>.
*/
import "Srl/srl";
import "closure";
import "libpthread.so.0";
module Threading {
class _Thread {
}
def Thread: alias ptr[_Thread];
class ThreadAttributes {
def flags: Int;
def stackSize: Int;
def contentionScope: Int;
def inheritSched: Int;
def detachState: Int;
def sched: Int;
def param: SchedParam;
def startTime: TimeSpec;
def deadLine: TimeSpec;
def period: TimeSpec;
}
class SchedParam {
def schedPriority: Int;
}
class TimeSpec {
def tvSec: ArchInt;
def tvNsec: Int[64];
}
class Mutex {
def data: array[Word[8], 40];
handler this.init() {
initMutex(this~ptr, 0);
}
handler this.init(attrs: ref[MutexAttributes]) {
initMutex(this~ptr, attrs~ptr);
}
handler this.lock() {
lockMutex(this~ptr);
}
handler this.unlock() {
unlockMutex(this~ptr);
}
}
class MutexAttributes {
def pshared: Int = 0;
def kind: Int = 0;
def protocol: Int = 0;
def robustness: Int = 0;
}
class Cond {
def data: array[Word[8], 48];
}
class CondAttributes {
def dummy: Int = 0;
}
def Key: alias Int;
class ThreadLocal [Type: type] {
def key: Key;
def initializer: closure(val: ref[Type]);
createKey(key~ptr, destruct~ptr);
handler this~init() {}
handler this~init(initializer: closure(ref[Type])) {
this.initializer = initializer;
}
func destruct(val: ptr) {
val~cast[ptr[Type]]~cnt~terminate();
Memory.free(val);
}
handler this.getValue(): ref[Type] {
def val: ptr = getSpecific(this.key);
if val == 0 {
val = Memory.alloc(Type~size);
val~cast[ptr[Type]]~cnt~init();
if not this.initializer.isNull() this.initializer(val~cast[ptr[Type]]~cnt);
setSpecific(this.key, val);
}
return val~cast[ptr[Type]]~cnt;
}
handler this.value: ref[Type] {
return this.getValue();
}
handler this.value = Type {
this.getValue() = value;
return value;
}
}
@expname[pthread_create]
func createThread(
pthread: ptr[Thread], attr: ptr[ThreadAttributes], startRoutine: ptr[func (ptr): ptr], arg: ptr
): Int;
@expname[pthread_join]
func joinThread(pthread: Thread, retval: ptr[ptr]): Int;
@expname[pthread_mutex_init]
func initMutex(mutex: ptr[Mutex], attrs: ptr[MutexAttributes]): Int;
@expname[pthread_mutex_lock]
func lockMutex(mutex: ptr[Mutex]): Int;
@expname[pthread_mutex_trylock]
func tryLockMutex(mutex: ptr[Mutex]): Int;
@expname[pthread_mutex_unlock]
func unlockMutex(mutex: ptr[Mutex]): Int;
@expname[pthread_cond_init]
func initCond(cond: ptr[Cond], attrs: ptr[CondAttributes]): Int;
@expname[pthread_cond_signal]
func signalCond(cond: ptr[Cond]): Int;
@expname[pthread_cond_wait]
func waitCond(cond: ptr[Cond], mutex: ptr[Mutex]): Int;
@expname[pthread_key_create]
func createKey(key: ptr[Key], destructor: ptr[func(ptr)]): Int;
@expname[pthread_getspecific]
func getSpecific(key: Key): ptr;
@expname[pthread_setspecific]
func setSpecific(key: Key, val: ptr): Int;
}