db_mutex.c 5.8 KB

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