static void try_lock_mutex_test(void) { int i, res; ethr_tid tid; res = ethr_mutex_init(&tlmt_mtx1); ASSERT(res == 0); res = ethr_mutex_init(&tlmt_mtx2); ASSERT(res == 0); res = ethr_cond_init(&tlmt_cnd2); ASSERT(res == 0); tlmt_mtx1_locked = 0; tlmt_mtx1_do_unlock = 0; res = ethr_thr_create(&tid, tlmt_thread, NULL, NULL); ASSERT(res == 0); ethr_mutex_lock(&tlmt_mtx2); while (!tlmt_mtx1_locked) { res = ethr_cond_wait(&tlmt_cnd2, &tlmt_mtx2); ASSERT(res == 0 || res == EINTR); } ethr_mutex_unlock(&tlmt_mtx2); for (i = 0; i < 10; i++) { res = ethr_mutex_trylock(&tlmt_mtx1); ASSERT(res == EBUSY); } ethr_mutex_lock(&tlmt_mtx2); tlmt_mtx1_do_unlock = 1; ethr_cond_signal(&tlmt_cnd2); while (tlmt_mtx1_locked) { res = ethr_cond_wait(&tlmt_cnd2, &tlmt_mtx2); ASSERT(res == 0 || res == EINTR); } ethr_mutex_unlock(&tlmt_mtx2); res = ethr_mutex_trylock(&tlmt_mtx1); ASSERT(res == 0); ethr_mutex_unlock(&tlmt_mtx1); res = ethr_thr_join(tid, NULL); ASSERT(res == 0); res = ethr_mutex_destroy(&tlmt_mtx1); ASSERT(res == 0); res = ethr_mutex_destroy(&tlmt_mtx2); ASSERT(res == 0); res = ethr_cond_destroy(&tlmt_cnd2); ASSERT(res == 0); }
static void * bct_thread(void *unused) { int res; ethr_mutex_lock(&bct_mutex); while (!bct_done) { bct_waiting++; if (bct_waiting == BCT_THREADS) ethr_cond_signal(&bct_cntrl_cond); do { res = ethr_cond_wait(&bct_cond, &bct_mutex); } while (res == EINTR); ASSERT(res == 0); bct_woken++; if (bct_woken == BCT_THREADS) ethr_cond_signal(&bct_cntrl_cond); } ethr_mutex_unlock(&bct_mutex); return NULL; }
static void * tlmt_thread(void *unused) { int res; ethr_mutex_lock(&tlmt_mtx1); ethr_mutex_lock(&tlmt_mtx2); tlmt_mtx1_locked = 1; ethr_cond_signal(&tlmt_cnd2); while (!tlmt_mtx1_do_unlock) { res = ethr_cond_wait(&tlmt_cnd2, &tlmt_mtx2); ASSERT(res == 0 || res == EINTR); } ethr_mutex_unlock(&tlmt_mtx2); ethr_mutex_unlock(&tlmt_mtx1); ethr_mutex_lock(&tlmt_mtx2); tlmt_mtx1_locked = 0; ethr_cond_signal(&tlmt_cnd2); ethr_mutex_unlock(&tlmt_mtx2); return NULL; }
static void * ett_thread2(void *unused) { int res; ethr_mutex_lock(&ett_mutex); while (!ett_terminate) { res = ethr_cond_wait(&ett_cond, &ett_mutex); ASSERT(res == 0); } ethr_mutex_unlock(&ett_mutex); return NULL; }
void erl_drv_cond_wait(ErlDrvCond *dcnd, ErlDrvMutex *dmtx) { #ifdef USE_THREADS if (!dcnd || !dmtx) { fatal_error(EINVAL, "erl_drv_cond_wait()"); } while (1) { int res = ethr_cond_wait(&dcnd->cnd, &dmtx->mtx); if (res == 0) break; } #endif }
void *mtt_thread(void *unused) { int res; ethr_mutex_lock(&mtt_mutex); while (!mtt_terminate) { res = ethr_cond_wait(&mtt_cond, &mtt_mutex); ASSERT(res == 0 || res == EINTR); } ethr_mutex_unlock(&mtt_mutex); return NULL; }
void * cwt_thread(void *unused) { int res; ethr_mutex_lock(&cwt_mutex); do { res = ethr_cond_wait(&cwt_cond, &cwt_mutex); } while (res == EINTR); ASSERT(res == 0); cwt_counter++; ethr_mutex_unlock(&cwt_mutex); return NULL; }
void erl_drv_cond_wait(ErlDrvCond *dcnd, ErlDrvMutex *dmtx) { #ifdef USE_THREADS int res; if (!dcnd || !dmtx) { res = EINVAL; error: fatal_error(res, "erl_drv_cond_wait()"); } while (1) { res = ethr_cond_wait(&dcnd->cnd, &dmtx->mtx); if (res == 0) break; if (res != EINTR) goto error; } #endif }
static void detached_thread_test(void) { ethr_thr_opts thr_opts = ETHR_THR_OPTS_DEFAULT_INITER; ethr_tid tid[DT_BATCH_SIZE]; int i, j, res; res = ethr_mutex_init(&dt_mutex); ASSERT(res == 0); res = ethr_cond_init(&dt_cond); ASSERT(res == 0); thr_opts.detached = 1; dt_count = 0; dt_limit = 0; for (i = 0; i < DT_THREADS/DT_BATCH_SIZE; i++) { dt_limit += DT_BATCH_SIZE; for (j = 0; j < DT_BATCH_SIZE; j++) { res = ethr_thr_create(&tid[j], dt_thread, NULL, &thr_opts); ASSERT(res == 0); } ethr_mutex_lock(&dt_mutex); while (dt_count < dt_limit) { res = ethr_cond_wait(&dt_cond, &dt_mutex); ASSERT(res == 0 || res == EINTR); } ethr_mutex_unlock(&dt_mutex); print_line("dt_count = %d", dt_count); } do_sleep(1); }
static void broadcast_test(void) { int res, i; ethr_tid tid[BCT_THREADS]; res = ethr_mutex_init(&bct_mutex); ASSERT(res == 0); res = ethr_cond_init(&bct_cntrl_cond); ASSERT(res == 0); res = ethr_cond_init(&bct_cond); ASSERT(res == 0); for (i = 0; i < BCT_THREADS; i++) { res = ethr_thr_create(&tid[i], bct_thread, NULL, NULL); ASSERT(res == 0); } ethr_mutex_lock(&bct_mutex); for (i = 0; i < BCT_NO_OF_WAITS; i++) { while (bct_waiting != BCT_THREADS) { res = ethr_cond_wait(&bct_cntrl_cond, &bct_mutex); ASSERT(res == 0 || res == EINTR); } bct_waiting = 0; bct_woken = 0; /* Wake all threads */ ethr_cond_broadcast(&bct_cond); while (bct_woken != BCT_THREADS) { res = ethr_cond_wait(&bct_cntrl_cond, &bct_mutex); ASSERT(res == 0 || res == EINTR); } } bct_done = 1; /* Wake all threads */ ethr_cond_broadcast(&bct_cond); ethr_mutex_unlock(&bct_mutex); for (i = 0; i < BCT_THREADS; i++) { res = ethr_thr_join(tid[i], NULL); ASSERT(res == 0); } res = ethr_mutex_destroy(&bct_mutex); ASSERT(res == 0); res = ethr_cond_destroy(&bct_cntrl_cond); ASSERT(res == 0); res = ethr_cond_destroy(&bct_cond); ASSERT(res == 0); }