sem_init.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. * Based upon 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: rpj@callisto.canberra.edu.au
  19. *
  20. * The original list of contributors to the Pthreads-win32 project
  21. * is contained in the file CONTRIBUTORS.ptw32 included with the
  22. * source 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 <stdio.h>
  42. #include <stdlib.h>
  43. #include "pthread.h"
  44. #include "semaphore.h"
  45. #include "implement.h"
  46. int
  47. sem_init (sem_t * sem, int pshared, unsigned int value)
  48. /*
  49. * ------------------------------------------------------
  50. * DOCPUBLIC
  51. * This function initializes a semaphore. The
  52. * initial value of the semaphore is 'value'
  53. *
  54. * PARAMETERS
  55. * sem
  56. * pointer to an instance of sem_t
  57. *
  58. * pshared
  59. * if zero, this semaphore may only be shared between
  60. * threads in the same process.
  61. * if nonzero, the semaphore can be shared between
  62. * processes
  63. *
  64. * value
  65. * initial value of the semaphore counter
  66. *
  67. * DESCRIPTION
  68. * This function initializes a semaphore. The
  69. * initial value of the semaphore is set to 'value'.
  70. *
  71. * RESULTS
  72. * 0 successfully created semaphore,
  73. * -1 failed, error in errno
  74. * ERRNO
  75. * EINVAL 'sem' is not a valid semaphore, or
  76. * 'value' >= SEM_VALUE_MAX
  77. * ENOMEM out of memory,
  78. * ENOSPC a required resource has been exhausted,
  79. * ENOSYS semaphores are not supported,
  80. * EPERM the process lacks appropriate privilege
  81. *
  82. * ------------------------------------------------------
  83. */
  84. {
  85. int result = 0;
  86. sem_t s = NULL;
  87. if (pshared != 0)
  88. {
  89. /*
  90. * Creating a semaphore that can be shared between
  91. * processes
  92. */
  93. result = EPERM;
  94. }
  95. else if (value > (unsigned int)SEM_VALUE_MAX)
  96. {
  97. result = EINVAL;
  98. }
  99. else
  100. {
  101. s = (sem_t) calloc (1, sizeof (*s));
  102. if (NULL == s)
  103. {
  104. result = ENOMEM;
  105. }
  106. else
  107. {
  108. s->value = value;
  109. if (pthread_mutex_init(&s->lock, NULL) == 0)
  110. {
  111. pte_osResult osResult = pte_osSemaphoreCreate(0, &s->sem);
  112. if (osResult != PTE_OS_OK)
  113. {
  114. (void) pthread_mutex_destroy(&s->lock);
  115. result = ENOSPC;
  116. }
  117. }
  118. else
  119. {
  120. result = ENOSPC;
  121. }
  122. if (result != 0)
  123. {
  124. free(s);
  125. }
  126. }
  127. }
  128. if (result != 0)
  129. {
  130. errno = result;
  131. return -1;
  132. }
  133. *sem = s;
  134. return 0;
  135. } /* sem_init */