123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- #include <errno.h>
- #include <sched.h>
- #include <stddef.h>
- #include <sys/time.h>
- #include "pthread.h"
- #include "internals.h"
- #include "spinlock.h"
- #include "queue.h"
- #include "restart.h"
- static int pthread_cond_timedwait_relative_old(pthread_cond_t *,
- pthread_mutex_t *, const struct timespec *);
- static int pthread_cond_timedwait_relative_new(pthread_cond_t *,
- pthread_mutex_t *, const struct timespec *);
- static int (*pthread_cond_tw_rel)(pthread_cond_t *, pthread_mutex_t *,
- const struct timespec *) = pthread_cond_timedwait_relative_old;
- void __pthread_init_condvar(int rt_sig_available)
- {
- if (rt_sig_available)
- pthread_cond_tw_rel = pthread_cond_timedwait_relative_new;
- }
- int pthread_cond_init(pthread_cond_t *cond,
- const pthread_condattr_t *cond_attr)
- {
- __pthread_init_lock(&cond->__c_lock);
- cond->__c_waiting = NULL;
- return 0;
- }
- int pthread_cond_destroy(pthread_cond_t *cond)
- {
- if (cond->__c_waiting != NULL) return EBUSY;
- return 0;
- }
- static int cond_extricate_func(void *obj, pthread_descr th)
- {
- volatile pthread_descr self = thread_self();
- pthread_cond_t *cond = obj;
- int did_remove = 0;
- __pthread_lock(&cond->__c_lock, self);
- did_remove = remove_from_queue(&cond->__c_waiting, th);
- __pthread_unlock(&cond->__c_lock);
- return did_remove;
- }
- int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
- {
- volatile pthread_descr self = thread_self();
- pthread_extricate_if extr;
- int already_canceled = 0;
-
- extr.pu_object = cond;
- extr.pu_extricate_func = cond_extricate_func;
-
- __pthread_set_own_extricate_if(self, &extr);
-
- __pthread_lock(&cond->__c_lock, self);
- if (!(THREAD_GETMEM(self, p_canceled)
- && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
- enqueue(&cond->__c_waiting, self);
- else
- already_canceled = 1;
- __pthread_unlock(&cond->__c_lock);
- if (already_canceled) {
- __pthread_set_own_extricate_if(self, 0);
- pthread_exit(PTHREAD_CANCELED);
- }
- pthread_mutex_unlock(mutex);
- suspend(self);
- __pthread_set_own_extricate_if(self, 0);
-
- if (THREAD_GETMEM(self, p_woken_by_cancel)
- && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
- THREAD_SETMEM(self, p_woken_by_cancel, 0);
- pthread_mutex_lock(mutex);
- pthread_exit(PTHREAD_CANCELED);
- }
- pthread_mutex_lock(mutex);
- return 0;
- }
- static int
- pthread_cond_timedwait_relative_old(pthread_cond_t *cond,
- pthread_mutex_t *mutex,
- const struct timespec * abstime)
- {
- volatile pthread_descr self = thread_self();
- sigset_t unblock, initial_mask;
- int already_canceled = 0;
- int was_signalled = 0;
- sigjmp_buf jmpbuf;
- pthread_extricate_if extr;
-
- extr.pu_object = cond;
- extr.pu_extricate_func = cond_extricate_func;
-
- __pthread_set_own_extricate_if(self, &extr);
-
- __pthread_lock(&cond->__c_lock, self);
- if (!(THREAD_GETMEM(self, p_canceled)
- && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
- enqueue(&cond->__c_waiting, self);
- else
- already_canceled = 1;
- __pthread_unlock(&cond->__c_lock);
- if (already_canceled) {
- __pthread_set_own_extricate_if(self, 0);
- pthread_exit(PTHREAD_CANCELED);
- }
- pthread_mutex_unlock(mutex);
- if (atomic_decrement(&self->p_resume_count) == 0) {
-
- if (sigsetjmp(jmpbuf, 1) == 0) {
- THREAD_SETMEM(self, p_signal_jmp, &jmpbuf);
- THREAD_SETMEM(self, p_signal, 0);
-
- sigemptyset(&unblock);
- sigaddset(&unblock, __pthread_sig_restart);
- sigprocmask(SIG_UNBLOCK, &unblock, &initial_mask);
- while (1) {
- struct timeval now;
- struct timespec reltime;
-
- gettimeofday (&now, NULL);
- reltime.tv_nsec = abstime->tv_nsec - now.tv_usec * 1000;
- reltime.tv_sec = abstime->tv_sec - now.tv_sec;
- if (reltime.tv_nsec < 0) {
- reltime.tv_nsec += 1000000000;
- reltime.tv_sec -= 1;
- }
-
- if (reltime.tv_sec < 0 || __libc_nanosleep(&reltime, NULL) == 0)
- break;
- }
-
- sigprocmask(SIG_SETMASK, &initial_mask, NULL);
- was_signalled = 0;
- } else {
- was_signalled = 1;
- }
- THREAD_SETMEM(self, p_signal_jmp, NULL);
- }
-
- if (!was_signalled) {
- if (atomic_increment(&self->p_resume_count) != -1) {
- __pthread_wait_for_restart_signal(self);
- atomic_decrement(&self->p_resume_count);
- } else {
- int was_on_queue;
- __pthread_lock(&cond->__c_lock, self);
- was_on_queue = remove_from_queue(&cond->__c_waiting, self);
- __pthread_unlock(&cond->__c_lock);
- if (was_on_queue) {
- __pthread_set_own_extricate_if(self, 0);
- pthread_mutex_lock(mutex);
- return ETIMEDOUT;
- }
- suspend(self);
- }
- }
- __pthread_set_own_extricate_if(self, 0);
-
- if (THREAD_GETMEM(self, p_woken_by_cancel)
- && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
- THREAD_SETMEM(self, p_woken_by_cancel, 0);
- pthread_mutex_lock(mutex);
- pthread_exit(PTHREAD_CANCELED);
- }
- pthread_mutex_lock(mutex);
- return 0;
- }
- static int
- pthread_cond_timedwait_relative_new(pthread_cond_t *cond,
- pthread_mutex_t *mutex,
- const struct timespec * abstime)
- {
- volatile pthread_descr self = thread_self();
- sigset_t unblock, initial_mask;
- int already_canceled = 0;
- int was_signalled = 0;
- sigjmp_buf jmpbuf;
- pthread_extricate_if extr;
-
- extr.pu_object = cond;
- extr.pu_extricate_func = cond_extricate_func;
-
- __pthread_set_own_extricate_if(self, &extr);
-
- __pthread_lock(&cond->__c_lock, self);
- if (!(THREAD_GETMEM(self, p_canceled)
- && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
- enqueue(&cond->__c_waiting, self);
- else
- already_canceled = 1;
- __pthread_unlock(&cond->__c_lock);
- if (already_canceled) {
- __pthread_set_own_extricate_if(self, 0);
- pthread_exit(PTHREAD_CANCELED);
- }
- pthread_mutex_unlock(mutex);
-
- if (sigsetjmp(jmpbuf, 1) == 0) {
- THREAD_SETMEM(self, p_signal_jmp, &jmpbuf);
- THREAD_SETMEM(self, p_signal, 0);
-
- sigemptyset(&unblock);
- sigaddset(&unblock, __pthread_sig_restart);
- sigprocmask(SIG_UNBLOCK, &unblock, &initial_mask);
- while (1) {
- struct timeval now;
- struct timespec reltime;
-
- gettimeofday (&now, NULL);
- reltime.tv_nsec = abstime->tv_nsec - now.tv_usec * 1000;
- reltime.tv_sec = abstime->tv_sec - now.tv_sec;
- if (reltime.tv_nsec < 0) {
- reltime.tv_nsec += 1000000000;
- reltime.tv_sec -= 1;
- }
-
- if (reltime.tv_sec < 0 || __libc_nanosleep(&reltime, NULL) == 0)
- break;
- }
-
- sigprocmask(SIG_SETMASK, &initial_mask, NULL);
- was_signalled = 0;
- } else {
- was_signalled = 1;
- }
- THREAD_SETMEM(self, p_signal_jmp, NULL);
-
- if (!was_signalled) {
- int was_on_queue;
-
- __pthread_lock(&cond->__c_lock, self);
- was_on_queue = remove_from_queue(&cond->__c_waiting, self);
- __pthread_unlock(&cond->__c_lock);
- if (was_on_queue) {
- __pthread_set_own_extricate_if(self, 0);
- pthread_mutex_lock(mutex);
- return ETIMEDOUT;
- }
-
- suspend(self);
- }
- __pthread_set_own_extricate_if(self, 0);
-
- if (THREAD_GETMEM(self, p_woken_by_cancel)
- && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
- THREAD_SETMEM(self, p_woken_by_cancel, 0);
- pthread_mutex_lock(mutex);
- pthread_exit(PTHREAD_CANCELED);
- }
- pthread_mutex_lock(mutex);
- return 0;
- }
- int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
- const struct timespec * abstime)
- {
-
- return pthread_cond_tw_rel(cond, mutex, abstime);
- }
- int pthread_cond_signal(pthread_cond_t *cond)
- {
- pthread_descr th;
- __pthread_lock(&cond->__c_lock, NULL);
- th = dequeue(&cond->__c_waiting);
- __pthread_unlock(&cond->__c_lock);
- if (th != NULL) restart(th);
- return 0;
- }
- int pthread_cond_broadcast(pthread_cond_t *cond)
- {
- pthread_descr tosignal, th;
- __pthread_lock(&cond->__c_lock, NULL);
-
- tosignal = cond->__c_waiting;
- cond->__c_waiting = NULL;
- __pthread_unlock(&cond->__c_lock);
-
- while ((th = dequeue(&tosignal)) != NULL) restart(th);
- return 0;
- }
- int pthread_condattr_init(pthread_condattr_t *attr)
- {
- return 0;
- }
- int pthread_condattr_destroy(pthread_condattr_t *attr)
- {
- return 0;
- }
- int pthread_condattr_getpshared (const pthread_condattr_t *attr, int *pshared)
- {
- *pshared = PTHREAD_PROCESS_PRIVATE;
- return 0;
- }
- int pthread_condattr_setpshared (pthread_condattr_t *attr, int pshared)
- {
- if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
- return EINVAL;
-
- if (pshared != PTHREAD_PROCESS_PRIVATE)
- return ENOSYS;
- return 0;
- }
|