semaphore.h 4.3 KB

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