db_mutex.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 <cmpxchg.h>
  32. #include <errno.h>
  33. #include <sys/time.h>
  34. #include <unistd.h>
  35. #if defined(__i386__)
  36. #define rmb() asm volatile("lock; addl $0,0(%%esp)" ::: "memory")
  37. #define cpu_relax() asm volatile("rep; nop" ::: "memory");
  38. #endif
  39. #if defined(__x86_64__)
  40. #include <unistd.h>
  41. #define rmb() asm volatile("lfence" ::: "memory")
  42. #define cpu_relax() asm volatile("rep; nop" ::: "memory");
  43. #endif
  44. #define MUTEX_SPINLOCK_TIMES 20
  45. int _DkMutexLockTimeout (struct mutex_handle * mut, int timeout)
  46. {
  47. int i, c = 0;
  48. if (timeout == -1)
  49. return -_DkMutexLock(mut);
  50. struct atomic_int * m = &mut->value;
  51. /* Spin and try to take lock */
  52. for (i = 0 ; i < MUTEX_SPINLOCK_TIMES ; i++)
  53. {
  54. c = atomic_dec_and_test(m);
  55. if (c)
  56. goto success;
  57. cpu_relax();
  58. }
  59. /* The lock is now contended */
  60. int ret;
  61. if (timeout == 0) {
  62. ret = c ? 0 : -PAL_ERROR_TRYAGAIN;
  63. goto out;
  64. }
  65. while (!c) {
  66. int val = atomic_read(m);
  67. if (val == 1)
  68. goto again;
  69. struct timespec waittime;
  70. long sec = timeout / 1000000;
  71. long microsec = timeout - (sec * 1000000);
  72. waittime.tv_sec = sec;
  73. waittime.tv_nsec = microsec * 1000;
  74. ret = INLINE_SYSCALL(_umtx_op, 5, m, UMTX_OP_WAIT_UINT, val,
  75. NULL, &waittime);
  76. if (IS_ERR(ret) && ERRNO(ret) != EWOULDBLOCK) {
  77. ret = unix_to_pal_error(ERRNO(ret));
  78. goto out;
  79. }
  80. #ifdef DEBUG_MUTEX
  81. if (IS_ERR(ret))
  82. printf("mutex held by thread %d\n", mut->owner);
  83. #endif
  84. again:
  85. /* Upon wakeup, we still need to check whether mutex is unlocked or
  86. * someone else took it.
  87. * If c==0 upon return from xchg (i.e., the older value of m==0), we
  88. * will exit the loop. Else, we sleep again (through a futex call).
  89. */
  90. c = atomic_dec_and_test(m);
  91. }
  92. success:
  93. #ifdef DEBUG_MUTEX
  94. mut->owner = INLINE_SYSCALL(gettid, 0);
  95. #endif
  96. ret = 0;
  97. out:
  98. return ret;
  99. }
  100. int _DkMutexLock (struct mutex_handle * mut)
  101. {
  102. int i, c = 0;
  103. int ret;
  104. struct atomic_int * m = &mut->value;
  105. /* Spin and try to take lock */
  106. for (i = 0; i < MUTEX_SPINLOCK_TIMES; i++) {
  107. c = atomic_dec_and_test(m);
  108. if (c)
  109. goto success;
  110. cpu_relax();
  111. }
  112. /* The lock is now contended */
  113. while (!c) {
  114. int val = atomic_read(m);
  115. if (val == 1)
  116. goto again;
  117. ret = INLINE_SYSCALL(_umtx_op, 5, m, UMTX_OP_WAIT, val, NULL, NULL);
  118. if (IS_ERR(ret) && ERRNO(ret) != EWOULDBLOCK) {
  119. ret = unix_to_pal_error(ERRNO(ret));
  120. goto out;
  121. }
  122. #ifdef DEBUG_MUTEX
  123. if (IS_ERR(ret))
  124. printf("mutex held by thread %d\n", mut->owner);
  125. #endif
  126. again:
  127. /* Upon wakeup, we still need to check whether mutex is unlocked or
  128. * someone else took it.
  129. * If c==0 upon return from xchg (i.e., the older value of m==0), we
  130. * will exit the loop. Else, we sleep again (through a futex call).
  131. */
  132. c = atomic_dec_and_test(m);
  133. }
  134. success:
  135. #ifdef DEBUG_MUTEX
  136. mut->owner = INLINE_SYSCALL(gettid, 0);
  137. #endif
  138. ret = 0;
  139. out:
  140. return ret;
  141. }
  142. int _DkMutexUnlock (struct mutex_handle * mut)
  143. {
  144. struct atomic_int * m = &mut->value;
  145. int ret = 0;
  146. int must_wake = 0;
  147. #ifdef DEBUG_MUTEX
  148. mut->owner = 0;
  149. #endif
  150. /* Unlock, and if not contended then exit. */
  151. if (atomic_read(m) < 0)
  152. must_wake = 1;
  153. atomic_set(m, 1);
  154. if (must_wake) {
  155. /* We need to wake someone up */
  156. ret = INLINE_SYSCALL(_umtx_op, 5, m, UMTX_OP_WAKE, 1,
  157. NULL, NULL);
  158. }
  159. if (IS_ERR(ret)) {
  160. ret = -PAL_ERROR_TRYAGAIN;
  161. goto out;
  162. }
  163. ret = 0;
  164. out:
  165. return ret;
  166. }