123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- #include <endian.h>
- #include <errno.h>
- #include <sysdep.h>
- #include <lowlevellock.h>
- #include <pthread.h>
- #include <pthreadP.h>
- #include <bits/kernel-features.h>
- extern void __condvar_cleanup (void *arg)
- __attribute__ ((visibility ("hidden")));
- struct _condvar_cleanup_buffer
- {
- int oldtype;
- pthread_cond_t *cond;
- pthread_mutex_t *mutex;
- unsigned int bc_seq;
- };
- int
- attribute_protected
- __pthread_cond_timedwait (
- pthread_cond_t *cond,
- pthread_mutex_t *mutex,
- const struct timespec *abstime)
- {
- struct _pthread_cleanup_buffer buffer;
- struct _condvar_cleanup_buffer cbuffer;
- int result = 0;
-
- if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
- return EINVAL;
- int pshared = (cond->__data.__mutex == (void *) ~0l)
- ? LLL_SHARED : LLL_PRIVATE;
-
- lll_lock (cond->__data.__lock, pshared);
-
- int err = __pthread_mutex_unlock_usercnt (mutex, 0);
- if (err)
- {
- 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;
- while (1)
- {
- struct timespec rt;
- {
- #ifdef __NR_clock_gettime
- INTERNAL_SYSCALL_DECL (err);
- # ifndef __ASSUME_POSIX_TIMERS
- int ret =
- # endif
- INTERNAL_SYSCALL (clock_gettime, err, 2,
- (cond->__data.__nwaiters
- & ((1 << COND_NWAITERS_SHIFT) - 1)),
- &rt);
- # ifndef __ASSUME_POSIX_TIMERS
- if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (ret, err), 0))
- {
- struct timeval tv;
- (void) gettimeofday (&tv, NULL);
-
- rt.tv_sec = abstime->tv_sec - tv.tv_sec;
- rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
- }
- else
- # endif
- {
-
- rt.tv_sec = abstime->tv_sec - rt.tv_sec;
- rt.tv_nsec = abstime->tv_nsec - rt.tv_nsec;
- }
- #else
-
- struct timeval tv;
- (void) gettimeofday (&tv, NULL);
-
- rt.tv_sec = abstime->tv_sec - tv.tv_sec;
- rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
- #endif
- }
- if (rt.tv_nsec < 0)
- {
- rt.tv_nsec += 1000000000;
- --rt.tv_sec;
- }
-
- if (__builtin_expect (rt.tv_sec < 0, 0))
- {
- if (cbuffer.bc_seq != cond->__data.__broadcast_seq)
- goto bc_out;
- goto timeout;
- }
- unsigned int futex_val = cond->__data.__futex;
-
- lll_unlock (cond->__data.__lock, pshared);
-
- cbuffer.oldtype = __pthread_enable_asynccancel ();
-
- err = lll_futex_timed_wait (&cond->__data.__futex,
- futex_val, &rt, 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;
- if (val != seq && cond->__data.__woken_seq != val)
- break;
-
- if (__builtin_expect (err == -ETIMEDOUT, 0))
- {
- timeout:
-
- ++cond->__data.__wakeup_seq;
- ++cond->__data.__futex;
-
- result = ETIMEDOUT;
- break;
- }
- }
-
- ++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);
-
- err = __pthread_mutex_cond_lock (mutex);
- return err ?: result;
- }
- weak_alias(__pthread_cond_timedwait, pthread_cond_timedwait)
|