Message ID | 787e8b4fb2282cd7af54e203dd4d3807d196a195.1551291557.git-series.mac@mcrowe.com |
---|---|
State | New |
Headers | show |
Series | Implement proposed POSIX _clockwait variants of existing _timedwait functions | expand |
On 27/02/2019 15:23, Mike Crowe wrote: > Add: > int pthread_rwlock_clockrdlock (pthread_rwlock_t *rwlock, > clockid_t clockid, > const struct timespec *abstime) > and: > int pthread_rwlock_clockwrlock (pthread_rwlock_t *rwlock, > clockid_t clockid, > const struct timespec *abstime) > > which behave like pthread_rwlock_timedrdlock and pthread_rwlock_timedwrlock > respectively, except they always measure abstime against the supplied > clockid. The functions currently support CLOCK_REALTIME and CLOCK_MONOTONIC > and return EINVAL if any other clock is specified. > > * sysdeps/nptl/pthread.h: Add pthread_rwlock_clockrdlock and > pthread_wrlock_clockwrlock. > > * nptl/Makefile: Build pthread_rwlock_clockrdlock.c and > pthread_rwlock_clockwrlock.c. > > * nptl/pthread_rwlock_clockrdlock.c: Implement pthread_rwlock_clockrdlock. > > * nptl/pthread_rwlock_clockwrlock.c: Implement pthread_rwlock_clockwrlock. > > * nptl/pthread_rwlock_common.c (__pthread_rwlock_rdlock_full): Add clockid > parameter and verify that it indicates a supported clock on entry so that > we fail even if it doesn't end up being used. Pass that clock on to > futex_abstimed_wait when necessary. (__pthread_rwlock_wrlock_full): > Likewise. > > * nptl/pthread_rwlock_rdlock.c: (__pthread_rwlock_rdlock): Pass > CLOCK_REALTIME to __pthread_rwlock_rdlock_full even though it won't be > used because there's no timeout. > > * nptl/pthread_rwlock_wrlock.c (__pthread_rwlock_wrlock): Pass > CLOCK_REALTIME to __pthread_rwlock_wrlock_full even though it won't be > used because there is no timeout. > > * nptl/pthread_rwlock_timedrdlock.c (pthread_rwlock_timedrdlock): Pass > CLOCK_REALTIME to __pthread_rwlock_rdlock_full since abstime uses that > clock. > > * nptl/pthread_rwlock_timedwrlock.c (pthread_rwlock_timedwrlock): Pass > CLOCK_REALTIME to __pthread_rwlock_wrlock_full since abstime uses that > clock. > > * nptl/tst-abstime.c (th): Add pthread_rwlock_clockrdlock and > pthread_rwlock_clockwrlock timeout tests to match the existing > pthread_rwlock_timedrdloock and pthread_rwlock_timedwrlock tests. > > * nptl/tst-rwlock14.c (do_test): Likewise. > > * nptl/tst-rwlock6.c (tf): Accept thread_args structure so that both a > rwlock and a clockid can be passed to the thread. (do_test_clock): Accept > clockid parameter to specify test clock. Use the magic clockid value of > CLOCK_USE_TIMEDLOCK to indicate that pthread_rwlock_timedrdlock and > pthread_rwlock_timedwrlock should be tested, otherwise pass the specified > clockid to pthread_rwlock_clockrdlock and pthread_rwlock_clockwrlock. > (do_test): Call do_test_clock to test each clockid in turn. > > * nptl/tst-rwlock7.c: Likewise, but also pass the name of the function being > tested to improve error reporting. > > * nptl/tst-rwlock9.c (writer_thread, reader_thread): Accept thread_args > structure so that both the (now int) thread number and clockid can be > passed to the thread. (do_test_clock): Pass the necessary thread_args > when creating the reader and writer threads. (do_test): Call > do_test_clock to test each clockid in turn. > > * manual/threads.texi: Add documentation for pthread_rwlock_clockrdlock and > pthread_rwlock_clockwrclock. > --- > manual/threads.texi | 28 ++- > nptl/Makefile | 2 +- > nptl/Versions | 1 +- > nptl/pthread_rwlock_clockrdlock.c | 27 ++- > nptl/pthread_rwlock_clockwrlock.c | 27 ++- > nptl/pthread_rwlock_common.c | 40 +-- > nptl/pthread_rwlock_rdlock.c | 2 +- > nptl/pthread_rwlock_timedrdlock.c | 2 +- > nptl/pthread_rwlock_timedwrlock.c | 2 +- > nptl/pthread_rwlock_wrlock.c | 2 +- > nptl/tst-abstime.c | 28 ++- > nptl/tst-rwlock14.c | 147 ++++++++++++- > nptl/tst-rwlock6.c | 105 ++++++--- > nptl/tst-rwlock7.c | 75 ++++-- > nptl/tst-rwlock9.c | 86 +++++-- > sysdeps/nptl/pthread.h | 8 +- > sysdeps/unix/sysv/linux/x86_64/64/libpthread.abilist | 2 +- > 17 files changed, 491 insertions(+), 93 deletions(-) > create mode 100644 nptl/pthread_rwlock_clockrdlock.c > create mode 100644 nptl/pthread_rwlock_clockwrlock.c > > diff --git a/manual/threads.texi b/manual/threads.texi > index 91462f5..83b8bb6 100644 > --- a/manual/threads.texi > +++ b/manual/threads.texi > @@ -699,6 +699,34 @@ specified or defaulted when @code{pthread_cond_init} was called. Currently, > @code{CLOCK_REALTIME}. > @end deftypefun > > +@comment pthread.h > +@comment POSIX-proposed > +@deftypefun int pthread_rwlock_clockrdlock (pthread_rwlock_t *@var{rwlock}, > + clockid_t @var{clockid}, > + const struct timespec *@var{abstime}) > + > +@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}} > +Behaves like @code{pthread_rwlock_timedrdlock} except the time > +@var{abstime} is measured against the clock specified by @var{clockid} > +rather than @code{CLOCK_REALTIME}. Currently, @var{clockid} must be either > +@code{CLOCK_MONOTONIC} or @code{CLOCK_REALTIME}, otherwise @code{EINVAL} is > +returned. > +@end deftypefun > + > +@comment pthread.h > +@comment POSIX-proposed > +@deftypefun int pthread_rwlock_clockwrlock (pthread_rwlock_t *@var{rwlock}, > + clockid_t @var{clockid}, > + const struct timespec *@var{abstime}) > + > +@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}} > +Behaves like @code{pthread_rwlock_timedwrlock} except the time > +@var{abstime} is measured against the clock specified by @var{clockid} > +rather than @code{CLOCK_REALTIME}. Currently, @var{clockid} must be either > +@code{CLOCK_MONOTONIC} or @code{CLOCK_REALTIME}, otherwise @code{EINVAL} is > +returned. > +@end deftypefun > + > @c FIXME these are undocumented: > @c pthread_atfork > @c pthread_attr_destroy > diff --git a/nptl/Makefile b/nptl/Makefile > index 7de4d40..f5e6d90 100644 > --- a/nptl/Makefile > +++ b/nptl/Makefile > @@ -76,7 +76,9 @@ libpthread-routines = nptl-init nptlfreeres vars events version pt-interp \ > pthread_mutexattr_gettype pthread_mutexattr_settype \ > pthread_rwlock_init pthread_rwlock_destroy \ > pthread_rwlock_rdlock pthread_rwlock_timedrdlock \ > + pthread_rwlock_clockrdlock \ > pthread_rwlock_wrlock pthread_rwlock_timedwrlock \ > + pthread_rwlock_clockwrlock \ > pthread_rwlock_tryrdlock pthread_rwlock_trywrlock \ > pthread_rwlock_unlock \ > pthread_rwlockattr_init pthread_rwlockattr_destroy \ > diff --git a/nptl/Versions b/nptl/Versions > index 8c094d0..ce79959 100644 > --- a/nptl/Versions > +++ b/nptl/Versions > @@ -279,6 +279,7 @@ libpthread { > > GLIBC_2.30 { > sem_clockwait; pthread_cond_clockwait; > + pthread_rwlock_clockrdlock; pthread_rwlock_clockwrlock; > } > > GLIBC_PRIVATE { Same naming issue noted by Joseph in first patch reply. > diff --git a/nptl/pthread_rwlock_clockrdlock.c b/nptl/pthread_rwlock_clockrdlock.c > new file mode 100644 > index 0000000..a6c7456 > --- /dev/null > +++ b/nptl/pthread_rwlock_clockrdlock.c > @@ -0,0 +1,27 @@ Missing one-line description. > +/* Copyright (C) 2019 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <http://www.gnu.org/licenses/>. */ > + > +#include "pthread_rwlock_common.c" > + > +/* See pthread_rwlock_common.c. */ > +int > +pthread_rwlock_clockrdlock (pthread_rwlock_t *rwlock, > + clockid_t clockid, > + const struct timespec *abstime) > +{ > + return __pthread_rwlock_rdlock_full (rwlock, clockid, abstime); > +} > diff --git a/nptl/pthread_rwlock_clockwrlock.c b/nptl/pthread_rwlock_clockwrlock.c > new file mode 100644 > index 0000000..d248a2a > --- /dev/null > +++ b/nptl/pthread_rwlock_clockwrlock.c > @@ -0,0 +1,27 @@ Missing one-line description. > +/* Copyright (C) 2019 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + <http://www.gnu.org/licenses/>. */ > + > +#include "pthread_rwlock_common.c" > + > +/* See pthread_rwlock_common.c. */ > +int > +pthread_rwlock_clockwrlock (pthread_rwlock_t *rwlock, > + clockid_t clockid, > + const struct timespec *abstime) > +{ > + return __pthread_rwlock_wrlock_full (rwlock, clockid, abstime); > +} > diff --git a/nptl/pthread_rwlock_common.c b/nptl/pthread_rwlock_common.c > index 120b880..50a366e 100644 > --- a/nptl/pthread_rwlock_common.c > +++ b/nptl/pthread_rwlock_common.c > @@ -278,17 +278,19 @@ __pthread_rwlock_rdunlock (pthread_rwlock_t *rwlock) > > static __always_inline int > __pthread_rwlock_rdlock_full (pthread_rwlock_t *rwlock, > + clockid_t clockid, > const struct timespec *abstime) > { > unsigned int r; > > - /* Make sure any passed in timeout value is valid. Note that the previous > - implementation assumed that this check *must* not be performed if there > - would in fact be no blocking; however, POSIX only requires that "the > - validity of the abstime parameter need not be checked if the lock can be > - immediately acquired" (i.e., we need not but may check it). */ > - if (abstime > - && __glibc_unlikely (abstime->tv_nsec >= 1000000000 > + /* Make sure any passed in clockid and timeout value are valid. Note > + that the previous implementation assumed that this check *must* > + not be performed if there would in fact be no blocking; however, > + POSIX only requires that "the validity of the abstime parameter > + need not be checked if the lock can be immediately acquired" > + (i.e., we need not but may check it). */ > + if (abstime && __glibc_unlikely (!futex_abstimed_supported_clockid (clockid) > + || abstime->tv_nsec >= 1000000000 > || abstime->tv_nsec < 0)) > return EINVAL; > > @@ -329,7 +331,7 @@ __pthread_rwlock_rdlock_full (pthread_rwlock_t *rwlock, > { > int private = __pthread_rwlock_get_private (rwlock); > int err = futex_abstimed_wait (&rwlock->__data.__readers, > - r, CLOCK_REALTIME, abstime, private); > + r, clockid, abstime, private); > /* We ignore EAGAIN and EINTR. On time-outs, we can just > return because we don't need to clean up anything. */ > if (err == ETIMEDOUT) > @@ -457,7 +459,7 @@ __pthread_rwlock_rdlock_full (pthread_rwlock_t *rwlock, > continue; > int err = futex_abstimed_wait (&rwlock->__data.__wrphase_futex, > 1 | PTHREAD_RWLOCK_FUTEX_USED, > - CLOCK_REALTIME, abstime, private); > + clockid, abstime, private); > if (err == ETIMEDOUT) > { > /* If we timed out, we need to unregister. If no read phase > @@ -584,15 +586,17 @@ __pthread_rwlock_wrunlock (pthread_rwlock_t *rwlock) > > static __always_inline int > __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock, > + clockid_t clockid, > const struct timespec *abstime) > { > - /* Make sure any passed in timeout value is valid. Note that the previous > - implementation assumed that this check *must* not be performed if there > - would in fact be no blocking; however, POSIX only requires that "the > - validity of the abstime parameter need not be checked if the lock can be > - immediately acquired" (i.e., we need not but may check it). */ > - if (abstime > - && __glibc_unlikely (abstime->tv_nsec >= 1000000000 > + /* Make sure any passed in clockid and timeout value are valid. Note > + that the previous implementation assumed that this check *must* > + not be performed if there would in fact be no blocking; however, > + POSIX only requires that "the validity of the abstime parameter > + need not be checked if the lock can be immediately acquired" > + (i.e., we need not but may check it). */ > + if (abstime && __glibc_unlikely (!futex_abstimed_supported_clockid (clockid) > + || abstime->tv_nsec >= 1000000000 > || abstime->tv_nsec < 0)) > return EINVAL; > > @@ -727,7 +731,7 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock, > may_share_futex_used_flag = true; > int err = futex_abstimed_wait (&rwlock->__data.__writers_futex, > 1 | PTHREAD_RWLOCK_FUTEX_USED, > - CLOCK_REALTIME, abstime, private); > + clockid, abstime, private); > if (err == ETIMEDOUT) > { > if (prefer_writer) > @@ -826,7 +830,7 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock, > continue; > int err = futex_abstimed_wait (&rwlock->__data.__wrphase_futex, > PTHREAD_RWLOCK_FUTEX_USED, > - CLOCK_REALTIME, abstime, private); > + clockid, abstime, private); > if (err == ETIMEDOUT) > { > if (rwlock->__data.__flags != PTHREAD_RWLOCK_PREFER_READER_NP) Ok. > diff --git a/nptl/pthread_rwlock_rdlock.c b/nptl/pthread_rwlock_rdlock.c > index 5fdc89e..387c824 100644 > --- a/nptl/pthread_rwlock_rdlock.c > +++ b/nptl/pthread_rwlock_rdlock.c > @@ -24,7 +24,7 @@ __pthread_rwlock_rdlock (pthread_rwlock_t *rwlock) > { > LIBC_PROBE (rdlock_entry, 1, rwlock); > > - int result = __pthread_rwlock_rdlock_full (rwlock, NULL); > + int result = __pthread_rwlock_rdlock_full (rwlock, CLOCK_REALTIME, NULL); > LIBC_PROBE (rdlock_acquire_read, 1, rwlock); > return result; > } Ok. > diff --git a/nptl/pthread_rwlock_timedrdlock.c b/nptl/pthread_rwlock_timedrdlock.c > index 84c1983..8f8e680 100644 > --- a/nptl/pthread_rwlock_timedrdlock.c > +++ b/nptl/pthread_rwlock_timedrdlock.c > @@ -23,5 +23,5 @@ int > pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock, > const struct timespec *abstime) > { > - return __pthread_rwlock_rdlock_full (rwlock, abstime); > + return __pthread_rwlock_rdlock_full (rwlock, CLOCK_REALTIME, abstime); > } Ok. > diff --git a/nptl/pthread_rwlock_timedwrlock.c b/nptl/pthread_rwlock_timedwrlock.c > index f0b745d..a5616de 100644 > --- a/nptl/pthread_rwlock_timedwrlock.c > +++ b/nptl/pthread_rwlock_timedwrlock.c > @@ -23,5 +23,5 @@ int > pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock, > const struct timespec *abstime) > { > - return __pthread_rwlock_wrlock_full (rwlock, abstime); > + return __pthread_rwlock_wrlock_full (rwlock, CLOCK_REALTIME, abstime); > } Ok. > diff --git a/nptl/pthread_rwlock_wrlock.c b/nptl/pthread_rwlock_wrlock.c > index 194a14c..da246d8 100644 > --- a/nptl/pthread_rwlock_wrlock.c > +++ b/nptl/pthread_rwlock_wrlock.c > @@ -24,7 +24,7 @@ __pthread_rwlock_wrlock (pthread_rwlock_t *rwlock) > { > LIBC_PROBE (wrlock_entry, 1, rwlock); > > - int result = __pthread_rwlock_wrlock_full (rwlock, NULL); > + int result = __pthread_rwlock_wrlock_full (rwlock, CLOCK_REALTIME, NULL); > LIBC_PROBE (wrlock_acquire_write, 1, rwlock); > return result; > } Ok. > diff --git a/nptl/tst-abstime.c b/nptl/tst-abstime.c > index 71610f8..f4c9f2d 100644 > --- a/nptl/tst-abstime.c > +++ b/nptl/tst-abstime.c > @@ -47,12 +47,40 @@ th (void *arg) > puts ("pthread_rwlock_timedrdlock did not return ETIMEDOUT"); > res = 1; > } > + r = pthread_rwlock_clockrdlock (&rw1, CLOCK_REALTIME, &t); > + if (r != ETIMEDOUT) > + { > + puts ("pthread_rwlock_clockrdlock (CLOCK_REALTIME) " > + "did not return ETIMEDOUT"); > + res = 1; > + } > + r = pthread_rwlock_clockrdlock (&rw1, CLOCK_MONOTONIC, &t); > + if (r != ETIMEDOUT) > + { > + puts ("pthread_rwlock_clockrdlock (CLOCK_MONOTONIC) " > + "did not return ETIMEDOUT"); > + res = 1; > + } I prefer even new additions to old tests should use libsupport (it would be preferable also to adapt old tests to libsupport in such cases). Use TEST_COMPARE (pthread_rwlock_clockrdlock (&rw1, CLOCK_REALTIME, &t), ETIMEOUT); and TEST_COMPARE (pthread_rwlock_clockrdlock (&rw1, CLOCK_MONOTONIC, &t), ETIMEOUT); > r = pthread_rwlock_timedwrlock (&rw2, &t); > if (r != ETIMEDOUT) > { > puts ("pthread_rwlock_timedwrlock did not return ETIMEDOUT"); > res = 1; > } > + r = pthread_rwlock_clockwrlock (&rw2, CLOCK_REALTIME, &t); > + if (r != ETIMEDOUT) > + { > + puts ("pthread_rwlock_clockwrlock(CLOCK_REALTIME) " > + "did not return ETIMEDOUT"); > + res = 1; > + } > + r = pthread_rwlock_clockwrlock (&rw2, CLOCK_MONOTONIC, &t); > + if (r != ETIMEDOUT) > + { > + puts ("pthread_rwlock_clockwrlock(CLOCK_MONOTONIC) " > + "did not return ETIMEDOUT"); > + res = 1; > + } > return (void *) res; > } > Ditto. > diff --git a/nptl/tst-rwlock14.c b/nptl/tst-rwlock14.c > index 6f57169..525ceac 100644 > --- a/nptl/tst-rwlock14.c > +++ b/nptl/tst-rwlock14.c > @@ -91,6 +91,30 @@ do_test (void) > result = 1; > } > > + e = pthread_rwlock_clockrdlock (&r, CLOCK_REALTIME, &ts); > + if (e == 0) > + { > + puts ("first rwlock_clockrdlock (CLOCK_REALTIME) did not fail"); > + result = 1; > + } > + else if (e != EINVAL) > + { > + puts ("first rwlock_clockrdlock (CLOCK_REALTIME) did not return EINVAL"); > + result = 1; > + } > + > + e = pthread_rwlock_clockrdlock (&r, CLOCK_MONOTONIC, &ts); > + if (e == 0) > + { > + puts ("first rwlock_clockrdlock (CLOCK_MONOTONIC) did not fail"); > + result = 1; > + } > + else if (e != EINVAL) > + { > + puts ("first rwlock_clockrdlock (CLOCK_MONOTONIC) did not return EINVAL"); > + result = 1; > + } > + Ditto. > e = pthread_rwlock_timedwrlock (&r, &ts); > if (e == 0) > { > @@ -103,6 +127,30 @@ do_test (void) > result = 1; > } > > + e = pthread_rwlock_clockwrlock (&r, CLOCK_REALTIME, &ts); > + if (e == 0) > + { > + puts ("first rwlock_clockwrlock (CLOCK_REALTIME) did not fail"); > + result = 1; > + } > + else if (e != EINVAL) > + { > + puts ("first rwlock_clockwrlock (CLOCK_REALTIME) did not return EINVAL"); > + result = 1; > + } > + > + e = pthread_rwlock_clockwrlock (&r, CLOCK_MONOTONIC, &ts); > + if (e == 0) > + { > + puts ("first rwlock_clockwrlock (CLOCK_MONOTONIC) did not fail"); > + result = 1; > + } > + else if (e != EINVAL) > + { > + puts ("first rwlock_clockwrlock (CLOCK_MONOTONIC) did not return EINVAL"); > + result = 1; > + } > + Ditto. > ts.tv_nsec = 1000000000; > > e = pthread_rwlock_timedrdlock (&r, &ts); > @@ -117,6 +165,31 @@ do_test (void) > result = 1; > } > > + e = pthread_rwlock_clockrdlock (&r, CLOCK_REALTIME, &ts); > + if (e == 0) > + { > + puts ("second rwlock_clockrdlock (CLOCK_REALTIME) did not fail"); > + result = 1; > + } > + else if (e != EINVAL) > + { > + puts ("second rwlock_clockrdlock (CLOCK_REALTIME) did not return EINVAL"); > + result = 1; > + } > + > + e = pthread_rwlock_clockrdlock (&r, CLOCK_MONOTONIC, &ts); > + if (e == 0) > + { > + puts ("second rwlock_clockrdlock (CLOCK_MONOTONIC) did not fail"); > + result = 1; > + } > + else if (e != EINVAL) > + { > + puts ("second rwlock_clockrdlock (CLOCK_MONOTONIC) " > + "did not return EINVAL"); > + result = 1; > + } > + Ditto. > e = pthread_rwlock_timedwrlock (&r, &ts); > if (e == 0) > { > @@ -129,6 +202,32 @@ do_test (void) > result = 1; > } > > + e = pthread_rwlock_clockwrlock (&r, CLOCK_MONOTONIC, &ts); > + if (e == 0) > + { > + puts ("second rwlock_clockwrlock (CLOCK_MONOTONIC) did not fail"); > + result = 1; > + } > + else if (e != EINVAL) > + { > + puts ("second rwlock_clockwrlock (CLOCK_MONOTONIC) " > + "did not return EINVAL"); > + result = 1; > + } > + > + e = pthread_rwlock_clockwrlock (&r, CLOCK_REALTIME, &ts); > + if (e == 0) > + { > + puts ("second rwlock_clockwrlock (CLOCK_REALTIME) did not fail"); > + result = 1; > + } > + else if (e != EINVAL) > + { > + puts ("second rwlock_clockwrlock (CLOCK_REALTIME) " > + "did not return EINVAL"); > + result = 1; > + } > + Ditto. > ts.tv_nsec = (__typeof (ts.tv_nsec)) 0x100001000LL; > if ((__typeof (ts.tv_nsec)) 0x100001000LL != 0x100001000LL) > ts.tv_nsec = 2000000000; > @@ -145,6 +244,30 @@ do_test (void) > result = 1; > } > > + e = pthread_rwlock_clockrdlock (&r, CLOCK_REALTIME, &ts); > + if (e == 0) > + { > + puts ("third rwlock_clockrdlock (CLOCK_REALTIME) did not fail"); > + result = 1; > + } > + else if (e != EINVAL) > + { > + puts ("third rwlock_clockrdlock (CLOCK_REALTIME) did not return EINVAL"); > + result = 1; > + } > + > + e = pthread_rwlock_clockrdlock (&r, CLOCK_MONOTONIC, &ts); > + if (e == 0) > + { > + puts ("third rwlock_clockrdlock(CLOCK_MONOTONIC) did not fail"); > + result = 1; > + } > + else if (e != EINVAL) > + { > + puts ("third rwlock_clockrdlock (CLOCK_MONOTONIC) did not return EINVAL"); > + result = 1; > + } > + > e = pthread_rwlock_timedwrlock (&r, &ts); > if (e == 0) > { > @@ -157,6 +280,30 @@ do_test (void) > result = 1; > } > Ditto. > + e = pthread_rwlock_clockwrlock (&r, CLOCK_REALTIME, &ts); > + if (e == 0) > + { > + puts ("third rwlock_clockwrlock (CLOCK_REALTIME) did not fail"); > + result = 1; > + } > + else if (e != EINVAL) > + { > + puts ("third rwlock_clockwrlock (CLOCK_REALTIME) did not return EINVAL"); > + result = 1; > + } > + > + e = pthread_rwlock_clockwrlock (&r, CLOCK_MONOTONIC, &ts); > + if (e == 0) > + { > + puts ("third rwlock_clockwrlock (CLOCK_MONOTONIC) did not fail"); > + result = 1; > + } > + else if (e != EINVAL) > + { > + puts ("third rwlock_clockwrlock (CLOCK_MONOTONIC) did not return EINVAL"); > + result = 1; > + } > + Ditto. > if (result == 0) > puts ("no bugs"); > > diff --git a/nptl/tst-rwlock6.c b/nptl/tst-rwlock6.c > index 67119fa..708915e 100644 > --- a/nptl/tst-rwlock6.c > +++ b/nptl/tst-rwlock6.c > @@ -25,6 +25,11 @@ > #include <support/timespec.h> > > > +/* A bogus clock value that tells run_test to use > + pthread_rwlock_timedrdlock and pthread_rwlock_timedwrlock rather > + than pthread_rwlock_clockrdlock and pthread_rwlock_clockwrlock. */ > +#define CLOCK_USE_TIMEDLOCK (-1) > + > static int kind[] = > { > PTHREAD_RWLOCK_PREFER_READER_NP, > @@ -32,75 +37,91 @@ static int kind[] = > PTHREAD_RWLOCK_PREFER_WRITER_NP, > }; > > +struct thread_args { > + pthread_rwlock_t *rwlock; > + clockid_t clockid; > + const char *fnname; > +}; Line-feed before open bracket. > > static void * > tf (void *arg) > { > - pthread_rwlock_t *r = arg; > + struct thread_args *args = arg; > + pthread_rwlock_t *r = args->rwlock; > + const clockid_t clockid = args->clockid; > + const clockid_t clockid_for_get = > + (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid; > + const char *fnname = args->fnname; > > /* Timeout: 0.3 secs. */ > struct timespec ts_start; > - (void) clock_gettime(CLOCK_REALTIME, &ts_start); > + (void) clock_gettime (clockid_for_get, &ts_start); > > struct timespec ts_timeout = {0, 300000000}; > - timespec_add(&ts_timeout, &ts_start, &ts_timeout); > + timespec_add (&ts_timeout, &ts_start, &ts_timeout); > > - puts ("child calling timedrdlock"); > + printf ("child calling %srdlock\n", fnname); > > - int err = pthread_rwlock_timedrdlock (r, &ts_timeout); > + int err = (clockid == CLOCK_USE_TIMEDLOCK) > + ? pthread_rwlock_timedrdlock (r, &ts_timeout) > + : pthread_rwlock_clockrdlock (r, clockid, &ts_timeout); > if (err == 0) > { > - puts ("rwlock_timedrdlock returned"); > + printf ("%srdlock returned\n", fnname); > pthread_exit ((void *) 1l); > } > > if (err != ETIMEDOUT) > { > - printf ("err = %s (%d), expected %s (%d)\n", > - strerror (err), err, strerror (ETIMEDOUT), ETIMEDOUT); > + printf ("%srdlock: err = %s (%d), expected %s (%d)\n", > + fnname, strerror (err), err, strerror (ETIMEDOUT), ETIMEDOUT); > pthread_exit ((void *) 1l); > } > > - puts ("1st child timedrdlock done"); > + printf ("1st child %srdlock done\n", fnname); > > struct timespec ts_end; > - (void) clock_gettime (CLOCK_REALTIME, &ts_end); > + (void) clock_gettime (clockid_for_get, &ts_end); > > struct timespec ts_duration; > - timespec_sub(&ts_duration, &ts_end, &ts_start); > + timespec_sub (&ts_duration, &ts_end, &ts_start); > > if (ts_duration.tv_sec !=0 || ts_duration.tv_nsec < 200000000) > { > - puts ("timeout too short"); > + printf ("%srdlock: timeout too short\n", fnname); > pthread_exit ((void *) 1l); > } > > - (void) clock_gettime (CLOCK_REALTIME, &ts_timeout); > + (void) clock_gettime (clockid_for_get, &ts_timeout); > ts_timeout.tv_sec += 10; > /* Note that the following operation makes ts invalid. */ > ts_timeout.tv_nsec += 1000000000; > > - err = pthread_rwlock_timedrdlock (r, &ts_timeout); > + err = (clockid == CLOCK_USE_TIMEDLOCK) > + ? pthread_rwlock_timedrdlock (r, &ts_timeout) > + : pthread_rwlock_clockrdlock (r, clockid, &ts_timeout); > if (err == 0) > { > - puts ("2nd timedrdlock succeeded"); > + printf ("2nd %srdlock succeeded\n", fnname); > pthread_exit ((void *) 1l); > } > if (err != EINVAL) > { > - puts ("2nd timedrdlock did not return EINVAL"); > + printf ("2nd %srdlock did not return EINVAL\n", fnname); > pthread_exit ((void *) 1l); > } > > - puts ("2nd child timedrdlock done"); > + printf ("2nd child %srdlock done\n", fnname); > > return NULL; > } > > > static int > -do_test (void) > +do_test_clock (clockid_t clockid, const char *fnname) > { > + const clockid_t clockid_for_get = > + (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid; > size_t cnt; > for (cnt = 0; cnt < sizeof (kind) / sizeof (kind[0]); ++cnt) > { > @@ -132,53 +153,63 @@ do_test (void) > } > > struct timespec ts; > - (void) clock_gettime (CLOCK_REALTIME, &ts); > + (void) clock_gettime (clockid_for_get, &ts); > ++ts.tv_sec; > > /* Get a write lock. */ > - int e = pthread_rwlock_timedwrlock (&r, &ts); > + int e = (clockid == CLOCK_USE_TIMEDLOCK) > + ? pthread_rwlock_timedwrlock (&r, &ts) > + : pthread_rwlock_clockwrlock (&r, clockid, &ts); > if (e != 0) > { > - printf ("round %Zu: rwlock_timedwrlock failed (%d)\n", cnt, e); > + printf ("round %Zu: %swrlock failed (%d)\n", cnt, fnname, e); > exit (1); > } > > - puts ("1st timedwrlock done"); > + printf ("1st %swrlock done\n", fnname); > > - (void) clock_gettime (CLOCK_REALTIME, &ts); > + (void) clock_gettime (clockid, &ts); > ++ts.tv_sec; > - e = pthread_rwlock_timedrdlock (&r, &ts); > + e = (clockid == CLOCK_USE_TIMEDLOCK) > + ? pthread_rwlock_timedrdlock (&r, &ts) > + : pthread_rwlock_clockrdlock (&r, clockid, &ts); > if (e == 0) > { > - puts ("timedrdlock succeeded"); > + printf ("%srdlock succeeded\n", fnname); > exit (1); > } > if (e != EDEADLK) > { > - puts ("timedrdlock did not return EDEADLK"); > + printf ("%srdlock did not return EDEADLK\n", fnname); > exit (1); > } > > - puts ("1st timedrdlock done"); > + printf ("1st %srdlock done\n", fnname); > > - (void) clock_gettime (CLOCK_REALTIME, &ts); > + (void) clock_gettime (clockid_for_get, &ts); > ++ts.tv_sec; > - e = pthread_rwlock_timedwrlock (&r, &ts); > + e = (clockid == CLOCK_USE_TIMEDLOCK) > + ? pthread_rwlock_timedwrlock (&r, &ts) > + : pthread_rwlock_clockwrlock (&r, clockid, &ts); > if (e == 0) > { > - puts ("2nd timedwrlock succeeded"); > + printf ("2nd %swrlock succeeded\n", fnname); > exit (1); > } > if (e != EDEADLK) > { > - puts ("2nd timedwrlock did not return EDEADLK"); > + printf ("2nd %swrlock did not return EDEADLK\n", fnname); > exit (1); > } > > - puts ("2nd timedwrlock done"); > + printf ("2nd %swrlock done\n", fnname); > > pthread_t th; > - if (pthread_create (&th, NULL, tf, &r) != 0) > + struct thread_args args; > + args.rwlock = &r; > + args.clockid = clockid; > + args.fnname = fnname; > + if (pthread_create (&th, NULL, tf, &args) != 0) > { > printf ("round %Zu: create failed\n", cnt); > exit (1); > @@ -210,5 +241,13 @@ do_test (void) > return 0; > } > > +static int do_test (void) > +{ > + do_test_clock (CLOCK_USE_TIMEDLOCK, "timed"); > + do_test_clock (CLOCK_REALTIME, "clock(realtime)"); > + do_test_clock (CLOCK_MONOTONIC, "clock(monotonic)"); > + return 0; > +} > + > #define TEST_FUNCTION do_test () > #include "../test-skeleton.c" > diff --git a/nptl/tst-rwlock7.c b/nptl/tst-rwlock7.c > index 812506c..0bccd18 100644 > --- a/nptl/tst-rwlock7.c > +++ b/nptl/tst-rwlock7.c > @@ -25,6 +25,11 @@ > #include <support/timespec.h> > > > +/* A bogus clock value that tells run_test to use > + pthread_rwlock_timedrdlock and pthread_rwlock_timedwrlock rather > + than pthread_rwlock_clockrdlock and pthread_rwlock_clockwrlock. */ > +#define CLOCK_USE_TIMEDLOCK (-1) > + > static int kind[] = > { > PTHREAD_RWLOCK_PREFER_READER_NP, > @@ -32,22 +37,34 @@ static int kind[] = > PTHREAD_RWLOCK_PREFER_WRITER_NP, > }; > > +struct thread_args > +{ > + pthread_rwlock_t *rwlock; > + clockid_t clockid; > + const char *fnname; > +}; > > static void * > tf (void *arg) > { > - pthread_rwlock_t *r = arg; > + struct thread_args *args = arg; > + pthread_rwlock_t *r = args->rwlock; > + clockid_t clockid = args->clockid; > + const char *fnname = args->fnname; > > /* Timeout: 0.3 secs. */ > struct timespec ts_start; > - (void) clock_gettime (CLOCK_REALTIME, &ts_start); > + (void) clock_gettime ((clockid == CLOCK_USE_TIMEDLOCK) > + ? CLOCK_REALTIME : clockid, &ts_start); > struct timespec ts_timeout = {0, 300000000}; > timespec_add(&ts_timeout, &ts_start, &ts_timeout); > > - int err = pthread_rwlock_timedwrlock (r, &ts_timeout); > + int err = (clockid == CLOCK_USE_TIMEDLOCK) > + ? pthread_rwlock_timedwrlock (r, &ts_timeout) > + : pthread_rwlock_clockwrlock (r, clockid, &ts_timeout); > if (err == 0) > { > - puts ("rwlock_timedwrlock returned"); > + printf ("pthread_rwlock_%swrlock returned\n", fnname); > pthread_exit ((void *) 1l); > } > > @@ -57,44 +74,49 @@ tf (void *arg) > strerror (err), err, strerror (ETIMEDOUT), ETIMEDOUT); > pthread_exit ((void *) 1l); > } > - puts ("child: timedwrlock failed with ETIMEDOUT"); > + printf ("child: pthread_rwlock_%swrlock failed with ETIMEDOUT\n", fnname); > > struct timespec ts_end; > - (void) clock_gettime (CLOCK_REALTIME, &ts_end); > + (void) clock_gettime ((clockid == CLOCK_USE_TIMEDLOCK) > + ? CLOCK_REALTIME : clockid, &ts_end); > struct timespec ts_diff; > timespec_sub (&ts_diff, &ts_end, &ts_start); > > if (ts_diff.tv_sec != 0 || ts_diff.tv_nsec < 200000000) > { > - puts ("timeout too short"); > + printf ("pthread_rwlock_%srwlock timeout too short: %ld.%09ld\n", > + fnname, (long)ts_diff.tv_sec, (long)ts_diff.tv_nsec); > pthread_exit ((void *) 1l); > } > > struct timespec ts_invalid; > - (void) clock_gettime (CLOCK_REALTIME, &ts_invalid); > + (void) clock_gettime ((clockid == CLOCK_USE_TIMEDLOCK) > + ? CLOCK_REALTIME : clockid, &ts_invalid); > ts_invalid.tv_sec += 10; > /* Note that the following operation makes ts invalid. */ > ts_invalid.tv_nsec += 1000000000000; > > - err = pthread_rwlock_timedwrlock (r, &ts_invalid); > + err = (clockid == CLOCK_USE_TIMEDLOCK) > + ? pthread_rwlock_timedwrlock (r, &ts_invalid) > + : pthread_rwlock_clockwrlock (r, clockid, &ts_invalid); > if (err == 0) > { > - puts ("2nd timedwrlock succeeded"); > + printf ("2nd pthread_%srwlock succeeded\n", fnname); > pthread_exit ((void *) 1l); > } > if (err != EINVAL) > { > - puts ("2nd timedwrlock did not return EINVAL"); > + printf ("2nd pthread_%srwlock did not return EINVAL\n", fnname); > pthread_exit ((void *) 1l); > } > - puts ("child: timedwrlock failed with EINVAL"); > + printf ("child: %s failed with EINVAL\n", fnname); > > return NULL; > } > > > static int > -do_test (void) > +do_test_clock (clockid_t clockid, const char *fnname) > { > size_t cnt; > for (cnt = 0; cnt < sizeof (kind) / sizeof (kind[0]); ++cnt) > @@ -127,20 +149,27 @@ do_test (void) > } > > struct timespec ts; > - (void) clock_gettime (CLOCK_REALTIME, &ts); > + (void) clock_gettime ((clockid == CLOCK_USE_TIMEDLOCK) ? > + CLOCK_REALTIME: clockid, &ts); > > ++ts.tv_sec; > > /* Get a read lock. */ > - if (pthread_rwlock_timedrdlock (&r, &ts) != 0) > + if ((clockid == CLOCK_USE_TIMEDLOCK) > + ? pthread_rwlock_timedrdlock (&r, &ts) > + : pthread_rwlock_clockrdlock (&r, clockid, &ts) != 0) > { > - printf ("round %Zu: rwlock_timedrdlock failed\n", cnt); > + printf ("round %Zu: pthread_rwlock%srdlock failed\n", cnt, fnname); > exit (1); > } > - printf ("%zu: got timedrdlock\n", cnt); > + printf ("%zu: got pthread_rwlock_%srdlock\n", cnt, fnname); > > + struct thread_args args; > + args.rwlock = &r; > + args.clockid = clockid; > + args.fnname = fnname; > pthread_t th; > - if (pthread_create (&th, NULL, tf, &r) != 0) > + if (pthread_create (&th, NULL, tf, &args) != 0) > { > printf ("round %Zu: create failed\n", cnt); > exit (1); > @@ -168,5 +197,15 @@ do_test (void) > return 0; > } > > +static int > +do_test (void) > +{ > + do_test_clock (CLOCK_USE_TIMEDLOCK, "timed"); > + do_test_clock (CLOCK_MONOTONIC, "clock(monotonic)"); > + do_test_clock (CLOCK_REALTIME, "clock(realtime)"); > + > + return 0; > +} > + > #define TEST_FUNCTION do_test () > #include "../test-skeleton.c" > diff --git a/nptl/tst-rwlock9.c b/nptl/tst-rwlock9.c > index ff15f90..2691f6a 100644 > --- a/nptl/tst-rwlock9.c > +++ b/nptl/tst-rwlock9.c > @@ -38,12 +38,28 @@ > # define KIND PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP > #endif > > +/* A bogus clock value that tells the tests to use > + pthread_rwlock_timedrdlock and pthread_rwlock_timedwrlock rather > + than pthread_rwlock_clockrdlock and pthread_rwlock_clockwrlock. */ > +#define CLOCK_USE_TIMEDLOCK (-1) > + > static pthread_rwlock_t lock; > > +struct thread_args > +{ > + int nr; > + clockid_t clockid; > + const char *fnname; > +}; > > static void * > -writer_thread (void *nr) > +writer_thread (void *arg) > { > + struct thread_args *args = arg; > + const int nr = args->nr; > + const clockid_t clockid = args->clockid; > + const char *fnname = args->fnname; > + > struct timespec ts; > struct timespec delay; > int n; > @@ -56,7 +72,8 @@ writer_thread (void *nr) > int e; > do > { > - (void) clock_gettime (CLOCK_REALTIME, &ts); > + (void) clock_gettime ((clockid == CLOCK_USE_TIMEDLOCK) > + ? CLOCK_REALTIME : clockid, &ts); > > ts.tv_nsec += 2 * TIMEOUT; > if (ts.tv_nsec >= 1000000000) > @@ -65,18 +82,20 @@ writer_thread (void *nr) > ++ts.tv_sec; > } > > - printf ("writer thread %ld tries again\n", (long int) nr); > + printf ("writer thread %d tries again\n", nr); > > - e = pthread_rwlock_timedwrlock (&lock, &ts); > + e = (clockid == CLOCK_USE_TIMEDLOCK) > + ? pthread_rwlock_timedwrlock (&lock, &ts) > + : pthread_rwlock_clockwrlock (&lock, clockid, &ts); > if (e != 0 && e != ETIMEDOUT) > { > - puts ("timedwrlock failed"); > + printf ("%swrlock failed", fnname); > exit (1); > } > } > while (e == ETIMEDOUT); > > - printf ("writer thread %ld succeeded\n", (long int) nr); > + printf ("writer thread %d succeeded\n", nr); > > nanosleep (&delay, NULL); > > @@ -86,7 +105,7 @@ writer_thread (void *nr) > exit (1); > } > > - printf ("writer thread %ld released\n", (long int) nr); > + printf ("writer thread %d released\n", nr); > } > > return NULL; > @@ -94,8 +113,13 @@ writer_thread (void *nr) > > > static void * > -reader_thread (void *nr) > +reader_thread (void *arg) > { > + struct thread_args *args = arg; > + const int nr = args->nr; > + const clockid_t clockid = args->clockid; > + const char *fnname = args->fnname; > + > struct timespec ts; > struct timespec delay; > int n; > @@ -108,8 +132,8 @@ reader_thread (void *nr) > int e; > do > { > - (void) clock_gettime (CLOCK_REALTIME, &ts); > - > + (void) clock_gettime ((clockid == CLOCK_USE_TIMEDLOCK) > + ? CLOCK_REALTIME : clockid, &ts); > ts.tv_nsec += TIMEOUT; > if (ts.tv_nsec >= 1000000000) > { > @@ -117,18 +141,20 @@ reader_thread (void *nr) > ++ts.tv_sec; > } > > - printf ("reader thread %ld tries again\n", (long int) nr); > + printf ("reader thread %d tries again\n", nr); > > - e = pthread_rwlock_timedrdlock (&lock, &ts); > + e = (clockid == CLOCK_USE_TIMEDLOCK) > + ? pthread_rwlock_timedrdlock (&lock, &ts) > + : pthread_rwlock_clockrdlock (&lock, clockid, &ts); > if (e != 0 && e != ETIMEDOUT) > { > - puts ("timedrdlock failed"); > + printf ("%srdlock failed\n", fnname); > exit (1); > } > } > while (e == ETIMEDOUT); > > - printf ("reader thread %ld succeeded\n", (long int) nr); > + printf ("reader thread %d succeeded\n", nr); > > nanosleep (&delay, NULL); > > @@ -138,7 +164,7 @@ reader_thread (void *nr) > exit (1); > } > > - printf ("reader thread %ld released\n", (long int) nr); > + printf ("reader thread %d released\n", nr); > } > > return NULL; > @@ -146,7 +172,7 @@ reader_thread (void *nr) > > > static int > -do_test (void) > +do_test_clock (clockid_t clockid, const char *fnname) > { > pthread_t thwr[NWRITERS]; > pthread_t thrd[NREADERS]; > @@ -178,21 +204,31 @@ do_test (void) > /* Make sure we see all message, even those on stdout. */ > setvbuf (stdout, NULL, _IONBF, 0); > > - for (n = 0; n < NWRITERS; ++n) > + struct thread_args wargs[NWRITERS]; > + for (n = 0; n < NWRITERS; ++n) { > + wargs[n].nr = n; > + wargs[n].clockid = clockid; > + wargs[n].fnname = fnname; > if (pthread_create (&thwr[n], NULL, writer_thread, > - (void *) (long int) n) != 0) > + &wargs[n]) != 0) > { > puts ("writer create failed"); > exit (1); > } > + } > > - for (n = 0; n < NREADERS; ++n) > + struct thread_args rargs[NREADERS]; > + for (n = 0; n < NREADERS; ++n) { > + rargs[n].nr = n; > + rargs[n].clockid = clockid; > + rargs[n].fnname = fnname; > if (pthread_create (&thrd[n], NULL, reader_thread, > - (void *) (long int) n) != 0) > + &rargs[n]) != 0) > { > puts ("reader create failed"); > exit (1); > } > + } > > /* Wait for all the threads. */ > for (n = 0; n < NWRITERS; ++n) > @@ -211,6 +247,16 @@ do_test (void) > return 0; > } > > +static int > +do_test (void) > +{ > + do_test_clock (CLOCK_USE_TIMEDLOCK, "timed"); > + do_test_clock (CLOCK_REALTIME, "clock(realtime)"); > + do_test_clock (CLOCK_MONOTONIC, "clock(monotonic)"); > + > + return 0; > +} > + > #undef TIMEOUT > #define TIMEOUT 30 > #define TEST_FUNCTION do_test () > diff --git a/sysdeps/nptl/pthread.h b/sysdeps/nptl/pthread.h > index d4fe9d9..3c06d77 100644 > --- a/sysdeps/nptl/pthread.h > +++ b/sysdeps/nptl/pthread.h > @@ -907,6 +907,10 @@ extern int pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock) > extern int pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock, > const struct timespec *__restrict > __abstime) __THROWNL __nonnull ((1, 2)); > +extern int pthread_rwlock_clockrdlock (pthread_rwlock_t *__restrict __rwlock, > + clockid_t __clockid, > + const struct timespec *__restrict > + __abstime) __THROWNL __nonnull ((1, 3)); > # endif > > /* Acquire write lock for RWLOCK. */ > @@ -922,6 +926,10 @@ extern int pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock) > extern int pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock, > const struct timespec *__restrict > __abstime) __THROWNL __nonnull ((1, 2)); > +extern int pthread_rwlock_clockwrlock (pthread_rwlock_t *__restrict __rwlock, > + clockid_t __clockid, > + const struct timespec *__restrict > + __abstime) __THROWNL __nonnull ((1, 3)); > # endif > > /* Unlock RWLOCK. */ > diff --git a/sysdeps/unix/sysv/linux/x86_64/64/libpthread.abilist b/sysdeps/unix/sysv/linux/x86_64/64/libpthread.abilist > index aaa1c3b..70f04cc 100644 > --- a/sysdeps/unix/sysv/linux/x86_64/64/libpthread.abilist > +++ b/sysdeps/unix/sysv/linux/x86_64/64/libpthread.abilist > @@ -246,6 +246,8 @@ GLIBC_2.3.4 pthread_getaffinity_np F > GLIBC_2.3.4 pthread_setaffinity_np F > GLIBC_2.3.4 pthread_setschedprio F > GLIBC_2.30 pthread_cond_clockwait F > +GLIBC_2.30 pthread_rwlock_clockrdlock F > +GLIBC_2.30 pthread_rwlock_clockwrlock F > GLIBC_2.30 sem_clockwait F > GLIBC_2.4 pthread_mutex_consistent_np F > GLIBC_2.4 pthread_mutex_getprioceiling F >
diff --git a/manual/threads.texi b/manual/threads.texi index 91462f5..83b8bb6 100644 --- a/manual/threads.texi +++ b/manual/threads.texi @@ -699,6 +699,34 @@ specified or defaulted when @code{pthread_cond_init} was called. Currently, @code{CLOCK_REALTIME}. @end deftypefun +@comment pthread.h +@comment POSIX-proposed +@deftypefun int pthread_rwlock_clockrdlock (pthread_rwlock_t *@var{rwlock}, + clockid_t @var{clockid}, + const struct timespec *@var{abstime}) + +@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}} +Behaves like @code{pthread_rwlock_timedrdlock} except the time +@var{abstime} is measured against the clock specified by @var{clockid} +rather than @code{CLOCK_REALTIME}. Currently, @var{clockid} must be either +@code{CLOCK_MONOTONIC} or @code{CLOCK_REALTIME}, otherwise @code{EINVAL} is +returned. +@end deftypefun + +@comment pthread.h +@comment POSIX-proposed +@deftypefun int pthread_rwlock_clockwrlock (pthread_rwlock_t *@var{rwlock}, + clockid_t @var{clockid}, + const struct timespec *@var{abstime}) + +@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}} +Behaves like @code{pthread_rwlock_timedwrlock} except the time +@var{abstime} is measured against the clock specified by @var{clockid} +rather than @code{CLOCK_REALTIME}. Currently, @var{clockid} must be either +@code{CLOCK_MONOTONIC} or @code{CLOCK_REALTIME}, otherwise @code{EINVAL} is +returned. +@end deftypefun + @c FIXME these are undocumented: @c pthread_atfork @c pthread_attr_destroy diff --git a/nptl/Makefile b/nptl/Makefile index 7de4d40..f5e6d90 100644 --- a/nptl/Makefile +++ b/nptl/Makefile @@ -76,7 +76,9 @@ libpthread-routines = nptl-init nptlfreeres vars events version pt-interp \ pthread_mutexattr_gettype pthread_mutexattr_settype \ pthread_rwlock_init pthread_rwlock_destroy \ pthread_rwlock_rdlock pthread_rwlock_timedrdlock \ + pthread_rwlock_clockrdlock \ pthread_rwlock_wrlock pthread_rwlock_timedwrlock \ + pthread_rwlock_clockwrlock \ pthread_rwlock_tryrdlock pthread_rwlock_trywrlock \ pthread_rwlock_unlock \ pthread_rwlockattr_init pthread_rwlockattr_destroy \ diff --git a/nptl/Versions b/nptl/Versions index 8c094d0..ce79959 100644 --- a/nptl/Versions +++ b/nptl/Versions @@ -279,6 +279,7 @@ libpthread { GLIBC_2.30 { sem_clockwait; pthread_cond_clockwait; + pthread_rwlock_clockrdlock; pthread_rwlock_clockwrlock; } GLIBC_PRIVATE { diff --git a/nptl/pthread_rwlock_clockrdlock.c b/nptl/pthread_rwlock_clockrdlock.c new file mode 100644 index 0000000..a6c7456 --- /dev/null +++ b/nptl/pthread_rwlock_clockrdlock.c @@ -0,0 +1,27 @@ +/* Copyright (C) 2019 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <http://www.gnu.org/licenses/>. */ + +#include "pthread_rwlock_common.c" + +/* See pthread_rwlock_common.c. */ +int +pthread_rwlock_clockrdlock (pthread_rwlock_t *rwlock, + clockid_t clockid, + const struct timespec *abstime) +{ + return __pthread_rwlock_rdlock_full (rwlock, clockid, abstime); +} diff --git a/nptl/pthread_rwlock_clockwrlock.c b/nptl/pthread_rwlock_clockwrlock.c new file mode 100644 index 0000000..d248a2a --- /dev/null +++ b/nptl/pthread_rwlock_clockwrlock.c @@ -0,0 +1,27 @@ +/* Copyright (C) 2019 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <http://www.gnu.org/licenses/>. */ + +#include "pthread_rwlock_common.c" + +/* See pthread_rwlock_common.c. */ +int +pthread_rwlock_clockwrlock (pthread_rwlock_t *rwlock, + clockid_t clockid, + const struct timespec *abstime) +{ + return __pthread_rwlock_wrlock_full (rwlock, clockid, abstime); +} diff --git a/nptl/pthread_rwlock_common.c b/nptl/pthread_rwlock_common.c index 120b880..50a366e 100644 --- a/nptl/pthread_rwlock_common.c +++ b/nptl/pthread_rwlock_common.c @@ -278,17 +278,19 @@ __pthread_rwlock_rdunlock (pthread_rwlock_t *rwlock) static __always_inline int __pthread_rwlock_rdlock_full (pthread_rwlock_t *rwlock, + clockid_t clockid, const struct timespec *abstime) { unsigned int r; - /* Make sure any passed in timeout value is valid. Note that the previous - implementation assumed that this check *must* not be performed if there - would in fact be no blocking; however, POSIX only requires that "the - validity of the abstime parameter need not be checked if the lock can be - immediately acquired" (i.e., we need not but may check it). */ - if (abstime - && __glibc_unlikely (abstime->tv_nsec >= 1000000000 + /* Make sure any passed in clockid and timeout value are valid. Note + that the previous implementation assumed that this check *must* + not be performed if there would in fact be no blocking; however, + POSIX only requires that "the validity of the abstime parameter + need not be checked if the lock can be immediately acquired" + (i.e., we need not but may check it). */ + if (abstime && __glibc_unlikely (!futex_abstimed_supported_clockid (clockid) + || abstime->tv_nsec >= 1000000000 || abstime->tv_nsec < 0)) return EINVAL; @@ -329,7 +331,7 @@ __pthread_rwlock_rdlock_full (pthread_rwlock_t *rwlock, { int private = __pthread_rwlock_get_private (rwlock); int err = futex_abstimed_wait (&rwlock->__data.__readers, - r, CLOCK_REALTIME, abstime, private); + r, clockid, abstime, private); /* We ignore EAGAIN and EINTR. On time-outs, we can just return because we don't need to clean up anything. */ if (err == ETIMEDOUT) @@ -457,7 +459,7 @@ __pthread_rwlock_rdlock_full (pthread_rwlock_t *rwlock, continue; int err = futex_abstimed_wait (&rwlock->__data.__wrphase_futex, 1 | PTHREAD_RWLOCK_FUTEX_USED, - CLOCK_REALTIME, abstime, private); + clockid, abstime, private); if (err == ETIMEDOUT) { /* If we timed out, we need to unregister. If no read phase @@ -584,15 +586,17 @@ __pthread_rwlock_wrunlock (pthread_rwlock_t *rwlock) static __always_inline int __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock, + clockid_t clockid, const struct timespec *abstime) { - /* Make sure any passed in timeout value is valid. Note that the previous - implementation assumed that this check *must* not be performed if there - would in fact be no blocking; however, POSIX only requires that "the - validity of the abstime parameter need not be checked if the lock can be - immediately acquired" (i.e., we need not but may check it). */ - if (abstime - && __glibc_unlikely (abstime->tv_nsec >= 1000000000 + /* Make sure any passed in clockid and timeout value are valid. Note + that the previous implementation assumed that this check *must* + not be performed if there would in fact be no blocking; however, + POSIX only requires that "the validity of the abstime parameter + need not be checked if the lock can be immediately acquired" + (i.e., we need not but may check it). */ + if (abstime && __glibc_unlikely (!futex_abstimed_supported_clockid (clockid) + || abstime->tv_nsec >= 1000000000 || abstime->tv_nsec < 0)) return EINVAL; @@ -727,7 +731,7 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock, may_share_futex_used_flag = true; int err = futex_abstimed_wait (&rwlock->__data.__writers_futex, 1 | PTHREAD_RWLOCK_FUTEX_USED, - CLOCK_REALTIME, abstime, private); + clockid, abstime, private); if (err == ETIMEDOUT) { if (prefer_writer) @@ -826,7 +830,7 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock, continue; int err = futex_abstimed_wait (&rwlock->__data.__wrphase_futex, PTHREAD_RWLOCK_FUTEX_USED, - CLOCK_REALTIME, abstime, private); + clockid, abstime, private); if (err == ETIMEDOUT) { if (rwlock->__data.__flags != PTHREAD_RWLOCK_PREFER_READER_NP) diff --git a/nptl/pthread_rwlock_rdlock.c b/nptl/pthread_rwlock_rdlock.c index 5fdc89e..387c824 100644 --- a/nptl/pthread_rwlock_rdlock.c +++ b/nptl/pthread_rwlock_rdlock.c @@ -24,7 +24,7 @@ __pthread_rwlock_rdlock (pthread_rwlock_t *rwlock) { LIBC_PROBE (rdlock_entry, 1, rwlock); - int result = __pthread_rwlock_rdlock_full (rwlock, NULL); + int result = __pthread_rwlock_rdlock_full (rwlock, CLOCK_REALTIME, NULL); LIBC_PROBE (rdlock_acquire_read, 1, rwlock); return result; } diff --git a/nptl/pthread_rwlock_timedrdlock.c b/nptl/pthread_rwlock_timedrdlock.c index 84c1983..8f8e680 100644 --- a/nptl/pthread_rwlock_timedrdlock.c +++ b/nptl/pthread_rwlock_timedrdlock.c @@ -23,5 +23,5 @@ int pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock, const struct timespec *abstime) { - return __pthread_rwlock_rdlock_full (rwlock, abstime); + return __pthread_rwlock_rdlock_full (rwlock, CLOCK_REALTIME, abstime); } diff --git a/nptl/pthread_rwlock_timedwrlock.c b/nptl/pthread_rwlock_timedwrlock.c index f0b745d..a5616de 100644 --- a/nptl/pthread_rwlock_timedwrlock.c +++ b/nptl/pthread_rwlock_timedwrlock.c @@ -23,5 +23,5 @@ int pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock, const struct timespec *abstime) { - return __pthread_rwlock_wrlock_full (rwlock, abstime); + return __pthread_rwlock_wrlock_full (rwlock, CLOCK_REALTIME, abstime); } diff --git a/nptl/pthread_rwlock_wrlock.c b/nptl/pthread_rwlock_wrlock.c index 194a14c..da246d8 100644 --- a/nptl/pthread_rwlock_wrlock.c +++ b/nptl/pthread_rwlock_wrlock.c @@ -24,7 +24,7 @@ __pthread_rwlock_wrlock (pthread_rwlock_t *rwlock) { LIBC_PROBE (wrlock_entry, 1, rwlock); - int result = __pthread_rwlock_wrlock_full (rwlock, NULL); + int result = __pthread_rwlock_wrlock_full (rwlock, CLOCK_REALTIME, NULL); LIBC_PROBE (wrlock_acquire_write, 1, rwlock); return result; } diff --git a/nptl/tst-abstime.c b/nptl/tst-abstime.c index 71610f8..f4c9f2d 100644 --- a/nptl/tst-abstime.c +++ b/nptl/tst-abstime.c @@ -47,12 +47,40 @@ th (void *arg) puts ("pthread_rwlock_timedrdlock did not return ETIMEDOUT"); res = 1; } + r = pthread_rwlock_clockrdlock (&rw1, CLOCK_REALTIME, &t); + if (r != ETIMEDOUT) + { + puts ("pthread_rwlock_clockrdlock (CLOCK_REALTIME) " + "did not return ETIMEDOUT"); + res = 1; + } + r = pthread_rwlock_clockrdlock (&rw1, CLOCK_MONOTONIC, &t); + if (r != ETIMEDOUT) + { + puts ("pthread_rwlock_clockrdlock (CLOCK_MONOTONIC) " + "did not return ETIMEDOUT"); + res = 1; + } r = pthread_rwlock_timedwrlock (&rw2, &t); if (r != ETIMEDOUT) { puts ("pthread_rwlock_timedwrlock did not return ETIMEDOUT"); res = 1; } + r = pthread_rwlock_clockwrlock (&rw2, CLOCK_REALTIME, &t); + if (r != ETIMEDOUT) + { + puts ("pthread_rwlock_clockwrlock(CLOCK_REALTIME) " + "did not return ETIMEDOUT"); + res = 1; + } + r = pthread_rwlock_clockwrlock (&rw2, CLOCK_MONOTONIC, &t); + if (r != ETIMEDOUT) + { + puts ("pthread_rwlock_clockwrlock(CLOCK_MONOTONIC) " + "did not return ETIMEDOUT"); + res = 1; + } return (void *) res; } diff --git a/nptl/tst-rwlock14.c b/nptl/tst-rwlock14.c index 6f57169..525ceac 100644 --- a/nptl/tst-rwlock14.c +++ b/nptl/tst-rwlock14.c @@ -91,6 +91,30 @@ do_test (void) result = 1; } + e = pthread_rwlock_clockrdlock (&r, CLOCK_REALTIME, &ts); + if (e == 0) + { + puts ("first rwlock_clockrdlock (CLOCK_REALTIME) did not fail"); + result = 1; + } + else if (e != EINVAL) + { + puts ("first rwlock_clockrdlock (CLOCK_REALTIME) did not return EINVAL"); + result = 1; + } + + e = pthread_rwlock_clockrdlock (&r, CLOCK_MONOTONIC, &ts); + if (e == 0) + { + puts ("first rwlock_clockrdlock (CLOCK_MONOTONIC) did not fail"); + result = 1; + } + else if (e != EINVAL) + { + puts ("first rwlock_clockrdlock (CLOCK_MONOTONIC) did not return EINVAL"); + result = 1; + } + e = pthread_rwlock_timedwrlock (&r, &ts); if (e == 0) { @@ -103,6 +127,30 @@ do_test (void) result = 1; } + e = pthread_rwlock_clockwrlock (&r, CLOCK_REALTIME, &ts); + if (e == 0) + { + puts ("first rwlock_clockwrlock (CLOCK_REALTIME) did not fail"); + result = 1; + } + else if (e != EINVAL) + { + puts ("first rwlock_clockwrlock (CLOCK_REALTIME) did not return EINVAL"); + result = 1; + } + + e = pthread_rwlock_clockwrlock (&r, CLOCK_MONOTONIC, &ts); + if (e == 0) + { + puts ("first rwlock_clockwrlock (CLOCK_MONOTONIC) did not fail"); + result = 1; + } + else if (e != EINVAL) + { + puts ("first rwlock_clockwrlock (CLOCK_MONOTONIC) did not return EINVAL"); + result = 1; + } + ts.tv_nsec = 1000000000; e = pthread_rwlock_timedrdlock (&r, &ts); @@ -117,6 +165,31 @@ do_test (void) result = 1; } + e = pthread_rwlock_clockrdlock (&r, CLOCK_REALTIME, &ts); + if (e == 0) + { + puts ("second rwlock_clockrdlock (CLOCK_REALTIME) did not fail"); + result = 1; + } + else if (e != EINVAL) + { + puts ("second rwlock_clockrdlock (CLOCK_REALTIME) did not return EINVAL"); + result = 1; + } + + e = pthread_rwlock_clockrdlock (&r, CLOCK_MONOTONIC, &ts); + if (e == 0) + { + puts ("second rwlock_clockrdlock (CLOCK_MONOTONIC) did not fail"); + result = 1; + } + else if (e != EINVAL) + { + puts ("second rwlock_clockrdlock (CLOCK_MONOTONIC) " + "did not return EINVAL"); + result = 1; + } + e = pthread_rwlock_timedwrlock (&r, &ts); if (e == 0) { @@ -129,6 +202,32 @@ do_test (void) result = 1; } + e = pthread_rwlock_clockwrlock (&r, CLOCK_MONOTONIC, &ts); + if (e == 0) + { + puts ("second rwlock_clockwrlock (CLOCK_MONOTONIC) did not fail"); + result = 1; + } + else if (e != EINVAL) + { + puts ("second rwlock_clockwrlock (CLOCK_MONOTONIC) " + "did not return EINVAL"); + result = 1; + } + + e = pthread_rwlock_clockwrlock (&r, CLOCK_REALTIME, &ts); + if (e == 0) + { + puts ("second rwlock_clockwrlock (CLOCK_REALTIME) did not fail"); + result = 1; + } + else if (e != EINVAL) + { + puts ("second rwlock_clockwrlock (CLOCK_REALTIME) " + "did not return EINVAL"); + result = 1; + } + ts.tv_nsec = (__typeof (ts.tv_nsec)) 0x100001000LL; if ((__typeof (ts.tv_nsec)) 0x100001000LL != 0x100001000LL) ts.tv_nsec = 2000000000; @@ -145,6 +244,30 @@ do_test (void) result = 1; } + e = pthread_rwlock_clockrdlock (&r, CLOCK_REALTIME, &ts); + if (e == 0) + { + puts ("third rwlock_clockrdlock (CLOCK_REALTIME) did not fail"); + result = 1; + } + else if (e != EINVAL) + { + puts ("third rwlock_clockrdlock (CLOCK_REALTIME) did not return EINVAL"); + result = 1; + } + + e = pthread_rwlock_clockrdlock (&r, CLOCK_MONOTONIC, &ts); + if (e == 0) + { + puts ("third rwlock_clockrdlock(CLOCK_MONOTONIC) did not fail"); + result = 1; + } + else if (e != EINVAL) + { + puts ("third rwlock_clockrdlock (CLOCK_MONOTONIC) did not return EINVAL"); + result = 1; + } + e = pthread_rwlock_timedwrlock (&r, &ts); if (e == 0) { @@ -157,6 +280,30 @@ do_test (void) result = 1; } + e = pthread_rwlock_clockwrlock (&r, CLOCK_REALTIME, &ts); + if (e == 0) + { + puts ("third rwlock_clockwrlock (CLOCK_REALTIME) did not fail"); + result = 1; + } + else if (e != EINVAL) + { + puts ("third rwlock_clockwrlock (CLOCK_REALTIME) did not return EINVAL"); + result = 1; + } + + e = pthread_rwlock_clockwrlock (&r, CLOCK_MONOTONIC, &ts); + if (e == 0) + { + puts ("third rwlock_clockwrlock (CLOCK_MONOTONIC) did not fail"); + result = 1; + } + else if (e != EINVAL) + { + puts ("third rwlock_clockwrlock (CLOCK_MONOTONIC) did not return EINVAL"); + result = 1; + } + if (result == 0) puts ("no bugs"); diff --git a/nptl/tst-rwlock6.c b/nptl/tst-rwlock6.c index 67119fa..708915e 100644 --- a/nptl/tst-rwlock6.c +++ b/nptl/tst-rwlock6.c @@ -25,6 +25,11 @@ #include <support/timespec.h> +/* A bogus clock value that tells run_test to use + pthread_rwlock_timedrdlock and pthread_rwlock_timedwrlock rather + than pthread_rwlock_clockrdlock and pthread_rwlock_clockwrlock. */ +#define CLOCK_USE_TIMEDLOCK (-1) + static int kind[] = { PTHREAD_RWLOCK_PREFER_READER_NP, @@ -32,75 +37,91 @@ static int kind[] = PTHREAD_RWLOCK_PREFER_WRITER_NP, }; +struct thread_args { + pthread_rwlock_t *rwlock; + clockid_t clockid; + const char *fnname; +}; static void * tf (void *arg) { - pthread_rwlock_t *r = arg; + struct thread_args *args = arg; + pthread_rwlock_t *r = args->rwlock; + const clockid_t clockid = args->clockid; + const clockid_t clockid_for_get = + (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid; + const char *fnname = args->fnname; /* Timeout: 0.3 secs. */ struct timespec ts_start; - (void) clock_gettime(CLOCK_REALTIME, &ts_start); + (void) clock_gettime (clockid_for_get, &ts_start); struct timespec ts_timeout = {0, 300000000}; - timespec_add(&ts_timeout, &ts_start, &ts_timeout); + timespec_add (&ts_timeout, &ts_start, &ts_timeout); - puts ("child calling timedrdlock"); + printf ("child calling %srdlock\n", fnname); - int err = pthread_rwlock_timedrdlock (r, &ts_timeout); + int err = (clockid == CLOCK_USE_TIMEDLOCK) + ? pthread_rwlock_timedrdlock (r, &ts_timeout) + : pthread_rwlock_clockrdlock (r, clockid, &ts_timeout); if (err == 0) { - puts ("rwlock_timedrdlock returned"); + printf ("%srdlock returned\n", fnname); pthread_exit ((void *) 1l); } if (err != ETIMEDOUT) { - printf ("err = %s (%d), expected %s (%d)\n", - strerror (err), err, strerror (ETIMEDOUT), ETIMEDOUT); + printf ("%srdlock: err = %s (%d), expected %s (%d)\n", + fnname, strerror (err), err, strerror (ETIMEDOUT), ETIMEDOUT); pthread_exit ((void *) 1l); } - puts ("1st child timedrdlock done"); + printf ("1st child %srdlock done\n", fnname); struct timespec ts_end; - (void) clock_gettime (CLOCK_REALTIME, &ts_end); + (void) clock_gettime (clockid_for_get, &ts_end); struct timespec ts_duration; - timespec_sub(&ts_duration, &ts_end, &ts_start); + timespec_sub (&ts_duration, &ts_end, &ts_start); if (ts_duration.tv_sec !=0 || ts_duration.tv_nsec < 200000000) { - puts ("timeout too short"); + printf ("%srdlock: timeout too short\n", fnname); pthread_exit ((void *) 1l); } - (void) clock_gettime (CLOCK_REALTIME, &ts_timeout); + (void) clock_gettime (clockid_for_get, &ts_timeout); ts_timeout.tv_sec += 10; /* Note that the following operation makes ts invalid. */ ts_timeout.tv_nsec += 1000000000; - err = pthread_rwlock_timedrdlock (r, &ts_timeout); + err = (clockid == CLOCK_USE_TIMEDLOCK) + ? pthread_rwlock_timedrdlock (r, &ts_timeout) + : pthread_rwlock_clockrdlock (r, clockid, &ts_timeout); if (err == 0) { - puts ("2nd timedrdlock succeeded"); + printf ("2nd %srdlock succeeded\n", fnname); pthread_exit ((void *) 1l); } if (err != EINVAL) { - puts ("2nd timedrdlock did not return EINVAL"); + printf ("2nd %srdlock did not return EINVAL\n", fnname); pthread_exit ((void *) 1l); } - puts ("2nd child timedrdlock done"); + printf ("2nd child %srdlock done\n", fnname); return NULL; } static int -do_test (void) +do_test_clock (clockid_t clockid, const char *fnname) { + const clockid_t clockid_for_get = + (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid; size_t cnt; for (cnt = 0; cnt < sizeof (kind) / sizeof (kind[0]); ++cnt) { @@ -132,53 +153,63 @@ do_test (void) } struct timespec ts; - (void) clock_gettime (CLOCK_REALTIME, &ts); + (void) clock_gettime (clockid_for_get, &ts); ++ts.tv_sec; /* Get a write lock. */ - int e = pthread_rwlock_timedwrlock (&r, &ts); + int e = (clockid == CLOCK_USE_TIMEDLOCK) + ? pthread_rwlock_timedwrlock (&r, &ts) + : pthread_rwlock_clockwrlock (&r, clockid, &ts); if (e != 0) { - printf ("round %Zu: rwlock_timedwrlock failed (%d)\n", cnt, e); + printf ("round %Zu: %swrlock failed (%d)\n", cnt, fnname, e); exit (1); } - puts ("1st timedwrlock done"); + printf ("1st %swrlock done\n", fnname); - (void) clock_gettime (CLOCK_REALTIME, &ts); + (void) clock_gettime (clockid, &ts); ++ts.tv_sec; - e = pthread_rwlock_timedrdlock (&r, &ts); + e = (clockid == CLOCK_USE_TIMEDLOCK) + ? pthread_rwlock_timedrdlock (&r, &ts) + : pthread_rwlock_clockrdlock (&r, clockid, &ts); if (e == 0) { - puts ("timedrdlock succeeded"); + printf ("%srdlock succeeded\n", fnname); exit (1); } if (e != EDEADLK) { - puts ("timedrdlock did not return EDEADLK"); + printf ("%srdlock did not return EDEADLK\n", fnname); exit (1); } - puts ("1st timedrdlock done"); + printf ("1st %srdlock done\n", fnname); - (void) clock_gettime (CLOCK_REALTIME, &ts); + (void) clock_gettime (clockid_for_get, &ts); ++ts.tv_sec; - e = pthread_rwlock_timedwrlock (&r, &ts); + e = (clockid == CLOCK_USE_TIMEDLOCK) + ? pthread_rwlock_timedwrlock (&r, &ts) + : pthread_rwlock_clockwrlock (&r, clockid, &ts); if (e == 0) { - puts ("2nd timedwrlock succeeded"); + printf ("2nd %swrlock succeeded\n", fnname); exit (1); } if (e != EDEADLK) { - puts ("2nd timedwrlock did not return EDEADLK"); + printf ("2nd %swrlock did not return EDEADLK\n", fnname); exit (1); } - puts ("2nd timedwrlock done"); + printf ("2nd %swrlock done\n", fnname); pthread_t th; - if (pthread_create (&th, NULL, tf, &r) != 0) + struct thread_args args; + args.rwlock = &r; + args.clockid = clockid; + args.fnname = fnname; + if (pthread_create (&th, NULL, tf, &args) != 0) { printf ("round %Zu: create failed\n", cnt); exit (1); @@ -210,5 +241,13 @@ do_test (void) return 0; } +static int do_test (void) +{ + do_test_clock (CLOCK_USE_TIMEDLOCK, "timed"); + do_test_clock (CLOCK_REALTIME, "clock(realtime)"); + do_test_clock (CLOCK_MONOTONIC, "clock(monotonic)"); + return 0; +} + #define TEST_FUNCTION do_test () #include "../test-skeleton.c" diff --git a/nptl/tst-rwlock7.c b/nptl/tst-rwlock7.c index 812506c..0bccd18 100644 --- a/nptl/tst-rwlock7.c +++ b/nptl/tst-rwlock7.c @@ -25,6 +25,11 @@ #include <support/timespec.h> +/* A bogus clock value that tells run_test to use + pthread_rwlock_timedrdlock and pthread_rwlock_timedwrlock rather + than pthread_rwlock_clockrdlock and pthread_rwlock_clockwrlock. */ +#define CLOCK_USE_TIMEDLOCK (-1) + static int kind[] = { PTHREAD_RWLOCK_PREFER_READER_NP, @@ -32,22 +37,34 @@ static int kind[] = PTHREAD_RWLOCK_PREFER_WRITER_NP, }; +struct thread_args +{ + pthread_rwlock_t *rwlock; + clockid_t clockid; + const char *fnname; +}; static void * tf (void *arg) { - pthread_rwlock_t *r = arg; + struct thread_args *args = arg; + pthread_rwlock_t *r = args->rwlock; + clockid_t clockid = args->clockid; + const char *fnname = args->fnname; /* Timeout: 0.3 secs. */ struct timespec ts_start; - (void) clock_gettime (CLOCK_REALTIME, &ts_start); + (void) clock_gettime ((clockid == CLOCK_USE_TIMEDLOCK) + ? CLOCK_REALTIME : clockid, &ts_start); struct timespec ts_timeout = {0, 300000000}; timespec_add(&ts_timeout, &ts_start, &ts_timeout); - int err = pthread_rwlock_timedwrlock (r, &ts_timeout); + int err = (clockid == CLOCK_USE_TIMEDLOCK) + ? pthread_rwlock_timedwrlock (r, &ts_timeout) + : pthread_rwlock_clockwrlock (r, clockid, &ts_timeout); if (err == 0) { - puts ("rwlock_timedwrlock returned"); + printf ("pthread_rwlock_%swrlock returned\n", fnname); pthread_exit ((void *) 1l); } @@ -57,44 +74,49 @@ tf (void *arg) strerror (err), err, strerror (ETIMEDOUT), ETIMEDOUT); pthread_exit ((void *) 1l); } - puts ("child: timedwrlock failed with ETIMEDOUT"); + printf ("child: pthread_rwlock_%swrlock failed with ETIMEDOUT\n", fnname); struct timespec ts_end; - (void) clock_gettime (CLOCK_REALTIME, &ts_end); + (void) clock_gettime ((clockid == CLOCK_USE_TIMEDLOCK) + ? CLOCK_REALTIME : clockid, &ts_end); struct timespec ts_diff; timespec_sub (&ts_diff, &ts_end, &ts_start); if (ts_diff.tv_sec != 0 || ts_diff.tv_nsec < 200000000) { - puts ("timeout too short"); + printf ("pthread_rwlock_%srwlock timeout too short: %ld.%09ld\n", + fnname, (long)ts_diff.tv_sec, (long)ts_diff.tv_nsec); pthread_exit ((void *) 1l); } struct timespec ts_invalid; - (void) clock_gettime (CLOCK_REALTIME, &ts_invalid); + (void) clock_gettime ((clockid == CLOCK_USE_TIMEDLOCK) + ? CLOCK_REALTIME : clockid, &ts_invalid); ts_invalid.tv_sec += 10; /* Note that the following operation makes ts invalid. */ ts_invalid.tv_nsec += 1000000000000; - err = pthread_rwlock_timedwrlock (r, &ts_invalid); + err = (clockid == CLOCK_USE_TIMEDLOCK) + ? pthread_rwlock_timedwrlock (r, &ts_invalid) + : pthread_rwlock_clockwrlock (r, clockid, &ts_invalid); if (err == 0) { - puts ("2nd timedwrlock succeeded"); + printf ("2nd pthread_%srwlock succeeded\n", fnname); pthread_exit ((void *) 1l); } if (err != EINVAL) { - puts ("2nd timedwrlock did not return EINVAL"); + printf ("2nd pthread_%srwlock did not return EINVAL\n", fnname); pthread_exit ((void *) 1l); } - puts ("child: timedwrlock failed with EINVAL"); + printf ("child: %s failed with EINVAL\n", fnname); return NULL; } static int -do_test (void) +do_test_clock (clockid_t clockid, const char *fnname) { size_t cnt; for (cnt = 0; cnt < sizeof (kind) / sizeof (kind[0]); ++cnt) @@ -127,20 +149,27 @@ do_test (void) } struct timespec ts; - (void) clock_gettime (CLOCK_REALTIME, &ts); + (void) clock_gettime ((clockid == CLOCK_USE_TIMEDLOCK) ? + CLOCK_REALTIME: clockid, &ts); ++ts.tv_sec; /* Get a read lock. */ - if (pthread_rwlock_timedrdlock (&r, &ts) != 0) + if ((clockid == CLOCK_USE_TIMEDLOCK) + ? pthread_rwlock_timedrdlock (&r, &ts) + : pthread_rwlock_clockrdlock (&r, clockid, &ts) != 0) { - printf ("round %Zu: rwlock_timedrdlock failed\n", cnt); + printf ("round %Zu: pthread_rwlock%srdlock failed\n", cnt, fnname); exit (1); } - printf ("%zu: got timedrdlock\n", cnt); + printf ("%zu: got pthread_rwlock_%srdlock\n", cnt, fnname); + struct thread_args args; + args.rwlock = &r; + args.clockid = clockid; + args.fnname = fnname; pthread_t th; - if (pthread_create (&th, NULL, tf, &r) != 0) + if (pthread_create (&th, NULL, tf, &args) != 0) { printf ("round %Zu: create failed\n", cnt); exit (1); @@ -168,5 +197,15 @@ do_test (void) return 0; } +static int +do_test (void) +{ + do_test_clock (CLOCK_USE_TIMEDLOCK, "timed"); + do_test_clock (CLOCK_MONOTONIC, "clock(monotonic)"); + do_test_clock (CLOCK_REALTIME, "clock(realtime)"); + + return 0; +} + #define TEST_FUNCTION do_test () #include "../test-skeleton.c" diff --git a/nptl/tst-rwlock9.c b/nptl/tst-rwlock9.c index ff15f90..2691f6a 100644 --- a/nptl/tst-rwlock9.c +++ b/nptl/tst-rwlock9.c @@ -38,12 +38,28 @@ # define KIND PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP #endif +/* A bogus clock value that tells the tests to use + pthread_rwlock_timedrdlock and pthread_rwlock_timedwrlock rather + than pthread_rwlock_clockrdlock and pthread_rwlock_clockwrlock. */ +#define CLOCK_USE_TIMEDLOCK (-1) + static pthread_rwlock_t lock; +struct thread_args +{ + int nr; + clockid_t clockid; + const char *fnname; +}; static void * -writer_thread (void *nr) +writer_thread (void *arg) { + struct thread_args *args = arg; + const int nr = args->nr; + const clockid_t clockid = args->clockid; + const char *fnname = args->fnname; + struct timespec ts; struct timespec delay; int n; @@ -56,7 +72,8 @@ writer_thread (void *nr) int e; do { - (void) clock_gettime (CLOCK_REALTIME, &ts); + (void) clock_gettime ((clockid == CLOCK_USE_TIMEDLOCK) + ? CLOCK_REALTIME : clockid, &ts); ts.tv_nsec += 2 * TIMEOUT; if (ts.tv_nsec >= 1000000000) @@ -65,18 +82,20 @@ writer_thread (void *nr) ++ts.tv_sec; } - printf ("writer thread %ld tries again\n", (long int) nr); + printf ("writer thread %d tries again\n", nr); - e = pthread_rwlock_timedwrlock (&lock, &ts); + e = (clockid == CLOCK_USE_TIMEDLOCK) + ? pthread_rwlock_timedwrlock (&lock, &ts) + : pthread_rwlock_clockwrlock (&lock, clockid, &ts); if (e != 0 && e != ETIMEDOUT) { - puts ("timedwrlock failed"); + printf ("%swrlock failed", fnname); exit (1); } } while (e == ETIMEDOUT); - printf ("writer thread %ld succeeded\n", (long int) nr); + printf ("writer thread %d succeeded\n", nr); nanosleep (&delay, NULL); @@ -86,7 +105,7 @@ writer_thread (void *nr) exit (1); } - printf ("writer thread %ld released\n", (long int) nr); + printf ("writer thread %d released\n", nr); } return NULL; @@ -94,8 +113,13 @@ writer_thread (void *nr) static void * -reader_thread (void *nr) +reader_thread (void *arg) { + struct thread_args *args = arg; + const int nr = args->nr; + const clockid_t clockid = args->clockid; + const char *fnname = args->fnname; + struct timespec ts; struct timespec delay; int n; @@ -108,8 +132,8 @@ reader_thread (void *nr) int e; do { - (void) clock_gettime (CLOCK_REALTIME, &ts); - + (void) clock_gettime ((clockid == CLOCK_USE_TIMEDLOCK) + ? CLOCK_REALTIME : clockid, &ts); ts.tv_nsec += TIMEOUT; if (ts.tv_nsec >= 1000000000) { @@ -117,18 +141,20 @@ reader_thread (void *nr) ++ts.tv_sec; } - printf ("reader thread %ld tries again\n", (long int) nr); + printf ("reader thread %d tries again\n", nr); - e = pthread_rwlock_timedrdlock (&lock, &ts); + e = (clockid == CLOCK_USE_TIMEDLOCK) + ? pthread_rwlock_timedrdlock (&lock, &ts) + : pthread_rwlock_clockrdlock (&lock, clockid, &ts); if (e != 0 && e != ETIMEDOUT) { - puts ("timedrdlock failed"); + printf ("%srdlock failed\n", fnname); exit (1); } } while (e == ETIMEDOUT); - printf ("reader thread %ld succeeded\n", (long int) nr); + printf ("reader thread %d succeeded\n", nr); nanosleep (&delay, NULL); @@ -138,7 +164,7 @@ reader_thread (void *nr) exit (1); } - printf ("reader thread %ld released\n", (long int) nr); + printf ("reader thread %d released\n", nr); } return NULL; @@ -146,7 +172,7 @@ reader_thread (void *nr) static int -do_test (void) +do_test_clock (clockid_t clockid, const char *fnname) { pthread_t thwr[NWRITERS]; pthread_t thrd[NREADERS]; @@ -178,21 +204,31 @@ do_test (void) /* Make sure we see all message, even those on stdout. */ setvbuf (stdout, NULL, _IONBF, 0); - for (n = 0; n < NWRITERS; ++n) + struct thread_args wargs[NWRITERS]; + for (n = 0; n < NWRITERS; ++n) { + wargs[n].nr = n; + wargs[n].clockid = clockid; + wargs[n].fnname = fnname; if (pthread_create (&thwr[n], NULL, writer_thread, - (void *) (long int) n) != 0) + &wargs[n]) != 0) { puts ("writer create failed"); exit (1); } + } - for (n = 0; n < NREADERS; ++n) + struct thread_args rargs[NREADERS]; + for (n = 0; n < NREADERS; ++n) { + rargs[n].nr = n; + rargs[n].clockid = clockid; + rargs[n].fnname = fnname; if (pthread_create (&thrd[n], NULL, reader_thread, - (void *) (long int) n) != 0) + &rargs[n]) != 0) { puts ("reader create failed"); exit (1); } + } /* Wait for all the threads. */ for (n = 0; n < NWRITERS; ++n) @@ -211,6 +247,16 @@ do_test (void) return 0; } +static int +do_test (void) +{ + do_test_clock (CLOCK_USE_TIMEDLOCK, "timed"); + do_test_clock (CLOCK_REALTIME, "clock(realtime)"); + do_test_clock (CLOCK_MONOTONIC, "clock(monotonic)"); + + return 0; +} + #undef TIMEOUT #define TIMEOUT 30 #define TEST_FUNCTION do_test () diff --git a/sysdeps/nptl/pthread.h b/sysdeps/nptl/pthread.h index d4fe9d9..3c06d77 100644 --- a/sysdeps/nptl/pthread.h +++ b/sysdeps/nptl/pthread.h @@ -907,6 +907,10 @@ extern int pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock) extern int pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock, const struct timespec *__restrict __abstime) __THROWNL __nonnull ((1, 2)); +extern int pthread_rwlock_clockrdlock (pthread_rwlock_t *__restrict __rwlock, + clockid_t __clockid, + const struct timespec *__restrict + __abstime) __THROWNL __nonnull ((1, 3)); # endif /* Acquire write lock for RWLOCK. */ @@ -922,6 +926,10 @@ extern int pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock) extern int pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock, const struct timespec *__restrict __abstime) __THROWNL __nonnull ((1, 2)); +extern int pthread_rwlock_clockwrlock (pthread_rwlock_t *__restrict __rwlock, + clockid_t __clockid, + const struct timespec *__restrict + __abstime) __THROWNL __nonnull ((1, 3)); # endif /* Unlock RWLOCK. */ diff --git a/sysdeps/unix/sysv/linux/x86_64/64/libpthread.abilist b/sysdeps/unix/sysv/linux/x86_64/64/libpthread.abilist index aaa1c3b..70f04cc 100644 --- a/sysdeps/unix/sysv/linux/x86_64/64/libpthread.abilist +++ b/sysdeps/unix/sysv/linux/x86_64/64/libpthread.abilist @@ -246,6 +246,8 @@ GLIBC_2.3.4 pthread_getaffinity_np F GLIBC_2.3.4 pthread_setaffinity_np F GLIBC_2.3.4 pthread_setschedprio F GLIBC_2.30 pthread_cond_clockwait F +GLIBC_2.30 pthread_rwlock_clockrdlock F +GLIBC_2.30 pthread_rwlock_clockwrlock F GLIBC_2.30 sem_clockwait F GLIBC_2.4 pthread_mutex_consistent_np F GLIBC_2.4 pthread_mutex_getprioceiling F