diff options
author | Torvald Riegel <triegel@redhat.com> | 2014-12-04 14:12:23 +0100 |
---|---|---|
committer | Torvald Riegel <triegel@redhat.com> | 2015-07-10 13:47:09 +0200 |
commit | a2f0363f817a58c4d3f439aa515a3b1d73efde36 (patch) | |
tree | 6204a1208c1421d573e8c91e4523376a18e1dd60 /nptl/pthread_rwlock_timedwrlock.c | |
parent | 203c1a898dd2e281eae30f3c57ceb8d4a50b00f4 (diff) | |
download | glibc-a2f0363f817a58c4d3f439aa515a3b1d73efde36.tar.gz glibc-a2f0363f817a58c4d3f439aa515a3b1d73efde36.tar.xz glibc-a2f0363f817a58c4d3f439aa515a3b1d73efde36.zip |
Add and use new glibc-internal futex API.
This adds new functions for futex operations, starting with wait, abstimed_wait, reltimed_wait, wake. They add documentation and error checking according to the current draft of the Linux kernel futex manpage. Waiting with absolute or relative timeouts is split into separate functions. This allows for removing a few cases of code duplication in pthreads code, which uses absolute timeouts; also, it allows us to put platform-specific code to go from an absolute to a relative timeout into the platform-specific futex abstractions.. Futex operations that can be canceled are also split out into separate functions suffixed by "_cancelable". There are separate versions for both Linux and NaCl; while they currently differ only slightly, my expectation is that the separate versions of lowlevellock-futex.h will eventually be merged into futex-internal.h when we get to move the lll_ functions over to the new futex API.
Diffstat (limited to 'nptl/pthread_rwlock_timedwrlock.c')
-rw-r--r-- | nptl/pthread_rwlock_timedwrlock.c | 56 |
1 files changed, 10 insertions, 46 deletions
diff --git a/nptl/pthread_rwlock_timedwrlock.c b/nptl/pthread_rwlock_timedwrlock.c index c54253450b..615623a75d 100644 --- a/nptl/pthread_rwlock_timedwrlock.c +++ b/nptl/pthread_rwlock_timedwrlock.c @@ -19,10 +19,10 @@ #include <errno.h> #include <sysdep.h> #include <lowlevellock.h> +#include <futex-internal.h> #include <pthread.h> #include <pthreadP.h> #include <sys/time.h> -#include <kernel-features.h> #include <stdbool.h> @@ -34,6 +34,8 @@ pthread_rwlock_timedwrlock (rwlock, abstime) { int result = 0; bool wake_readers = false; + int futex_shared = + rwlock->__data.__shared == LLL_PRIVATE ? FUTEX_PRIVATE : FUTEX_SHARED; /* Make sure we are alone. */ lll_lock (rwlock->__data.__lock, rwlock->__data.__shared); @@ -71,37 +73,6 @@ pthread_rwlock_timedwrlock (rwlock, abstime) break; } - /* Work around the fact that the kernel rejects negative timeout values - despite them being valid. */ - if (__glibc_unlikely (abstime->tv_sec < 0)) - { - result = ETIMEDOUT; - break; - } - -#if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \ - || !defined lll_futex_timed_wait_bitset) - /* Get the current time. So far we support only one clock. */ - struct timeval tv; - (void) __gettimeofday (&tv, NULL); - - /* Convert the absolute timeout value to a relative timeout. */ - struct timespec rt; - rt.tv_sec = abstime->tv_sec - tv.tv_sec; - rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000; - if (rt.tv_nsec < 0) - { - rt.tv_nsec += 1000000000; - --rt.tv_sec; - } - /* Did we already time out? */ - if (rt.tv_sec < 0) - { - result = ETIMEDOUT; - break; - } -#endif - /* Remember that we are a writer. */ if (++rwlock->__data.__nr_writers_queued == 0) { @@ -116,17 +87,11 @@ pthread_rwlock_timedwrlock (rwlock, abstime) /* Free the lock. */ lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared); - /* Wait for the writer or reader(s) to finish. */ -#if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \ - || !defined lll_futex_timed_wait_bitset) - err = lll_futex_timed_wait (&rwlock->__data.__writer_wakeup, - waitval, &rt, rwlock->__data.__shared); -#else - err = lll_futex_timed_wait_bitset (&rwlock->__data.__writer_wakeup, - waitval, abstime, - FUTEX_CLOCK_REALTIME, - rwlock->__data.__shared); -#endif + /* Wait for the writer or reader(s) to finish. We handle ETIMEDOUT + below; on other return values, we decide how to continue based on + the state of the rwlock. */ + err = futex_abstimed_wait (&rwlock->__data.__writer_wakeup, waitval, + abstime, futex_shared); /* Get the lock. */ lll_lock (rwlock->__data.__lock, rwlock->__data.__shared); @@ -135,7 +100,7 @@ pthread_rwlock_timedwrlock (rwlock, abstime) --rwlock->__data.__nr_writers_queued; /* Did the futex call time out? */ - if (err == -ETIMEDOUT) + if (err == ETIMEDOUT) { result = ETIMEDOUT; /* If we prefer writers, it can have happened that readers blocked @@ -166,8 +131,7 @@ pthread_rwlock_timedwrlock (rwlock, abstime) /* Might be required after timeouts. */ if (wake_readers) - lll_futex_wake (&rwlock->__data.__readers_wakeup, INT_MAX, - rwlock->__data.__shared); + futex_wake (&rwlock->__data.__readers_wakeup, INT_MAX, futex_shared); return result; } |