unix,win: wait for threads to start

It appears that, at least on Windows, the process terminates abnormally
when the program exits before the worker threads complete initializing.
Wait for the threads to spin up in `init_threads()` to avoid that.

PR-URL: https://github.com/libuv/libuv/pull/1662
Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jamie Davis <davisjam@vt.edu>
This commit is contained in:
Ben Noordhuis 2017-12-06 23:47:38 +01:00
parent 8a6d1b32c5
commit 647fbc026e

View File

@ -38,7 +38,6 @@ static uv_thread_t* threads;
static uv_thread_t default_threads[4];
static QUEUE exit_message;
static QUEUE wq;
static volatile int initialized;
static void uv__cancelled(struct uv__work* w) {
@ -53,7 +52,8 @@ static void worker(void* arg) {
struct uv__work* w;
QUEUE* q;
(void) arg;
uv_sem_post((uv_sem_t*) arg);
arg = NULL;
for (;;) {
uv_mutex_lock(&mutex);
@ -105,7 +105,7 @@ static void post(QUEUE* q) {
UV_DESTRUCTOR(static void cleanup(void)) {
unsigned int i;
if (initialized == 0)
if (nthreads == 0)
return;
post(&exit_message);
@ -122,7 +122,6 @@ UV_DESTRUCTOR(static void cleanup(void)) {
threads = NULL;
nthreads = 0;
initialized = 0;
}
#endif
@ -130,6 +129,7 @@ UV_DESTRUCTOR(static void cleanup(void)) {
static void init_threads(void) {
unsigned int i;
const char* val;
uv_sem_t sem;
nthreads = ARRAY_SIZE(default_threads);
val = getenv("UV_THREADPOOL_SIZE");
@ -157,11 +157,17 @@ static void init_threads(void) {
QUEUE_INIT(&wq);
if (uv_sem_init(&sem, 0))
abort();
for (i = 0; i < nthreads; i++)
if (uv_thread_create(threads + i, worker, NULL))
if (uv_thread_create(threads + i, worker, &sem))
abort();
initialized = 1;
for (i = 0; i < nthreads; i++)
uv_sem_wait(&sem);
uv_sem_destroy(&sem);
}