db_semaphore.c 6.7 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 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, int 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, int timeout)
  126. {
  127. /* Pass it up to the no-timeout version if no timeout requested */
  128. if (timeout == -1)
  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. _DkMutexLockTimeout(mut, timeout);
  134. return 0;
  135. }
  136. if (count > sem->semaphore.max_value)
  137. return -PAL_ERROR_INVAL;
  138. struct atomic_int * value = &sem->semaphore.value.i;
  139. int c = 0;
  140. if (!value)
  141. return -PAL_ERROR_BADHANDLE;
  142. if (count == 1)
  143. c = atomic_dec_and_test_nonnegative (value);
  144. else
  145. c = atomic_sub_and_test_nonnegative (count, value);
  146. if (c)
  147. return 0;
  148. /* We didn't get the lock. Bump the count back up. */
  149. if (count == 1)
  150. atomic_inc (value);
  151. else
  152. atomic_add (count, value);
  153. if (!timeout)
  154. return 0;
  155. struct timespec waittime;
  156. long sec = timeout / 1000000;
  157. long microsec = timeout - (sec * 1000000);
  158. waittime.tv_sec = sec;
  159. waittime.tv_nsec = microsec * 1000;
  160. int ret = 0;
  161. atomic_inc (&sem->semaphore.nwaiters);
  162. while (1) {
  163. ret = INLINE_SYSCALL(futex, 6, value, FUTEX_WAIT, 0,
  164. &waittime, NULL, 0);
  165. if (ERRNO(ret) == EWOULDBLOCK) {
  166. ret = 0;
  167. } else {
  168. ret = unix_to_pal_error(ERRNO(ret));
  169. break;
  170. }
  171. if (count == 1)
  172. c = atomic_dec_and_test_nonnegative (value);
  173. else
  174. c = atomic_sub_and_test_nonnegative (count, value);
  175. if (c)
  176. break;
  177. }
  178. /* We didn't get the lock. Bump the count back up. */
  179. if (count == 1)
  180. atomic_inc (value);
  181. else
  182. atomic_add (count, value);
  183. atomic_dec (&sem->semaphore.nwaiters);
  184. return ret;
  185. }
  186. void _DkSemaphoreRelease (PAL_HANDLE sem, int count)
  187. {
  188. /* optimization: use it as a mutex */
  189. if (sem->semaphore.max_value == 1) {
  190. struct mutex_handle * mut =
  191. &sem->semaphore.value.mut;
  192. _DkMutexUnlock(mut);
  193. return;
  194. }
  195. struct atomic_int * value = &sem->semaphore.value.i;
  196. if (count == 1)
  197. atomic_inc (value);
  198. else
  199. atomic_add (count, value);
  200. int nwaiters = atomic_read (&sem->semaphore.nwaiters);
  201. if (nwaiters > 0)
  202. INLINE_SYSCALL(futex, 6, value, FUTEX_WAKE, nwaiters, NULL, NULL, 0);
  203. }
  204. int _DkSemaphoreGetCurrentCount (PAL_HANDLE sem)
  205. {
  206. if (sem->semaphore.max_value == 1) {
  207. struct mutex_handle * m = &sem->semaphore.value.mut;
  208. return m->b.locked;
  209. }
  210. int c = atomic_read(&sem->semaphore.value.i);
  211. return sem->semaphore.max_value - c;
  212. }