db_mutex.c 5.2 KB

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