int _pthread_once(pthread_once_t *once_control, void (*init_routine) (void)) { struct pthread *curthread; int wakeup = 0; if (once_control->state == ONCE_DONE) return (0); _pthread_mutex_lock(&once_lock); while (*(volatile int *)&(once_control->state) == ONCE_IN_PROGRESS) _pthread_cond_wait(&once_cv, &once_lock); /* * If previous thread was canceled, then the state still * could be ONCE_NEVER_DONE, we need to check it again. */ if (*(volatile int *)&(once_control->state) == ONCE_NEVER_DONE) { once_control->state = ONCE_IN_PROGRESS; _pthread_mutex_unlock(&once_lock); curthread = _get_curthread(); THR_CLEANUP_PUSH(curthread, once_cancel_handler, once_control); init_routine(); THR_CLEANUP_POP(curthread, 0); _pthread_mutex_lock(&once_lock); once_control->state = ONCE_DONE; wakeup = 1; } _pthread_mutex_unlock(&once_lock); if (wakeup) _pthread_cond_broadcast(&once_cv); return (0); }
int _sem_wait(sem_t *sem) { struct pthread *curthread; int retval; if (sem_check_validity(sem) != 0) return (-1); curthread = _get_curthread(); if ((*sem)->syssem != 0) { _thr_cancel_enter(curthread); retval = ksem_wait((*sem)->semid); _thr_cancel_leave(curthread, retval != 0); } else { _pthread_testcancel(); _pthread_mutex_lock(&(*sem)->lock); while ((*sem)->count <= 0) { (*sem)->nwaiters++; THR_CLEANUP_PUSH(curthread, decrease_nwaiters, sem); _pthread_cond_wait(&(*sem)->gtzero, &(*sem)->lock); THR_CLEANUP_POP(curthread, 0); (*sem)->nwaiters--; } (*sem)->count--; _pthread_mutex_unlock(&(*sem)->lock); retval = 0; } return (retval); }
int _pthread_once(pthread_once_t *once_control, void (*init_routine) (void)) { struct pthread *curthread; int state; _thr_check_init(); for (;;) { state = once_control->state; if (state == ONCE_DONE) return (0); if (state == ONCE_NEVER_DONE) { if (atomic_cmpset_acq_int(&once_control->state, state, ONCE_IN_PROGRESS)) break; } else if (state == ONCE_IN_PROGRESS) { if (atomic_cmpset_acq_int(&once_control->state, state, ONCE_WAIT)) _thr_umtx_wait_uint(&once_control->state, ONCE_WAIT, NULL, 0); } else if (state == ONCE_WAIT) { _thr_umtx_wait_uint(&once_control->state, state, NULL, 0); } else return (EINVAL); } curthread = _get_curthread(); THR_CLEANUP_PUSH(curthread, once_cancel_handler, once_control); init_routine(); THR_CLEANUP_POP(curthread, 0); if (atomic_cmpset_rel_int(&once_control->state, ONCE_IN_PROGRESS, ONCE_DONE)) return (0); atomic_store_rel_int(&once_control->state, ONCE_DONE); _thr_umtx_wake(&once_control->state, INT_MAX, 0); return (0); }
static int join_common(pthread_t pthread, void **thread_return, const struct timespec *abstime) { struct pthread *curthread = tls_get_curthread(); struct timespec ts, ts2, *tsp; void *tmp; long state; int oldcancel; int ret = 0; if (pthread == NULL) return (EINVAL); if (pthread == curthread) return (EDEADLK); THREAD_LIST_LOCK(curthread); if ((ret = _thr_find_thread(curthread, pthread, 1)) != 0) { ret = ESRCH; } else if ((pthread->tlflags & TLFLAGS_DETACHED) != 0) { ret = ESRCH; } else if (pthread->joiner != NULL) { /* Multiple joiners are not supported. */ ret = ENOTSUP; } if (ret) { THREAD_LIST_UNLOCK(curthread); return (ret); } /* Set the running thread to be the joiner: */ pthread->joiner = curthread; THREAD_LIST_UNLOCK(curthread); THR_CLEANUP_PUSH(curthread, backout_join, pthread); oldcancel = _thr_cancel_enter(curthread); while ((state = pthread->state) != PS_DEAD) { if (abstime != NULL) { clock_gettime(CLOCK_REALTIME, &ts); TIMESPEC_SUB(&ts2, abstime, &ts); if (ts2.tv_sec < 0) { ret = ETIMEDOUT; break; } tsp = &ts2; } else tsp = NULL; ret = _thr_umtx_wait(&pthread->state, state, tsp, CLOCK_REALTIME); if (ret == ETIMEDOUT) break; } _thr_cancel_leave(curthread, oldcancel); THR_CLEANUP_POP(curthread, 0); if (ret == ETIMEDOUT) { THREAD_LIST_LOCK(curthread); pthread->joiner = NULL; THREAD_LIST_UNLOCK(curthread); } else { ret = 0; tmp = pthread->ret; THREAD_LIST_LOCK(curthread); pthread->tlflags |= TLFLAGS_DETACHED; pthread->joiner = NULL; THR_GCLIST_ADD(pthread); THREAD_LIST_UNLOCK(curthread); if (thread_return != NULL) *thread_return = tmp; } return (ret); }
/* * Cancellation behavior: * if the thread is canceled, joinee is not recycled. */ static int join_common(pthread_t pthread, void **thread_return, const struct timespec *abstime) { struct pthread *curthread = _get_curthread(); struct timespec ts, ts2, *tsp; void *tmp; long tid; int ret = 0; if (pthread == NULL) return (EINVAL); if (pthread == curthread) return (EDEADLK); if ((ret = _thr_find_thread(curthread, pthread, 1)) != 0) return (ESRCH); if ((pthread->flags & THR_FLAGS_DETACHED) != 0) { ret = EINVAL; } else if (pthread->joiner != NULL) { /* Multiple joiners are not supported. */ ret = ENOTSUP; } if (ret) { THR_THREAD_UNLOCK(curthread, pthread); return (ret); } /* Set the running thread to be the joiner: */ pthread->joiner = curthread; THR_THREAD_UNLOCK(curthread, pthread); THR_CLEANUP_PUSH(curthread, backout_join, pthread); _thr_cancel_enter(curthread); tid = pthread->tid; while (pthread->tid != TID_TERMINATED) { _thr_testcancel(curthread); if (abstime != NULL) { clock_gettime(CLOCK_REALTIME, &ts); TIMESPEC_SUB(&ts2, abstime, &ts); if (ts2.tv_sec < 0) { ret = ETIMEDOUT; break; } tsp = &ts2; } else tsp = NULL; ret = _thr_umtx_wait(&pthread->tid, tid, tsp); if (ret == ETIMEDOUT) break; } _thr_cancel_leave(curthread, 0); THR_CLEANUP_POP(curthread, 0); if (ret == ETIMEDOUT) { THR_THREAD_LOCK(curthread, pthread); pthread->joiner = NULL; THR_THREAD_UNLOCK(curthread, pthread); } else { ret = 0; tmp = pthread->ret; THR_THREAD_LOCK(curthread, pthread); pthread->flags |= THR_FLAGS_DETACHED; pthread->joiner = NULL; _thr_try_gc(curthread, pthread); /* thread lock released */ if (thread_return != NULL) *thread_return = tmp; } return (ret); }