db_semaphore.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <linux/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 (dec < 0)
  39. break;
  40. old = atomic_cmpxchg((v), c, dec);
  41. if (old == c)
  42. break;
  43. c = old;
  44. }
  45. return dec;
  46. }
  47. int
  48. _DkSemaphoreCreate (PAL_HANDLE handle, int initialCount, int maxCount)
  49. {
  50. /*
  51. * 1. Allocate memory for db_sem (this includes a futex variable).
  52. * 2. Pack it into a PAL_HANDLE
  53. * 3. Set the semaphore object with the argument values (count, maxCount)
  54. */
  55. SET_HANDLE_TYPE(handle, semaphore);
  56. atomic_set(&handle->semaphore.nwaiters, 0);
  57. handle->semaphore.max_value = maxCount;
  58. /* optimization: if maxCount == 1, we make it into mutex */
  59. if (handle->semaphore.max_value == 1) {
  60. handle->semaphore.value.mut.u = initialCount;
  61. } else {
  62. atomic_set(&handle->semaphore.value.i, maxCount - initialCount);
  63. }
  64. return 0;
  65. }
  66. void _DkSemaphoreDestroy (PAL_HANDLE semaphoreHandle)
  67. {
  68. /* do nothing */
  69. }
  70. int _DkMutexLockTimeout (struct mutex_handle * mut, int timeout);
  71. int _DkSemaphoreAcquire (PAL_HANDLE sem, int count)
  72. {
  73. /* optimization: use it as a mutex */
  74. if (sem->semaphore.max_value == 1) {
  75. struct mutex_handle * mut = &sem->semaphore.value.mut;
  76. return _DkMutexLock(mut);
  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 = ocall_futex((int *) &value->counter, FUTEX_WAIT, 0, NULL);
  99. if (ret < 0) {
  100. if (ret == -PAL_ERROR_TRYAGAIN)
  101. ret = 0;
  102. else
  103. break;
  104. }
  105. if (count == 1)
  106. c = atomic_dec_and_test_nonnegative (value);
  107. else
  108. c = atomic_sub_and_test_nonnegative (count, value);
  109. if (c)
  110. break;
  111. /* We didn't get the lock. Bump the count back up. */
  112. if (count == 1)
  113. atomic_inc (value);
  114. else
  115. atomic_add (count, value);
  116. }
  117. atomic_dec (&sem->semaphore.nwaiters);
  118. return ret;
  119. }
  120. int _DkSemaphoreAcquireTimeout (PAL_HANDLE sem, int count, uint64_t timeout)
  121. {
  122. /* Pass it up to the no-timeout version if no timeout requested */
  123. if (timeout == -1)
  124. return _DkSemaphoreAcquire(sem, count);
  125. /* optimization: use it as a mutex */
  126. if (sem->semaphore.max_value == 1) {
  127. struct mutex_handle * mut = & sem->semaphore.value.mut;
  128. return _DkMutexLockTimeout(mut, timeout);
  129. }
  130. if (count > sem->semaphore.max_value)
  131. return -PAL_ERROR_INVAL;
  132. struct atomic_int * value = &sem->semaphore.value.i;
  133. int c = 0;
  134. if (!value)
  135. return -PAL_ERROR_BADHANDLE;
  136. if (count == 1)
  137. c = atomic_dec_and_test_nonnegative (value);
  138. else
  139. c = atomic_sub_and_test_nonnegative (count, value);
  140. if (c)
  141. return 0;
  142. /* We didn't get the lock. Bump the count back up. */
  143. if (count == 1)
  144. atomic_inc ((struct atomic_int *) value);
  145. else
  146. atomic_add (count, (struct atomic_int *) value);
  147. if (!timeout)
  148. return -PAL_ERROR_TRYAGAIN;
  149. unsigned long waittime = timeout;
  150. int ret = 0;
  151. atomic_inc (&sem->semaphore.nwaiters);
  152. while (1) {
  153. ret = ocall_futex((int *) &value->counter, FUTEX_WAIT, 0, timeout >= 0 ? &waittime : NULL);
  154. if (ret < 0) {
  155. if (ret == -PAL_ERROR_TRYAGAIN)
  156. ret = 0;
  157. else
  158. break;
  159. }
  160. if (count == 1)
  161. c = atomic_dec_and_test_nonnegative (value);
  162. else
  163. c = atomic_sub_and_test_nonnegative (count, value);
  164. if (c)
  165. break;
  166. }
  167. /* We didn't get the lock. Bump the count back up. */
  168. if (count == 1)
  169. atomic_inc (value);
  170. else
  171. atomic_add (count, value);
  172. atomic_dec (&sem->semaphore.nwaiters);
  173. return ret;
  174. }
  175. void _DkSemaphoreRelease (PAL_HANDLE sem, int count)
  176. {
  177. /* optimization: use it as a mutex */
  178. if (sem->semaphore.max_value == 1) {
  179. struct mutex_handle * mut =
  180. &sem->semaphore.value.mut;
  181. int ret = _DkMutexUnlock(mut);
  182. if (ret < 0)
  183. _DkRaiseFailure(ret);
  184. return;
  185. }
  186. struct atomic_int * value = &sem->semaphore.value.i;
  187. if (count == 1)
  188. atomic_inc (value);
  189. else
  190. atomic_add (count, value);
  191. int nwaiters = atomic_read (&sem->semaphore.nwaiters);
  192. if (nwaiters > 0)
  193. ocall_futex((int *) &value->counter, FUTEX_WAKE, nwaiters, NULL);
  194. }
  195. int _DkSemaphoreGetCurrentCount (PAL_HANDLE sem)
  196. {
  197. if (sem->semaphore.max_value == 1) {
  198. struct mutex_handle * m = &sem->semaphore.value.mut;
  199. return m->b.locked;
  200. }
  201. int c = atomic_read(&sem->semaphore.value.i);
  202. return sem->semaphore.max_value - c;
  203. }