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. _DkMutexLock(mut);
  77. return 0;
  78. }
  79. if (count > sem->semaphore.max_value)
  80. return -PAL_ERROR_INVAL;
  81. struct atomic_int * value = &sem->semaphore.value.i;
  82. int c = 0;
  83. if (!value)
  84. return -PAL_ERROR_BADHANDLE;
  85. if (count == 1)
  86. c = atomic_dec_and_test_nonnegative (value);
  87. else
  88. c = atomic_sub_and_test_nonnegative (count, value);
  89. if (c)
  90. return 0;
  91. /* We didn't get the lock. Bump the count back up. */
  92. if (count == 1)
  93. atomic_inc (value);
  94. else
  95. atomic_add (count, value);
  96. int ret = 0;
  97. atomic_inc (&sem->semaphore.nwaiters);
  98. while (1) {
  99. ret = ocall_futex((int *) &value->counter, FUTEX_WAIT, 0, NULL);
  100. if (ret < 0) {
  101. if (ret == -PAL_ERROR_TRYAGAIN)
  102. ret = 0;
  103. else
  104. break;
  105. }
  106. if (count == 1)
  107. c = atomic_dec_and_test_nonnegative (value);
  108. else
  109. c = atomic_sub_and_test_nonnegative (count, value);
  110. if (c)
  111. break;
  112. /* We didn't get the lock. Bump the count back up. */
  113. if (count == 1)
  114. atomic_inc (value);
  115. else
  116. atomic_add (count, value);
  117. }
  118. atomic_dec (&sem->semaphore.nwaiters);
  119. return ret;
  120. }
  121. int _DkSemaphoreAcquireTimeout (PAL_HANDLE sem, int count, int timeout)
  122. {
  123. /* Pass it up to the no-timeout version if no timeout requested */
  124. if (timeout == -1)
  125. return _DkSemaphoreAcquire(sem, count);
  126. /* optimization: use it as a mutex */
  127. if (sem->semaphore.max_value == 1) {
  128. struct mutex_handle * mut = & sem->semaphore.value.mut;
  129. _DkMutexLockTimeout(mut, timeout);
  130. return 0;
  131. }
  132. if (count > sem->semaphore.max_value)
  133. return -PAL_ERROR_INVAL;
  134. struct atomic_int * value = &sem->semaphore.value.i;
  135. int c = 0;
  136. if (!value)
  137. return -PAL_ERROR_BADHANDLE;
  138. if (count == 1)
  139. c = atomic_dec_and_test_nonnegative (value);
  140. else
  141. c = atomic_sub_and_test_nonnegative (count, value);
  142. if (c)
  143. return 0;
  144. /* We didn't get the lock. Bump the count back up. */
  145. if (count == 1)
  146. atomic_inc ((struct atomic_int *) value);
  147. else
  148. atomic_add (count, (struct atomic_int *) value);
  149. if (!timeout)
  150. return 0;
  151. unsigned long waittime = timeout;
  152. int ret = 0;
  153. atomic_inc (&sem->semaphore.nwaiters);
  154. while (1) {
  155. ret = ocall_futex((int *) &value->counter, FUTEX_WAIT, 0, timeout >= 0 ? &waittime : NULL);
  156. if (ret < 0) {
  157. if (ret == -PAL_ERROR_TRYAGAIN)
  158. ret = 0;
  159. else
  160. break;
  161. }
  162. if (count == 1)
  163. c = atomic_dec_and_test_nonnegative (value);
  164. else
  165. c = atomic_sub_and_test_nonnegative (count, value);
  166. if (c)
  167. break;
  168. }
  169. /* We didn't get the lock. Bump the count back up. */
  170. if (count == 1)
  171. atomic_inc (value);
  172. else
  173. atomic_add (count, value);
  174. atomic_dec (&sem->semaphore.nwaiters);
  175. return ret;
  176. }
  177. void _DkSemaphoreRelease (PAL_HANDLE sem, int count)
  178. {
  179. /* optimization: use it as a mutex */
  180. if (sem->semaphore.max_value == 1) {
  181. struct mutex_handle * mut =
  182. &sem->semaphore.value.mut;
  183. _DkMutexUnlock(mut);
  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. }