123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- #include <endian.h>
- #include <errno.h>
- #include <sysdep.h>
- #include <lowlevellock.h>
- #include <pthread.h>
- #include <pthreadP.h>
- struct _condvar_cleanup_buffer
- {
- int oldtype;
- pthread_cond_t *cond;
- pthread_mutex_t *mutex;
- unsigned int bc_seq;
- };
- void
- __attribute__ ((visibility ("hidden")))
- __condvar_cleanup (void *arg)
- {
- struct _condvar_cleanup_buffer *cbuffer =
- (struct _condvar_cleanup_buffer *) arg;
- unsigned int destroying;
- int pshared = (cbuffer->cond->__data.__mutex == (void *) ~0l)
- ? LLL_SHARED : LLL_PRIVATE;
-
- lll_lock (cbuffer->cond->__data.__lock, pshared);
- if (cbuffer->bc_seq == cbuffer->cond->__data.__broadcast_seq)
- {
-
- if (cbuffer->cond->__data.__wakeup_seq
- < cbuffer->cond->__data.__total_seq)
- {
- ++cbuffer->cond->__data.__wakeup_seq;
- ++cbuffer->cond->__data.__futex;
- }
- ++cbuffer->cond->__data.__woken_seq;
- }
- cbuffer->cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT;
-
- destroying = 0;
- if (cbuffer->cond->__data.__total_seq == -1ULL
- && cbuffer->cond->__data.__nwaiters < (1 << COND_NWAITERS_SHIFT))
- {
- lll_futex_wake (&cbuffer->cond->__data.__nwaiters, 1, pshared);
- destroying = 1;
- }
-
- lll_unlock (cbuffer->cond->__data.__lock, pshared);
-
- if (! destroying)
- lll_futex_wake (&cbuffer->cond->__data.__futex, INT_MAX, pshared);
-
- __pthread_mutex_cond_lock (cbuffer->mutex);
- }
- int
- attribute_protected
- __pthread_cond_wait (
- pthread_cond_t *cond,
- pthread_mutex_t *mutex)
- {
- struct _pthread_cleanup_buffer buffer;
- struct _condvar_cleanup_buffer cbuffer;
- int err;
- int pshared = (cond->__data.__mutex == (void *) ~0l)
- ? LLL_SHARED : LLL_PRIVATE;
-
- lll_lock (cond->__data.__lock, pshared);
-
- err = __pthread_mutex_unlock_usercnt (mutex, 0);
- if (__builtin_expect (err, 0))
- {
- lll_unlock (cond->__data.__lock, pshared);
- return err;
- }
-
- ++cond->__data.__total_seq;
- ++cond->__data.__futex;
- cond->__data.__nwaiters += 1 << COND_NWAITERS_SHIFT;
-
- if (cond->__data.__mutex != (void *) ~0l)
- cond->__data.__mutex = mutex;
-
- cbuffer.cond = cond;
- cbuffer.mutex = mutex;
-
- __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);
-
- unsigned long long int val;
- unsigned long long int seq;
- val = seq = cond->__data.__wakeup_seq;
-
- cbuffer.bc_seq = cond->__data.__broadcast_seq;
- do
- {
- unsigned int futex_val = cond->__data.__futex;
-
- lll_unlock (cond->__data.__lock, pshared);
-
- cbuffer.oldtype = __pthread_enable_asynccancel ();
-
- lll_futex_wait (&cond->__data.__futex, futex_val, pshared);
-
- __pthread_disable_asynccancel (cbuffer.oldtype);
-
- lll_lock (cond->__data.__lock, pshared);
-
- if (cbuffer.bc_seq != cond->__data.__broadcast_seq)
- goto bc_out;
-
- val = cond->__data.__wakeup_seq;
- }
- while (val == seq || cond->__data.__woken_seq == val);
-
- ++cond->__data.__woken_seq;
- bc_out:
- cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT;
-
- if (cond->__data.__total_seq == -1ULL
- && cond->__data.__nwaiters < (1 << COND_NWAITERS_SHIFT))
- lll_futex_wake (&cond->__data.__nwaiters, 1, pshared);
-
- lll_unlock (cond->__data.__lock, pshared);
-
- __pthread_cleanup_pop (&buffer, 0);
-
- return __pthread_mutex_cond_lock (mutex);
- }
- weak_alias(__pthread_cond_wait, pthread_cond_wait)
|