diff options
Diffstat (limited to 'Utilities/cmlibuv/src/threadpool.c')
-rw-r--r-- | Utilities/cmlibuv/src/threadpool.c | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/Utilities/cmlibuv/src/threadpool.c b/Utilities/cmlibuv/src/threadpool.c index 1089341..413d1c2 100644 --- a/Utilities/cmlibuv/src/threadpool.c +++ b/Utilities/cmlibuv/src/threadpool.c @@ -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); } |