db_semaphore.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * db_semaphore.c
  17. *
  18. * This file contains APIs that provides operations of semaphores.
  19. */
  20. #include "pal_defs.h"
  21. #include "pal_freebsd_defs.h"
  22. #include "pal.h"
  23. #include "pal_internal.h"
  24. #include "pal_freebsd.h"
  25. #include "pal_error.h"
  26. #include "api.h"
  27. #include <cmpxchg.h>
  28. #include <atomic.h>
  29. #include <limits.h>
  30. #include <errno.h>
  31. #include <sys/time.h>
  32. static inline int atomic_dec_if_positive (struct atomic_int *v)
  33. {
  34. int c, old, dec;
  35. c = atomic_read(v);
  36. for (;;) {
  37. dec = c - 1;
  38. if (unlikely(dec < 0))
  39. break;
  40. old = atomic_cmpxchg((v), c, dec);
  41. if (likely(old == c))
  42. break;
  43. c = old;
  44. }
  45. return dec;
  46. }
  47. int _DkSemaphoreCreate (PAL_HANDLE handle, int initialCount, int maxCount)
  48. {
  49. /*
  50. * 1. Allocate memory for db_sem (this includes a futex variable).
  51. * 2. Pack it into a PAL_HANDLE
  52. * 3. Set the semaphore object with the argument values (count, maxCount)
  53. */
  54. SET_HANDLE_TYPE(handle, semaphore);
  55. atomic_set(&handle->semaphore.nwaiters, 0);
  56. handle->semaphore.max_value = maxCount;
  57. /* optimization: if maxCount == 1, we make it into mutex */
  58. if (handle->semaphore.max_value == 1) {
  59. atomic_set(&handle->semaphore.value.mut.value, 1 - initialCount);
  60. } else {
  61. atomic_set(&handle->semaphore.value.i, maxCount - initialCount);
  62. }
  63. return 0;
  64. }
  65. void _DkSemaphoreDestroy (PAL_HANDLE semaphoreHandle)
  66. {
  67. free(semaphoreHandle);
  68. }
  69. int _DkMutexLockTimeout (struct mutex_handle * mut, int timeout);
  70. int _DkSemaphoreAcquire (PAL_HANDLE sem, int count)
  71. {
  72. /* optimization: use it as a mutex */
  73. if (sem->semaphore.max_value == 1) {
  74. struct mutex_handle * mut = &sem->semaphore.value.mut;
  75. _DkMutexLock(mut);
  76. return 0;
  77. }
  78. if (count > sem->semaphore.max_value)
  79. return -PAL_ERROR_INVAL;
  80. struct atomic_int * value = &sem->semaphore.value.i;
  81. int c = 0;
  82. if (!value)
  83. return -PAL_ERROR_BADHANDLE;
  84. if (count == 1)
  85. c = atomic_dec_and_test_nonnegative (value);
  86. else
  87. c = atomic_sub_and_test_nonnegative (count, value);
  88. if (c)
  89. return 0;
  90. /* We didn't get the lock. Bump the count back up. */
  91. if (count == 1)
  92. atomic_inc (value);
  93. else
  94. atomic_add (count, value);
  95. int ret = 0;
  96. atomic_inc (&sem->semaphore.nwaiters);
  97. while (1) {
  98. ret = INLINE_SYSCALL(_umtx_op, 5, value, UMTX_OP_WAIT, 0,
  99. NULL, NULL);
  100. if (IS_ERR(ret)) {
  101. if (ERRNO(ret) == EWOULDBLOCK) {
  102. ret = 0;
  103. } else {
  104. ret = unix_to_pal_error(ERRNO(ret));
  105. break;
  106. }
  107. }
  108. if (count == 1)
  109. c = atomic_dec_and_test_nonnegative (value);
  110. else
  111. c = atomic_sub_and_test_nonnegative (count, value);
  112. if (c)
  113. break;
  114. /* We didn't get the lock. Bump the count back up. */
  115. if (count == 1)
  116. atomic_inc (value);
  117. else
  118. atomic_add (count, value);
  119. }
  120. atomic_dec (&sem->semaphore.nwaiters);
  121. return ret;
  122. }
  123. int _DkSemaphoreAcquireTimeout (PAL_HANDLE sem, int count, int timeout)
  124. {
  125. /* Pass it up to the no-timeout version if no timeout requested */
  126. if (timeout == -1)
  127. return _DkSemaphoreAcquire(sem, count);
  128. /* optimization: use it as a mutex */
  129. if (sem->semaphore.max_value == 1) {
  130. struct mutex_handle * mut = & sem->semaphore.value.mut;
  131. _DkMutexLockTimeout(mut, timeout);
  132. return 0;
  133. }
  134. if (count > sem->semaphore.max_value)
  135. return -PAL_ERROR_INVAL;
  136. struct atomic_int * value = &sem->semaphore.value.i;
  137. int c = 0;
  138. if (!value)
  139. return -PAL_ERROR_BADHANDLE;
  140. if (count == 1)
  141. c = atomic_dec_and_test_nonnegative (value);
  142. else
  143. c = atomic_sub_and_test_nonnegative (count, value);
  144. if (c)
  145. return 0;
  146. /* We didn't get the lock. Bump the count back up. */
  147. if (count == 1)
  148. atomic_inc (value);
  149. else
  150. atomic_add (count, value);
  151. if (!timeout)
  152. return 0;
  153. struct timespec waittime;
  154. long sec = timeout / 1000000;
  155. long microsec = timeout - (sec * 1000000);
  156. waittime.tv_sec = sec;
  157. waittime.tv_nsec = microsec * 1000;
  158. int ret = 0;
  159. atomic_inc (&sem->semaphore.nwaiters);
  160. while (1) {
  161. ret = INLINE_SYSCALL(_umtx_op, 5, value, UMTX_OP_WAIT_UINT, 0,
  162. NULL, &waittime);
  163. if (ERRNO(ret) == EWOULDBLOCK) {
  164. ret = 0;
  165. } else {
  166. ret = unix_to_pal_error(ERRNO(ret));
  167. break;
  168. }
  169. if (count == 1)
  170. c = atomic_dec_and_test_nonnegative (value);
  171. else
  172. c = atomic_sub_and_test_nonnegative (count, value);
  173. if (c)
  174. break;
  175. }
  176. /* We didn't get the lock. Bump the count back up. */
  177. if (count == 1)
  178. atomic_inc (value);
  179. else
  180. atomic_add (count, value);
  181. atomic_dec (&sem->semaphore.nwaiters);
  182. return ret;
  183. }
  184. void _DkSemaphoreRelease (PAL_HANDLE sem, int count)
  185. {
  186. /* optimization: use it as a mutex */
  187. if (sem->semaphore.max_value == 1) {
  188. struct mutex_handle * mut =
  189. &sem->semaphore.value.mut;
  190. _DkMutexUnlock(mut);
  191. return;
  192. }
  193. struct atomic_int * value = &sem->semaphore.value.i;
  194. if (count == 1)
  195. atomic_inc (value);
  196. else
  197. atomic_add (count, value);
  198. int nwaiters = atomic_read (&sem->semaphore.nwaiters);
  199. if (nwaiters > 0)
  200. INLINE_SYSCALL(_umtx_op, 5, value, UMTX_OP_WAKE,
  201. nwaiters, NULL, NULL);
  202. }
  203. int _DkSemaphoreGetCurrentCount (PAL_HANDLE sem)
  204. {
  205. if (sem->semaphore.max_value == 1) {
  206. struct mutex_handle * mut =
  207. &sem->semaphore.value.mut;
  208. return atomic_read(&mut->value);
  209. }
  210. int c = atomic_read(&sem->semaphore.value.i);
  211. return sem->semaphore.max_value - c;
  212. }