semaphore.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <errno.h>
  8. #include <semaphore.h>
  9. int sem_close(sem_t*)
  10. {
  11. errno = ENOSYS;
  12. return -1;
  13. }
  14. int sem_destroy(sem_t* sem)
  15. {
  16. auto rc = pthread_mutex_destroy(&sem->mtx);
  17. if (rc != 0) {
  18. errno = rc;
  19. return -1;
  20. }
  21. rc = pthread_cond_destroy(&sem->cv);
  22. if (rc != 0) {
  23. errno = rc;
  24. return -1;
  25. }
  26. return 0;
  27. }
  28. int sem_getvalue(sem_t* sem, int* sval)
  29. {
  30. auto rc = pthread_mutex_trylock(&sem->mtx);
  31. if (rc == EBUSY) {
  32. *sval = 0;
  33. return 0;
  34. }
  35. if (rc != 0) {
  36. errno = rc;
  37. return -1;
  38. }
  39. *sval = sem->value;
  40. pthread_mutex_unlock(&sem->mtx);
  41. return 0;
  42. }
  43. int sem_init(sem_t* sem, int shared, unsigned int value)
  44. {
  45. if (shared) {
  46. errno = ENOSYS;
  47. return -1;
  48. }
  49. if (value > SEM_VALUE_MAX) {
  50. errno = EINVAL;
  51. return -1;
  52. }
  53. auto rc = pthread_mutex_init(&sem->mtx, nullptr);
  54. if (rc != 0) {
  55. errno = rc;
  56. return -1;
  57. }
  58. rc = pthread_cond_init(&sem->cv, nullptr);
  59. if (rc != 0) {
  60. errno = rc;
  61. return -1;
  62. }
  63. sem->value = value;
  64. return 0;
  65. }
  66. sem_t* sem_open(const char*, int, ...)
  67. {
  68. errno = ENOSYS;
  69. return nullptr;
  70. }
  71. int sem_post(sem_t* sem)
  72. {
  73. auto rc = pthread_mutex_lock(&sem->mtx);
  74. if (rc != 0) {
  75. errno = rc;
  76. return -1;
  77. }
  78. if (sem->value == SEM_VALUE_MAX) {
  79. pthread_mutex_unlock(&sem->mtx);
  80. errno = EOVERFLOW;
  81. return -1;
  82. }
  83. sem->value++;
  84. rc = pthread_cond_signal(&sem->cv);
  85. if (rc != 0) {
  86. pthread_mutex_unlock(&sem->mtx);
  87. errno = rc;
  88. return -1;
  89. }
  90. rc = pthread_mutex_unlock(&sem->mtx);
  91. if (rc != 0) {
  92. errno = rc;
  93. return -1;
  94. }
  95. return 0;
  96. }
  97. int sem_trywait(sem_t* sem)
  98. {
  99. auto rc = pthread_mutex_lock(&sem->mtx);
  100. if (rc != 0) {
  101. errno = rc;
  102. return -1;
  103. }
  104. if (sem->value == 0) {
  105. pthread_mutex_unlock(&sem->mtx);
  106. errno = EAGAIN;
  107. return -1;
  108. }
  109. sem->value--;
  110. rc = pthread_mutex_unlock(&sem->mtx);
  111. if (rc != 0) {
  112. errno = rc;
  113. return -1;
  114. }
  115. return 0;
  116. }
  117. int sem_unlink(const char*)
  118. {
  119. errno = ENOSYS;
  120. return -1;
  121. }
  122. int sem_wait(sem_t* sem)
  123. {
  124. auto rc = pthread_mutex_lock(&sem->mtx);
  125. if (rc != 0) {
  126. errno = rc;
  127. return -1;
  128. }
  129. while (sem->value == 0) {
  130. rc = pthread_cond_wait(&sem->cv, &sem->mtx);
  131. if (rc != 0) {
  132. pthread_mutex_unlock(&sem->mtx);
  133. errno = rc;
  134. return -1;
  135. }
  136. }
  137. sem->value--;
  138. rc = pthread_mutex_unlock(&sem->mtx);
  139. if (rc != 0) {
  140. errno = rc;
  141. return -1;
  142. }
  143. return 0;
  144. }