semaphore.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Amazon FreeRTOS POSIX V1.1.0
  3. * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * http://aws.amazon.com/freertos
  23. * http://www.FreeRTOS.org
  24. */
  25. /**
  26. * @file semaphore.h
  27. * @brief Semaphores.
  28. *
  29. * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/semaphore.h.html
  30. */
  31. #ifndef _FREERTOS_POSIX_SEMAPHORE_H_
  32. #define _FREERTOS_POSIX_SEMAPHORE_H_
  33. /* FreeRTOS+POSIX includes. */
  34. #include "FreeRTOS_POSIX/time.h"
  35. #include "FreeRTOS_POSIX_types.h"
  36. /**
  37. * @brief Semaphore type.
  38. */
  39. typedef PosixSemType_t sem_t;
  40. /**
  41. * @brief Destroy an unnamed semaphore.
  42. *
  43. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_destroy.html
  44. *
  45. * @retval 0 - upon successful completion
  46. *
  47. * @note Semaphore is destroyed regardless of whether there is any thread currently blocked on this semaphore.
  48. */
  49. int sem_destroy( sem_t * sem );
  50. /**
  51. * @brief Get the value of a semaphore.
  52. *
  53. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_getvalue.html
  54. *
  55. * @retval 0 - Upon successful completion
  56. *
  57. * @note If sem is locked, then the object to which sval points is set to zero.
  58. */
  59. int sem_getvalue( sem_t * sem,
  60. int * sval );
  61. /**
  62. * @brief Initialize an unnamed semaphore.
  63. *
  64. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_init.html
  65. *
  66. * @note pshared is ignored. Semaphores will always be considered "shared".
  67. *
  68. * @retval 0 - upon successful completion
  69. * @retval -1 - otherwise. System error variable errno is also set in this case.
  70. *
  71. * @sideeffect Possible errno values
  72. * <br>
  73. * EINVAL - The value argument exceeds {SEM_VALUE_MAX}.
  74. * <br>
  75. * ENOSPC - A resource required to initialize the semaphore has been exhausted.
  76. */
  77. int sem_init( sem_t * sem,
  78. int pshared,
  79. unsigned value );
  80. /**
  81. * @brief Unlock a semaphore.
  82. *
  83. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_post.html
  84. *
  85. * @retval 0 - upon successful completion
  86. */
  87. int sem_post( sem_t * sem );
  88. /**
  89. * @brief Lock a semaphore with timeout.
  90. *
  91. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_timedwait.html
  92. *
  93. * @retval 0 - upon successful completion
  94. * @retval -1 - otherwise. System error variable errno is also set in this case.
  95. *
  96. * @sideeffect Possible errno values
  97. * <br>
  98. * EINVAL - parameter specified a nanoseconds field value less than zero or greater
  99. * than or equal to 1000 million
  100. * <br>
  101. * ETIMEDOUT - The semaphore could not be locked before the specified timeout expired.
  102. *
  103. * @note Deadlock detection is not implemented.
  104. */
  105. int sem_timedwait( sem_t * sem,
  106. const struct timespec * abstime );
  107. /**
  108. * @brief Lock a semaphore if available.
  109. *
  110. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_trywait.html
  111. *
  112. * @retval 0 - upon successful completion
  113. * @retval -1 - otherwise. System error variable errno is also set in this case.
  114. *
  115. * @sideeffect Possible errno values
  116. * <br>
  117. * EAGAIN - The semaphore was already locked, so it cannot be immediately locked by the sem_trywait() operation.
  118. */
  119. int sem_trywait( sem_t * sem );
  120. /**
  121. * @brief Lock a semaphore.
  122. *
  123. * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_wait.html
  124. *
  125. * @retval 0 - upon successful completion
  126. * @retval -1 - otherwise. System error variable errno is also set in this case.
  127. *
  128. * @note Deadlock detection is not implemented.
  129. */
  130. int sem_wait( sem_t * sem );
  131. #endif /* ifndef _FREERTOS_POSIX_SEMAPHORE_H_ */