db_semaphore.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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_linux_defs.h"
  22. #include "pal.h"
  23. #include "pal_internal.h"
  24. #include "pal_linux.h"
  25. #include "pal_error.h"
  26. #include "api.h"
  27. #include <cmpxchg.h>
  28. #include <atomic.h>
  29. #include <linux/futex.h>
  30. #include <limits.h>
  31. #include <errno.h>
  32. #include <linux/time.h>
  33. static inline int atomic_dec_if_positive (struct atomic_int *v)
  34. {
  35. int c, old, dec;
  36. c = atomic_read(v);
  37. for (;;) {
  38. dec = c - 1;
  39. if (unlikely(dec < 0))
  40. break;
  41. old = atomic_cmpxchg((v), c, dec);
  42. if (likely(old == c))
  43. break;
  44. c = old;
  45. }
  46. return dec;
  47. }
  48. int
  49. _DkSemaphoreCreate (PAL_HANDLE handle, int initialCount, int maxCount)
  50. {
  51. /*
  52. * 1. Allocate memory for db_sem (this includes a futex variable).
  53. * 2. Pack it into a PAL_HANDLE
  54. * 3. Set the semaphore object with the argument values (count, maxCount)
  55. */
  56. SET_HANDLE_TYPE(handle, semaphore);
  57. atomic_set(&handle->semaphore.nwaiters, 0);
  58. handle->semaphore.max_value = maxCount;
  59. /* optimization: if maxCount == 1, we make it into mutex */
  60. if (handle->semaphore.max_value == 1) {
  61. handle->semaphore.value.mut.u = initialCount;
  62. } else {
  63. atomic_set(&handle->semaphore.value.i, maxCount - initialCount);
  64. }
  65. return 0;
  66. }
  67. void _DkSemaphoreDestroy (PAL_HANDLE semaphoreHandle)
  68. {
  69. free(semaphoreHandle);
  70. }
  71. int _DkMutexLockTimeout (struct mutex_handle * mut, uint64_t timeout);
  72. int _DkSemaphoreAcquire (PAL_HANDLE sem, int count)
  73. {
  74. /* optimization: use it as a mutex */
  75. if (sem->semaphore.max_value == 1) {
  76. struct mutex_handle * mut = &sem->semaphore.value.mut;
  77. _DkMutexLock(mut);
  78. return 0;
  79. }
  80. if (count > sem->semaphore.max_value)
  81. return -PAL_ERROR_INVAL;
  82. struct atomic_int * value = &sem->semaphore.value.i;
  83. int c = 0;
  84. if (!value)
  85. return -PAL_ERROR_BADHANDLE;
  86. if (count == 1)
  87. c = atomic_dec_and_test_nonnegative (value);
  88. else
  89. c = atomic_sub_and_test_nonnegative (count, value);
  90. if (c)
  91. return 0;
  92. /* We didn't get the lock. Bump the count back up. */
  93. if (count == 1)
  94. atomic_inc (value);
  95. else
  96. atomic_add (count, value);
  97. int ret = 0;
  98. atomic_inc (&sem->semaphore.nwaiters);
  99. while (1) {
  100. ret = INLINE_SYSCALL(futex, 6, value, FUTEX_WAIT, 0,
  101. NULL, NULL, 0);
  102. if (IS_ERR(ret)) {
  103. if (ERRNO(ret) == EWOULDBLOCK) {
  104. ret = 0;
  105. } else {
  106. ret = unix_to_pal_error(ERRNO(ret));
  107. break;
  108. }
  109. }
  110. if (count == 1)
  111. c = atomic_dec_and_test_nonnegative (value);
  112. else
  113. c = atomic_sub_and_test_nonnegative (count, value);
  114. if (c)
  115. break;
  116. /* We didn't get the lock. Bump the count back up. */
  117. if (count == 1)
  118. atomic_inc (value);
  119. else
  120. atomic_add (count, value);
  121. }
  122. atomic_dec (&sem->semaphore.nwaiters);
  123. return ret;
  124. }
  125. int _DkSemaphoreAcquireTimeout (PAL_HANDLE sem, int count, uint64_t timeout)
  126. {
  127. /* Pass it up to the no-timeout version if no timeout requested */
  128. if (timeout == NO_TIMEOUT)
  129. return _DkSemaphoreAcquire(sem, count);
  130. /* optimization: use it as a mutex */
  131. if (sem->semaphore.max_value == 1) {
  132. struct mutex_handle * mut = & sem->semaphore.value.mut;
  133. return _DkMutexLockTimeout(mut, timeout);
  134. }
  135. if (count > sem->semaphore.max_value)
  136. return -PAL_ERROR_INVAL;
  137. struct atomic_int * value = &sem->semaphore.value.i;
  138. int c = 0;
  139. if (!value)
  140. return -PAL_ERROR_BADHANDLE;
  141. if (count == 1)
  142. c = atomic_dec_and_test_nonnegative (value);
  143. else
  144. c = atomic_sub_and_test_nonnegative (count, value);
  145. if (c)
  146. return 0;
  147. /* We didn't get the lock. Bump the count back up. */
  148. if (count == 1)
  149. atomic_inc (value);
  150. else
  151. atomic_add (count, value);
  152. if (!timeout)
  153. return -PAL_ERROR_TRYAGAIN;
  154. struct timespec waittime;
  155. long sec = timeout / 1000000;
  156. long microsec = timeout - (sec * 1000000);
  157. waittime.tv_sec = sec;
  158. waittime.tv_nsec = microsec * 1000;
  159. int ret = 0;
  160. atomic_inc (&sem->semaphore.nwaiters);
  161. while (1) {
  162. ret = INLINE_SYSCALL(futex, 6, value, FUTEX_WAIT, 0,
  163. &waittime, NULL, 0);
  164. if (ERRNO(ret) == EWOULDBLOCK) {
  165. ret = 0;
  166. } else {
  167. ret = unix_to_pal_error(ERRNO(ret));
  168. break;
  169. }
  170. if (count == 1)
  171. c = atomic_dec_and_test_nonnegative (value);
  172. else
  173. c = atomic_sub_and_test_nonnegative (count, value);
  174. if (c)
  175. break;
  176. }
  177. /* We didn't get the lock. Bump the count back up. */
  178. if (count == 1)
  179. atomic_inc (value);
  180. else
  181. atomic_add (count, value);
  182. atomic_dec (&sem->semaphore.nwaiters);
  183. return ret;
  184. }
  185. void _DkSemaphoreRelease (PAL_HANDLE sem, int count)
  186. {
  187. /* optimization: use it as a mutex */
  188. if (sem->semaphore.max_value == 1) {
  189. struct mutex_handle * mut =
  190. &sem->semaphore.value.mut;
  191. _DkMutexUnlock(mut);
  192. return;
  193. }
  194. struct atomic_int * value = &sem->semaphore.value.i;
  195. if (count == 1)
  196. atomic_inc (value);
  197. else
  198. atomic_add (count, value);
  199. int nwaiters = atomic_read (&sem->semaphore.nwaiters);
  200. if (nwaiters > 0)
  201. INLINE_SYSCALL(futex, 6, value, FUTEX_WAKE, nwaiters, NULL, NULL, 0);
  202. }
  203. int _DkSemaphoreGetCurrentCount (PAL_HANDLE sem)
  204. {
  205. if (sem->semaphore.max_value == 1) {
  206. struct mutex_handle * m = &sem->semaphore.value.mut;
  207. return m->b.locked;
  208. }
  209. int c = atomic_read(&sem->semaphore.value.i);
  210. return sem->semaphore.max_value - c;
  211. }