semaphore.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Assertions.h>
  27. #include <errno.h>
  28. #include <semaphore.h>
  29. int sem_close(sem_t*)
  30. {
  31. errno = ENOSYS;
  32. return -1;
  33. }
  34. int sem_destroy(sem_t* sem)
  35. {
  36. auto rc = pthread_mutex_destroy(&sem->mtx);
  37. if (rc != 0) {
  38. errno = rc;
  39. return -1;
  40. }
  41. rc = pthread_cond_destroy(&sem->cv);
  42. if (rc != 0) {
  43. errno = rc;
  44. return -1;
  45. }
  46. return 0;
  47. }
  48. int sem_getvalue(sem_t*, int*)
  49. {
  50. VERIFY_NOT_REACHED();
  51. }
  52. int sem_init(sem_t* sem, int shared, unsigned int value)
  53. {
  54. if (shared) {
  55. errno = ENOSYS;
  56. return -1;
  57. }
  58. if (value > SEM_VALUE_MAX) {
  59. errno = EINVAL;
  60. return -1;
  61. }
  62. auto rc = pthread_mutex_init(&sem->mtx, nullptr);
  63. if (rc != 0) {
  64. errno = rc;
  65. return -1;
  66. }
  67. rc = pthread_cond_init(&sem->cv, nullptr);
  68. if (rc != 0) {
  69. errno = rc;
  70. return -1;
  71. }
  72. sem->value = value;
  73. return 0;
  74. }
  75. sem_t* sem_open(const char*, int, ...)
  76. {
  77. errno = ENOSYS;
  78. return nullptr;
  79. }
  80. int sem_post(sem_t* sem)
  81. {
  82. auto rc = pthread_mutex_lock(&sem->mtx);
  83. if (rc != 0) {
  84. errno = rc;
  85. return -1;
  86. }
  87. if (sem->value == SEM_VALUE_MAX) {
  88. pthread_mutex_unlock(&sem->mtx);
  89. errno = EOVERFLOW;
  90. return -1;
  91. }
  92. sem->value++;
  93. rc = pthread_cond_signal(&sem->cv);
  94. if (rc != 0) {
  95. pthread_mutex_unlock(&sem->mtx);
  96. errno = rc;
  97. return -1;
  98. }
  99. rc = pthread_mutex_unlock(&sem->mtx);
  100. if (errno != 0) {
  101. errno = rc;
  102. return -1;
  103. }
  104. return 0;
  105. }
  106. int sem_trywait(sem_t* sem)
  107. {
  108. auto rc = pthread_mutex_lock(&sem->mtx);
  109. if (rc != 0) {
  110. errno = rc;
  111. return -1;
  112. }
  113. if (sem->value == 0) {
  114. pthread_mutex_unlock(&sem->mtx);
  115. errno = EAGAIN;
  116. return -1;
  117. }
  118. sem->value--;
  119. rc = pthread_mutex_unlock(&sem->mtx);
  120. if (rc != 0) {
  121. errno = rc;
  122. return -1;
  123. }
  124. return 0;
  125. }
  126. int sem_unlink(const char*)
  127. {
  128. errno = ENOSYS;
  129. return -1;
  130. }
  131. int sem_wait(sem_t* sem)
  132. {
  133. auto rc = pthread_mutex_lock(&sem->mtx);
  134. if (errno != 0) {
  135. errno = rc;
  136. return -1;
  137. }
  138. while (sem->value == 0) {
  139. rc = pthread_cond_wait(&sem->cv, &sem->mtx);
  140. if (rc != 0) {
  141. pthread_mutex_unlock(&sem->mtx);
  142. errno = rc;
  143. return -1;
  144. }
  145. }
  146. sem->value--;
  147. rc = pthread_mutex_unlock(&sem->mtx);
  148. if (rc != 0) {
  149. errno = rc;
  150. return -1;
  151. }
  152. return 0;
  153. }