pthread_cond_destroy.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Copyright (C) 2002, 2003, 2004, 2007 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <errno.h>
  17. #include "pthreadP.h"
  18. int
  19. attribute_protected
  20. __pthread_cond_destroy (
  21. pthread_cond_t *cond)
  22. {
  23. int pshared = (cond->__data.__mutex == (void *) ~0l)
  24. ? LLL_SHARED : LLL_PRIVATE;
  25. /* Make sure we are alone. */
  26. lll_lock (cond->__data.__lock, pshared);
  27. if (cond->__data.__total_seq > cond->__data.__wakeup_seq)
  28. {
  29. /* If there are still some waiters which have not been
  30. woken up, this is an application bug. */
  31. lll_unlock (cond->__data.__lock, pshared);
  32. return EBUSY;
  33. }
  34. /* Tell pthread_cond_*wait that this condvar is being destroyed. */
  35. cond->__data.__total_seq = -1ULL;
  36. /* If there are waiters which have been already signalled or
  37. broadcasted, but still are using the pthread_cond_t structure,
  38. pthread_cond_destroy needs to wait for them. */
  39. unsigned int nwaiters = cond->__data.__nwaiters;
  40. if (nwaiters >= (1 << COND_NWAITERS_SHIFT))
  41. {
  42. /* Wake everybody on the associated mutex in case there are
  43. threads that have been requeued to it.
  44. Without this, pthread_cond_destroy could block potentially
  45. for a long time or forever, as it would depend on other
  46. thread's using the mutex.
  47. When all threads waiting on the mutex are woken up, pthread_cond_wait
  48. only waits for threads to acquire and release the internal
  49. condvar lock. */
  50. if (cond->__data.__mutex != NULL
  51. && cond->__data.__mutex != (void *) ~0l)
  52. {
  53. pthread_mutex_t *mut = (pthread_mutex_t *) cond->__data.__mutex;
  54. lll_futex_wake (&mut->__data.__lock, INT_MAX,
  55. PTHREAD_MUTEX_PSHARED (mut));
  56. }
  57. do
  58. {
  59. lll_unlock (cond->__data.__lock, pshared);
  60. lll_futex_wait (&cond->__data.__nwaiters, nwaiters, pshared);
  61. lll_lock (cond->__data.__lock, pshared);
  62. nwaiters = cond->__data.__nwaiters;
  63. }
  64. while (nwaiters >= (1 << COND_NWAITERS_SHIFT));
  65. }
  66. return 0;
  67. }
  68. weak_alias(__pthread_cond_destroy, pthread_cond_destroy)