123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- #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"
- int pthread_cond_init(pthread_cond_t *cond,
- const pthread_condattr_t *cond_attr attribute_unused)
- {
- __pthread_init_lock(&cond->__c_lock);
- cond->__c_waiting = NULL;
- return 0;
- }
- libpthread_hidden_def(pthread_cond_init)
- int pthread_cond_destroy(pthread_cond_t *cond)
- {
- if (cond->__c_waiting != NULL) return EBUSY;
- return 0;
- }
- libpthread_hidden_def(pthread_cond_destroy)
- 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;
- int spurious_wakeup_count;
-
- if (mutex->__m_kind != PTHREAD_MUTEX_TIMED_NP
- && mutex->__m_kind != PTHREAD_MUTEX_ADAPTIVE_NP
- && mutex->__m_owner != self)
- return EINVAL;
-
- extr.pu_object = cond;
- extr.pu_extricate_func = cond_extricate_func;
-
- THREAD_SETMEM(self, p_condvar_avail, 0);
- __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_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
- }
- __pthread_mutex_unlock(mutex);
- spurious_wakeup_count = 0;
- while (1)
- {
- suspend(self);
- if (THREAD_GETMEM(self, p_condvar_avail) == 0
- && (THREAD_GETMEM(self, p_woken_by_cancel) == 0
- || THREAD_GETMEM(self, p_cancelstate) != PTHREAD_CANCEL_ENABLE))
- {
-
- spurious_wakeup_count++;
- continue;
- }
- break;
- }
- __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_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
- }
-
- while (spurious_wakeup_count--)
- restart(self);
- __pthread_mutex_lock(mutex);
- return 0;
- }
- libpthread_hidden_def(pthread_cond_wait)
- static int
- pthread_cond_timedwait_relative(pthread_cond_t *cond,
- pthread_mutex_t *mutex,
- const struct timespec * abstime)
- {
- volatile pthread_descr self = thread_self();
- int already_canceled = 0;
- pthread_extricate_if extr;
- int spurious_wakeup_count;
-
- if (mutex->__m_kind != PTHREAD_MUTEX_TIMED_NP
- && mutex->__m_kind != PTHREAD_MUTEX_ADAPTIVE_NP
- && mutex->__m_owner != self)
- return EINVAL;
-
- extr.pu_object = cond;
- extr.pu_extricate_func = cond_extricate_func;
-
- THREAD_SETMEM(self, p_condvar_avail, 0);
- __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_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
- }
- __pthread_mutex_unlock(mutex);
- spurious_wakeup_count = 0;
- while (1)
- {
- if (!timedsuspend(self, abstime)) {
- 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);
- }
- if (THREAD_GETMEM(self, p_condvar_avail) == 0
- && (THREAD_GETMEM(self, p_woken_by_cancel) == 0
- || THREAD_GETMEM(self, p_cancelstate) != PTHREAD_CANCEL_ENABLE))
- {
-
- spurious_wakeup_count++;
- continue;
- }
- break;
- }
- __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_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
- }
-
- while (spurious_wakeup_count--)
- restart(self);
- __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_timedwait_relative(cond, mutex, abstime);
- }
- libpthread_hidden_def(pthread_cond_timedwait)
- 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) {
- th->p_condvar_avail = 1;
- WRITE_MEMORY_BARRIER();
- restart(th);
- }
- return 0;
- }
- libpthread_hidden_def(pthread_cond_signal)
- 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) {
- th->p_condvar_avail = 1;
- WRITE_MEMORY_BARRIER();
- restart(th);
- }
- return 0;
- }
- libpthread_hidden_def(pthread_cond_broadcast)
- int pthread_condattr_init(pthread_condattr_t *attr attribute_unused)
- {
- return 0;
- }
- libpthread_hidden_def(pthread_condattr_init)
- int pthread_condattr_destroy(pthread_condattr_t *attr attribute_unused)
- {
- return 0;
- }
- libpthread_hidden_def(pthread_condattr_destroy)
- int pthread_condattr_getpshared (const pthread_condattr_t *attr attribute_unused, int *pshared)
- {
- *pshared = PTHREAD_PROCESS_PRIVATE;
- return 0;
- }
- int pthread_condattr_setpshared (pthread_condattr_t *attr attribute_unused, int pshared)
- {
- if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
- return EINVAL;
-
- if (pshared != PTHREAD_PROCESS_PRIVATE)
- return ENOSYS;
- return 0;
- }
|