2
2
3
3
#define _GNU_SOURCE
4
4
#include <pthread.h>
5
- #include <semaphore.h>
6
5
#include <signal.h>
7
6
#include <stdint.h>
8
7
#include <stdio.h>
9
8
10
- // BDWGC also uses SIGRTMIN+6 on Linux, which seems like a reasonable choice.
11
9
#ifdef __linux__
10
+ #include <semaphore.h>
11
+
12
+ // BDWGC also uses SIGRTMIN+6 on Linux, which seems like a reasonable choice.
12
13
#define taskPauseSignal (SIGRTMIN + 6)
13
- #endif
14
+
15
+ #elif __APPLE__
16
+ #include <dispatch/dispatch.h>
17
+ // Use an arbitrary signal number (31-63) in the hope that it isn't used
18
+ // elsewhere.
19
+ #define taskPauseSignal 60
20
+
21
+ #endif // __linux__, __APPLE__
14
22
15
23
// Pointer to the current task.Task structure.
16
24
// Ideally the entire task.Task structure would be a thread-local variable but
@@ -22,7 +30,11 @@ struct state_pass {
22
30
void * args ;
23
31
void * task ;
24
32
uintptr_t * stackTop ;
33
+ #if __APPLE__
34
+ dispatch_semaphore_t startlock ;
35
+ #else
25
36
sem_t startlock ;
37
+ #endif
26
38
};
27
39
28
40
// Handle the GC pause in Go.
@@ -60,7 +72,11 @@ static void* start_wrapper(void *arg) {
60
72
61
73
// Notify the caller that the thread has successfully started and
62
74
// initialized.
75
+ #if __APPLE__
76
+ dispatch_semaphore_signal (state -> startlock );
77
+ #else
63
78
sem_post (& state -> startlock );
79
+ #endif
64
80
65
81
// Run the goroutine function.
66
82
start (args );
@@ -84,11 +100,19 @@ int tinygo_task_start(uintptr_t fn, void *args, void *task, pthread_t *thread, u
84
100
.task = task ,
85
101
.stackTop = stackTop ,
86
102
};
103
+ #if __APPLE__
104
+ state .startlock = dispatch_semaphore_create (0 );
105
+ #else
87
106
sem_init (& state .startlock , 0 , 0 );
107
+ #endif
88
108
int result = pthread_create (thread , NULL , & start_wrapper , & state );
89
109
90
110
// Wait until the thread has been created and read all state_pass variables.
111
+ #if __APPLE__
112
+ dispatch_semaphore_wait (state .startlock , DISPATCH_TIME_FOREVER );
113
+ #else
91
114
sem_wait (& state .startlock );
115
+ #endif
92
116
93
117
return result ;
94
118
}
0 commit comments