semaphore.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. if (sem->value == SEM_VALUE_MAX) {
  83. pthread_mutex_unlock(&sem->mtx);
  84. errno = EOVERFLOW;
  85. return -1;
  86. }
  87. sem->value++;
  88. auto rc = pthread_cond_signal(&sem->cv);
  89. if (rc != 0) {
  90. pthread_mutex_unlock(&sem->mtx);
  91. errno = rc;
  92. return -1;
  93. }
  94. rc = pthread_mutex_unlock(&sem->mtx);
  95. if (errno != 0) {
  96. errno = rc;
  97. return -1;
  98. }
  99. return 0;
  100. }
  101. int sem_trywait(sem_t* sem)
  102. {
  103. auto rc = pthread_mutex_lock(&sem->mtx);
  104. if (rc != 0) {
  105. errno = rc;
  106. return -1;
  107. }
  108. if (sem->value == 0) {
  109. pthread_mutex_unlock(&sem->mtx);
  110. errno = EAGAIN;
  111. return -1;
  112. }
  113. sem->value--;
  114. return 0;
  115. }
  116. int sem_unlink(const char*)
  117. {
  118. errno = ENOSYS;
  119. return -1;
  120. }
  121. int sem_wait(sem_t* sem)
  122. {
  123. auto rc = pthread_mutex_lock(&sem->mtx);
  124. if (errno != 0) {
  125. errno = rc;
  126. return -1;
  127. }
  128. while (sem->value == 0) {
  129. rc = pthread_cond_wait(&sem->cv, &sem->mtx);
  130. if (rc != 0) {
  131. pthread_mutex_unlock(&sem->mtx);
  132. errno = rc;
  133. return -1;
  134. }
  135. }
  136. sem->value--;
  137. return 0;
  138. }