db_mutex.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_mutex.c
  15. *
  16. * This file contains APIs that provide operations of (futex based) mutexes.
  17. * Based on "Mutexes and Condition Variables using Futexes"
  18. * (http://locklessinc.com/articles/mutex_cv_futex)
  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 <limits.h>
  28. #include <atomic.h>
  29. #include <errno.h>
  30. #include <sys/time.h>
  31. #include <unistd.h>
  32. #if defined(__i386__)
  33. #define RMB() asm volatile("lock; addl $0,0(%%esp)" ::: "memory")
  34. #define CPU_RELAX() asm volatile("rep; nop" ::: "memory");
  35. #endif
  36. #if defined(__x86_64__)
  37. #include <unistd.h>
  38. #define RMB() asm volatile("lfence" ::: "memory")
  39. #define CPU_RELAX() asm volatile("rep; nop" ::: "memory");
  40. #endif
  41. #define MUTEX_SPINLOCK_TIMES 20
  42. int _DkMutexLockTimeout (struct mutex_handle * mut, int timeout)
  43. {
  44. int i, c = 0;
  45. if (timeout == -1)
  46. return -_DkMutexLock(mut);
  47. struct atomic_int * m = &mut->value;
  48. /* Spin and try to take lock */
  49. for (i = 0 ; i < MUTEX_SPINLOCK_TIMES ; i++)
  50. {
  51. c = atomic_dec_and_test(m);
  52. if (c)
  53. goto success;
  54. CPU_RELAX();
  55. }
  56. /* The lock is now contended */
  57. int ret;
  58. if (timeout == 0) {
  59. ret = c ? 0 : -PAL_ERROR_TRYAGAIN;
  60. goto out;
  61. }
  62. while (!c) {
  63. int val = atomic_read(m);
  64. if (val == 1)
  65. goto again;
  66. struct timespec waittime;
  67. long sec = timeout / 1000000;
  68. long microsec = timeout - (sec * 1000000);
  69. waittime.tv_sec = sec;
  70. waittime.tv_nsec = microsec * 1000;
  71. ret = INLINE_SYSCALL(_umtx_op, 5, m, UMTX_OP_WAIT_UINT, val,
  72. NULL, &waittime);
  73. if (IS_ERR(ret) && ERRNO(ret) != EWOULDBLOCK) {
  74. ret = unix_to_pal_error(ERRNO(ret));
  75. goto out;
  76. }
  77. #ifdef DEBUG_MUTEX
  78. if (IS_ERR(ret))
  79. printf("mutex held by thread %d\n", mut->owner);
  80. #endif
  81. again:
  82. /* Upon wakeup, we still need to check whether mutex is unlocked or
  83. * someone else took it.
  84. * If c==0 upon return from xchg (i.e., the older value of m==0), we
  85. * will exit the loop. Else, we sleep again (through a futex call).
  86. */
  87. c = atomic_dec_and_test(m);
  88. }
  89. success:
  90. #ifdef DEBUG_MUTEX
  91. mut->owner = INLINE_SYSCALL(gettid, 0);
  92. #endif
  93. ret = 0;
  94. out:
  95. return ret;
  96. }
  97. int _DkMutexLock (struct mutex_handle * mut)
  98. {
  99. int i, c = 0;
  100. int ret;
  101. struct atomic_int * m = &mut->value;
  102. /* Spin and try to take lock */
  103. for (i = 0; i < MUTEX_SPINLOCK_TIMES; i++) {
  104. c = atomic_dec_and_test(m);
  105. if (c)
  106. goto success;
  107. CPU_RELAX();
  108. }
  109. /* The lock is now contended */
  110. while (!c) {
  111. int val = atomic_read(m);
  112. if (val == 1)
  113. goto again;
  114. ret = INLINE_SYSCALL(_umtx_op, 5, m, UMTX_OP_WAIT, val, NULL, NULL);
  115. if (IS_ERR(ret) && ERRNO(ret) != EWOULDBLOCK) {
  116. ret = unix_to_pal_error(ERRNO(ret));
  117. goto out;
  118. }
  119. #ifdef DEBUG_MUTEX
  120. if (IS_ERR(ret))
  121. printf("mutex held by thread %d\n", mut->owner);
  122. #endif
  123. again:
  124. /* Upon wakeup, we still need to check whether mutex is unlocked or
  125. * someone else took it.
  126. * If c==0 upon return from xchg (i.e., the older value of m==0), we
  127. * will exit the loop. Else, we sleep again (through a futex call).
  128. */
  129. c = atomic_dec_and_test(m);
  130. }
  131. success:
  132. #ifdef DEBUG_MUTEX
  133. mut->owner = INLINE_SYSCALL(gettid, 0);
  134. #endif
  135. ret = 0;
  136. out:
  137. return ret;
  138. }
  139. int _DkMutexUnlock (struct mutex_handle * mut)
  140. {
  141. struct atomic_int * m = &mut->value;
  142. int ret = 0;
  143. int must_wake = 0;
  144. #ifdef DEBUG_MUTEX
  145. mut->owner = 0;
  146. #endif
  147. /* Unlock, and if not contended then exit. */
  148. if (atomic_read(m) < 0)
  149. must_wake = 1;
  150. atomic_set(m, 1);
  151. if (must_wake) {
  152. /* We need to wake someone up */
  153. ret = INLINE_SYSCALL(_umtx_op, 5, m, UMTX_OP_WAKE, 1,
  154. NULL, NULL);
  155. }
  156. if (IS_ERR(ret)) {
  157. ret = -PAL_ERROR_TRYAGAIN;
  158. goto out;
  159. }
  160. ret = 0;
  161. out:
  162. return ret;
  163. }
  164. int _DkMutexAcquireTimeout (PAL_HANDLE handle, int timeout)
  165. {
  166. return _DkMutexLockTimeout(&handle->mutex.mut, timeout);
  167. }
  168. void _DkMutexRelease (PAL_HANDLE handle)
  169. {
  170. _DkMutexUnlock(&handle->mutex.mut);
  171. return;
  172. }
  173. static int mutex_wait (PAL_HANDLE handle, uint64_t timeout)
  174. {
  175. return _DkMutexAcquireTimeout(handle, timeout);
  176. }
  177. struct handle_ops mutex_ops = {
  178. .wait = &mutex_wait,
  179. };
  180. int _DkMutexCreate (PAL_HANDLE *handle, int count) {
  181. PAL_HANDLE mut = malloc(HANDLE_SIZE(mutex));
  182. SET_HANDLE_TYPE(mut, mutex);
  183. atomic_set(&mut->mutex.mut.value, 0);
  184. *handle = mut;
  185. return 0;
  186. }