diff options
author | Adhemerval Zanella <adhemerval.zanella@linaro.org> | 2021-08-11 18:17:41 +0000 |
---|---|---|
committer | Adhemerval Zanella <adhemerval.zanella@linaro.org> | 2021-10-01 10:11:11 -0300 |
commit | 2313ab153de29849f8fb0817ed3789fa1745225a (patch) | |
tree | 00bb8f53d537de33fb4c8ab831daaaf0563798c8 /nptl | |
parent | 9cba3fa34b15017b269f2674ce7656bbc9d4d06d (diff) | |
download | glibc-2313ab153de29849f8fb0817ed3789fa1745225a.tar.gz glibc-2313ab153de29849f8fb0817ed3789fa1745225a.tar.xz glibc-2313ab153de29849f8fb0817ed3789fa1745225a.zip |
nptl: Add CLOCK_MONOTONIC support for PI mutexes
Linux added FUTEX_LOCK_PI2 to support clock selection (commit bf22a6976897977b0a3f1aeba6823c959fc4fdae). With the new flag we can now proper support CLOCK_MONOTONIC for pthread_mutex_clocklock with Priority Inheritance. If kernel does not support, EINVAL is returned instead. The difference is the futex operation will be issued and the kernel will advertise the missing support (instead of hard-code error return). Checked on x86_64-linux-gnu and i686-linux-gnu on Linux 5.14, 5.11, and 4.15.
Diffstat (limited to 'nptl')
-rw-r--r-- | nptl/pthread_mutex_timedlock.c | 7 | ||||
-rw-r--r-- | nptl/tst-mutexpi10.c | 42 |
2 files changed, 26 insertions, 23 deletions
diff --git a/nptl/pthread_mutex_timedlock.c b/nptl/pthread_mutex_timedlock.c index a695faeb7e..57f3f28869 100644 --- a/nptl/pthread_mutex_timedlock.c +++ b/nptl/pthread_mutex_timedlock.c @@ -299,13 +299,6 @@ __pthread_mutex_clocklock_common (pthread_mutex_t *mutex, case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP: case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP: { - /* Currently futex FUTEX_LOCK_PI operation only provides support for - CLOCK_REALTIME and trying to emulate by converting a - CLOCK_MONOTONIC to CLOCK_REALTIME will take in account possible - changes to the wall clock. */ - if (__glibc_unlikely (clockid != CLOCK_REALTIME)) - return EINVAL; - int kind, robust; { /* See concurrency notes regarding __kind in struct __pthread_mutex_s diff --git a/nptl/tst-mutexpi10.c b/nptl/tst-mutexpi10.c index da781d0d7a..34a7bea21b 100644 --- a/nptl/tst-mutexpi10.c +++ b/nptl/tst-mutexpi10.c @@ -38,29 +38,39 @@ do_test (void) PTHREAD_MUTEX_STALLED, PTHREAD_MUTEX_ROBUST }; - + const struct { + int clk; + int r; + } clocks[] = { + { CLOCK_REALTIME, 0 }, + { CLOCK_MONOTONIC, 0 }, + { CLOCK_REALTIME_COARSE, EINVAL } + }; for (int t = 0; t < array_length (types); t++) for (int r = 0; r < array_length (robust); r++) - { - pthread_mutexattr_t attr; - - xpthread_mutexattr_init (&attr); - xpthread_mutexattr_setprotocol (&attr, PTHREAD_PRIO_INHERIT); - xpthread_mutexattr_settype (&attr, types[t]); - xpthread_mutexattr_setrobust (&attr, robust[r]); + for (int c = 0; c < array_length (clocks); c++) + { + pthread_mutexattr_t attr; + xpthread_mutexattr_init (&attr); + xpthread_mutexattr_setprotocol (&attr, PTHREAD_PRIO_INHERIT); + xpthread_mutexattr_settype (&attr, types[t]); + xpthread_mutexattr_setrobust (&attr, robust[r]); - pthread_mutex_t mtx; - xpthread_mutex_init (&mtx, &attr); + pthread_mutex_t mtx; + xpthread_mutex_init (&mtx, &attr); - struct timespec tmo = timespec_add (xclock_now (CLOCK_MONOTONIC), - make_timespec (0, 100000000)); + /* Uncontended case does not trigger any futex call. */ + struct timespec tmo = timespec_add (xclock_now (clocks[c].clk), + make_timespec (0, 100000000)); - TEST_COMPARE (pthread_mutex_clocklock (&mtx, CLOCK_MONOTONIC, &tmo), - EINVAL); + TEST_COMPARE (pthread_mutex_clocklock (&mtx, clocks[c].clk, &tmo), + clocks[c].r); + if (clocks[c].r == 0) + TEST_COMPARE (pthread_mutex_unlock (&mtx), 0); - xpthread_mutex_destroy (&mtx); - } + xpthread_mutex_destroy (&mtx); + } return 0; } |