From 8081702460726304af496be52234385094392a6f Mon Sep 17 00:00:00 2001 From: Samuel Thibault Date: Mon, 1 Jun 2020 17:27:48 +0000 Subject: htl: Make pthread_cond_destroy wait for threads to be woken This allows to reuse the storage after calling pthread_cond_destroy. * sysdeps/htl/bits/types/struct___pthread_cond.h (__pthread_cond): Replace unused struct __pthread_condimpl *__impl field with unsigned int __wrefs. (__PTHREAD_COND_INITIALIZER): Update accordingly. * sysdeps/htl/pt-cond-timedwait.c (__pthread_cond_timedwait_internal): Register as waiter in __wrefs field. On unregistering, wake any pending pthread_cond_destroy. * sysdeps/htl/pt-cond-destroy.c (__pthread_cond_destroy): Register wake request in __wrefs. * nptl/Makefile (tests): Move tst-cond20 tst-cond21 to... * sysdeps/pthread/Makefile (tests): ... here. * nptl/tst-cond20.c nptl/tst-cond21.c: Move to... * sysdeps/pthread/tst-cond20.c sysdeps/pthread/tst-cond21.c: ... here. --- sysdeps/htl/pt-cond-destroy.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'sysdeps/htl/pt-cond-destroy.c') diff --git a/sysdeps/htl/pt-cond-destroy.c b/sysdeps/htl/pt-cond-destroy.c index 0664f3f6cc..722516a8e2 100644 --- a/sysdeps/htl/pt-cond-destroy.c +++ b/sysdeps/htl/pt-cond-destroy.c @@ -22,14 +22,26 @@ int __pthread_cond_destroy (pthread_cond_t *cond) { - int ret = 0; + /* Set the wake request flag. */ + unsigned int wrefs = atomic_fetch_or_acquire (&cond->__wrefs, 1); __pthread_spin_wait (&cond->__lock); if (cond->__queue) - ret = EBUSY; + { + __pthread_spin_unlock (&cond->__lock); + return EBUSY; + } __pthread_spin_unlock (&cond->__lock); - return ret; + while (wrefs >> 1 != 0) + { + gsync_wait (__mach_task_self (), (vm_offset_t) &cond->__wrefs, wrefs, + 0, 0, 0); + wrefs = atomic_load_acquire (&cond->__wrefs); + } + /* The memory the condvar occupies can now be reused. */ + + return 0; } weak_alias (__pthread_cond_destroy, pthread_cond_destroy); -- cgit 1.4.1