1
1
module par.semaphore
2
2
3
+ import std.compilerInfo
3
4
import config
4
5
6
+ datatype TimeSpecT
7
+ tv_sec: Int
8
+ tv_nsec: Long
9
+
5
10
//! Allows a limited numbers of threads to simultaneously "acquire" a resource
6
11
//! before releasing it.
7
12
//! Compared to the mutex, we can release the semaphore without acquiring it
@@ -10,35 +15,59 @@ datatype Semaphore
10
15
11
16
fun ctor(this: @Semaphore, startValue: UInt = 0)
12
17
_handle ctor
13
- var res = _Impl.semaphore_create(_Impl.mach_task_self(), _handle, _Impl.SYNC_POLICY_FIFO, Int(startValue))
18
+ [ct] if platformName == "Darwin"
19
+ var res = _Impl.semaphore_create(_Impl.mach_task_self(), _handle, _Impl.SYNC_POLICY_FIFO, Int(startValue))
20
+ else
21
+ var res = _Impl.sem_init(_handle, Int(0), UInt(startValue))
14
22
15
23
fun dtor(this: @Semaphore)
16
- _Impl.semaphore_destroy(_Impl.mach_task_self(), _handle)
24
+ [ct] if platformName == "Darwin"
25
+ _Impl.semaphore_destroy(_Impl.mach_task_self(), _handle)
26
+ else
27
+ _Impl.sem_destroy(_handle)
17
28
18
29
//! Increments the counter of the semaphore
19
30
fun release(s: @Semaphore)
20
- _Impl.semaphore_signal(s._handle)
31
+ [ct] if platformName == "Darwin"
32
+ _Impl.semaphore_signal(s._handle)
33
+ else
34
+ _Impl.sem_post(s._handle)
21
35
22
36
//! Decrements the semaphore counter
23
37
//! If the counter reaches zero, the call blocks until somebody calls 'release'
24
38
fun acquire(s: @Semaphore)
25
- while 0 != _Impl.semaphore_wait(s._handle)
26
- /*keep trying*/;
39
+ [ct] if platformName == "Darwin"
40
+ while 0 != _Impl.semaphore_wait(s._handle)
41
+ /* keep trying */;
42
+ else
43
+ while 0 != _Impl.sem_wait(s._handle)
44
+ /* keep trying */;
27
45
28
46
package _Impl
29
- using TaskT = Int
30
- using SemaphoreT = Int
31
- using SYNC_POLICY_FIFO = 0
47
+ [ct] if platformName == "Darwin"
48
+ using TaskT = Int
49
+ using SYNC_POLICY_FIFO = 0
32
50
33
- [native("mach_task_self")] fun mach_task_self(): TaskT;
34
- [native("semaphore_create")] fun semaphore_create(task: TaskT, s: @SemaphoreT, policy, value: Int): Int;
35
- [native("semaphore_destroy")] fun semaphore_destroy(task: TaskT, s: SemaphoreT): Int;
36
- [native("semaphore_signal")] fun semaphore_signal(s: SemaphoreT): Int;
37
- [native("semaphore_wait")] fun semaphore_wait(s: SemaphoreT): Int;
51
+ using SemaphoreT = Int
38
52
39
- //using SemType = Int;
53
+ [ct] if platformName == "Linux"
54
+ using ModeT = Int
40
55
41
- //[native("sem_init")] fun sem_init(s: @SemType, pshared: Int, value: UInt): Int;
42
- //[native("sem_destroy")] fun sem_destroy(s: @SemType): Int;
43
- //[native("sem_wait")] fun sem_wait(s: @SemType): Int;
44
- //[native("sem_post")] fun sem_post(s: @SemType): Int;
56
+ [ct] if platformName == "Darwin"
57
+ [native("mach_task_self")] fun mach_task_self(): TaskT;
58
+ [native("semaphore_create")] fun semaphore_create(task: TaskT, s: @SemaphoreT, policy, value: Int): Int;
59
+ [native("semaphore_destroy")] fun semaphore_destroy(task: TaskT, s: SemaphoreT): Int;
60
+ [native("semaphore_signal")] fun semaphore_signal(s: SemaphoreT): Int;
61
+ [native("semaphore_wait")] fun semaphore_wait(s: SemaphoreT): Int;
62
+ else
63
+ [native("sem_init")] fun sem_init(sem: @SemaphoreT, pshared: Int, value: UInt): Int
64
+ [native("sem_destroy")] fun sem_destroy(sem: @SemaphoreT): Int
65
+ [native("sem_wait")] fun sem_wait(sem: @SemaphoreT): Int
66
+ [native("sem_trywait")] fun sem_trywait(sem: @SemaphoreT): Int
67
+ [native("sem_timedwait")] fun sem_timedwait(sem: @SemaphoreT, timeout: @TimeSpecT): Int
68
+ [native("sem_post")] fun sem_post(sem: @SemaphoreT): Int
69
+ [native("sem_getvalue")] fun sem_getvalue(sem: @SemaphoreT, sval: @Int): Int
70
+ [native("sem_close")] fun sem_close(sem: @SemaphoreT): Int
71
+ [native("sem_unlink")] fun sem_unlink(name: @Char): Int
72
+ [native("sem_open")] fun sem_open(name: @Char, oflag: Int): @SemaphoreT
73
+ [native("sem_open")] fun sem_open(name: @Char, oflag: Int, mode: ModeT, value: UInt): @SemaphoreT
0 commit comments