From 28cada041811fe49c8da1eb7ffdbc949719f9009 Mon Sep 17 00:00:00 2001 From: Samuel Thibault Date: Tue, 26 May 2020 00:09:11 +0000 Subject: htl: Add clock variants * htl/pt-join.c (__pthread_join): Move implementation to... (__pthread_join_common): ... new function. Add try, timed and clock support. (__pthread_join): Reimplement on top of __pthread_join_common. (__pthread_tryjoin_np, __pthread_timedjoin_np, __pthread_clockjoin_np): Implement on top of __pthread_join_common. (pthread_tryjoin_np, pthread_timedjoin_np, pthread_clockjoin_np): New aliases. * hurd/hurdlock.c (__lll_abstimed_wait, __lll_abstimed_xwait, __lll_abstimed_lock): Check for supported clock. * sysdeps/htl/pt-cond-timedwait.c (__pthread_cond_timedwait_internal): Add clockid parameter and support it. (__pthread_cond_timedwait): Pass -1 as clockid. (__pthread_cond_clockwait): New function. (pthread_cond_clockwait): New alias. * sysdeps/htl/pt-cond-wait.c (__pthread_cond_timedwait_internal): Update prototype. (__pthread_cond_wait): Pass -1 as clockid. * sysdeps/htl/pt-rwlock-timedrdlock.c (__pthread_rwlock_timedrdlock_internal): Add clockid parameter, and support id. (__pthread_rwlock_clockrdlock): New function. (pthread_rwlock_clockrdlock): New alias. * sysdeps/htl/pt-rwlock-rdlock.c (__pthread_rwlock_timedrdlock_internal): Update prototype. (__pthread_rwlock_rdlock): Pass -1 as clockid. * sysdeps/htl/pt-rwlock-timedwrlock.c (__pthread_rwlock_timedwrlock_internal): Add clockid parameter, and support id. (__pthread_rwlock_clockwrlock): New function. (pthread_rwlock_clockwrlock): New alias. * sysdeps/htl/pt-rwlock-wrlock.c (__pthread_rwlock_timedwrlock_internal): Update prototype. (__pthread_rwlock_wrlock): Pass -1 as clockid. * sysdeps/mach/hurd/htl/pt-mutex-timedlock.c (__pthread_mutex_timedlock): Move implementation to (__pthread_mutex_clocklock): New function with additional clockid parameter and support it. (pthread_mutex_clocklock): New alias. (__pthread_mutex_timedlock): Reimplement on top of __pthread_mutex_clocklock. * sysdeps/htl/pthread.h (pthread_tryjoin_np, pthread_timedjoin_np, pthread_clockjoin_np, pthread_mutex_clocklock, pthread_cond_clockwait, pthread_rwlock_clockrdlock, pthread_rwlock_clockwrlock): New prototypes. * sysdeps/htl/pthreadP.h (__pthread_cond_clockwait): New prototype. * htl/Versions (GLIBC_2.32): Add pthread_cond_clockwait, pthread_mutex_clocklock, pthread_rwlock_clockrdlock, pthread_rwlock_clockwrlock, pthread_tryjoin_np, pthread_timedjoin_np, pthread_clockjoin_np. * sysdeps/mach/hurd/i386/libpthread.abilist (pthread_clockjoin_np, pthread_cond_clockwait, pthread_mutex_clocklock, pthread_rwlock_clockrdlock, pthread_rwlock_clockwrlock, pthread_timedjoin_np, pthread_tryjoin_np): New functions. * nptl/tst-abstime.c, nptl/tst-join10.c, nptl/tst-join11.c, nptl/tst-join12.c, nptl/tst-join13.c, nptl/tst-join14.c, nptl/tst-join2.c, nptl/tst-join3.c, nptl/tst-join8.c, nptl/tst-join9.c, nptl/tst-mutex-errorcheck.c, nptl/tst-pthread-mutexattr.c, nptl/tst-mutex11.c, nptl/tst-mutex5.c, nptl/tst-mutex7.c, nptl/tst-mutex7robus.c, nptl/tst-mutex9.c, nptl/tst-rwlock12.c, nptl/tst-rwlock14.c: Move to sysdeps/pthread. * sysdeps/pthread/tst-mutex8.c: Move back to nptl. * nptl/Makefile (tests): Move tst-mutex5, tst-mutex7, tst-mutex7robust, tst-mutex9, tst-mutex11, tst-rwlock12, tst-rwlock14, tst-join2, tst-join3, tst-join8, tst-join9 tst-join10, tst-join11, tst-join12, tst-join13, tst-join14, tst-abstime, tst-mutex-errorcheck, tst-pthread-mutexattr to ... * sysdeps/pthread/Makefile (tests): ... here. --- htl/Versions | 7 + htl/pt-join.c | 62 +++- hurd/hurdlock.c | 12 + nptl/Makefile | 16 +- nptl/tst-abstime.c | 73 ----- nptl/tst-join10.c | 20 -- nptl/tst-join11.c | 21 -- nptl/tst-join12.c | 20 -- nptl/tst-join13.c | 21 -- nptl/tst-join14.c | 74 ----- nptl/tst-join2.c | 103 ------- nptl/tst-join3.c | 99 ------- nptl/tst-join8.c | 20 -- nptl/tst-join9.c | 21 -- nptl/tst-mutex-errorcheck.c | 52 ---- nptl/tst-mutex11.c | 69 ----- nptl/tst-mutex5.c | 119 -------- nptl/tst-mutex7.c | 189 ------------- nptl/tst-mutex7robust.c | 7 - nptl/tst-mutex8.c | 435 +++++++++++++++++++++++++++++ nptl/tst-mutex9.c | 140 ---------- nptl/tst-pthread-mutexattr.c | 60 ---- nptl/tst-rwlock12.c | 207 -------------- nptl/tst-rwlock14.c | 96 ------- sysdeps/htl/pt-cond-timedwait.c | 24 +- sysdeps/htl/pt-cond-wait.c | 3 +- sysdeps/htl/pt-rwlock-rdlock.c | 3 +- sysdeps/htl/pt-rwlock-timedrdlock.c | 19 +- sysdeps/htl/pt-rwlock-timedwrlock.c | 19 +- sysdeps/htl/pt-rwlock-wrlock.c | 3 +- sysdeps/htl/pthread.h | 62 ++++ sysdeps/htl/pthreadP.h | 5 + sysdeps/mach/hurd/htl/pt-mutex-timedlock.c | 20 +- sysdeps/mach/hurd/i386/libpthread.abilist | 7 + sysdeps/pthread/Makefile | 14 +- sysdeps/pthread/tst-abstime.c | 73 +++++ sysdeps/pthread/tst-join10.c | 20 ++ sysdeps/pthread/tst-join11.c | 21 ++ sysdeps/pthread/tst-join12.c | 20 ++ sysdeps/pthread/tst-join13.c | 21 ++ sysdeps/pthread/tst-join14.c | 74 +++++ sysdeps/pthread/tst-join2.c | 103 +++++++ sysdeps/pthread/tst-join3.c | 99 +++++++ sysdeps/pthread/tst-join8.c | 20 ++ sysdeps/pthread/tst-join9.c | 21 ++ sysdeps/pthread/tst-mutex-errorcheck.c | 52 ++++ sysdeps/pthread/tst-mutex11.c | 69 +++++ sysdeps/pthread/tst-mutex5.c | 119 ++++++++ sysdeps/pthread/tst-mutex7.c | 189 +++++++++++++ sysdeps/pthread/tst-mutex7robust.c | 7 + sysdeps/pthread/tst-mutex8.c | 435 ----------------------------- sysdeps/pthread/tst-mutex9.c | 140 ++++++++++ sysdeps/pthread/tst-pthread-mutexattr.c | 60 ++++ sysdeps/pthread/tst-rwlock12.c | 207 ++++++++++++++ sysdeps/pthread/tst-rwlock14.c | 96 +++++++ 55 files changed, 2082 insertions(+), 1886 deletions(-) delete mode 100644 nptl/tst-abstime.c delete mode 100644 nptl/tst-join10.c delete mode 100644 nptl/tst-join11.c delete mode 100644 nptl/tst-join12.c delete mode 100644 nptl/tst-join13.c delete mode 100644 nptl/tst-join14.c delete mode 100644 nptl/tst-join2.c delete mode 100644 nptl/tst-join3.c delete mode 100644 nptl/tst-join8.c delete mode 100644 nptl/tst-join9.c delete mode 100644 nptl/tst-mutex-errorcheck.c delete mode 100644 nptl/tst-mutex11.c delete mode 100644 nptl/tst-mutex5.c delete mode 100644 nptl/tst-mutex7.c delete mode 100644 nptl/tst-mutex7robust.c create mode 100644 nptl/tst-mutex8.c delete mode 100644 nptl/tst-mutex9.c delete mode 100644 nptl/tst-pthread-mutexattr.c delete mode 100644 nptl/tst-rwlock12.c delete mode 100644 nptl/tst-rwlock14.c create mode 100644 sysdeps/pthread/tst-abstime.c create mode 100644 sysdeps/pthread/tst-join10.c create mode 100644 sysdeps/pthread/tst-join11.c create mode 100644 sysdeps/pthread/tst-join12.c create mode 100644 sysdeps/pthread/tst-join13.c create mode 100644 sysdeps/pthread/tst-join14.c create mode 100644 sysdeps/pthread/tst-join2.c create mode 100644 sysdeps/pthread/tst-join3.c create mode 100644 sysdeps/pthread/tst-join8.c create mode 100644 sysdeps/pthread/tst-join9.c create mode 100644 sysdeps/pthread/tst-mutex-errorcheck.c create mode 100644 sysdeps/pthread/tst-mutex11.c create mode 100644 sysdeps/pthread/tst-mutex5.c create mode 100644 sysdeps/pthread/tst-mutex7.c create mode 100644 sysdeps/pthread/tst-mutex7robust.c delete mode 100644 sysdeps/pthread/tst-mutex8.c create mode 100644 sysdeps/pthread/tst-mutex9.c create mode 100644 sysdeps/pthread/tst-pthread-mutexattr.c create mode 100644 sysdeps/pthread/tst-rwlock12.c create mode 100644 sysdeps/pthread/tst-rwlock14.c diff --git a/htl/Versions b/htl/Versions index a0962e6a42..5fa9ef637c 100644 --- a/htl/Versions +++ b/htl/Versions @@ -151,10 +151,17 @@ libpthread { cnd_broadcast; cnd_destroy; cnd_init; cnd_signal; cnd_timedwait; cnd_wait; tss_create; tss_delete; tss_get; tss_set; + pthread_cond_clockwait; + pthread_mutexattr_getrobust; pthread_mutexattr_getrobust_np; pthread_mutexattr_setrobust; pthread_mutexattr_setrobust_np; pthread_mutex_consistent; pthread_mutex_consistent_np; + pthread_mutex_clocklock; + + pthread_rwlock_clockrdlock; pthread_rwlock_clockwrlock; + + pthread_tryjoin_np; pthread_timedjoin_np; pthread_clockjoin_np; } GLIBC_PRIVATE { diff --git a/htl/pt-join.c b/htl/pt-join.c index 0473511be9..f3d17dbc65 100644 --- a/htl/pt-join.c +++ b/htl/pt-join.c @@ -24,8 +24,10 @@ /* Make calling thread wait for termination of thread THREAD. Return the exit status of the thread in *STATUS. */ -int -__pthread_join (pthread_t thread, void **status) +static int +__pthread_join_common (pthread_t thread, void **status, int try, + clockid_t clockid, + const struct timespec *abstime) { struct __pthread *pthread; int err = 0; @@ -39,18 +41,30 @@ __pthread_join (pthread_t thread, void **status) return EDEADLK; __pthread_mutex_lock (&pthread->state_lock); - pthread_cleanup_push ((void (*)(void *)) __pthread_mutex_unlock, - &pthread->state_lock); - /* Rely on pthread_cond_wait being a cancellation point to make - pthread_join one too. */ - while (pthread->state == PTHREAD_JOINABLE) - __pthread_cond_wait (&pthread->state_cond, &pthread->state_lock); + if (try == 0) + { + pthread_cleanup_push ((void (*)(void *)) __pthread_mutex_unlock, + &pthread->state_lock); - pthread_cleanup_pop (0); + /* Rely on pthread_cond_wait being a cancellation point to make + pthread_join one too. */ + while (pthread->state == PTHREAD_JOINABLE && err != ETIMEDOUT) + err = __pthread_cond_clockwait (&pthread->state_cond, + &pthread->state_lock, + clockid, abstime); + + pthread_cleanup_pop (0); + } switch (pthread->state) { + case PTHREAD_JOINABLE: + __pthread_mutex_unlock (&pthread->state_lock); + if (err != ETIMEDOUT) + err = EBUSY; + break; + case PTHREAD_EXITED: /* THREAD has already exited. Salvage its exit status. */ if (status != NULL) @@ -76,4 +90,34 @@ __pthread_join (pthread_t thread, void **status) return err; } + +int +__pthread_join (pthread_t thread, void **status) +{ + return __pthread_join_common (thread, status, 0, CLOCK_REALTIME, NULL); +} weak_alias (__pthread_join, pthread_join); + +int +__pthread_tryjoin_np (pthread_t thread, void **status) +{ + return __pthread_join_common (thread, status, 1, CLOCK_REALTIME, NULL); +} +weak_alias (__pthread_tryjoin_np, pthread_tryjoin_np); + +int +__pthread_timedjoin_np (pthread_t thread, void **status, + const struct timespec *abstime) +{ + return __pthread_join_common (thread, status, 0, CLOCK_REALTIME, abstime); +} +weak_alias (__pthread_timedjoin_np, pthread_timedjoin_np); + +int +__pthread_clockjoin_np (pthread_t thread, void **status, + clockid_t clockid, + const struct timespec *abstime) +{ + return __pthread_join_common (thread, status, 0, clockid, abstime); +} +weak_alias (__pthread_clockjoin_np, pthread_clockjoin_np); diff --git a/hurd/hurdlock.c b/hurd/hurdlock.c index 50da0a2121..59d017fc02 100644 --- a/hurd/hurdlock.c +++ b/hurd/hurdlock.c @@ -47,6 +47,9 @@ int __lll_abstimed_wait (void *ptr, int val, const struct timespec *tsp, int flags, int clk) { + if (clk != CLOCK_REALTIME) + return EINVAL; + int mlsec = compute_reltime (tsp, clk); return mlsec < 0 ? KERN_TIMEDOUT : lll_timed_wait (ptr, val, mlsec, flags); } @@ -55,6 +58,9 @@ int __lll_abstimed_xwait (void *ptr, int lo, int hi, const struct timespec *tsp, int flags, int clk) { + if (clk != CLOCK_REALTIME) + return EINVAL; + int mlsec = compute_reltime (tsp, clk); return mlsec < 0 ? KERN_TIMEDOUT : lll_timed_xwait (ptr, lo, hi, mlsec, flags); @@ -64,6 +70,9 @@ int __lll_abstimed_lock (void *ptr, const struct timespec *tsp, int flags, int clk) { + if (clk != CLOCK_REALTIME) + return EINVAL; + if (lll_trylock (ptr) == 0) return 0; @@ -147,6 +156,9 @@ __lll_robust_abstimed_lock (void *ptr, int wait_time = 25; unsigned int val; + if (clk != CLOCK_REALTIME) + return EINVAL; + while (1) { val = *iptr; diff --git a/nptl/Makefile b/nptl/Makefile index 587b241367..0d07d6cccb 100644 --- a/nptl/Makefile +++ b/nptl/Makefile @@ -259,8 +259,8 @@ CFLAGS-tst-minstack-throw.o = -std=gnu++11 LDLIBS-tst-minstack-throw = -lstdc++ tests = tst-attr2 tst-attr3 tst-default-attr \ - tst-mutex5 tst-mutex7 tst-mutex9 tst-mutex11 tst-mutex5a tst-mutex7a \ - tst-mutex7robust tst-mutexpi1 tst-mutexpi2 tst-mutexpi3 tst-mutexpi4 \ + tst-mutex5a tst-mutex7a \ + tst-mutexpi1 tst-mutexpi2 tst-mutexpi3 tst-mutexpi4 \ tst-mutexpi5 tst-mutexpi5a tst-mutexpi6 tst-mutexpi7 tst-mutexpi7a \ tst-mutexpi9 \ tst-cond11 tst-cond20 tst-cond21 tst-cond22 tst-cond26 tst-cond27 \ @@ -268,16 +268,13 @@ tests = tst-attr2 tst-attr3 tst-default-attr \ tst-robustpi6 tst-robustpi7 tst-robustpi9 \ tst-rwlock2 tst-rwlock2a tst-rwlock2b tst-rwlock3 \ tst-rwlock6 tst-rwlock7 tst-rwlock8 \ - tst-rwlock9 tst-rwlock10 tst-rwlock11 tst-rwlock12 \ - tst-rwlock14 tst-rwlock15 tst-rwlock17 tst-rwlock18 \ + tst-rwlock9 tst-rwlock10 tst-rwlock11 \ + tst-rwlock15 tst-rwlock17 tst-rwlock18 \ tst-once5 \ tst-sem5 tst-sem17 \ tst-align tst-align3 \ tst-kill1 tst-kill2 tst-kill3 tst-kill4 tst-kill5 tst-kill6 \ tst-raise1 \ - tst-join2 tst-join3 \ - tst-join8 tst-join9 tst-join10 tst-join11 tst-join12 tst-join13 \ - tst-join14 \ tst-detach1 \ tst-eintr2 tst-eintr3 tst-eintr4 tst-eintr5 \ tst-tsd1 tst-tsd2 tst-tsd3 tst-tsd4 tst-tsd5 tst-tsd6 \ @@ -301,7 +298,7 @@ tests = tst-attr2 tst-attr3 tst-default-attr \ tst-exit1 tst-exit2 tst-exit3 \ tst-stdio1 tst-stdio2 \ tst-stack1 tst-stack2 tst-stack3 tst-stack4 \ - tst-pthread-attr-affinity tst-pthread-mutexattr \ + tst-pthread-attr-affinity \ tst-unload \ tst-dlsym1 \ tst-sysconf \ @@ -312,14 +309,13 @@ tests = tst-attr2 tst-attr3 tst-default-attr \ tst-context1 \ tst-sched1 \ tst-backtrace1 \ - tst-abstime \ tst-vfork1 tst-vfork2 tst-vfork1x tst-vfork2x \ tst-getpid3 \ tst-setuid3 \ tst-initializers1 $(addprefix tst-initializers1-,\ c89 gnu89 c99 gnu99 c11 gnu11) \ tst-bad-schedattr \ - tst-thread_local1 tst-mutex-errorcheck \ + tst-thread_local1 \ tst-robust-fork tst-create-detached tst-memstream \ tst-thread-exit-clobber tst-minstack-cancel tst-minstack-exit \ tst-minstack-throw \ diff --git a/nptl/tst-abstime.c b/nptl/tst-abstime.c deleted file mode 100644 index 90ed9194e3..0000000000 --- a/nptl/tst-abstime.c +++ /dev/null @@ -1,73 +0,0 @@ -/* Copyright (C) 2010-2020 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Andreas Schwab , 2010. - - 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 - . */ - -#include -#include -#include -#include -#include -#include - -static pthread_cond_t c = PTHREAD_COND_INITIALIZER; -static pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER; -static pthread_rwlock_t rw1 = PTHREAD_RWLOCK_INITIALIZER; -static pthread_rwlock_t rw2 = PTHREAD_RWLOCK_INITIALIZER; -static sem_t sem; - -static void * -th (void *arg) -{ - struct timespec t = { -2, 0 }; - - TEST_COMPARE (pthread_mutex_timedlock (&m1, &t), ETIMEDOUT); - TEST_COMPARE (pthread_mutex_clocklock (&m1, CLOCK_REALTIME, &t), ETIMEDOUT); - TEST_COMPARE (pthread_mutex_clocklock (&m1, CLOCK_MONOTONIC, &t), ETIMEDOUT); - TEST_COMPARE (pthread_rwlock_timedrdlock (&rw1, &t), ETIMEDOUT); - TEST_COMPARE (pthread_rwlock_timedwrlock (&rw2, &t), ETIMEDOUT); - TEST_COMPARE (pthread_rwlock_clockrdlock (&rw1, CLOCK_REALTIME, &t), - ETIMEDOUT); - TEST_COMPARE (pthread_rwlock_clockwrlock (&rw2, CLOCK_REALTIME, &t), - ETIMEDOUT); - TEST_COMPARE (pthread_rwlock_clockrdlock (&rw1, CLOCK_MONOTONIC, &t), - ETIMEDOUT); - TEST_COMPARE (pthread_rwlock_clockwrlock (&rw2, CLOCK_MONOTONIC, &t), - ETIMEDOUT); - return NULL; -} - -static int -do_test (void) -{ - struct timespec t = { -2, 0 }; - - sem_init (&sem, 0, 0); - TEST_COMPARE (sem_timedwait (&sem, &t), -1); - TEST_COMPARE (errno, ETIMEDOUT); - - xpthread_mutex_lock (&m1); - xpthread_rwlock_wrlock (&rw1); - xpthread_rwlock_rdlock (&rw2); - xpthread_mutex_lock (&m2); - pthread_t pth = xpthread_create (0, th, 0); - TEST_COMPARE (pthread_cond_timedwait (&c, &m2, &t), ETIMEDOUT); - xpthread_join (pth); - return 0; -} - -#include diff --git a/nptl/tst-join10.c b/nptl/tst-join10.c deleted file mode 100644 index 6e94c00f75..0000000000 --- a/nptl/tst-join10.c +++ /dev/null @@ -1,20 +0,0 @@ -/* Check if pthread_clockjoin_np is a cancellation entrypoint. - Copyright (C) 2019-2020 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 - . */ - -#define USE_PTHREAD_CLOCKJOIN_NP_REALTIME 1 -#include diff --git a/nptl/tst-join11.c b/nptl/tst-join11.c deleted file mode 100644 index ed8312bc6b..0000000000 --- a/nptl/tst-join11.c +++ /dev/null @@ -1,21 +0,0 @@ -/* Check if pthread_clockjoin_np is a cancellation entrypoint. - Copyright (C) 2019-2020 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 - . */ - -#define USE_PTHREAD_CLOCKJOIN_NP_REALTIME 1 -#define WAIT_IN_CHILD 1 -#include diff --git a/nptl/tst-join12.c b/nptl/tst-join12.c deleted file mode 100644 index 57d713c82d..0000000000 --- a/nptl/tst-join12.c +++ /dev/null @@ -1,20 +0,0 @@ -/* Check if pthread_clockjoin_np is a cancellation entrypoint. - Copyright (C) 2019-2020 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 - . */ - -#define USE_PTHREAD_CLOCKJOIN_NP_MONOTONIC 1 -#include diff --git a/nptl/tst-join13.c b/nptl/tst-join13.c deleted file mode 100644 index 61a9106ebf..0000000000 --- a/nptl/tst-join13.c +++ /dev/null @@ -1,21 +0,0 @@ -/* Check if pthread_clockjoin_np is a cancellation entrypoint. - Copyright (C) 2019-2020 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 - . */ - -#define USE_PTHREAD_CLOCKJOIN_NP_MONOTONIC 1 -#define WAIT_IN_CHILD 1 -#include diff --git a/nptl/tst-join14.c b/nptl/tst-join14.c deleted file mode 100644 index 0109324453..0000000000 --- a/nptl/tst-join14.c +++ /dev/null @@ -1,74 +0,0 @@ -/* pthread_timedjoin_np, pthread_clockjoin_np NULL timeout test. - Copyright (C) 2019-2020 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 - . */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#define CLOCK_USE_TIMEDJOIN (-1) - - -static void * -tf (void *arg) -{ - struct timespec ts = make_timespec(0, 100000); - nanosleep(&ts, NULL); - - return (void *) 42l; -} - - -/* Check that pthread_timedjoin_np and pthread_clockjoin_np wait "forever" if - * passed a timeout parameter of NULL. We can't actually wait forever, but we - * can be sure that we did at least wait for some time by checking the exit - * status of the thread. */ -static int -do_test_clock (clockid_t clockid) -{ - pthread_t th = xpthread_create (NULL, tf, NULL); - - void *status; - int val = (clockid == CLOCK_USE_TIMEDJOIN) - ? pthread_timedjoin_np (th, &status, NULL) - : pthread_clockjoin_np (th, &status, clockid, NULL); - TEST_COMPARE (val, 0); - - if (status != (void *) 42l) - FAIL_EXIT1 ("return value %p, expected %p\n", status, (void *) 42l); - - return 0; -} - -static int -do_test (void) -{ - do_test_clock (CLOCK_USE_TIMEDJOIN); - do_test_clock (CLOCK_REALTIME); - do_test_clock (CLOCK_MONOTONIC); - return 0; -} - -#include diff --git a/nptl/tst-join2.c b/nptl/tst-join2.c deleted file mode 100644 index 22dfd11b82..0000000000 --- a/nptl/tst-join2.c +++ /dev/null @@ -1,103 +0,0 @@ -/* Copyright (C) 2002-2020 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Ulrich Drepper , 2002. - - 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 - . */ - -#include -#include -#include -#include -#include -#include - - -static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; - - -static void * -tf (void *arg) -{ - if (pthread_mutex_lock (&lock) != 0) - { - puts ("child: mutex_lock failed"); - return NULL; - } - - return (void *) 42l; -} - - -static int -do_test (void) -{ - pthread_t th; - - if (pthread_mutex_lock (&lock) != 0) - { - puts ("mutex_lock failed"); - exit (1); - } - - if (pthread_create (&th, NULL, tf, NULL) != 0) - { - puts ("mutex_create failed"); - exit (1); - } - - void *status; - int val = pthread_tryjoin_np (th, &status); - if (val == 0) - { - puts ("1st tryjoin succeeded"); - exit (1); - } - else if (val != EBUSY) - { - puts ("1st tryjoin didn't return EBUSY"); - exit (1); - } - - if (pthread_mutex_unlock (&lock) != 0) - { - puts ("mutex_unlock failed"); - exit (1); - } - - while ((val = pthread_tryjoin_np (th, &status)) != 0) - { - if (val != EBUSY) - { - printf ("tryjoin returned %s (%d), expected only 0 or EBUSY\n", - strerror (val), val); - exit (1); - } - - /* Delay minimally. */ - struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 }; - nanosleep (&ts, NULL); - } - - if (status != (void *) 42l) - { - printf ("return value %p, expected %p\n", status, (void *) 42l); - exit (1); - } - - return 0; -} - -#define TEST_FUNCTION do_test () -#include "../test-skeleton.c" diff --git a/nptl/tst-join3.c b/nptl/tst-join3.c deleted file mode 100644 index ffebcf586f..0000000000 --- a/nptl/tst-join3.c +++ /dev/null @@ -1,99 +0,0 @@ -/* Copyright (C) 2002-2020 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Ulrich Drepper , 2002. - - 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 - . */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#define CLOCK_USE_TIMEDJOIN (-1) - -static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; - - -static void * -tf (void *arg) -{ - xpthread_mutex_lock (&lock); - xpthread_mutex_unlock (&lock); - - return (void *) 42l; -} - - -static int -do_test_clock (clockid_t clockid) -{ - const clockid_t clockid_for_get = - (clockid == CLOCK_USE_TIMEDJOIN) ? CLOCK_REALTIME : clockid; - - xpthread_mutex_lock (&lock); - pthread_t th = xpthread_create (NULL, tf, NULL); - - void *status; - struct timespec timeout = timespec_add (xclock_now (clockid_for_get), - make_timespec (0, 200000000)); - - int val; - if (clockid == CLOCK_USE_TIMEDJOIN) - val = pthread_timedjoin_np (th, &status, &timeout); - else - val = pthread_clockjoin_np (th, &status, clockid, &timeout); - - TEST_COMPARE (val, ETIMEDOUT); - - xpthread_mutex_unlock (&lock); - - while (1) - { - timeout = timespec_add (xclock_now (clockid_for_get), - make_timespec (0, 200000000)); - - if (clockid == CLOCK_USE_TIMEDJOIN) - val = pthread_timedjoin_np (th, &status, &timeout); - else - val = pthread_clockjoin_np (th, &status, clockid, &timeout); - if (val == 0) - break; - - TEST_COMPARE (val, ETIMEDOUT); - } - - if (status != (void *) 42l) - FAIL_EXIT1 ("return value %p, expected %p\n", status, (void *) 42l); - - return 0; -} - -static int -do_test (void) -{ - do_test_clock (CLOCK_USE_TIMEDJOIN); - do_test_clock (CLOCK_REALTIME); - do_test_clock (CLOCK_MONOTONIC); - return 0; -} - -#include diff --git a/nptl/tst-join8.c b/nptl/tst-join8.c deleted file mode 100644 index 3eddabab6f..0000000000 --- a/nptl/tst-join8.c +++ /dev/null @@ -1,20 +0,0 @@ -/* Check if pthread_timedjoin_np is a cancellation entrypoint. - Copyright (C) 2019-2020 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 - . */ - -#define USE_PTHREAD_TIMEDJOIN_NP 1 -#include diff --git a/nptl/tst-join9.c b/nptl/tst-join9.c deleted file mode 100644 index e9d0f26146..0000000000 --- a/nptl/tst-join9.c +++ /dev/null @@ -1,21 +0,0 @@ -/* Check if pthread_timedjoin_np is a cancellation entrypoint. - Copyright (C) 2019-2020 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 - . */ - -#define USE_PTHREAD_TIMEDJOIN_NP 1 -#define WAIT_IN_CHILD 1 -#include diff --git a/nptl/tst-mutex-errorcheck.c b/nptl/tst-mutex-errorcheck.c deleted file mode 100644 index cb140f94a0..0000000000 --- a/nptl/tst-mutex-errorcheck.c +++ /dev/null @@ -1,52 +0,0 @@ -/* Check that error checking mutexes are not subject to lock elision. - Copyright (C) 2016-2020 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 - . */ - -#include -#include -#include -#include -#include - -static int -do_test (void) -{ - struct timespec tms = { 0 }; - pthread_mutex_t mutex; - pthread_mutexattr_t mutexattr; - int ret = 0; - - TEST_COMPARE (pthread_mutexattr_init (&mutexattr), 0); - TEST_COMPARE (pthread_mutexattr_settype (&mutexattr, - PTHREAD_MUTEX_ERRORCHECK), 0); - - TEST_COMPARE (pthread_mutex_init (&mutex, &mutexattr), 0); - TEST_COMPARE (pthread_mutexattr_destroy (&mutexattr), 0); - - /* The call to pthread_mutex_timedlock erroneously enabled lock elision - on the mutex, which then triggered an assertion failure in - pthread_mutex_unlock. It would also defeat the error checking nature - of the mutex. */ - TEST_COMPARE (pthread_mutex_timedlock (&mutex, &tms), 0); - TEST_COMPARE (pthread_mutex_timedlock (&mutex, &tms), EDEADLK); - - TEST_COMPARE (pthread_mutex_unlock (&mutex), 0); - - return ret; -} - -#include diff --git a/nptl/tst-mutex11.c b/nptl/tst-mutex11.c deleted file mode 100644 index 2685f9e8bc..0000000000 --- a/nptl/tst-mutex11.c +++ /dev/null @@ -1,69 +0,0 @@ -/* Test unsupported/bad clocks passed to pthread_mutex_clocklock. - - Copyright (C) 2019-2020 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 - . */ - -#include -#include -#include -#include -#include -#include - -static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; - -static void test_bad_clockid (clockid_t clockid) -{ - const struct timespec ts = {0,0}; - TEST_COMPARE (pthread_mutex_clocklock (&mut, clockid, &ts), EINVAL); -} - -#define NOT_A_VALID_CLOCK 123456 - -static int -do_test (void) -{ - /* These clocks are meaningless to pthread_mutex_clocklock. */ -#if defined(CLOCK_PROCESS_CPUTIME_ID) - test_bad_clockid (CLOCK_PROCESS_CPUTIME_ID); -#endif -#if defined(CLOCK_THREAD_CPUTIME_ID) - test_bad_clockid (CLOCK_PROCESS_CPUTIME_ID); -#endif - - /* These clocks might be meaningful, but are currently unsupported by - pthread_mutex_clocklock. */ -#if defined(CLOCK_REALTIME_COARSE) - test_bad_clockid (CLOCK_REALTIME_COARSE); -#endif -#if defined(CLOCK_MONOTONIC_RAW) - test_bad_clockid (CLOCK_MONOTONIC_RAW); -#endif -#if defined(CLOCK_MONOTONIC_COARSE) - test_bad_clockid (CLOCK_MONOTONIC_COARSE); -#endif -#if defined(CLOCK_BOOTTIME) - test_bad_clockid (CLOCK_BOOTTIME); -#endif - - /* This is a completely invalid clock. */ - test_bad_clockid (NOT_A_VALID_CLOCK); - - return 0; -} - -#include diff --git a/nptl/tst-mutex5.c b/nptl/tst-mutex5.c deleted file mode 100644 index 14490768c3..0000000000 --- a/nptl/tst-mutex5.c +++ /dev/null @@ -1,119 +0,0 @@ -/* Copyright (C) 2002-2020 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Ulrich Drepper , 2002. - - 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 - . */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#ifndef TYPE -# define TYPE PTHREAD_MUTEX_NORMAL -#endif - -/* A bogus clock value that tells run_test to use - pthread_mutex_timedlock rather than pthread_mutex_clocklock. */ -#define CLOCK_USE_TIMEDLOCK (-1) - -static int -do_test_clock (clockid_t clockid, const char *fnname) -{ - pthread_mutex_t m; - pthread_mutexattr_t a; - const clockid_t clockid_for_get = - (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid; - - TEST_COMPARE (pthread_mutexattr_init (&a), 0); - TEST_COMPARE (pthread_mutexattr_settype (&a, TYPE), 0); - -#ifdef ENABLE_PI - TEST_COMPARE (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT), 0); -#endif - - int err = pthread_mutex_init (&m, &a); - if (err != 0) - { -#ifdef ENABLE_PI - if (err == ENOTSUP) - FAIL_UNSUPPORTED ("PI mutexes unsupported"); -#endif - FAIL_EXIT1 ("mutex_init failed"); - } - - TEST_COMPARE (pthread_mutexattr_destroy (&a), 0); - TEST_COMPARE (pthread_mutex_lock (&m), 0); - if (pthread_mutex_trylock (&m) == 0) - FAIL_EXIT1 ("mutex_trylock succeeded"); - - /* Wait 2 seconds. */ - struct timespec ts_timeout = timespec_add (xclock_now (clockid_for_get), - make_timespec (2, 0)); - - if (clockid == CLOCK_USE_TIMEDLOCK) - TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), ETIMEDOUT); - else - TEST_COMPARE (pthread_mutex_clocklock (&m, clockid, &ts_timeout), - ETIMEDOUT); - TEST_TIMESPEC_BEFORE_NOW (ts_timeout, clockid_for_get); - - /* The following makes the ts value invalid. */ - ts_timeout.tv_nsec += 1000000000; - - if (clockid == CLOCK_USE_TIMEDLOCK) - TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), EINVAL); - else - TEST_COMPARE (pthread_mutex_clocklock (&m, clockid, &ts_timeout), EINVAL); - TEST_COMPARE (pthread_mutex_unlock (&m), 0); - - const struct timespec ts_start = xclock_now (CLOCK_REALTIME); - - /* Wait 2 seconds. */ - ts_timeout = timespec_add (ts_start, make_timespec (2, 0)); - - if (clockid == CLOCK_USE_TIMEDLOCK) - TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), 0); - else - TEST_COMPARE (pthread_mutex_clocklock (&m, clockid, &ts_timeout), 0); - - const struct timespec ts_end = xclock_now (clockid_for_get); - - /* Check that timedlock didn't delay. We use a limit of 0.1 secs. */ - TEST_TIMESPEC_BEFORE (ts_end, - timespec_add (ts_start, make_timespec (0, 100000000))); - - TEST_COMPARE (pthread_mutex_unlock (&m), 0); - TEST_COMPARE (pthread_mutex_destroy (&m), 0); - - return 0; -} - -static int do_test (void) -{ - do_test_clock (CLOCK_USE_TIMEDLOCK, "timedlock"); - do_test_clock (CLOCK_REALTIME, "clocklock(realtime)"); - do_test_clock (CLOCK_MONOTONIC, "clocklock(monotonic)"); - return 0; -} - -#include diff --git a/nptl/tst-mutex7.c b/nptl/tst-mutex7.c deleted file mode 100644 index 3938670b54..0000000000 --- a/nptl/tst-mutex7.c +++ /dev/null @@ -1,189 +0,0 @@ -/* Copyright (C) 2002-2020 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Ulrich Drepper , 2002. - - 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 - . */ - -#include -#include -#include -#include -#include - -/* This test is a template for other tests to use. Other tests define - the following macros to change the behaviour of the template test. - The test is very simple, it configures N threads given the parameters - below and then proceeds to go through mutex lock and unlock - operations in each thread as described before for the thread - function. */ -#ifndef TYPE -# define TYPE PTHREAD_MUTEX_DEFAULT -#endif -#ifndef ROBUST -# define ROBUST PTHREAD_MUTEX_STALLED -#endif -#ifndef DELAY_NSEC -# define DELAY_NSEC 11000 -#endif -#ifndef ROUNDS -# define ROUNDS 1000 -#endif -#ifndef N -# define N 100 -#endif - -static pthread_mutex_t lock; - -/* Each thread locks and the subsequently unlocks the lock, yielding - the smallest critical section possible. After the unlock the thread - waits DELAY_NSEC nanoseconds before doing the lock and unlock again. - Every thread does this ROUNDS times. The lock and unlock are - checked for errors. */ -static void * -tf (void *arg) -{ - int nr = (long int) arg; - int cnt; - struct timespec ts = { .tv_sec = 0, .tv_nsec = DELAY_NSEC }; - - for (cnt = 0; cnt < ROUNDS; ++cnt) - { - if (pthread_mutex_lock (&lock) != 0) - { - printf ("thread %d: failed to get the lock\n", nr); - return (void *) 1l; - } - - if (pthread_mutex_unlock (&lock) != 0) - { - printf ("thread %d: failed to release the lock\n", nr); - return (void *) 1l; - } - - if ((ts.tv_sec > 0) || (ts.tv_nsec > 0)) - nanosleep (&ts, NULL); - } - - return NULL; -} - -/* Setup and run N threads, where each thread does as described - in the above thread function. The threads are given a minimal 1MiB - stack since they don't do anything between the lock and unlock. */ -static int -do_test (void) -{ - pthread_mutexattr_t a; - - if (pthread_mutexattr_init (&a) != 0) - { - puts ("mutexattr_init failed"); - exit (1); - } - - if (pthread_mutexattr_settype (&a, TYPE) != 0) - { - puts ("mutexattr_settype failed"); - exit (1); - } - - if (pthread_mutexattr_setrobust (&a, ROBUST) != 0) - { - puts ("mutexattr_setrobust failed"); - exit (1); - } - -#ifdef ENABLE_PI - if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0) - { - puts ("pthread_mutexattr_setprotocol failed"); - return 1; - } -#endif - - int e = pthread_mutex_init (&lock, &a); - if (e != 0) - { -#ifdef ENABLE_PI - if (e == ENOTSUP) - { - puts ("PI mutexes unsupported"); - return 0; - } -#endif - puts ("mutex_init failed"); - return 1; - } - - if (pthread_mutexattr_destroy (&a) != 0) - { - puts ("mutexattr_destroy failed"); - return 1; - } - - pthread_attr_t at; - pthread_t th[N]; - int cnt; - - if (pthread_attr_init (&at) != 0) - { - puts ("attr_init failed"); - return 1; - } - - if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0) - { - puts ("attr_setstacksize failed"); - return 1; - } - - if (pthread_mutex_lock (&lock) != 0) - { - puts ("locking in parent failed"); - return 1; - } - - for (cnt = 0; cnt < N; ++cnt) - if (pthread_create (&th[cnt], &at, tf, (void *) (long int) cnt) != 0) - { - printf ("creating thread %d failed\n", cnt); - return 1; - } - - if (pthread_attr_destroy (&at) != 0) - { - puts ("attr_destroy failed"); - return 1; - } - - if (pthread_mutex_unlock (&lock) != 0) - { - puts ("unlocking in parent failed"); - return 1; - } - - for (cnt = 0; cnt < N; ++cnt) - if (pthread_join (th[cnt], NULL) != 0) - { - printf ("joining thread %d failed\n", cnt); - return 1; - } - - return 0; -} - -#define TIMEOUT 60 -#define TEST_FUNCTION do_test () -#include "../test-skeleton.c" diff --git a/nptl/tst-mutex7robust.c b/nptl/tst-mutex7robust.c deleted file mode 100644 index 8221a61d29..0000000000 --- a/nptl/tst-mutex7robust.c +++ /dev/null @@ -1,7 +0,0 @@ -/* Bug 21778: Fix oversight in robust mutex lock acquisition. */ -#define TYPE PTHREAD_MUTEX_NORMAL -#define ROBUST PTHREAD_MUTEX_ROBUST -#define DELAY_NSEC 0 -#define ROUNDS 1000 -#define N 32 -#include "tst-mutex7.c" diff --git a/nptl/tst-mutex8.c b/nptl/tst-mutex8.c new file mode 100644 index 0000000000..8e56ea5a3a --- /dev/null +++ b/nptl/tst-mutex8.c @@ -0,0 +1,435 @@ +/* Copyright (C) 2003-2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 2003. + + 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 + . */ + +/* This test checks behavior not required by POSIX. */ +#include +#include +#include +#include +#include +#include +#include + +static pthread_mutex_t *m; +static pthread_barrier_t b; +static pthread_cond_t c; +static bool done; + + +static void +cl (void *arg) +{ + if (pthread_mutex_unlock (m) != 0) + { + puts ("cl: mutex_unlocked failed"); + exit (1); + } +} + + +static void * +tf (void *arg) +{ + if (pthread_mutex_lock (m) != 0) + { + puts ("tf: mutex_lock failed"); + return (void *) 1l; + } + + int e = pthread_barrier_wait (&b); + if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) + { + puts ("barrier_wait failed"); + return (void *) 1l; + } + + if (arg == NULL) + do + if (pthread_cond_wait (&c, m) != 0) + { + puts ("tf: cond_wait failed"); + return (void *) 1l; + } + while (! done); + else + do + { + pthread_cleanup_push (cl, NULL); + + if (pthread_cond_wait (&c, m) != 0) + { + puts ("tf: cond_wait failed"); + return (void *) 1l; + } + + pthread_cleanup_pop (0); + } + while (! done); + + if (pthread_mutex_unlock (m) != 0) + { + puts ("tf: mutex_unlock failed"); + return (void *) 1l; + } + + return NULL; +} + + +static int +check_type (const char *mas, pthread_mutexattr_t *ma) +{ + int e; + + /* Check if a mutex will be elided. Lock elision can only be activated via + the tunables framework. By default, lock elision is disabled. */ + bool assume_elided_mutex = false; +#if HAVE_TUNABLES + int ma_type = PTHREAD_MUTEX_TIMED_NP; + if (ma != NULL) + { + e = pthread_mutexattr_gettype (ma, &ma_type); + if (e != 0) + { + printf ("pthread_mutexattr_gettype failed with %d (%m)\n", e); + return 1; + } + } + if (ma_type == PTHREAD_MUTEX_TIMED_NP) + { + /* This type of mutex can be elided if elision is enabled via the tunables + framework. Some tests below are failing if the mutex is elided. + Thus we only run those if we assume that the mutex won't be elided. */ + if (TUNABLE_GET_FULL (glibc, elision, enable, int32_t, NULL) == 1) + assume_elided_mutex = true; + } +#endif + + e = pthread_mutex_init (m, ma); + if (e != 0) + { +#ifdef ENABLE_PI + if (e == ENOTSUP) + { + puts ("PI mutexes unsupported"); + return 0; + } +#endif + printf ("1st mutex_init failed for %s\n", mas); + return 1; + } + + if (pthread_mutex_destroy (m) != 0) + { + printf ("immediate mutex_destroy failed for %s\n", mas); + return 1; + } + + if (pthread_mutex_init (m, ma) != 0) + { + printf ("2nd mutex_init failed for %s\n", mas); + return 1; + } + + if (pthread_mutex_lock (m) != 0) + { + printf ("1st mutex_lock failed for %s\n", mas); + return 1; + } + + /* Elided mutexes don't fail destroy, thus only test this if we don't assume + elision. */ + if (assume_elided_mutex == false) + { + e = pthread_mutex_destroy (m); + if (e == 0) + { + printf ("mutex_destroy of self-locked mutex succeeded for %s\n", mas); + return 1; + } + if (e != EBUSY) + { + printf ("\ +mutex_destroy of self-locked mutex did not return EBUSY %s\n", + mas); + return 1; + } + } + + if (pthread_mutex_unlock (m) != 0) + { + printf ("1st mutex_unlock failed for %s\n", mas); + return 1; + } + + if (pthread_mutex_trylock (m) != 0) + { + printf ("mutex_trylock failed for %s\n", mas); + return 1; + } + + /* Elided mutexes don't fail destroy. */ + if (assume_elided_mutex == false) + { + e = pthread_mutex_destroy (m); + if (e == 0) + { + printf ("mutex_destroy of self-trylocked mutex succeeded for %s\n", + mas); + return 1; + } + if (e != EBUSY) + { + printf ("\ +mutex_destroy of self-trylocked mutex did not return EBUSY %s\n", + mas); + return 1; + } + } + + if (pthread_mutex_unlock (m) != 0) + { + printf ("2nd mutex_unlock failed for %s\n", mas); + return 1; + } + + pthread_t th; + if (pthread_create (&th, NULL, tf, NULL) != 0) + { + puts ("1st create failed"); + return 1; + } + done = false; + + e = pthread_barrier_wait (&b); + if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) + { + puts ("1st barrier_wait failed"); + return 1; + } + + if (pthread_mutex_lock (m) != 0) + { + printf ("2nd mutex_lock failed for %s\n", mas); + return 1; + } + + if (pthread_mutex_unlock (m) != 0) + { + printf ("3rd mutex_unlock failed for %s\n", mas); + return 1; + } + + /* Elided mutexes don't fail destroy. */ + if (assume_elided_mutex == false) + { + e = pthread_mutex_destroy (m); + if (e == 0) + { + printf ("mutex_destroy of condvar-used mutex succeeded for %s\n", + mas); + return 1; + } + if (e != EBUSY) + { + printf ("\ +mutex_destroy of condvar-used mutex did not return EBUSY for %s\n", mas); + return 1; + } + } + + done = true; + if (pthread_cond_signal (&c) != 0) + { + puts ("cond_signal failed"); + return 1; + } + + void *r; + if (pthread_join (th, &r) != 0) + { + puts ("join failed"); + return 1; + } + if (r != NULL) + { + puts ("thread didn't return NULL"); + return 1; + } + + if (pthread_mutex_destroy (m) != 0) + { + printf ("mutex_destroy after condvar-use failed for %s\n", mas); + return 1; + } + + if (pthread_mutex_init (m, ma) != 0) + { + printf ("3rd mutex_init failed for %s\n", mas); + return 1; + } + + if (pthread_create (&th, NULL, tf, (void *) 1) != 0) + { + puts ("2nd create failed"); + return 1; + } + done = false; + + e = pthread_barrier_wait (&b); + if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) + { + puts ("2nd barrier_wait failed"); + return 1; + } + + if (pthread_mutex_lock (m) != 0) + { + printf ("3rd mutex_lock failed for %s\n", mas); + return 1; + } + + if (pthread_mutex_unlock (m) != 0) + { + printf ("4th mutex_unlock failed for %s\n", mas); + return 1; + } + + /* Elided mutexes don't fail destroy. */ + if (assume_elided_mutex == false) + { + e = pthread_mutex_destroy (m); + if (e == 0) + { + printf ("2nd mutex_destroy of condvar-used mutex succeeded for %s\n", + mas); + return 1; + } + if (e != EBUSY) + { + printf ("\ +2nd mutex_destroy of condvar-used mutex did not return EBUSY for %s\n", + mas); + return 1; + } + } + + if (pthread_cancel (th) != 0) + { + puts ("cond_cancel failed"); + return 1; + } + + if (pthread_join (th, &r) != 0) + { + puts ("join failed"); + return 1; + } + if (r != PTHREAD_CANCELED) + { + puts ("thread not canceled"); + return 1; + } + + if (pthread_mutex_destroy (m) != 0) + { + printf ("mutex_destroy after condvar-canceled failed for %s\n", mas); + return 1; + } + + return 0; +} + + +static int +do_test (void) +{ + pthread_mutex_t mm; + m = &mm; + + if (pthread_barrier_init (&b, NULL, 2) != 0) + { + puts ("barrier_init failed"); + return 1; + } + + if (pthread_cond_init (&c, NULL) != 0) + { + puts ("cond_init failed"); + return 1; + } + + puts ("check normal mutex"); + int res = check_type ("normal", NULL); + + pthread_mutexattr_t ma; + if (pthread_mutexattr_init (&ma) != 0) + { + puts ("1st mutexattr_init failed"); + return 1; + } + if (pthread_mutexattr_settype (&ma, PTHREAD_MUTEX_RECURSIVE) != 0) + { + puts ("1st mutexattr_settype failed"); + return 1; + } +#ifdef ENABLE_PI + if (pthread_mutexattr_setprotocol (&ma, PTHREAD_PRIO_INHERIT)) + { + puts ("1st pthread_mutexattr_setprotocol failed"); + return 1; + } +#endif + puts ("check recursive mutex"); + res |= check_type ("recursive", &ma); + if (pthread_mutexattr_destroy (&ma) != 0) + { + puts ("1st mutexattr_destroy failed"); + return 1; + } + + if (pthread_mutexattr_init (&ma) != 0) + { + puts ("2nd mutexattr_init failed"); + return 1; + } + if (pthread_mutexattr_settype (&ma, PTHREAD_MUTEX_ERRORCHECK) != 0) + { + puts ("2nd mutexattr_settype failed"); + return 1; + } +#ifdef ENABLE_PI + if (pthread_mutexattr_setprotocol (&ma, PTHREAD_PRIO_INHERIT)) + { + puts ("2nd pthread_mutexattr_setprotocol failed"); + return 1; + } +#endif + puts ("check error-checking mutex"); + res |= check_type ("error-checking", &ma); + if (pthread_mutexattr_destroy (&ma) != 0) + { + puts ("2nd mutexattr_destroy failed"); + return 1; + } + + return res; +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c" diff --git a/nptl/tst-mutex9.c b/nptl/tst-mutex9.c deleted file mode 100644 index 2d7927b7c2..0000000000 --- a/nptl/tst-mutex9.c +++ /dev/null @@ -1,140 +0,0 @@ -/* Copyright (C) 2003-2020 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Ulrich Drepper , 2003. - - 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 - . */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -/* A bogus clock value that tells run_test to use pthread_mutex_timedlock - rather than pthread_mutex_clocklock. */ -#define CLOCK_USE_TIMEDLOCK (-1) - -static void -do_test_clock (clockid_t clockid) -{ - const clockid_t clockid_for_get = - (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid; - size_t ps = sysconf (_SC_PAGESIZE); - char tmpfname[] = "/tmp/tst-mutex9.XXXXXX"; - char data[ps]; - void *mem; - int fd; - pthread_mutex_t *m; - pthread_mutexattr_t a; - pid_t pid; - - fd = mkstemp (tmpfname); - if (fd == -1) - FAIL_EXIT1 ("cannot open temporary file: %m\n"); - - /* Make sure it is always removed. */ - unlink (tmpfname); - - /* Create one page of data. */ - memset (data, '\0', ps); - - /* Write the data to the file. */ - xwrite (fd, data, ps); - - mem = xmmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd); - - m = (pthread_mutex_t *) (((uintptr_t) mem + __alignof (pthread_mutex_t)) - & ~(__alignof (pthread_mutex_t) - 1)); - - TEST_COMPARE (pthread_mutexattr_init (&a), 0); - - TEST_COMPARE (pthread_mutexattr_setpshared (&a, PTHREAD_PROCESS_SHARED), 0); - - TEST_COMPARE (pthread_mutexattr_settype (&a, PTHREAD_MUTEX_RECURSIVE), 0); - -#ifdef ENABLE_PI - TEST_COMPARE (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT), 0); -#endif - - int e; - if ((e = pthread_mutex_init (m, &a)) != 0) - { -#ifdef ENABLE_PI - if (e == ENOTSUP) - FAIL_UNSUPPORTED ("PI mutexes unsupported"); -#endif - FAIL_EXIT1 ("mutex_init failed"); - } - - TEST_COMPARE (pthread_mutex_lock (m), 0); - - TEST_COMPARE (pthread_mutexattr_destroy (&a), 0); - - puts ("going to fork now"); - pid = xfork (); - if (pid == 0) - { - if (pthread_mutex_trylock (m) == 0) - FAIL_EXIT1 ("child: mutex_trylock succeeded"); - - if (pthread_mutex_unlock (m) == 0) - FAIL_EXIT1 ("child: mutex_unlock succeeded"); - - const struct timespec ts = timespec_add (xclock_now (clockid_for_get), - make_timespec (0, 500000000)); - - if (clockid == CLOCK_USE_TIMEDLOCK) - TEST_COMPARE (pthread_mutex_timedlock (m, &ts), ETIMEDOUT); - else - TEST_COMPARE (pthread_mutex_clocklock (m, clockid, &ts), ETIMEDOUT); - - alarm (1); - - pthread_mutex_lock (m); - - puts ("child: mutex_lock returned"); - - exit (0); - } - - sleep (2); - - int status; - if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid) - FAIL_EXIT1 ("waitpid failed"); - if (! WIFSIGNALED (status)) - FAIL_EXIT1 ("child not killed by signal"); - TEST_COMPARE (WTERMSIG (status), SIGALRM); -} - -static int -do_test (void) -{ - do_test_clock (CLOCK_USE_TIMEDLOCK); - do_test_clock (CLOCK_REALTIME); - do_test_clock (CLOCK_MONOTONIC); - return 0; -} - -#include diff --git a/nptl/tst-pthread-mutexattr.c b/nptl/tst-pthread-mutexattr.c deleted file mode 100644 index d0615bfcf8..0000000000 --- a/nptl/tst-pthread-mutexattr.c +++ /dev/null @@ -1,60 +0,0 @@ -/* Make sure that pthread_mutexattr_gettype returns a valid kind. - - Copyright (C) 2015-2020 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 - . */ - -#include -#include -#include - -static int -do_test (void) -{ - pthread_mutexattr_t attr; - int kind; - int error; - - error = pthread_mutexattr_init (&attr); - if (error) - { - printf ("pthread_mutexattr_init: %s\n", strerror (error)); - return 1; - } - error = pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_DEFAULT); - if (error) - { - printf ("pthread_mutexattr_settype (1): %s\n", strerror (error)); - return 1; - } - error = pthread_mutexattr_gettype (&attr, &kind); - if (error) - { - printf ("pthread_mutexattr_gettype: %s\n", strerror (error)); - return 1; - } - error = pthread_mutexattr_settype (&attr, kind); - if (error) - { - printf ("pthread_mutexattr_settype (2): %s\n", strerror (error)); - return 1; - } - return 0; -} - - -#define TEST_FUNCTION do_test () -#include "../test-skeleton.c" diff --git a/nptl/tst-rwlock12.c b/nptl/tst-rwlock12.c deleted file mode 100644 index 73937fe5d6..0000000000 --- a/nptl/tst-rwlock12.c +++ /dev/null @@ -1,207 +0,0 @@ -/* Copyright (C) 2002-2020 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Ulrich Drepper , 2002. - - 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 - . */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -static int -do_test (void) -{ - size_t ps = sysconf (_SC_PAGESIZE); - char tmpfname[] = "/tmp/tst-rwlock12.XXXXXX"; - char data[ps]; - void *mem; - int fd; - pthread_mutex_t *m; - pthread_mutexattr_t ma; - pthread_rwlock_t *r; - pthread_rwlockattr_t ra; - pid_t pid; - int status = 0; - - fd = mkstemp (tmpfname); - if (fd == -1) - { - printf ("cannot open temporary file: %m\n"); - return 1; - } - - /* Make sure it is always removed. */ - unlink (tmpfname); - - /* Create one page of data. */ - memset (data, '\0', ps); - - /* Write the data to the file. */ - if (write (fd, data, ps) != (ssize_t) ps) - { - puts ("short write"); - return 1; - } - - mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if (mem == MAP_FAILED) - { - printf ("mmap failed: %m\n"); - return 1; - } - - r = (pthread_rwlock_t *) (((uintptr_t) mem + __alignof (pthread_rwlock_t)) - & ~(__alignof (pthread_rwlock_t) - 1)); - /* The following assumes alignment for a mutex is at least as high - as that for a rwlock. Which is true in our case. */ - m = (pthread_mutex_t *) (r + 1); - - if (pthread_rwlockattr_init (&ra) != 0) - { - puts ("rwlockattr_init failed"); - return 1; - } - - if (pthread_rwlockattr_setpshared (&ra, PTHREAD_PROCESS_SHARED) != 0) - { - puts ("rwlockattr_setpshared failed"); - return 1; - } - - if (pthread_rwlock_init (r, &ra) != 0) - { - puts ("rwlock_init failed"); - return 1; - } - - if (pthread_mutexattr_init (&ma) != 0) - { - puts ("rwlockattr_init failed"); - return 1; - } - - if (pthread_mutexattr_setpshared (&ma, PTHREAD_PROCESS_SHARED) != 0) - { - puts ("mutexattr_setpshared failed"); - return 1; - } - - if (pthread_mutex_init (m, &ma) != 0) - { - puts ("mutex_init failed"); - return 1; - } - - /* Lock the mutex. */ - if (pthread_mutex_lock (m) != 0) - { - puts ("mutex_lock failed"); - return 1; - } - - puts ("going to fork now"); - pid = fork (); - if (pid == -1) - { - puts ("fork failed"); - return 1; - } - else if (pid == 0) - { - /* Lock the mutex. */ - if (pthread_mutex_lock (m) != 0) - { - puts ("child: mutex_lock failed"); - return 1; - } - - /* Try to get the rwlock. */ - if (pthread_rwlock_trywrlock (r) == 0) - { - puts ("rwlock_trywrlock succeeded"); - return 1; - } - - /* Try again. */ - struct timespec ts = { .tv_sec = 0, .tv_nsec = 500000000 }; - int e = pthread_rwlock_timedwrlock (r, &ts); - if (e == 0) - { - puts ("rwlock_timedwrlock succeeded"); - return 1; - } - if (e != ETIMEDOUT) - { - puts ("rwlock_timedwrlock didn't return ETIMEDOUT"); - status = 1; - } - - if (pthread_rwlock_tryrdlock (r) == 0) - { - puts ("rwlock_tryrdlock succeeded"); - return 1; - } - - e = pthread_rwlock_timedrdlock (r, &ts); - if (e == 0) - { - puts ("rwlock_timedrdlock succeeded"); - return 1; - } - if (e != ETIMEDOUT) - { - puts ("rwlock_timedrdlock didn't return ETIMEDOUT"); - status = 1; - } - } - else - { - /* Lock the rwlock for writing. */ - if (pthread_rwlock_wrlock (r) != 0) - { - puts ("rwlock_wrlock failed"); - kill (pid, SIGTERM); - return 1; - } - - /* Allow the child to run. */ - if (pthread_mutex_unlock (m) != 0) - { - puts ("mutex_unlock failed"); - kill (pid, SIGTERM); - return 1; - } - - /* Just wait for the child. */ - if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid) - { - puts ("waitpid failed"); - kill (pid, SIGTERM); - return 1; - } - } - - return status; -} - -#define TEST_FUNCTION do_test () -#include "../test-skeleton.c" diff --git a/nptl/tst-rwlock14.c b/nptl/tst-rwlock14.c deleted file mode 100644 index 3583b19e3c..0000000000 --- a/nptl/tst-rwlock14.c +++ /dev/null @@ -1,96 +0,0 @@ -/* Copyright (C) 2004-2020 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Ulrich Drepper , 2004. - - 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 - . */ - -#include -#include -#include -#include -#include -#include -#include -#include - - -static pthread_barrier_t b; -static pthread_rwlock_t r = PTHREAD_RWLOCK_INITIALIZER; - - -static void * -tf (void *arg) -{ - /* Lock the read-write lock. */ - TEST_COMPARE (pthread_rwlock_wrlock (&r), 0); - - pthread_t mt = *(pthread_t *) arg; - - xpthread_barrier_wait (&b); - - /* This call will never return. */ - xpthread_join (mt); - - return NULL; -} - - -static int -do_test (void) -{ - struct timespec ts; - - xclock_gettime (CLOCK_REALTIME, &ts); - xpthread_barrier_init (&b, NULL, 2); - - pthread_t me = pthread_self (); - xpthread_create (NULL, tf, &me); - - /* Wait until the rwlock is locked. */ - xpthread_barrier_wait (&b); - - ts.tv_nsec = -1; - - TEST_COMPARE (pthread_rwlock_timedrdlock (&r, &ts), EINVAL); - TEST_COMPARE (pthread_rwlock_clockrdlock (&r, CLOCK_REALTIME, &ts), EINVAL); - TEST_COMPARE (pthread_rwlock_clockrdlock (&r, CLOCK_MONOTONIC, &ts), EINVAL); - TEST_COMPARE (pthread_rwlock_timedwrlock (&r, &ts), EINVAL); - TEST_COMPARE (pthread_rwlock_clockwrlock (&r, CLOCK_REALTIME, &ts), EINVAL); - TEST_COMPARE (pthread_rwlock_clockwrlock (&r, CLOCK_MONOTONIC, &ts), EINVAL); - - ts.tv_nsec = 1000000000; - - TEST_COMPARE (pthread_rwlock_timedrdlock (&r, &ts), EINVAL); - TEST_COMPARE (pthread_rwlock_clockrdlock (&r, CLOCK_REALTIME, &ts), EINVAL); - TEST_COMPARE (pthread_rwlock_clockrdlock (&r, CLOCK_MONOTONIC, &ts), EINVAL); - TEST_COMPARE (pthread_rwlock_timedwrlock (&r, &ts), EINVAL); - TEST_COMPARE (pthread_rwlock_clockwrlock (&r, CLOCK_REALTIME, &ts), EINVAL); - TEST_COMPARE (pthread_rwlock_clockwrlock (&r, CLOCK_MONOTONIC, &ts), EINVAL); - - ts.tv_nsec = (__typeof (ts.tv_nsec)) 0x100001000LL; - if ((__typeof (ts.tv_nsec)) 0x100001000LL != 0x100001000LL) - ts.tv_nsec = 2000000000; - - TEST_COMPARE (pthread_rwlock_timedrdlock (&r, &ts), EINVAL); - TEST_COMPARE (pthread_rwlock_clockrdlock (&r, CLOCK_REALTIME, &ts), EINVAL); - TEST_COMPARE (pthread_rwlock_clockrdlock (&r, CLOCK_MONOTONIC, &ts), EINVAL); - TEST_COMPARE (pthread_rwlock_timedwrlock (&r, &ts), EINVAL); - TEST_COMPARE (pthread_rwlock_clockwrlock (&r, CLOCK_REALTIME, &ts), EINVAL); - TEST_COMPARE (pthread_rwlock_clockwrlock (&r, CLOCK_MONOTONIC, &ts), EINVAL); - - return 0; -} - -#include diff --git a/sysdeps/htl/pt-cond-timedwait.c b/sysdeps/htl/pt-cond-timedwait.c index d27f7ba8d6..a0ced9a074 100644 --- a/sysdeps/htl/pt-cond-timedwait.c +++ b/sysdeps/htl/pt-cond-timedwait.c @@ -24,6 +24,7 @@ extern int __pthread_cond_timedwait_internal (pthread_cond_t *cond, pthread_mutex_t *mutex, + clockid_t clockid, const struct timespec *abstime); int @@ -31,11 +32,22 @@ __pthread_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) { - return __pthread_cond_timedwait_internal (cond, mutex, abstime); + return __pthread_cond_timedwait_internal (cond, mutex, -1, abstime); } weak_alias (__pthread_cond_timedwait, pthread_cond_timedwait); +int +__pthread_cond_clockwait (pthread_cond_t *cond, + pthread_mutex_t *mutex, + clockid_t clockid, + const struct timespec *abstime) +{ + return __pthread_cond_timedwait_internal (cond, mutex, clockid, abstime); +} + +weak_alias (__pthread_cond_clockwait, pthread_cond_clockwait); + struct cancel_ctx { struct __pthread *wakeup; @@ -69,11 +81,17 @@ cancel_hook (void *arg) int __pthread_cond_timedwait_internal (pthread_cond_t *cond, pthread_mutex_t *mutex, + clockid_t clockid, const struct timespec *abstime) { error_t err; int cancelled, oldtype, drain; - clockid_t clock_id = __pthread_default_condattr.__clock; + clockid_t clock_id; + + if (clockid != -1) + clock_id = clockid; + else + clock_id = __pthread_default_condattr.__clock; if (abstime && ! valid_nanoseconds (abstime->tv_nsec)) return EINVAL; @@ -114,7 +132,7 @@ __pthread_cond_timedwait_internal (pthread_cond_t *cond, already unblocked, progressing on the return path. */ __pthread_spin_wait (&cond->__lock); __pthread_enqueue (&cond->__queue, self); - if (cond->__attr != NULL) + if (cond->__attr != NULL && clockid == -1) clock_id = cond->__attr->__clock; __pthread_spin_unlock (&cond->__lock); } diff --git a/sysdeps/htl/pt-cond-wait.c b/sysdeps/htl/pt-cond-wait.c index 2e87db0ca6..00a6922659 100644 --- a/sysdeps/htl/pt-cond-wait.c +++ b/sysdeps/htl/pt-cond-wait.c @@ -23,6 +23,7 @@ /* Implemented in pt-cond-timedwait.c. */ extern int __pthread_cond_timedwait_internal (pthread_cond_t *cond, pthread_mutex_t *mutex, + clockid_t clockid, const struct timespec *abstime); @@ -32,7 +33,7 @@ extern int __pthread_cond_timedwait_internal (pthread_cond_t *cond, int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex) { - return __pthread_cond_timedwait_internal (cond, mutex, 0); + return __pthread_cond_timedwait_internal (cond, mutex, -1, 0); } weak_alias (__pthread_cond_wait, pthread_cond_wait); diff --git a/sysdeps/htl/pt-rwlock-rdlock.c b/sysdeps/htl/pt-rwlock-rdlock.c index 07aa119cbc..6588f01f80 100644 --- a/sysdeps/htl/pt-rwlock-rdlock.c +++ b/sysdeps/htl/pt-rwlock-rdlock.c @@ -22,6 +22,7 @@ /* Implemented in pt-rwlock-timedrdlock.c. */ extern int __pthread_rwlock_timedrdlock_internal (struct __pthread_rwlock *rwlock, + clockid_t clockid, const struct timespec *abstime); @@ -29,6 +30,6 @@ extern int __pthread_rwlock_timedrdlock_internal (struct __pthread_rwlock int __pthread_rwlock_rdlock (struct __pthread_rwlock *rwlock) { - return __pthread_rwlock_timedrdlock_internal (rwlock, 0); + return __pthread_rwlock_timedrdlock_internal (rwlock, -1, 0); } weak_alias (__pthread_rwlock_rdlock, pthread_rwlock_rdlock); diff --git a/sysdeps/htl/pt-rwlock-timedrdlock.c b/sysdeps/htl/pt-rwlock-timedrdlock.c index c2827662fd..7019085489 100644 --- a/sysdeps/htl/pt-rwlock-timedrdlock.c +++ b/sysdeps/htl/pt-rwlock-timedrdlock.c @@ -27,6 +27,7 @@ wait forever. */ int __pthread_rwlock_timedrdlock_internal (struct __pthread_rwlock *rwlock, + clockid_t clockid, const struct timespec *abstime) { error_t err; @@ -62,7 +63,10 @@ __pthread_rwlock_timedrdlock_internal (struct __pthread_rwlock *rwlock, assert (rwlock->__readers == 0); if (abstime != NULL && ! valid_nanoseconds (abstime->tv_nsec)) - return EINVAL; + { + __pthread_spin_unlock (&rwlock->__lock); + return EINVAL; + } self = _pthread_self (); @@ -72,7 +76,7 @@ __pthread_rwlock_timedrdlock_internal (struct __pthread_rwlock *rwlock, /* Block the thread. */ if (abstime != NULL) - err = __pthread_timedblock (self, abstime, CLOCK_REALTIME); + err = __pthread_timedblock (self, abstime, clockid); else { err = 0; @@ -116,6 +120,15 @@ int __pthread_rwlock_timedrdlock (struct __pthread_rwlock *rwlock, const struct timespec *abstime) { - return __pthread_rwlock_timedrdlock_internal (rwlock, abstime); + return __pthread_rwlock_timedrdlock_internal (rwlock, CLOCK_REALTIME, abstime); } weak_alias (__pthread_rwlock_timedrdlock, pthread_rwlock_timedrdlock) + +int +__pthread_rwlock_clockrdlock (struct __pthread_rwlock *rwlock, + clockid_t clockid, + const struct timespec *abstime) +{ + return __pthread_rwlock_timedrdlock_internal (rwlock, clockid, abstime); +} +weak_alias (__pthread_rwlock_clockrdlock, pthread_rwlock_clockrdlock) diff --git a/sysdeps/htl/pt-rwlock-timedwrlock.c b/sysdeps/htl/pt-rwlock-timedwrlock.c index d0293c1e96..e9f8da07db 100644 --- a/sysdeps/htl/pt-rwlock-timedwrlock.c +++ b/sysdeps/htl/pt-rwlock-timedwrlock.c @@ -27,6 +27,7 @@ shall not time out. */ int __pthread_rwlock_timedwrlock_internal (struct __pthread_rwlock *rwlock, + clockid_t clockid, const struct timespec *abstime) { error_t err; @@ -48,7 +49,10 @@ __pthread_rwlock_timedwrlock_internal (struct __pthread_rwlock *rwlock, /* The lock is busy. */ if (abstime != NULL && ! valid_nanoseconds (abstime->tv_nsec)) - return EINVAL; + { + __pthread_spin_unlock (&rwlock->__lock); + return EINVAL; + } self = _pthread_self (); @@ -58,7 +62,7 @@ __pthread_rwlock_timedwrlock_internal (struct __pthread_rwlock *rwlock, /* Block the thread. */ if (abstime != NULL) - err = __pthread_timedblock (self, abstime, CLOCK_REALTIME); + err = __pthread_timedblock (self, abstime, clockid); else { err = 0; @@ -99,6 +103,15 @@ int __pthread_rwlock_timedwrlock (struct __pthread_rwlock *rwlock, const struct timespec *abstime) { - return __pthread_rwlock_timedwrlock_internal (rwlock, abstime); + return __pthread_rwlock_timedwrlock_internal (rwlock, CLOCK_REALTIME, abstime); } weak_alias (__pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock) + +int +__pthread_rwlock_clockwrlock (struct __pthread_rwlock *rwlock, + clockid_t clockid, + const struct timespec *abstime) +{ + return __pthread_rwlock_timedwrlock_internal (rwlock, clockid, abstime); +} +weak_alias (__pthread_rwlock_clockwrlock, pthread_rwlock_clockwrlock) diff --git a/sysdeps/htl/pt-rwlock-wrlock.c b/sysdeps/htl/pt-rwlock-wrlock.c index dcf09d98c7..c3e9c64051 100644 --- a/sysdeps/htl/pt-rwlock-wrlock.c +++ b/sysdeps/htl/pt-rwlock-wrlock.c @@ -24,6 +24,7 @@ /* Implemented in pt-rwlock-timedwrlock.c. */ extern int __pthread_rwlock_timedwrlock_internal (struct __pthread_rwlock *rwlock, + clockid_t clockid, const struct timespec *abstime); @@ -31,6 +32,6 @@ extern int __pthread_rwlock_timedwrlock_internal (struct __pthread_rwlock int __pthread_rwlock_wrlock (struct __pthread_rwlock *rwlock) { - return __pthread_rwlock_timedwrlock_internal (rwlock, 0); + return __pthread_rwlock_timedwrlock_internal (rwlock, -1, 0); } weak_alias (__pthread_rwlock_wrlock, pthread_rwlock_wrlock); diff --git a/sysdeps/htl/pthread.h b/sysdeps/htl/pthread.h index d639385eb3..c44f70b158 100644 --- a/sysdeps/htl/pthread.h +++ b/sysdeps/htl/pthread.h @@ -222,6 +222,32 @@ extern void pthread_exit (void *__status) __attribute__ ((__noreturn__)); the exit status of the thread in *STATUS. */ extern int pthread_join (pthread_t __threadp, void **__status); +#ifdef __USE_GNU +/* Check whether thread TH has terminated. If yes return the status of + the thread in *THREAD_RETURN, if THREAD_RETURN is not NULL. */ +extern int pthread_tryjoin_np (pthread_t __th, void **__thread_return) __THROW; + +/* Make calling thread wait for termination of the thread TH, but only + until TIMEOUT. The exit status of the thread is stored in + *THREAD_RETURN, if THREAD_RETURN is not NULL. + + This function is a cancellation point and therefore not marked with + __THROW. */ +extern int pthread_timedjoin_np (pthread_t __th, void **__thread_return, + const struct timespec *__abstime); + +/* Make calling thread wait for termination of the thread TH, but only + until TIMEOUT measured against the clock specified by CLOCKID. The + exit status of the thread is stored in *THREAD_RETURN, if + THREAD_RETURN is not NULL. + + This function is a cancellation point and therefore not marked with + __THROW. */ +extern int pthread_clockjoin_np (pthread_t __th, void **__thread_return, + clockid_t __clockid, + const struct timespec *__abstime); +#endif + /* Indicate that the storage for THREAD can be reclaimed when it terminates. */ extern int pthread_detach (pthread_t __threadp); @@ -401,6 +427,13 @@ extern int pthread_mutex_timedlock (struct __pthread_mutex *__restrict __mutex, __THROWNL __nonnull ((1, 2)); #endif +#ifdef __USE_GNU +extern int pthread_mutex_clocklock (pthread_mutex_t *__restrict __mutex, + clockid_t __clockid, + const struct timespec *__restrict + __abstime) __THROWNL __nonnull ((1, 3)); +#endif + /* Unlock MUTEX. */ extern int pthread_mutex_unlock (pthread_mutex_t *__mutex) __THROWNL __nonnull ((1)); @@ -517,6 +550,21 @@ extern int pthread_cond_timedwait (pthread_cond_t *__restrict __cond, pthread_mutex_t *__restrict __mutex, __const struct timespec *__restrict __abstime) __nonnull ((1, 2, 3)); + +# ifdef __USE_GNU +/* Wait for condition variable COND to be signaled or broadcast until + ABSTIME measured by the specified clock. MUTEX is assumed to be + locked before. CLOCK is the clock to use. ABSTIME is an absolute + time specification against CLOCK's epoch. + + This function is a cancellation point and therefore not marked with + __THROW. */ +extern int pthread_cond_clockwait (pthread_cond_t *__restrict __cond, + pthread_mutex_t *__restrict __mutex, + __clockid_t __clock_id, + const struct timespec *__restrict __abstime) + __nonnull ((1, 2, 4)); +# endif /* Spin locks. */ @@ -623,6 +671,13 @@ extern int pthread_rwlock_timedrdlock (struct __pthread_rwlock *__restrict __rwl __THROWNL __nonnull ((1, 2)); # endif +# ifdef __USE_GNU +extern int pthread_rwlock_clockrdlock (pthread_rwlock_t *__restrict __rwlock, + clockid_t __clockid, + const struct timespec *__restrict + __abstime) __THROWNL __nonnull ((1, 3)); +# endif + /* Acquire the rwlock *RWLOCK for writing. */ extern int pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock) __THROWNL __nonnull ((1)); @@ -639,6 +694,13 @@ extern int pthread_rwlock_timedwrlock (struct __pthread_rwlock *__restrict __rwl __THROWNL __nonnull ((1, 2)); # endif +# ifdef __USE_GNU +extern int pthread_rwlock_clockwrlock (pthread_rwlock_t *__restrict __rwlock, + clockid_t __clockid, + const struct timespec *__restrict + __abstime) __THROWNL __nonnull ((1, 3)); +# endif + /* Release the lock held by the current thread on *RWLOCK. */ extern int pthread_rwlock_unlock (pthread_rwlock_t *__rwlock) __THROWNL __nonnull ((1)); diff --git a/sysdeps/htl/pthreadP.h b/sysdeps/htl/pthreadP.h index 1726ebb122..7486c9383e 100644 --- a/sysdeps/htl/pthreadP.h +++ b/sysdeps/htl/pthreadP.h @@ -50,6 +50,11 @@ extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex); extern int __pthread_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime); +extern int __pthread_cond_clockwait (pthread_cond_t *cond, + pthread_mutex_t *mutex, + clockid_t clockid, + const struct timespec *abstime) + __nonnull ((1, 2, 4)); extern int __pthread_cond_destroy (pthread_cond_t *cond); typedef struct __cthread *__cthread_t; diff --git a/sysdeps/mach/hurd/htl/pt-mutex-timedlock.c b/sysdeps/mach/hurd/htl/pt-mutex-timedlock.c index b11bfc87c8..198b340429 100644 --- a/sysdeps/mach/hurd/htl/pt-mutex-timedlock.c +++ b/sysdeps/mach/hurd/htl/pt-mutex-timedlock.c @@ -24,7 +24,9 @@ #include int -__pthread_mutex_timedlock (pthread_mutex_t *mtxp, const struct timespec *tsp) +__pthread_mutex_clocklock (pthread_mutex_t *mtxp, + clockid_t clockid, + const struct timespec *tsp) { struct __pthread *self; int ret, flags = mtxp->__flags & GSYNC_SHARED; @@ -32,7 +34,7 @@ __pthread_mutex_timedlock (pthread_mutex_t *mtxp, const struct timespec *tsp) switch (MTX_TYPE (mtxp)) { case PT_MTX_NORMAL: - ret = lll_abstimed_lock (&mtxp->__lock, tsp, flags); + ret = lll_abstimed_lock (&mtxp->__lock, tsp, flags, clockid); break; case PT_MTX_RECURSIVE: @@ -45,7 +47,7 @@ __pthread_mutex_timedlock (pthread_mutex_t *mtxp, const struct timespec *tsp) ++mtxp->__cnt; ret = 0; } - else if ((ret = lll_abstimed_lock (&mtxp->__lock, tsp, flags)) == 0) + else if ((ret = lll_abstimed_lock (&mtxp->__lock, tsp, flags, clockid)) == 0) { mtx_set_owner (mtxp, self, flags); mtxp->__cnt = 1; @@ -57,7 +59,7 @@ __pthread_mutex_timedlock (pthread_mutex_t *mtxp, const struct timespec *tsp) self = _pthread_self (); if (mtx_owned_p (mtxp, self, flags)) ret = EDEADLK; - else if ((ret = lll_abstimed_lock (&mtxp->__lock, tsp, flags)) == 0) + else if ((ret = lll_abstimed_lock (&mtxp->__lock, tsp, flags, clockid)) == 0) mtx_set_owner (mtxp, self, flags); break; @@ -66,7 +68,7 @@ __pthread_mutex_timedlock (pthread_mutex_t *mtxp, const struct timespec *tsp) case PT_MTX_RECURSIVE | PTHREAD_MUTEX_ROBUST: case PT_MTX_ERRORCHECK | PTHREAD_MUTEX_ROBUST: self = _pthread_self (); - ROBUST_LOCK (self, mtxp, lll_robust_abstimed_lock, tsp, flags); + ROBUST_LOCK (self, mtxp, lll_robust_abstimed_lock, tsp, flags, clockid); break; default: @@ -76,5 +78,13 @@ __pthread_mutex_timedlock (pthread_mutex_t *mtxp, const struct timespec *tsp) return ret; } +weak_alias (__pthread_mutex_clocklock, pthread_mutex_clocklock) + +int +__pthread_mutex_timedlock (pthread_mutex_t *mutex, + const struct timespec *tsp) +{ + return __pthread_mutex_clocklock (mutex, CLOCK_REALTIME, tsp); +} weak_alias (__pthread_mutex_timedlock, pthread_mutex_timedlock) hidden_def (__pthread_mutex_timedlock) diff --git a/sysdeps/mach/hurd/i386/libpthread.abilist b/sysdeps/mach/hurd/i386/libpthread.abilist index 6e75c90009..a1026f0d1e 100644 --- a/sysdeps/mach/hurd/i386/libpthread.abilist +++ b/sysdeps/mach/hurd/i386/libpthread.abilist @@ -155,12 +155,19 @@ GLIBC_2.32 mtx_lock F GLIBC_2.32 mtx_timedlock F GLIBC_2.32 mtx_trylock F GLIBC_2.32 mtx_unlock F +GLIBC_2.32 pthread_clockjoin_np F +GLIBC_2.32 pthread_cond_clockwait F +GLIBC_2.32 pthread_mutex_clocklock F GLIBC_2.32 pthread_mutex_consistent F GLIBC_2.32 pthread_mutex_consistent_np F GLIBC_2.32 pthread_mutexattr_getrobust F GLIBC_2.32 pthread_mutexattr_getrobust_np F GLIBC_2.32 pthread_mutexattr_setrobust F GLIBC_2.32 pthread_mutexattr_setrobust_np F +GLIBC_2.32 pthread_rwlock_clockrdlock F +GLIBC_2.32 pthread_rwlock_clockwrlock F +GLIBC_2.32 pthread_timedjoin_np F +GLIBC_2.32 pthread_tryjoin_np F GLIBC_2.32 thrd_create F GLIBC_2.32 thrd_detach F GLIBC_2.32 thrd_exit F diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile index 2f8aff35b2..b8dcadd2db 100644 --- a/sysdeps/pthread/Makefile +++ b/sysdeps/pthread/Makefile @@ -51,17 +51,23 @@ tests += tst-cnd-basic tst-mtx-trylock tst-cnd-broadcast \ tst-cond14 tst-cond15 tst-cond16 tst-cond17 tst-cond18 tst-cond19 \ tst-cond23 tst-cond24 tst-cond25 \ tst-cond-except \ - tst-join1 tst-join4 tst-join5 tst-join6 tst-join7 \ + tst-join1 tst-join2 tst-join3 tst-join4 tst-join5 tst-join6 tst-join7 \ + tst-join8 tst-join9 tst-join10 tst-join11 tst-join12 tst-join13 \ + tst-join14 \ tst-key1 tst-key2 tst-key3 tst-key4 \ - tst-mutex1 tst-mutex2 tst-mutex3 tst-mutex4 tst-mutex6 tst-mutex10 \ + tst-mutex-errorcheck tst-mutex1 tst-mutex2 tst-mutex3 tst-mutex4 \ + tst-mutex5 tst-mutex6 tst-mutex7 tst-mutex7robust tst-mutex9 \ + tst-mutex10 tst-mutex11 tst-pthread-mutexattr \ tst-once1 tst-once2 tst-once3 tst-once4 \ tst-robust1 tst-robust2 tst-robust3 tst-robust4 tst-robust5 \ tst-robust6 tst-robust7 tst-robust9 tst-robust10 \ - tst-rwlock1 tst-rwlock4 tst-rwlock5 tst-rwlock13 tst-rwlock16 \ + tst-rwlock1 tst-rwlock4 tst-rwlock5 tst-rwlock12 \ + tst-rwlock13 tst-rwlock14 tst-rwlock16 \ tst-rwlock-tryrdlock-stall tst-rwlock-trywrlock-stall \ tst-sem1 tst-sem2 tst-sem3 tst-sem4 tst-sem6 tst-sem7 \ tst-sem8 tst-sem9 tst-sem10 tst-sem14 tst-sem15 tst-sem16 \ - tst-spin1 tst-spin2 tst-spin3 tst-spin4 + tst-spin1 tst-spin2 tst-spin3 tst-spin4 \ + tst-abstime tests-internal += tst-robust8 diff --git a/sysdeps/pthread/tst-abstime.c b/sysdeps/pthread/tst-abstime.c new file mode 100644 index 0000000000..90ed9194e3 --- /dev/null +++ b/sysdeps/pthread/tst-abstime.c @@ -0,0 +1,73 @@ +/* Copyright (C) 2010-2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Andreas Schwab , 2010. + + 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 + . */ + +#include +#include +#include +#include +#include +#include + +static pthread_cond_t c = PTHREAD_COND_INITIALIZER; +static pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER; +static pthread_rwlock_t rw1 = PTHREAD_RWLOCK_INITIALIZER; +static pthread_rwlock_t rw2 = PTHREAD_RWLOCK_INITIALIZER; +static sem_t sem; + +static void * +th (void *arg) +{ + struct timespec t = { -2, 0 }; + + TEST_COMPARE (pthread_mutex_timedlock (&m1, &t), ETIMEDOUT); + TEST_COMPARE (pthread_mutex_clocklock (&m1, CLOCK_REALTIME, &t), ETIMEDOUT); + TEST_COMPARE (pthread_mutex_clocklock (&m1, CLOCK_MONOTONIC, &t), ETIMEDOUT); + TEST_COMPARE (pthread_rwlock_timedrdlock (&rw1, &t), ETIMEDOUT); + TEST_COMPARE (pthread_rwlock_timedwrlock (&rw2, &t), ETIMEDOUT); + TEST_COMPARE (pthread_rwlock_clockrdlock (&rw1, CLOCK_REALTIME, &t), + ETIMEDOUT); + TEST_COMPARE (pthread_rwlock_clockwrlock (&rw2, CLOCK_REALTIME, &t), + ETIMEDOUT); + TEST_COMPARE (pthread_rwlock_clockrdlock (&rw1, CLOCK_MONOTONIC, &t), + ETIMEDOUT); + TEST_COMPARE (pthread_rwlock_clockwrlock (&rw2, CLOCK_MONOTONIC, &t), + ETIMEDOUT); + return NULL; +} + +static int +do_test (void) +{ + struct timespec t = { -2, 0 }; + + sem_init (&sem, 0, 0); + TEST_COMPARE (sem_timedwait (&sem, &t), -1); + TEST_COMPARE (errno, ETIMEDOUT); + + xpthread_mutex_lock (&m1); + xpthread_rwlock_wrlock (&rw1); + xpthread_rwlock_rdlock (&rw2); + xpthread_mutex_lock (&m2); + pthread_t pth = xpthread_create (0, th, 0); + TEST_COMPARE (pthread_cond_timedwait (&c, &m2, &t), ETIMEDOUT); + xpthread_join (pth); + return 0; +} + +#include diff --git a/sysdeps/pthread/tst-join10.c b/sysdeps/pthread/tst-join10.c new file mode 100644 index 0000000000..6e94c00f75 --- /dev/null +++ b/sysdeps/pthread/tst-join10.c @@ -0,0 +1,20 @@ +/* Check if pthread_clockjoin_np is a cancellation entrypoint. + Copyright (C) 2019-2020 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 + . */ + +#define USE_PTHREAD_CLOCKJOIN_NP_REALTIME 1 +#include diff --git a/sysdeps/pthread/tst-join11.c b/sysdeps/pthread/tst-join11.c new file mode 100644 index 0000000000..ed8312bc6b --- /dev/null +++ b/sysdeps/pthread/tst-join11.c @@ -0,0 +1,21 @@ +/* Check if pthread_clockjoin_np is a cancellation entrypoint. + Copyright (C) 2019-2020 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 + . */ + +#define USE_PTHREAD_CLOCKJOIN_NP_REALTIME 1 +#define WAIT_IN_CHILD 1 +#include diff --git a/sysdeps/pthread/tst-join12.c b/sysdeps/pthread/tst-join12.c new file mode 100644 index 0000000000..57d713c82d --- /dev/null +++ b/sysdeps/pthread/tst-join12.c @@ -0,0 +1,20 @@ +/* Check if pthread_clockjoin_np is a cancellation entrypoint. + Copyright (C) 2019-2020 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 + . */ + +#define USE_PTHREAD_CLOCKJOIN_NP_MONOTONIC 1 +#include diff --git a/sysdeps/pthread/tst-join13.c b/sysdeps/pthread/tst-join13.c new file mode 100644 index 0000000000..61a9106ebf --- /dev/null +++ b/sysdeps/pthread/tst-join13.c @@ -0,0 +1,21 @@ +/* Check if pthread_clockjoin_np is a cancellation entrypoint. + Copyright (C) 2019-2020 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 + . */ + +#define USE_PTHREAD_CLOCKJOIN_NP_MONOTONIC 1 +#define WAIT_IN_CHILD 1 +#include diff --git a/sysdeps/pthread/tst-join14.c b/sysdeps/pthread/tst-join14.c new file mode 100644 index 0000000000..0109324453 --- /dev/null +++ b/sysdeps/pthread/tst-join14.c @@ -0,0 +1,74 @@ +/* pthread_timedjoin_np, pthread_clockjoin_np NULL timeout test. + Copyright (C) 2019-2020 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 + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define CLOCK_USE_TIMEDJOIN (-1) + + +static void * +tf (void *arg) +{ + struct timespec ts = make_timespec(0, 100000); + nanosleep(&ts, NULL); + + return (void *) 42l; +} + + +/* Check that pthread_timedjoin_np and pthread_clockjoin_np wait "forever" if + * passed a timeout parameter of NULL. We can't actually wait forever, but we + * can be sure that we did at least wait for some time by checking the exit + * status of the thread. */ +static int +do_test_clock (clockid_t clockid) +{ + pthread_t th = xpthread_create (NULL, tf, NULL); + + void *status; + int val = (clockid == CLOCK_USE_TIMEDJOIN) + ? pthread_timedjoin_np (th, &status, NULL) + : pthread_clockjoin_np (th, &status, clockid, NULL); + TEST_COMPARE (val, 0); + + if (status != (void *) 42l) + FAIL_EXIT1 ("return value %p, expected %p\n", status, (void *) 42l); + + return 0; +} + +static int +do_test (void) +{ + do_test_clock (CLOCK_USE_TIMEDJOIN); + do_test_clock (CLOCK_REALTIME); + do_test_clock (CLOCK_MONOTONIC); + return 0; +} + +#include diff --git a/sysdeps/pthread/tst-join2.c b/sysdeps/pthread/tst-join2.c new file mode 100644 index 0000000000..22dfd11b82 --- /dev/null +++ b/sysdeps/pthread/tst-join2.c @@ -0,0 +1,103 @@ +/* Copyright (C) 2002-2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 2002. + + 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 + . */ + +#include +#include +#include +#include +#include +#include + + +static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; + + +static void * +tf (void *arg) +{ + if (pthread_mutex_lock (&lock) != 0) + { + puts ("child: mutex_lock failed"); + return NULL; + } + + return (void *) 42l; +} + + +static int +do_test (void) +{ + pthread_t th; + + if (pthread_mutex_lock (&lock) != 0) + { + puts ("mutex_lock failed"); + exit (1); + } + + if (pthread_create (&th, NULL, tf, NULL) != 0) + { + puts ("mutex_create failed"); + exit (1); + } + + void *status; + int val = pthread_tryjoin_np (th, &status); + if (val == 0) + { + puts ("1st tryjoin succeeded"); + exit (1); + } + else if (val != EBUSY) + { + puts ("1st tryjoin didn't return EBUSY"); + exit (1); + } + + if (pthread_mutex_unlock (&lock) != 0) + { + puts ("mutex_unlock failed"); + exit (1); + } + + while ((val = pthread_tryjoin_np (th, &status)) != 0) + { + if (val != EBUSY) + { + printf ("tryjoin returned %s (%d), expected only 0 or EBUSY\n", + strerror (val), val); + exit (1); + } + + /* Delay minimally. */ + struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 }; + nanosleep (&ts, NULL); + } + + if (status != (void *) 42l) + { + printf ("return value %p, expected %p\n", status, (void *) 42l); + exit (1); + } + + return 0; +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c" diff --git a/sysdeps/pthread/tst-join3.c b/sysdeps/pthread/tst-join3.c new file mode 100644 index 0000000000..ffebcf586f --- /dev/null +++ b/sysdeps/pthread/tst-join3.c @@ -0,0 +1,99 @@ +/* Copyright (C) 2002-2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 2002. + + 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 + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define CLOCK_USE_TIMEDJOIN (-1) + +static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; + + +static void * +tf (void *arg) +{ + xpthread_mutex_lock (&lock); + xpthread_mutex_unlock (&lock); + + return (void *) 42l; +} + + +static int +do_test_clock (clockid_t clockid) +{ + const clockid_t clockid_for_get = + (clockid == CLOCK_USE_TIMEDJOIN) ? CLOCK_REALTIME : clockid; + + xpthread_mutex_lock (&lock); + pthread_t th = xpthread_create (NULL, tf, NULL); + + void *status; + struct timespec timeout = timespec_add (xclock_now (clockid_for_get), + make_timespec (0, 200000000)); + + int val; + if (clockid == CLOCK_USE_TIMEDJOIN) + val = pthread_timedjoin_np (th, &status, &timeout); + else + val = pthread_clockjoin_np (th, &status, clockid, &timeout); + + TEST_COMPARE (val, ETIMEDOUT); + + xpthread_mutex_unlock (&lock); + + while (1) + { + timeout = timespec_add (xclock_now (clockid_for_get), + make_timespec (0, 200000000)); + + if (clockid == CLOCK_USE_TIMEDJOIN) + val = pthread_timedjoin_np (th, &status, &timeout); + else + val = pthread_clockjoin_np (th, &status, clockid, &timeout); + if (val == 0) + break; + + TEST_COMPARE (val, ETIMEDOUT); + } + + if (status != (void *) 42l) + FAIL_EXIT1 ("return value %p, expected %p\n", status, (void *) 42l); + + return 0; +} + +static int +do_test (void) +{ + do_test_clock (CLOCK_USE_TIMEDJOIN); + do_test_clock (CLOCK_REALTIME); + do_test_clock (CLOCK_MONOTONIC); + return 0; +} + +#include diff --git a/sysdeps/pthread/tst-join8.c b/sysdeps/pthread/tst-join8.c new file mode 100644 index 0000000000..3eddabab6f --- /dev/null +++ b/sysdeps/pthread/tst-join8.c @@ -0,0 +1,20 @@ +/* Check if pthread_timedjoin_np is a cancellation entrypoint. + Copyright (C) 2019-2020 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 + . */ + +#define USE_PTHREAD_TIMEDJOIN_NP 1 +#include diff --git a/sysdeps/pthread/tst-join9.c b/sysdeps/pthread/tst-join9.c new file mode 100644 index 0000000000..e9d0f26146 --- /dev/null +++ b/sysdeps/pthread/tst-join9.c @@ -0,0 +1,21 @@ +/* Check if pthread_timedjoin_np is a cancellation entrypoint. + Copyright (C) 2019-2020 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 + . */ + +#define USE_PTHREAD_TIMEDJOIN_NP 1 +#define WAIT_IN_CHILD 1 +#include diff --git a/sysdeps/pthread/tst-mutex-errorcheck.c b/sysdeps/pthread/tst-mutex-errorcheck.c new file mode 100644 index 0000000000..cb140f94a0 --- /dev/null +++ b/sysdeps/pthread/tst-mutex-errorcheck.c @@ -0,0 +1,52 @@ +/* Check that error checking mutexes are not subject to lock elision. + Copyright (C) 2016-2020 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 + . */ + +#include +#include +#include +#include +#include + +static int +do_test (void) +{ + struct timespec tms = { 0 }; + pthread_mutex_t mutex; + pthread_mutexattr_t mutexattr; + int ret = 0; + + TEST_COMPARE (pthread_mutexattr_init (&mutexattr), 0); + TEST_COMPARE (pthread_mutexattr_settype (&mutexattr, + PTHREAD_MUTEX_ERRORCHECK), 0); + + TEST_COMPARE (pthread_mutex_init (&mutex, &mutexattr), 0); + TEST_COMPARE (pthread_mutexattr_destroy (&mutexattr), 0); + + /* The call to pthread_mutex_timedlock erroneously enabled lock elision + on the mutex, which then triggered an assertion failure in + pthread_mutex_unlock. It would also defeat the error checking nature + of the mutex. */ + TEST_COMPARE (pthread_mutex_timedlock (&mutex, &tms), 0); + TEST_COMPARE (pthread_mutex_timedlock (&mutex, &tms), EDEADLK); + + TEST_COMPARE (pthread_mutex_unlock (&mutex), 0); + + return ret; +} + +#include diff --git a/sysdeps/pthread/tst-mutex11.c b/sysdeps/pthread/tst-mutex11.c new file mode 100644 index 0000000000..2685f9e8bc --- /dev/null +++ b/sysdeps/pthread/tst-mutex11.c @@ -0,0 +1,69 @@ +/* Test unsupported/bad clocks passed to pthread_mutex_clocklock. + + Copyright (C) 2019-2020 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 + . */ + +#include +#include +#include +#include +#include +#include + +static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; + +static void test_bad_clockid (clockid_t clockid) +{ + const struct timespec ts = {0,0}; + TEST_COMPARE (pthread_mutex_clocklock (&mut, clockid, &ts), EINVAL); +} + +#define NOT_A_VALID_CLOCK 123456 + +static int +do_test (void) +{ + /* These clocks are meaningless to pthread_mutex_clocklock. */ +#if defined(CLOCK_PROCESS_CPUTIME_ID) + test_bad_clockid (CLOCK_PROCESS_CPUTIME_ID); +#endif +#if defined(CLOCK_THREAD_CPUTIME_ID) + test_bad_clockid (CLOCK_PROCESS_CPUTIME_ID); +#endif + + /* These clocks might be meaningful, but are currently unsupported by + pthread_mutex_clocklock. */ +#if defined(CLOCK_REALTIME_COARSE) + test_bad_clockid (CLOCK_REALTIME_COARSE); +#endif +#if defined(CLOCK_MONOTONIC_RAW) + test_bad_clockid (CLOCK_MONOTONIC_RAW); +#endif +#if defined(CLOCK_MONOTONIC_COARSE) + test_bad_clockid (CLOCK_MONOTONIC_COARSE); +#endif +#if defined(CLOCK_BOOTTIME) + test_bad_clockid (CLOCK_BOOTTIME); +#endif + + /* This is a completely invalid clock. */ + test_bad_clockid (NOT_A_VALID_CLOCK); + + return 0; +} + +#include diff --git a/sysdeps/pthread/tst-mutex5.c b/sysdeps/pthread/tst-mutex5.c new file mode 100644 index 0000000000..14490768c3 --- /dev/null +++ b/sysdeps/pthread/tst-mutex5.c @@ -0,0 +1,119 @@ +/* Copyright (C) 2002-2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 2002. + + 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 + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#ifndef TYPE +# define TYPE PTHREAD_MUTEX_NORMAL +#endif + +/* A bogus clock value that tells run_test to use + pthread_mutex_timedlock rather than pthread_mutex_clocklock. */ +#define CLOCK_USE_TIMEDLOCK (-1) + +static int +do_test_clock (clockid_t clockid, const char *fnname) +{ + pthread_mutex_t m; + pthread_mutexattr_t a; + const clockid_t clockid_for_get = + (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid; + + TEST_COMPARE (pthread_mutexattr_init (&a), 0); + TEST_COMPARE (pthread_mutexattr_settype (&a, TYPE), 0); + +#ifdef ENABLE_PI + TEST_COMPARE (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT), 0); +#endif + + int err = pthread_mutex_init (&m, &a); + if (err != 0) + { +#ifdef ENABLE_PI + if (err == ENOTSUP) + FAIL_UNSUPPORTED ("PI mutexes unsupported"); +#endif + FAIL_EXIT1 ("mutex_init failed"); + } + + TEST_COMPARE (pthread_mutexattr_destroy (&a), 0); + TEST_COMPARE (pthread_mutex_lock (&m), 0); + if (pthread_mutex_trylock (&m) == 0) + FAIL_EXIT1 ("mutex_trylock succeeded"); + + /* Wait 2 seconds. */ + struct timespec ts_timeout = timespec_add (xclock_now (clockid_for_get), + make_timespec (2, 0)); + + if (clockid == CLOCK_USE_TIMEDLOCK) + TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), ETIMEDOUT); + else + TEST_COMPARE (pthread_mutex_clocklock (&m, clockid, &ts_timeout), + ETIMEDOUT); + TEST_TIMESPEC_BEFORE_NOW (ts_timeout, clockid_for_get); + + /* The following makes the ts value invalid. */ + ts_timeout.tv_nsec += 1000000000; + + if (clockid == CLOCK_USE_TIMEDLOCK) + TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), EINVAL); + else + TEST_COMPARE (pthread_mutex_clocklock (&m, clockid, &ts_timeout), EINVAL); + TEST_COMPARE (pthread_mutex_unlock (&m), 0); + + const struct timespec ts_start = xclock_now (CLOCK_REALTIME); + + /* Wait 2 seconds. */ + ts_timeout = timespec_add (ts_start, make_timespec (2, 0)); + + if (clockid == CLOCK_USE_TIMEDLOCK) + TEST_COMPARE (pthread_mutex_timedlock (&m, &ts_timeout), 0); + else + TEST_COMPARE (pthread_mutex_clocklock (&m, clockid, &ts_timeout), 0); + + const struct timespec ts_end = xclock_now (clockid_for_get); + + /* Check that timedlock didn't delay. We use a limit of 0.1 secs. */ + TEST_TIMESPEC_BEFORE (ts_end, + timespec_add (ts_start, make_timespec (0, 100000000))); + + TEST_COMPARE (pthread_mutex_unlock (&m), 0); + TEST_COMPARE (pthread_mutex_destroy (&m), 0); + + return 0; +} + +static int do_test (void) +{ + do_test_clock (CLOCK_USE_TIMEDLOCK, "timedlock"); + do_test_clock (CLOCK_REALTIME, "clocklock(realtime)"); + do_test_clock (CLOCK_MONOTONIC, "clocklock(monotonic)"); + return 0; +} + +#include diff --git a/sysdeps/pthread/tst-mutex7.c b/sysdeps/pthread/tst-mutex7.c new file mode 100644 index 0000000000..3938670b54 --- /dev/null +++ b/sysdeps/pthread/tst-mutex7.c @@ -0,0 +1,189 @@ +/* Copyright (C) 2002-2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 2002. + + 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 + . */ + +#include +#include +#include +#include +#include + +/* This test is a template for other tests to use. Other tests define + the following macros to change the behaviour of the template test. + The test is very simple, it configures N threads given the parameters + below and then proceeds to go through mutex lock and unlock + operations in each thread as described before for the thread + function. */ +#ifndef TYPE +# define TYPE PTHREAD_MUTEX_DEFAULT +#endif +#ifndef ROBUST +# define ROBUST PTHREAD_MUTEX_STALLED +#endif +#ifndef DELAY_NSEC +# define DELAY_NSEC 11000 +#endif +#ifndef ROUNDS +# define ROUNDS 1000 +#endif +#ifndef N +# define N 100 +#endif + +static pthread_mutex_t lock; + +/* Each thread locks and the subsequently unlocks the lock, yielding + the smallest critical section possible. After the unlock the thread + waits DELAY_NSEC nanoseconds before doing the lock and unlock again. + Every thread does this ROUNDS times. The lock and unlock are + checked for errors. */ +static void * +tf (void *arg) +{ + int nr = (long int) arg; + int cnt; + struct timespec ts = { .tv_sec = 0, .tv_nsec = DELAY_NSEC }; + + for (cnt = 0; cnt < ROUNDS; ++cnt) + { + if (pthread_mutex_lock (&lock) != 0) + { + printf ("thread %d: failed to get the lock\n", nr); + return (void *) 1l; + } + + if (pthread_mutex_unlock (&lock) != 0) + { + printf ("thread %d: failed to release the lock\n", nr); + return (void *) 1l; + } + + if ((ts.tv_sec > 0) || (ts.tv_nsec > 0)) + nanosleep (&ts, NULL); + } + + return NULL; +} + +/* Setup and run N threads, where each thread does as described + in the above thread function. The threads are given a minimal 1MiB + stack since they don't do anything between the lock and unlock. */ +static int +do_test (void) +{ + pthread_mutexattr_t a; + + if (pthread_mutexattr_init (&a) != 0) + { + puts ("mutexattr_init failed"); + exit (1); + } + + if (pthread_mutexattr_settype (&a, TYPE) != 0) + { + puts ("mutexattr_settype failed"); + exit (1); + } + + if (pthread_mutexattr_setrobust (&a, ROBUST) != 0) + { + puts ("mutexattr_setrobust failed"); + exit (1); + } + +#ifdef ENABLE_PI + if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0) + { + puts ("pthread_mutexattr_setprotocol failed"); + return 1; + } +#endif + + int e = pthread_mutex_init (&lock, &a); + if (e != 0) + { +#ifdef ENABLE_PI + if (e == ENOTSUP) + { + puts ("PI mutexes unsupported"); + return 0; + } +#endif + puts ("mutex_init failed"); + return 1; + } + + if (pthread_mutexattr_destroy (&a) != 0) + { + puts ("mutexattr_destroy failed"); + return 1; + } + + pthread_attr_t at; + pthread_t th[N]; + int cnt; + + if (pthread_attr_init (&at) != 0) + { + puts ("attr_init failed"); + return 1; + } + + if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0) + { + puts ("attr_setstacksize failed"); + return 1; + } + + if (pthread_mutex_lock (&lock) != 0) + { + puts ("locking in parent failed"); + return 1; + } + + for (cnt = 0; cnt < N; ++cnt) + if (pthread_create (&th[cnt], &at, tf, (void *) (long int) cnt) != 0) + { + printf ("creating thread %d failed\n", cnt); + return 1; + } + + if (pthread_attr_destroy (&at) != 0) + { + puts ("attr_destroy failed"); + return 1; + } + + if (pthread_mutex_unlock (&lock) != 0) + { + puts ("unlocking in parent failed"); + return 1; + } + + for (cnt = 0; cnt < N; ++cnt) + if (pthread_join (th[cnt], NULL) != 0) + { + printf ("joining thread %d failed\n", cnt); + return 1; + } + + return 0; +} + +#define TIMEOUT 60 +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c" diff --git a/sysdeps/pthread/tst-mutex7robust.c b/sysdeps/pthread/tst-mutex7robust.c new file mode 100644 index 0000000000..8221a61d29 --- /dev/null +++ b/sysdeps/pthread/tst-mutex7robust.c @@ -0,0 +1,7 @@ +/* Bug 21778: Fix oversight in robust mutex lock acquisition. */ +#define TYPE PTHREAD_MUTEX_NORMAL +#define ROBUST PTHREAD_MUTEX_ROBUST +#define DELAY_NSEC 0 +#define ROUNDS 1000 +#define N 32 +#include "tst-mutex7.c" diff --git a/sysdeps/pthread/tst-mutex8.c b/sysdeps/pthread/tst-mutex8.c deleted file mode 100644 index 8e56ea5a3a..0000000000 --- a/sysdeps/pthread/tst-mutex8.c +++ /dev/null @@ -1,435 +0,0 @@ -/* Copyright (C) 2003-2020 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Ulrich Drepper , 2003. - - 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 - . */ - -/* This test checks behavior not required by POSIX. */ -#include -#include -#include -#include -#include -#include -#include - -static pthread_mutex_t *m; -static pthread_barrier_t b; -static pthread_cond_t c; -static bool done; - - -static void -cl (void *arg) -{ - if (pthread_mutex_unlock (m) != 0) - { - puts ("cl: mutex_unlocked failed"); - exit (1); - } -} - - -static void * -tf (void *arg) -{ - if (pthread_mutex_lock (m) != 0) - { - puts ("tf: mutex_lock failed"); - return (void *) 1l; - } - - int e = pthread_barrier_wait (&b); - if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) - { - puts ("barrier_wait failed"); - return (void *) 1l; - } - - if (arg == NULL) - do - if (pthread_cond_wait (&c, m) != 0) - { - puts ("tf: cond_wait failed"); - return (void *) 1l; - } - while (! done); - else - do - { - pthread_cleanup_push (cl, NULL); - - if (pthread_cond_wait (&c, m) != 0) - { - puts ("tf: cond_wait failed"); - return (void *) 1l; - } - - pthread_cleanup_pop (0); - } - while (! done); - - if (pthread_mutex_unlock (m) != 0) - { - puts ("tf: mutex_unlock failed"); - return (void *) 1l; - } - - return NULL; -} - - -static int -check_type (const char *mas, pthread_mutexattr_t *ma) -{ - int e; - - /* Check if a mutex will be elided. Lock elision can only be activated via - the tunables framework. By default, lock elision is disabled. */ - bool assume_elided_mutex = false; -#if HAVE_TUNABLES - int ma_type = PTHREAD_MUTEX_TIMED_NP; - if (ma != NULL) - { - e = pthread_mutexattr_gettype (ma, &ma_type); - if (e != 0) - { - printf ("pthread_mutexattr_gettype failed with %d (%m)\n", e); - return 1; - } - } - if (ma_type == PTHREAD_MUTEX_TIMED_NP) - { - /* This type of mutex can be elided if elision is enabled via the tunables - framework. Some tests below are failing if the mutex is elided. - Thus we only run those if we assume that the mutex won't be elided. */ - if (TUNABLE_GET_FULL (glibc, elision, enable, int32_t, NULL) == 1) - assume_elided_mutex = true; - } -#endif - - e = pthread_mutex_init (m, ma); - if (e != 0) - { -#ifdef ENABLE_PI - if (e == ENOTSUP) - { - puts ("PI mutexes unsupported"); - return 0; - } -#endif - printf ("1st mutex_init failed for %s\n", mas); - return 1; - } - - if (pthread_mutex_destroy (m) != 0) - { - printf ("immediate mutex_destroy failed for %s\n", mas); - return 1; - } - - if (pthread_mutex_init (m, ma) != 0) - { - printf ("2nd mutex_init failed for %s\n", mas); - return 1; - } - - if (pthread_mutex_lock (m) != 0) - { - printf ("1st mutex_lock failed for %s\n", mas); - return 1; - } - - /* Elided mutexes don't fail destroy, thus only test this if we don't assume - elision. */ - if (assume_elided_mutex == false) - { - e = pthread_mutex_destroy (m); - if (e == 0) - { - printf ("mutex_destroy of self-locked mutex succeeded for %s\n", mas); - return 1; - } - if (e != EBUSY) - { - printf ("\ -mutex_destroy of self-locked mutex did not return EBUSY %s\n", - mas); - return 1; - } - } - - if (pthread_mutex_unlock (m) != 0) - { - printf ("1st mutex_unlock failed for %s\n", mas); - return 1; - } - - if (pthread_mutex_trylock (m) != 0) - { - printf ("mutex_trylock failed for %s\n", mas); - return 1; - } - - /* Elided mutexes don't fail destroy. */ - if (assume_elided_mutex == false) - { - e = pthread_mutex_destroy (m); - if (e == 0) - { - printf ("mutex_destroy of self-trylocked mutex succeeded for %s\n", - mas); - return 1; - } - if (e != EBUSY) - { - printf ("\ -mutex_destroy of self-trylocked mutex did not return EBUSY %s\n", - mas); - return 1; - } - } - - if (pthread_mutex_unlock (m) != 0) - { - printf ("2nd mutex_unlock failed for %s\n", mas); - return 1; - } - - pthread_t th; - if (pthread_create (&th, NULL, tf, NULL) != 0) - { - puts ("1st create failed"); - return 1; - } - done = false; - - e = pthread_barrier_wait (&b); - if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) - { - puts ("1st barrier_wait failed"); - return 1; - } - - if (pthread_mutex_lock (m) != 0) - { - printf ("2nd mutex_lock failed for %s\n", mas); - return 1; - } - - if (pthread_mutex_unlock (m) != 0) - { - printf ("3rd mutex_unlock failed for %s\n", mas); - return 1; - } - - /* Elided mutexes don't fail destroy. */ - if (assume_elided_mutex == false) - { - e = pthread_mutex_destroy (m); - if (e == 0) - { - printf ("mutex_destroy of condvar-used mutex succeeded for %s\n", - mas); - return 1; - } - if (e != EBUSY) - { - printf ("\ -mutex_destroy of condvar-used mutex did not return EBUSY for %s\n", mas); - return 1; - } - } - - done = true; - if (pthread_cond_signal (&c) != 0) - { - puts ("cond_signal failed"); - return 1; - } - - void *r; - if (pthread_join (th, &r) != 0) - { - puts ("join failed"); - return 1; - } - if (r != NULL) - { - puts ("thread didn't return NULL"); - return 1; - } - - if (pthread_mutex_destroy (m) != 0) - { - printf ("mutex_destroy after condvar-use failed for %s\n", mas); - return 1; - } - - if (pthread_mutex_init (m, ma) != 0) - { - printf ("3rd mutex_init failed for %s\n", mas); - return 1; - } - - if (pthread_create (&th, NULL, tf, (void *) 1) != 0) - { - puts ("2nd create failed"); - return 1; - } - done = false; - - e = pthread_barrier_wait (&b); - if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) - { - puts ("2nd barrier_wait failed"); - return 1; - } - - if (pthread_mutex_lock (m) != 0) - { - printf ("3rd mutex_lock failed for %s\n", mas); - return 1; - } - - if (pthread_mutex_unlock (m) != 0) - { - printf ("4th mutex_unlock failed for %s\n", mas); - return 1; - } - - /* Elided mutexes don't fail destroy. */ - if (assume_elided_mutex == false) - { - e = pthread_mutex_destroy (m); - if (e == 0) - { - printf ("2nd mutex_destroy of condvar-used mutex succeeded for %s\n", - mas); - return 1; - } - if (e != EBUSY) - { - printf ("\ -2nd mutex_destroy of condvar-used mutex did not return EBUSY for %s\n", - mas); - return 1; - } - } - - if (pthread_cancel (th) != 0) - { - puts ("cond_cancel failed"); - return 1; - } - - if (pthread_join (th, &r) != 0) - { - puts ("join failed"); - return 1; - } - if (r != PTHREAD_CANCELED) - { - puts ("thread not canceled"); - return 1; - } - - if (pthread_mutex_destroy (m) != 0) - { - printf ("mutex_destroy after condvar-canceled failed for %s\n", mas); - return 1; - } - - return 0; -} - - -static int -do_test (void) -{ - pthread_mutex_t mm; - m = &mm; - - if (pthread_barrier_init (&b, NULL, 2) != 0) - { - puts ("barrier_init failed"); - return 1; - } - - if (pthread_cond_init (&c, NULL) != 0) - { - puts ("cond_init failed"); - return 1; - } - - puts ("check normal mutex"); - int res = check_type ("normal", NULL); - - pthread_mutexattr_t ma; - if (pthread_mutexattr_init (&ma) != 0) - { - puts ("1st mutexattr_init failed"); - return 1; - } - if (pthread_mutexattr_settype (&ma, PTHREAD_MUTEX_RECURSIVE) != 0) - { - puts ("1st mutexattr_settype failed"); - return 1; - } -#ifdef ENABLE_PI - if (pthread_mutexattr_setprotocol (&ma, PTHREAD_PRIO_INHERIT)) - { - puts ("1st pthread_mutexattr_setprotocol failed"); - return 1; - } -#endif - puts ("check recursive mutex"); - res |= check_type ("recursive", &ma); - if (pthread_mutexattr_destroy (&ma) != 0) - { - puts ("1st mutexattr_destroy failed"); - return 1; - } - - if (pthread_mutexattr_init (&ma) != 0) - { - puts ("2nd mutexattr_init failed"); - return 1; - } - if (pthread_mutexattr_settype (&ma, PTHREAD_MUTEX_ERRORCHECK) != 0) - { - puts ("2nd mutexattr_settype failed"); - return 1; - } -#ifdef ENABLE_PI - if (pthread_mutexattr_setprotocol (&ma, PTHREAD_PRIO_INHERIT)) - { - puts ("2nd pthread_mutexattr_setprotocol failed"); - return 1; - } -#endif - puts ("check error-checking mutex"); - res |= check_type ("error-checking", &ma); - if (pthread_mutexattr_destroy (&ma) != 0) - { - puts ("2nd mutexattr_destroy failed"); - return 1; - } - - return res; -} - -#define TEST_FUNCTION do_test () -#include "../test-skeleton.c" diff --git a/sysdeps/pthread/tst-mutex9.c b/sysdeps/pthread/tst-mutex9.c new file mode 100644 index 0000000000..2d7927b7c2 --- /dev/null +++ b/sysdeps/pthread/tst-mutex9.c @@ -0,0 +1,140 @@ +/* Copyright (C) 2003-2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 2003. + + 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 + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +/* A bogus clock value that tells run_test to use pthread_mutex_timedlock + rather than pthread_mutex_clocklock. */ +#define CLOCK_USE_TIMEDLOCK (-1) + +static void +do_test_clock (clockid_t clockid) +{ + const clockid_t clockid_for_get = + (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid; + size_t ps = sysconf (_SC_PAGESIZE); + char tmpfname[] = "/tmp/tst-mutex9.XXXXXX"; + char data[ps]; + void *mem; + int fd; + pthread_mutex_t *m; + pthread_mutexattr_t a; + pid_t pid; + + fd = mkstemp (tmpfname); + if (fd == -1) + FAIL_EXIT1 ("cannot open temporary file: %m\n"); + + /* Make sure it is always removed. */ + unlink (tmpfname); + + /* Create one page of data. */ + memset (data, '\0', ps); + + /* Write the data to the file. */ + xwrite (fd, data, ps); + + mem = xmmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd); + + m = (pthread_mutex_t *) (((uintptr_t) mem + __alignof (pthread_mutex_t)) + & ~(__alignof (pthread_mutex_t) - 1)); + + TEST_COMPARE (pthread_mutexattr_init (&a), 0); + + TEST_COMPARE (pthread_mutexattr_setpshared (&a, PTHREAD_PROCESS_SHARED), 0); + + TEST_COMPARE (pthread_mutexattr_settype (&a, PTHREAD_MUTEX_RECURSIVE), 0); + +#ifdef ENABLE_PI + TEST_COMPARE (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT), 0); +#endif + + int e; + if ((e = pthread_mutex_init (m, &a)) != 0) + { +#ifdef ENABLE_PI + if (e == ENOTSUP) + FAIL_UNSUPPORTED ("PI mutexes unsupported"); +#endif + FAIL_EXIT1 ("mutex_init failed"); + } + + TEST_COMPARE (pthread_mutex_lock (m), 0); + + TEST_COMPARE (pthread_mutexattr_destroy (&a), 0); + + puts ("going to fork now"); + pid = xfork (); + if (pid == 0) + { + if (pthread_mutex_trylock (m) == 0) + FAIL_EXIT1 ("child: mutex_trylock succeeded"); + + if (pthread_mutex_unlock (m) == 0) + FAIL_EXIT1 ("child: mutex_unlock succeeded"); + + const struct timespec ts = timespec_add (xclock_now (clockid_for_get), + make_timespec (0, 500000000)); + + if (clockid == CLOCK_USE_TIMEDLOCK) + TEST_COMPARE (pthread_mutex_timedlock (m, &ts), ETIMEDOUT); + else + TEST_COMPARE (pthread_mutex_clocklock (m, clockid, &ts), ETIMEDOUT); + + alarm (1); + + pthread_mutex_lock (m); + + puts ("child: mutex_lock returned"); + + exit (0); + } + + sleep (2); + + int status; + if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid) + FAIL_EXIT1 ("waitpid failed"); + if (! WIFSIGNALED (status)) + FAIL_EXIT1 ("child not killed by signal"); + TEST_COMPARE (WTERMSIG (status), SIGALRM); +} + +static int +do_test (void) +{ + do_test_clock (CLOCK_USE_TIMEDLOCK); + do_test_clock (CLOCK_REALTIME); + do_test_clock (CLOCK_MONOTONIC); + return 0; +} + +#include diff --git a/sysdeps/pthread/tst-pthread-mutexattr.c b/sysdeps/pthread/tst-pthread-mutexattr.c new file mode 100644 index 0000000000..d0615bfcf8 --- /dev/null +++ b/sysdeps/pthread/tst-pthread-mutexattr.c @@ -0,0 +1,60 @@ +/* Make sure that pthread_mutexattr_gettype returns a valid kind. + + Copyright (C) 2015-2020 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 + . */ + +#include +#include +#include + +static int +do_test (void) +{ + pthread_mutexattr_t attr; + int kind; + int error; + + error = pthread_mutexattr_init (&attr); + if (error) + { + printf ("pthread_mutexattr_init: %s\n", strerror (error)); + return 1; + } + error = pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_DEFAULT); + if (error) + { + printf ("pthread_mutexattr_settype (1): %s\n", strerror (error)); + return 1; + } + error = pthread_mutexattr_gettype (&attr, &kind); + if (error) + { + printf ("pthread_mutexattr_gettype: %s\n", strerror (error)); + return 1; + } + error = pthread_mutexattr_settype (&attr, kind); + if (error) + { + printf ("pthread_mutexattr_settype (2): %s\n", strerror (error)); + return 1; + } + return 0; +} + + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c" diff --git a/sysdeps/pthread/tst-rwlock12.c b/sysdeps/pthread/tst-rwlock12.c new file mode 100644 index 0000000000..73937fe5d6 --- /dev/null +++ b/sysdeps/pthread/tst-rwlock12.c @@ -0,0 +1,207 @@ +/* Copyright (C) 2002-2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 2002. + + 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 + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +static int +do_test (void) +{ + size_t ps = sysconf (_SC_PAGESIZE); + char tmpfname[] = "/tmp/tst-rwlock12.XXXXXX"; + char data[ps]; + void *mem; + int fd; + pthread_mutex_t *m; + pthread_mutexattr_t ma; + pthread_rwlock_t *r; + pthread_rwlockattr_t ra; + pid_t pid; + int status = 0; + + fd = mkstemp (tmpfname); + if (fd == -1) + { + printf ("cannot open temporary file: %m\n"); + return 1; + } + + /* Make sure it is always removed. */ + unlink (tmpfname); + + /* Create one page of data. */ + memset (data, '\0', ps); + + /* Write the data to the file. */ + if (write (fd, data, ps) != (ssize_t) ps) + { + puts ("short write"); + return 1; + } + + mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (mem == MAP_FAILED) + { + printf ("mmap failed: %m\n"); + return 1; + } + + r = (pthread_rwlock_t *) (((uintptr_t) mem + __alignof (pthread_rwlock_t)) + & ~(__alignof (pthread_rwlock_t) - 1)); + /* The following assumes alignment for a mutex is at least as high + as that for a rwlock. Which is true in our case. */ + m = (pthread_mutex_t *) (r + 1); + + if (pthread_rwlockattr_init (&ra) != 0) + { + puts ("rwlockattr_init failed"); + return 1; + } + + if (pthread_rwlockattr_setpshared (&ra, PTHREAD_PROCESS_SHARED) != 0) + { + puts ("rwlockattr_setpshared failed"); + return 1; + } + + if (pthread_rwlock_init (r, &ra) != 0) + { + puts ("rwlock_init failed"); + return 1; + } + + if (pthread_mutexattr_init (&ma) != 0) + { + puts ("rwlockattr_init failed"); + return 1; + } + + if (pthread_mutexattr_setpshared (&ma, PTHREAD_PROCESS_SHARED) != 0) + { + puts ("mutexattr_setpshared failed"); + return 1; + } + + if (pthread_mutex_init (m, &ma) != 0) + { + puts ("mutex_init failed"); + return 1; + } + + /* Lock the mutex. */ + if (pthread_mutex_lock (m) != 0) + { + puts ("mutex_lock failed"); + return 1; + } + + puts ("going to fork now"); + pid = fork (); + if (pid == -1) + { + puts ("fork failed"); + return 1; + } + else if (pid == 0) + { + /* Lock the mutex. */ + if (pthread_mutex_lock (m) != 0) + { + puts ("child: mutex_lock failed"); + return 1; + } + + /* Try to get the rwlock. */ + if (pthread_rwlock_trywrlock (r) == 0) + { + puts ("rwlock_trywrlock succeeded"); + return 1; + } + + /* Try again. */ + struct timespec ts = { .tv_sec = 0, .tv_nsec = 500000000 }; + int e = pthread_rwlock_timedwrlock (r, &ts); + if (e == 0) + { + puts ("rwlock_timedwrlock succeeded"); + return 1; + } + if (e != ETIMEDOUT) + { + puts ("rwlock_timedwrlock didn't return ETIMEDOUT"); + status = 1; + } + + if (pthread_rwlock_tryrdlock (r) == 0) + { + puts ("rwlock_tryrdlock succeeded"); + return 1; + } + + e = pthread_rwlock_timedrdlock (r, &ts); + if (e == 0) + { + puts ("rwlock_timedrdlock succeeded"); + return 1; + } + if (e != ETIMEDOUT) + { + puts ("rwlock_timedrdlock didn't return ETIMEDOUT"); + status = 1; + } + } + else + { + /* Lock the rwlock for writing. */ + if (pthread_rwlock_wrlock (r) != 0) + { + puts ("rwlock_wrlock failed"); + kill (pid, SIGTERM); + return 1; + } + + /* Allow the child to run. */ + if (pthread_mutex_unlock (m) != 0) + { + puts ("mutex_unlock failed"); + kill (pid, SIGTERM); + return 1; + } + + /* Just wait for the child. */ + if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid) + { + puts ("waitpid failed"); + kill (pid, SIGTERM); + return 1; + } + } + + return status; +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c" diff --git a/sysdeps/pthread/tst-rwlock14.c b/sysdeps/pthread/tst-rwlock14.c new file mode 100644 index 0000000000..3583b19e3c --- /dev/null +++ b/sysdeps/pthread/tst-rwlock14.c @@ -0,0 +1,96 @@ +/* Copyright (C) 2004-2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 2004. + + 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 + . */ + +#include +#include +#include +#include +#include +#include +#include +#include + + +static pthread_barrier_t b; +static pthread_rwlock_t r = PTHREAD_RWLOCK_INITIALIZER; + + +static void * +tf (void *arg) +{ + /* Lock the read-write lock. */ + TEST_COMPARE (pthread_rwlock_wrlock (&r), 0); + + pthread_t mt = *(pthread_t *) arg; + + xpthread_barrier_wait (&b); + + /* This call will never return. */ + xpthread_join (mt); + + return NULL; +} + + +static int +do_test (void) +{ + struct timespec ts; + + xclock_gettime (CLOCK_REALTIME, &ts); + xpthread_barrier_init (&b, NULL, 2); + + pthread_t me = pthread_self (); + xpthread_create (NULL, tf, &me); + + /* Wait until the rwlock is locked. */ + xpthread_barrier_wait (&b); + + ts.tv_nsec = -1; + + TEST_COMPARE (pthread_rwlock_timedrdlock (&r, &ts), EINVAL); + TEST_COMPARE (pthread_rwlock_clockrdlock (&r, CLOCK_REALTIME, &ts), EINVAL); + TEST_COMPARE (pthread_rwlock_clockrdlock (&r, CLOCK_MONOTONIC, &ts), EINVAL); + TEST_COMPARE (pthread_rwlock_timedwrlock (&r, &ts), EINVAL); + TEST_COMPARE (pthread_rwlock_clockwrlock (&r, CLOCK_REALTIME, &ts), EINVAL); + TEST_COMPARE (pthread_rwlock_clockwrlock (&r, CLOCK_MONOTONIC, &ts), EINVAL); + + ts.tv_nsec = 1000000000; + + TEST_COMPARE (pthread_rwlock_timedrdlock (&r, &ts), EINVAL); + TEST_COMPARE (pthread_rwlock_clockrdlock (&r, CLOCK_REALTIME, &ts), EINVAL); + TEST_COMPARE (pthread_rwlock_clockrdlock (&r, CLOCK_MONOTONIC, &ts), EINVAL); + TEST_COMPARE (pthread_rwlock_timedwrlock (&r, &ts), EINVAL); + TEST_COMPARE (pthread_rwlock_clockwrlock (&r, CLOCK_REALTIME, &ts), EINVAL); + TEST_COMPARE (pthread_rwlock_clockwrlock (&r, CLOCK_MONOTONIC, &ts), EINVAL); + + ts.tv_nsec = (__typeof (ts.tv_nsec)) 0x100001000LL; + if ((__typeof (ts.tv_nsec)) 0x100001000LL != 0x100001000LL) + ts.tv_nsec = 2000000000; + + TEST_COMPARE (pthread_rwlock_timedrdlock (&r, &ts), EINVAL); + TEST_COMPARE (pthread_rwlock_clockrdlock (&r, CLOCK_REALTIME, &ts), EINVAL); + TEST_COMPARE (pthread_rwlock_clockrdlock (&r, CLOCK_MONOTONIC, &ts), EINVAL); + TEST_COMPARE (pthread_rwlock_timedwrlock (&r, &ts), EINVAL); + TEST_COMPARE (pthread_rwlock_clockwrlock (&r, CLOCK_REALTIME, &ts), EINVAL); + TEST_COMPARE (pthread_rwlock_clockwrlock (&r, CLOCK_MONOTONIC, &ts), EINVAL); + + return 0; +} + +#include -- cgit 1.4.1