sem_destroy.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * -------------------------------------------------------------
  3. *
  4. * Module: sem_destroy.c
  5. *
  6. * Purpose:
  7. * Semaphores aren't actually part of the PThreads standard.
  8. * They are defined by the POSIX Standard:
  9. *
  10. * POSIX 1003.1b-1993 (POSIX.1b)
  11. *
  12. * -------------------------------------------------------------
  13. *
  14. * --------------------------------------------------------------------------
  15. *
  16. * Pthreads-win32 - POSIX Threads Library for Win32
  17. * Copyright(C) 1998 John E. Bossom
  18. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  19. *
  20. * Contact Email: [email protected]
  21. *
  22. * The current list of contributors is contained
  23. * in the file CONTRIBUTORS included with the source
  24. * code distribution. The list can also be seen at the
  25. * following World Wide Web location:
  26. * http://sources.redhat.com/pthreads-win32/contributors.html
  27. *
  28. * This library is free software; you can redistribute it and/or
  29. * modify it under the terms of the GNU Lesser General Public
  30. * License as published by the Free Software Foundation; either
  31. * version 2 of the License, or (at your option) any later version.
  32. *
  33. * This library is distributed in the hope that it will be useful,
  34. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  36. * Lesser General Public License for more details.
  37. *
  38. * You should have received a copy of the GNU Lesser General Public
  39. * License along with this library in the file COPYING.LIB;
  40. * if not, write to the Free Software Foundation, Inc.,
  41. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  42. */
  43. #include "pthread.h"
  44. #include "semaphore.h"
  45. #include "implement.h"
  46. int
  47. sem_destroy (sem_t * sem)
  48. /*
  49. * ------------------------------------------------------
  50. * DOCPUBLIC
  51. * This function destroys an unnamed semaphore.
  52. *
  53. * PARAMETERS
  54. * sem
  55. * pointer to an instance of sem_t
  56. *
  57. * DESCRIPTION
  58. * This function destroys an unnamed semaphore.
  59. *
  60. * RESULTS
  61. * 0 successfully destroyed semaphore,
  62. * -1 failed, error in errno
  63. * ERRNO
  64. * EINVAL 'sem' is not a valid semaphore,
  65. * ENOSYS semaphores are not supported,
  66. * EBUSY threads (or processes) are currently
  67. * blocked on 'sem'
  68. *
  69. * ------------------------------------------------------
  70. */
  71. {
  72. int result = 0;
  73. sem_t s = NULL;
  74. if (sem == NULL || *sem == NULL)
  75. {
  76. result = EINVAL;
  77. }
  78. else
  79. {
  80. s = *sem;
  81. if ((result = pthread_mutex_lock (&s->lock)) == 0)
  82. {
  83. if (s->value < 0)
  84. {
  85. (void) pthread_mutex_unlock (&s->lock);
  86. result = EBUSY;
  87. }
  88. else
  89. {
  90. /* There are no threads currently blocked on this semaphore. */
  91. if (!CloseHandle (s->sem))
  92. {
  93. (void) pthread_mutex_unlock (&s->lock);
  94. result = EINVAL;
  95. }
  96. else
  97. {
  98. /*
  99. * Invalidate the semaphore handle when we have the lock.
  100. * Other sema operations should test this after acquiring the lock
  101. * to check that the sema is still valid, i.e. before performing any
  102. * operations. This may only be necessary before the sema op routine
  103. * returns so that the routine can return EINVAL - e.g. if setting
  104. * s->value to SEM_VALUE_MAX below does force a fall-through.
  105. */
  106. *sem = NULL;
  107. /* Prevent anyone else actually waiting on or posting this sema.
  108. */
  109. s->value = SEM_VALUE_MAX;
  110. (void) pthread_mutex_unlock (&s->lock);
  111. do
  112. {
  113. /* Give other threads a chance to run and exit any sema op
  114. * routines. Due to the SEM_VALUE_MAX value, if sem_post or
  115. * sem_wait were blocked by us they should fall through.
  116. */
  117. Sleep(0);
  118. }
  119. while (pthread_mutex_destroy (&s->lock) == EBUSY);
  120. }
  121. }
  122. }
  123. }
  124. if (result != 0)
  125. {
  126. errno = result;
  127. return -1;
  128. }
  129. free (s);
  130. return 0;
  131. } /* sem_destroy */