semaphore.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*++
  2. Copyright (c) 2015 Minoca Corp.
  3. This file is licensed under the terms of the GNU Lesser General Public
  4. License version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details.
  6. Module Name:
  7. semaphore.h
  8. Abstract:
  9. This header contains definitions for POSIX semaphores.
  10. Author:
  11. Evan Green 4-May-2015
  12. --*/
  13. #ifndef _SEMAPHORE_H
  14. #define _SEMAPHORE_H
  15. //
  16. // ------------------------------------------------------------------- Includes
  17. //
  18. #include <pthread.h>
  19. //
  20. // ---------------------------------------------------------------- Definitions
  21. //
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. //
  26. // ------------------------------------------------------ Data Type Definitions
  27. //
  28. typedef union {
  29. char Data[32];
  30. long int AlignMember;
  31. } sem_t;
  32. //
  33. // -------------------------------------------------------------------- Globals
  34. //
  35. //
  36. // -------------------------------------------------------- Function Prototypes
  37. //
  38. PTHREAD_API
  39. int
  40. sem_init (
  41. sem_t *Semaphore,
  42. int Shared,
  43. unsigned int Value
  44. );
  45. /*++
  46. Routine Description:
  47. This routine initializes a semaphore object.
  48. Arguments:
  49. Semaphore - Supplies a pointer to the semaphore to initialize.
  50. Shared - Supplies a boolean indicating whether the semaphore should be
  51. shared across processes (non-zero) or private to a particular process
  52. (zero).
  53. Value - Supplies the initial value to set in the semaphore.
  54. Return Value:
  55. 0 on success.
  56. -1 on failure, and errno will be set to contain more information.
  57. --*/
  58. PTHREAD_API
  59. int
  60. sem_destroy (
  61. sem_t *Semaphore
  62. );
  63. /*++
  64. Routine Description:
  65. This routine releases all resources associated with a POSIX semaphore.
  66. Arguments:
  67. Semaphore - Supplies a pointer to the semaphore to destroy.
  68. Return Value:
  69. 0 on success.
  70. -1 on failure, and errno will be set to contain more information.
  71. --*/
  72. PTHREAD_API
  73. int
  74. sem_wait (
  75. sem_t *Semaphore
  76. );
  77. /*++
  78. Routine Description:
  79. This routine blocks until the given semaphore can be decremented to zero or
  80. above. On success, the semaphore value will be decremented.
  81. Arguments:
  82. Semaphore - Supplies a pointer to the semaphore to wait on.
  83. Return Value:
  84. 0 on success.
  85. -1 on failure, and errno will be set to contain more information.
  86. --*/
  87. PTHREAD_API
  88. int
  89. sem_timedwait (
  90. sem_t *Semaphore,
  91. const struct timespec *AbsoluteTimeout
  92. );
  93. /*++
  94. Routine Description:
  95. This routine blocks until the given semaphore can be decremented to zero or
  96. above. This routine may time out after the specified deadline.
  97. Arguments:
  98. Semaphore - Supplies a pointer to the semaphore to wait on.
  99. AbsoluteTimeout - Supplies the deadline as an absolute time after which
  100. the operation should fail and return ETIMEDOUT.
  101. Return Value:
  102. 0 on success.
  103. -1 on failure, and errno will be set to contain more information.
  104. --*/
  105. PTHREAD_API
  106. int
  107. sem_trywait (
  108. sem_t *Semaphore
  109. );
  110. /*++
  111. Routine Description:
  112. This routine blocks until the given semaphore can be decremented to zero or
  113. above. This routine may time out after the specified deadline.
  114. Arguments:
  115. Semaphore - Supplies a pointer to the semaphore to wait on.
  116. Return Value:
  117. 0 on success.
  118. -1 on failure, and errno will be set to contain more information.
  119. --*/
  120. PTHREAD_API
  121. int
  122. sem_getvalue (
  123. sem_t *Semaphore,
  124. int *SemaphoreValue
  125. );
  126. /*++
  127. Routine Description:
  128. This routine returns the current count of the semaphore.
  129. Arguments:
  130. Semaphore - Supplies a pointer to the semaphore.
  131. SemaphoreValue - Supplies a pointer where the semaphore value will be
  132. returned.
  133. Return Value:
  134. 0 on success.
  135. -1 on failure, and errno will be set to contain more information.
  136. --*/
  137. PTHREAD_API
  138. int
  139. sem_post (
  140. sem_t *Semaphore
  141. );
  142. /*++
  143. Routine Description:
  144. This routine increments the semaphore value. If the value is incremented
  145. above zero, then threads waiting on the semaphore will be released to
  146. try and acquire it.
  147. Arguments:
  148. Semaphore - Supplies a pointer to the semaphore.
  149. Return Value:
  150. 0 on success.
  151. -1 on failure, and errno will be set to contain more information.
  152. --*/
  153. #ifdef __cplusplus
  154. }
  155. #endif
  156. #endif