db_mutex.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. struct timespec waittime;
  68. long sec = timeout / 1000000;
  69. long microsec = timeout - (sec * 1000000);
  70. waittime.tv_sec = sec;
  71. waittime.tv_nsec = microsec * 1000;
  72. ret = INLINE_SYSCALL(futex, 6, m, FUTEX_WAIT, 2, &waittime, NULL, 0);
  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. /* Upon wakeup, we still need to check whether mutex is unlocked or
  82. * someone else took it.
  83. * If c==0 upon return from xchg (i.e., the older value of m==0), we
  84. * will exit the loop. Else, we sleep again (through a futex call).
  85. */
  86. c = atomic_dec_and_test(m);
  87. }
  88. success:
  89. #ifdef DEBUG_MUTEX
  90. mut->owner = INLINE_SYSCALL(gettid, 0);
  91. #endif
  92. ret = 0;
  93. out:
  94. return ret;
  95. }
  96. int _DkMutexLock (struct mutex_handle * mut)
  97. {
  98. int i, c = 0;
  99. int ret;
  100. struct atomic_int * m = &mut->value;
  101. /* Spin and try to take lock */
  102. for (i = 0; i < MUTEX_SPINLOCK_TIMES; i++) {
  103. c = atomic_dec_and_test(m);
  104. if (c)
  105. goto success;
  106. cpu_relax();
  107. }
  108. /* The lock is now contended */
  109. while (!c) {
  110. ret = INLINE_SYSCALL(futex, 6, m, FUTEX_WAIT, 2, NULL, NULL, 0);
  111. if (IS_ERR(ret) && ERRNO(ret) != EWOULDBLOCK) {
  112. ret = unix_to_pal_error(ERRNO(ret));
  113. goto out;
  114. }
  115. #ifdef DEBUG_MUTEX
  116. if (IS_ERR(ret))
  117. printf("mutex held by thread %d\n", mut->owner);
  118. #endif
  119. /* Upon wakeup, we still need to check whether mutex is unlocked or
  120. * someone else took it.
  121. * If c==0 upon return from xchg (i.e., the older value of m==0), we
  122. * will exit the loop. Else, we sleep again (through a futex call).
  123. */
  124. c = atomic_dec_and_test(m);
  125. }
  126. success:
  127. #ifdef DEBUG_MUTEX
  128. mut->owner = INLINE_SYSCALL(gettid, 0);
  129. #endif
  130. ret = 0;
  131. out:
  132. return ret;
  133. }
  134. int _DkMutexUnlock (struct mutex_handle * mut)
  135. {
  136. int ret = 0;
  137. int must_wake = 0;
  138. struct atomic_int * m = &mut->value;
  139. #ifdef DEBUG_MUTEX
  140. mut->owner = 0;
  141. #endif
  142. /* Unlock, and if not contended then exit. */
  143. if (atomic_read(m) < 0)
  144. must_wake = 1;
  145. atomic_set(m, 1);
  146. if (must_wake) {
  147. /* We need to wake someone up */
  148. ret = INLINE_SYSCALL(futex, 6, m, FUTEX_WAKE, 1, NULL, NULL, 0);
  149. }
  150. if (IS_ERR(ret)) {
  151. ret = -PAL_ERROR_TRYAGAIN;
  152. goto out;
  153. }
  154. ret = 0;
  155. out:
  156. return ret;
  157. }