sem_init.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * -------------------------------------------------------------
  3. *
  4. * Module: sem_init.c
  5. *
  6. * Purpose:
  7. * Semaphores aren't actually part of PThreads.
  8. * They are defined by the POSIX Standard:
  9. *
  10. * POSIX 1003.1-2001
  11. *
  12. * -------------------------------------------------------------
  13. *
  14. * Pthreads-win32 - POSIX Threads Library for Win32
  15. * Copyright(C) 1998 John E. Bossom
  16. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  17. *
  18. * Contact Email: [email protected]
  19. *
  20. * The current list of contributors is contained
  21. * in the file CONTRIBUTORS included with the source
  22. * code distribution. The list can also be seen at the
  23. * following World Wide Web location:
  24. * http://sources.redhat.com/pthreads-win32/contributors.html
  25. *
  26. * This library is free software; you can redistribute it and/or
  27. * modify it under the terms of the GNU Lesser General Public
  28. * License as published by the Free Software Foundation; either
  29. * version 2 of the License, or (at your option) any later version.
  30. *
  31. * This library is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  34. * Lesser General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU Lesser General Public
  37. * License along with this library in the file COPYING.LIB;
  38. * if not, write to the Free Software Foundation, Inc.,
  39. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  40. */
  41. #include "pthread.h"
  42. #include "semaphore.h"
  43. #include "implement.h"
  44. int
  45. sem_init (sem_t * sem, int pshared, unsigned int value)
  46. /*
  47. * ------------------------------------------------------
  48. * DOCPUBLIC
  49. * This function initializes a semaphore. The
  50. * initial value of the semaphore is 'value'
  51. *
  52. * PARAMETERS
  53. * sem
  54. * pointer to an instance of sem_t
  55. *
  56. * pshared
  57. * if zero, this semaphore may only be shared between
  58. * threads in the same process.
  59. * if nonzero, the semaphore can be shared between
  60. * processes
  61. *
  62. * value
  63. * initial value of the semaphore counter
  64. *
  65. * DESCRIPTION
  66. * This function initializes a semaphore. The
  67. * initial value of the semaphore is set to 'value'.
  68. *
  69. * RESULTS
  70. * 0 successfully created semaphore,
  71. * -1 failed, error in errno
  72. * ERRNO
  73. * EINVAL 'sem' is not a valid semaphore, or
  74. * 'value' >= SEM_VALUE_MAX
  75. * ENOMEM out of memory,
  76. * ENOSPC a required resource has been exhausted,
  77. * ENOSYS semaphores are not supported,
  78. * EPERM the process lacks appropriate privilege
  79. *
  80. * ------------------------------------------------------
  81. */
  82. {
  83. int result = 0;
  84. sem_t s = NULL;
  85. if (pshared != 0)
  86. {
  87. /*
  88. * Creating a semaphore that can be shared between
  89. * processes
  90. */
  91. result = EPERM;
  92. }
  93. else if (value > (unsigned int)SEM_VALUE_MAX)
  94. {
  95. result = EINVAL;
  96. }
  97. else
  98. {
  99. s = (sem_t) calloc (1, sizeof (*s));
  100. if (NULL == s)
  101. {
  102. result = ENOMEM;
  103. }
  104. else
  105. {
  106. s->value = value;
  107. if (pthread_mutex_init(&s->lock, NULL) == 0)
  108. {
  109. #if defined(NEED_SEM)
  110. s->sem = CreateEvent (NULL,
  111. PTW32_FALSE, /* auto (not manual) reset */
  112. PTW32_FALSE, /* initial state is unset */
  113. NULL);
  114. if (0 == s->sem)
  115. {
  116. free (s);
  117. (void) pthread_mutex_destroy(&s->lock);
  118. result = ENOSPC;
  119. }
  120. else
  121. {
  122. s->leftToUnblock = 0;
  123. }
  124. #else /* NEED_SEM */
  125. if ((s->sem = CreateSemaphore (NULL, /* Always NULL */
  126. (long) 0, /* Force threads to wait */
  127. (long) SEM_VALUE_MAX, /* Maximum value */
  128. NULL)) == 0) /* Name */
  129. {
  130. (void) pthread_mutex_destroy(&s->lock);
  131. result = ENOSPC;
  132. }
  133. #endif /* NEED_SEM */
  134. }
  135. else
  136. {
  137. result = ENOSPC;
  138. }
  139. if (result != 0)
  140. {
  141. free(s);
  142. }
  143. }
  144. }
  145. if (result != 0)
  146. {
  147. errno = result;
  148. return -1;
  149. }
  150. *sem = s;
  151. return 0;
  152. } /* sem_init */