Skip to content

Commit a38829c

Browse files
aykevldeadprogram
authored andcommitted
internal/task: use -stack-size flag when starting a new thread
Found this bug while trying to use the upstream testing package instead of our own. The io/fs package wasn't passing, because the test was run in a separate goroutine (and therefore a separate thread, with its own stack) instead of all in the same thread with our own stack creation/switching implementation.
1 parent 1dbc6c8 commit a38829c

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/internal/task/task_threads.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static void* start_wrapper(void *arg) {
101101
};
102102

103103
// Start a new goroutine in an OS thread.
104-
int tinygo_task_start(uintptr_t fn, void *args, void *task, pthread_t *thread, uintptr_t *stackTop, void *context) {
104+
int tinygo_task_start(uintptr_t fn, void *args, void *task, pthread_t *thread, uintptr_t *stackTop, uintptr_t stackSize, void *context) {
105105
// Sanity check. Should get optimized away.
106106
if (sizeof(pthread_t) != sizeof(void*)) {
107107
__builtin_trap();
@@ -118,7 +118,11 @@ int tinygo_task_start(uintptr_t fn, void *args, void *task, pthread_t *thread, u
118118
#else
119119
sem_init(&state.startlock, 0, 0);
120120
#endif
121-
int result = pthread_create(thread, NULL, &start_wrapper, &state);
121+
pthread_attr_t attrs;
122+
pthread_attr_init(&attrs);
123+
pthread_attr_setstacksize(&attrs, stackSize);
124+
int result = pthread_create(thread, &attrs, &start_wrapper, &state);
125+
pthread_attr_destroy(&attrs);
122126

123127
// Wait until the thread has been created and read all state_pass variables.
124128
#if __APPLE__

src/internal/task/task_threads.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func start(fn uintptr, args unsafe.Pointer, stackSize uintptr) {
109109
// and the stop-the-world GC won't see threads that haven't started yet or
110110
// are not fully started yet.
111111
activeTaskLock.Lock()
112-
errCode := tinygo_task_start(fn, args, t, &t.state.thread, &t.state.stackTop)
112+
errCode := tinygo_task_start(fn, args, t, &t.state.thread, &t.state.stackTop, stackSize)
113113
if errCode != 0 {
114114
runtimePanic("could not start thread")
115115
}
@@ -266,7 +266,7 @@ func tinygo_task_init(t *Task, thread *threadID, numCPU *int32)
266266
// Here same as for tinygo_task_init.
267267
//
268268
//go:linkname tinygo_task_start tinygo_task_start
269-
func tinygo_task_start(fn uintptr, args unsafe.Pointer, t *Task, thread *threadID, stackTop *uintptr) int32
269+
func tinygo_task_start(fn uintptr, args unsafe.Pointer, t *Task, thread *threadID, stackTop *uintptr, stackSize uintptr) int32
270270

271271
// Pause the thread by sending it a signal.
272272
//

0 commit comments

Comments
 (0)