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
#include <unistd.h>
10
9
11
- // BDWGC also uses SIGRTMIN+6 on Linux, which seems like a reasonable choice.
12
10
#ifdef __linux__
11
+ #include <semaphore.h>
12
+
13
+ // BDWGC also uses SIGRTMIN+6 on Linux, which seems like a reasonable choice.
13
14
#define taskPauseSignal (SIGRTMIN + 6)
14
- #endif
15
+
16
+ #elif __APPLE__
17
+ #include <dispatch/dispatch.h>
18
+ // Use an arbitrary signal number (31-63) in the hope that it isn't used
19
+ // elsewhere.
20
+ #define taskPauseSignal 60
21
+
22
+ #endif // __linux__, __APPLE__
15
23
16
24
// Pointer to the current task.Task structure.
17
25
// Ideally the entire task.Task structure would be a thread-local variable but
@@ -23,7 +31,11 @@ struct state_pass {
23
31
void * args ;
24
32
void * task ;
25
33
uintptr_t * stackTop ;
34
+ #if __APPLE__
35
+ dispatch_semaphore_t startlock ;
36
+ #else
26
37
sem_t startlock ;
38
+ #endif
27
39
};
28
40
29
41
// Handle the GC pause in Go.
@@ -69,7 +81,11 @@ static void* start_wrapper(void *arg) {
69
81
70
82
// Notify the caller that the thread has successfully started and
71
83
// initialized.
84
+ #if __APPLE__
85
+ dispatch_semaphore_signal (state -> startlock );
86
+ #else
72
87
sem_post (& state -> startlock );
88
+ #endif
73
89
74
90
// Run the goroutine function.
75
91
start (args );
@@ -93,11 +109,19 @@ int tinygo_task_start(uintptr_t fn, void *args, void *task, pthread_t *thread, u
93
109
.task = task ,
94
110
.stackTop = stackTop ,
95
111
};
112
+ #if __APPLE__
113
+ state .startlock = dispatch_semaphore_create (0 );
114
+ #else
96
115
sem_init (& state .startlock , 0 , 0 );
116
+ #endif
97
117
int result = pthread_create (thread , NULL , & start_wrapper , & state );
98
118
99
119
// Wait until the thread has been created and read all state_pass variables.
120
+ #if __APPLE__
121
+ dispatch_semaphore_wait (state .startlock , DISPATCH_TIME_FOREVER );
122
+ #else
100
123
sem_wait (& state .startlock );
124
+ #endif
101
125
102
126
return result ;
103
127
}
0 commit comments