pthread_cond_destroy.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. __pthread_cond_destroy (
  20. pthread_cond_t *cond)
  21. {
  22. int pshared = (cond->__data.__mutex == (void *) ~0l)
  23. ? LLL_SHARED : LLL_PRIVATE;
  24. /* Make sure we are alone. */
  25. lll_lock (cond->__data.__lock, pshared);
  26. if (cond->__data.__total_seq > cond->__data.__wakeup_seq)
  27. {
  28. /* If there are still some waiters which have not been
  29. woken up, this is an application bug. */
  30. lll_unlock (cond->__data.__lock, pshared);
  31. return EBUSY;
  32. }
  33. /* Tell pthread_cond_*wait that this condvar is being destroyed. */
  34. cond->__data.__total_seq = -1ULL;
  35. /* If there are waiters which have been already signalled or
  36. broadcasted, but still are using the pthread_cond_t structure,
  37. pthread_cond_destroy needs to wait for them. */
  38. unsigned int nwaiters = cond->__data.__nwaiters;
  39. if (nwaiters >= (1 << COND_NWAITERS_SHIFT))
  40. {
  41. /* Wake everybody on the associated mutex in case there are
  42. threads that have been requeued to it.
  43. Without this, pthread_cond_destroy could block potentially
  44. for a long time or forever, as it would depend on other
  45. thread's using the mutex.
  46. When all threads waiting on the mutex are woken up, pthread_cond_wait
  47. only waits for threads to acquire and release the internal
  48. condvar lock. */
  49. if (cond->__data.__mutex != NULL
  50. && cond->__data.__mutex != (void *) ~0l)
  51. {
  52. pthread_mutex_t *mut = (pthread_mutex_t *) cond->__data.__mutex;
  53. lll_futex_wake (&mut->__data.__lock, INT_MAX,
  54. PTHREAD_MUTEX_PSHARED (mut));
  55. }
  56. do
  57. {
  58. lll_unlock (cond->__data.__lock, pshared);
  59. lll_futex_wait (&cond->__data.__nwaiters, nwaiters, pshared);
  60. lll_lock (cond->__data.__lock, pshared);
  61. nwaiters = cond->__data.__nwaiters;
  62. }
  63. while (nwaiters >= (1 << COND_NWAITERS_SHIFT));
  64. }
  65. return 0;
  66. }
  67. weak_alias(__pthread_cond_destroy, pthread_cond_destroy)