@@ -174,6 +174,7 @@ int moonbitlang_async_job_poll_fd(struct job *job) {
174
174
struct worker {
175
175
pthread_t id ;
176
176
struct job * job ;
177
+ int waiting ;
177
178
#ifdef WAKEUP_METHOD_COND_VAR
178
179
pthread_mutex_t mutex ;
179
180
pthread_cond_t cond ;
@@ -375,21 +376,23 @@ void *worker_loop(void *data) {
375
376
break ;
376
377
}
377
378
}
379
+ self -> waiting = 1 ;
378
380
write (pool .notify_send , & (job -> job_id ), sizeof (int ));
379
381
380
- job = 0 ;
381
382
#ifdef WAKEUP_METHOD_SIGNAL
382
383
sigwait (& pool .wakeup_signal , & sig );
383
384
#elif defined(WAKEUP_METHOD_COND_VAR )
384
385
pthread_mutex_lock (& (self -> mutex ));
386
+ while (self -> waiting ) {
385
387
#ifdef __MACH__
386
- // There's a bug in the MacOS's `pthread_cond_wait`,
387
- // see https://github.com/graphia-app/graphia/issues/33
388
- // We know the arguments must be valid here, so use a loop to work around
389
- while (pthread_cond_wait (& (self -> cond ), & (self -> mutex )) == EINVAL ) {}
388
+ // There's a bug in the MacOS's `pthread_cond_wait`,
389
+ // see https://github.com/graphia-app/graphia/issues/33
390
+ // We know the arguments must be valid here, so use a loop to work around
391
+ while (pthread_cond_wait (& (self -> cond ), & (self -> mutex )) == EINVAL ) {}
390
392
#else
391
- pthread_cond_wait (& (self -> cond ), & (self -> mutex ));
393
+ pthread_cond_wait (& (self -> cond ), & (self -> mutex ));
392
394
#endif
395
+ }
393
396
pthread_mutex_unlock (& (self -> mutex ));
394
397
#endif
395
398
job = self -> job ;
@@ -404,6 +407,7 @@ void moonbitlang_async_wake_worker(struct worker *worker, struct job *job) {
404
407
pthread_kill (worker -> id , SIGUSR1 );
405
408
#elif defined(WAKEUP_METHOD_COND_VAR )
406
409
pthread_mutex_lock (& (worker -> mutex ));
410
+ worker -> waiting = 0 ;
407
411
pthread_cond_signal (& (worker -> cond ));
408
412
pthread_mutex_unlock (& (worker -> mutex ));
409
413
#endif
@@ -459,6 +463,7 @@ struct worker *moonbitlang_async_spawn_worker(struct job *init_job) {
459
463
sizeof (struct worker )
460
464
);
461
465
worker -> job = init_job ;
466
+ worker -> waiting = 0 ;
462
467
pthread_create (& (worker -> id ), & attr , & worker_loop , worker );
463
468
pthread_attr_destroy (& attr );
464
469
return worker ;
0 commit comments