Skip to content

Commit

Permalink
Fix "failed to suspend thread" error on macOS. Fixes #922
Browse files Browse the repository at this point in the history
  • Loading branch information
nickg committed Jul 22, 2024
1 parent 1d10b51 commit fc30124
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
generic `<>` (#924)
- Fixed a crash when a generic package containing a subprogram is passed
to an entity package generic (#925).
- Fixed an intermittent "failed to suspend thread" fatal error on macOS
(#922).

## Version 1.13.0 - 2024-07-13
- Type and package generics now work correctly with components and
Expand Down
37 changes: 18 additions & 19 deletions src/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,17 +589,9 @@ static DWORD win32_thread_wrapper(LPVOID param)
}
#endif

nvc_thread_t *thread_create(thread_fn_t fn, void *arg, const char *fmt, ...)
static void thread_start(nvc_thread_t *thread)
{
va_list ap;
va_start(ap, fmt);
char *name = xvasprintf(fmt, ap);
va_end(ap);

// Avoid races with stop_world
SCOPED_LOCK(stop_lock);

nvc_thread_t *thread = thread_new(fn, arg, USER_THREAD, name);
assert_lock_held(&stop_lock);

#ifdef __MINGW32__
if ((thread->handle = CreateThread(NULL, 0, win32_thread_wrapper,
Expand All @@ -613,6 +605,20 @@ nvc_thread_t *thread_create(thread_fn_t fn, void *arg, const char *fmt, ...)
#ifdef __APPLE__
thread->port = pthread_mach_thread_np(thread->handle);
#endif
}

nvc_thread_t *thread_create(thread_fn_t fn, void *arg, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
char *name = xvasprintf(fmt, ap);
va_end(ap);

// Avoid races with stop_world
SCOPED_LOCK(stop_lock);

nvc_thread_t *thread = thread_new(fn, arg, USER_THREAD, name);
thread_start(thread);

return thread;
}
Expand Down Expand Up @@ -1113,17 +1119,10 @@ static void create_workers(int needed)
while (relaxed_load(&running_threads) < MIN(max_workers, needed)) {
static int counter = 0;
char *name = xasprintf("worker thread %d", atomic_add(&counter, 1));
SCOPED_LOCK(stop_lock); // Avoid races with stop_world
nvc_thread_t *thread =
thread_new(worker_thread, NULL, WORKER_THREAD, name);

#ifdef __MINGW32__
if ((thread->handle = CreateThread(NULL, 0, win32_thread_wrapper,
thread, 0, NULL)) == NULL)
fatal_errno("CreateThread");
#else
PTHREAD_CHECK(pthread_create, &(thread->handle), NULL,
thread_wrapper, thread);
#endif
thread_start(thread);
}

platform_cond_broadcast(&wake_workers);
Expand Down

0 comments on commit fc30124

Please sign in to comment.