diff options
author | Samuel Thibault <samuel.thibault@ens-lyon.org> | 2020-05-26 00:09:11 +0000 |
---|---|---|
committer | Samuel Thibault <samuel.thibault@ens-lyon.org> | 2020-05-26 00:46:07 +0000 |
commit | 28cada041811fe49c8da1eb7ffdbc949719f9009 (patch) | |
tree | 8b90e7057ebc557a755e0ceb665e037c6fbb58a6 /sysdeps | |
parent | 02802fafcf6e11ea3f998f685035ffe568dfddeb (diff) | |
download | glibc-28cada041811fe49c8da1eb7ffdbc949719f9009.zip glibc-28cada041811fe49c8da1eb7ffdbc949719f9009.tar.gz glibc-28cada041811fe49c8da1eb7ffdbc949719f9009.tar.bz2 |
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.
Diffstat (limited to 'sysdeps')
31 files changed, 1569 insertions, 456 deletions
diff --git a/sysdeps/htl/pt-cond-timedwait.c b/sysdeps/htl/pt-cond-timedwait.c index d27f7ba..a0ced9a 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 2e87db0..00a6922 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 07aa119..6588f01 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 c282766..7019085 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 d0293c1..e9f8da0 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 dcf09d9..c3e9c64 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 d639385..c44f70b 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 1726ebb..7486c93 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 b11bfc8..198b340 100644 --- a/sysdeps/mach/hurd/htl/pt-mutex-timedlock.c +++ b/sysdeps/mach/hurd/htl/pt-mutex-timedlock.c @@ -24,7 +24,9 @@ #include <hurdlock.h> 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 6e75c90..a1026f0 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 2f8aff3..b8dcadd 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 0000000..90ed919 --- /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 <schwab@redhat.com>, 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 + <https://www.gnu.org/licenses/>. */ + +#include <errno.h> +#include <pthread.h> +#include <semaphore.h> +#include <stdio.h> +#include <support/check.h> +#include <support/xthread.h> + +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 <support/test-driver.c> diff --git a/sysdeps/pthread/tst-join10.c b/sysdeps/pthread/tst-join10.c new file mode 100644 index 0000000..6e94c00 --- /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 + <http://www.gnu.org/licenses/>. */ + +#define USE_PTHREAD_CLOCKJOIN_NP_REALTIME 1 +#include <sysdeps/pthread/tst-join5.c> diff --git a/sysdeps/pthread/tst-join11.c b/sysdeps/pthread/tst-join11.c new file mode 100644 index 0000000..ed8312b --- /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 + <http://www.gnu.org/licenses/>. */ + +#define USE_PTHREAD_CLOCKJOIN_NP_REALTIME 1 +#define WAIT_IN_CHILD 1 +#include <sysdeps/pthread/tst-join5.c> diff --git a/sysdeps/pthread/tst-join12.c b/sysdeps/pthread/tst-join12.c new file mode 100644 index 0000000..57d713c --- /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 + <http://www.gnu.org/licenses/>. */ + +#define USE_PTHREAD_CLOCKJOIN_NP_MONOTONIC 1 +#include <sysdeps/pthread/tst-join5.c> diff --git a/sysdeps/pthread/tst-join13.c b/sysdeps/pthread/tst-join13.c new file mode 100644 index 0000000..61a9106 --- /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 + <http://www.gnu.org/licenses/>. */ + +#define USE_PTHREAD_CLOCKJOIN_NP_MONOTONIC 1 +#define WAIT_IN_CHILD 1 +#include <sysdeps/pthread/tst-join5.c> diff --git a/sysdeps/pthread/tst-join14.c b/sysdeps/pthread/tst-join14.c new file mode 100644 index 0000000..0109324 --- /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 + <https://www.gnu.org/licenses/>. */ + +#include <errno.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/time.h> +#include <support/check.h> +#include <support/timespec.h> +#include <support/xthread.h> +#include <support/xtime.h> + + +#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 <support/test-driver.c> diff --git a/sysdeps/pthread/tst-join2.c b/sysdeps/pthread/tst-join2.c new file mode 100644 index 0000000..22dfd11 --- /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 <drepper@redhat.com>, 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 + <https://www.gnu.org/licenses/>. */ + +#include <errno.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + + +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 0000000..ffebcf5 --- /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 <drepper@redhat.com>, 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 + <https://www.gnu.org/licenses/>. */ + +#include <errno.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/time.h> +#include <support/check.h> +#include <support/timespec.h> +#include <support/xthread.h> +#include <support/xtime.h> + + +#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 <support/test-driver.c> diff --git a/sysdeps/pthread/tst-join8.c b/sysdeps/pthread/tst-join8.c new file mode 100644 index 0000000..3eddaba --- /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 + <https://www.gnu.org/licenses/>. */ + +#define USE_PTHREAD_TIMEDJOIN_NP 1 +#include <sysdeps/pthread/tst-join5.c> diff --git a/sysdeps/pthread/tst-join9.c b/sysdeps/pthread/tst-join9.c new file mode 100644 index 0000000..e9d0f26 --- /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 + <https://www.gnu.org/licenses/>. */ + +#define USE_PTHREAD_TIMEDJOIN_NP 1 +#define WAIT_IN_CHILD 1 +#include <sysdeps/pthread/tst-join5.c> diff --git a/sysdeps/pthread/tst-mutex-errorcheck.c b/sysdeps/pthread/tst-mutex-errorcheck.c new file mode 100644 index 0000000..cb140f9 --- /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 + <https://www.gnu.org/licenses/>. */ + +#include <stdio.h> +#include <errno.h> +#include <time.h> +#include <pthread.h> +#include <support/check.h> + +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 <support/test-driver.c> diff --git a/sysdeps/pthread/tst-mutex11.c b/sysdeps/pthread/tst-mutex11.c new file mode 100644 index 0000000..2685f9e --- /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 + <https://www.gnu.org/licenses/>. */ + +#include <errno.h> +#include <pthread.h> +#include <stdio.h> +#include <time.h> +#include <unistd.h> +#include <support/check.h> + +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 <support/test-driver.c> diff --git a/sysdeps/pthread/tst-mutex5.c b/sysdeps/pthread/tst-mutex5.c new file mode 100644 index 0000000..1449076 --- /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 <drepper@redhat.com>, 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 + <https://www.gnu.org/licenses/>. */ + +#include <errno.h> +#include <pthread.h> +#include <stdio.h> +#include <time.h> +#include <unistd.h> +#include <sys/time.h> +#include <stdint.h> +#include <config.h> +#include <support/check.h> +#include <support/timespec.h> + + +#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 <support/test-driver.c> diff --git a/sysdeps/pthread/tst-mutex7.c b/sysdeps/pthread/tst-mutex7.c new file mode 100644 index 0000000..3938670 --- /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 <drepper@redhat.com>, 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 + <https://www.gnu.org/licenses/>. */ + +#include <errno.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> + +/* 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 0000000..8221a61 --- /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 8e56ea5..0000000 --- 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 <drepper@redhat.com>, 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 - <https://www.gnu.org/licenses/>. */ - -/* This test checks behavior not required by POSIX. */ -#include <errno.h> -#include <pthread.h> -#include <stdbool.h> -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <elf/dl-tunables.h> - -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 0000000..2d7927b --- /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 <drepper@redhat.com>, 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 + <https://www.gnu.org/licenses/>. */ + +#include <errno.h> +#include <pthread.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/mman.h> +#include <sys/time.h> +#include <sys/wait.h> +#include <support/check.h> +#include <support/timespec.h> +#include <support/xunistd.h> + + +/* 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 <support/test-driver.c> diff --git a/sysdeps/pthread/tst-pthread-mutexattr.c b/sysdeps/pthread/tst-pthread-mutexattr.c new file mode 100644 index 0000000..d0615bf --- /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 + <https://www.gnu.org/licenses/>. */ + +#include <stdio.h> +#include <string.h> +#include <pthread.h> + +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 0000000..73937fe --- /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 <drepper@redhat.com>, 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 + <https://www.gnu.org/licenses/>. */ + +#include <errno.h> +#include <pthread.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/mman.h> +#include <sys/wait.h> + + +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 0000000..3583b19 --- /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 <drepper@redhat.com>, 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 + <https://www.gnu.org/licenses/>. */ + +#include <errno.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <support/check.h> +#include <support/xthread.h> +#include <support/xtime.h> + + +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 <support/test-driver.c> |