sem_destroy.c 4.9 KB

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