db_mutex.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #ifdef __i386__
  37. # define barrier() asm volatile("" ::: "memory");
  38. # define rmb() asm volatile("lock; addl $0,0(%%esp)" ::: "memory")
  39. # define cpu_relax() asm volatile("rep; nop" ::: "memory");
  40. #endif
  41. #ifdef __x86_64__
  42. # include <unistd.h>
  43. # define barrier() asm volatile("" ::: "memory");
  44. # define rmb() asm volatile("lfence" ::: "memory")
  45. # define cpu_relax() asm volatile("rep; nop" ::: "memory");
  46. #endif
  47. #define MUTEX_SPINLOCK_TIMES 100
  48. int _DkMutexLockTimeout (struct mutex_handle * m, uint64_t timeout)
  49. {
  50. int ret = 0;
  51. #ifdef DEBUG_MUTEX
  52. int tid = INLINE_SYSCALL(gettid, 0);
  53. #endif
  54. if (timeout == -1)
  55. return -_DkMutexLock(m);
  56. if (!xchg(&m->b.locked, 1))
  57. goto success;
  58. if (timeout == 0) {
  59. ret = -PAL_ERROR_TRYAGAIN;
  60. goto out;
  61. }
  62. while (xchg(&m->u, 257) & 1) {
  63. struct timespec waittime;
  64. long sec = timeout / 1000000;
  65. long microsec = timeout - (sec * 1000000);
  66. waittime.tv_sec = sec;
  67. waittime.tv_nsec = microsec * 1000;
  68. ret = INLINE_SYSCALL(futex, 6, m, FUTEX_WAIT, 257, &waittime, NULL, 0);
  69. if (IS_ERR(ret)) {
  70. if (ERRNO(ret) == EWOULDBLOCK) {
  71. xchg(&m->b.contended, 0);
  72. ret = -PAL_ERROR_TRYAGAIN;
  73. goto out;
  74. }
  75. #ifdef DEBUG_MUTEX
  76. printf("futex failed (err = %d)\n", ERRNO(ret));
  77. #endif
  78. ret = unix_to_pal_error(ERRNO(ret));
  79. goto out;
  80. }
  81. }
  82. success:
  83. #ifdef DEBUG_MUTEX
  84. m->owner = tid;
  85. #endif
  86. ret = 0;
  87. out:
  88. #ifdef DEBUG_MUTEX
  89. if (ret < 0)
  90. printf("mutex failed (%s, tid = %d)\n", PAL_STRERROR(ret), tid);
  91. #endif
  92. return ret;
  93. }
  94. int _DkMutexLock (struct mutex_handle * m)
  95. {
  96. int ret = 0, i;
  97. #ifdef DEBUG_MUTEX
  98. int tid = INLINE_SYSCALL(gettid, 0);
  99. #endif
  100. /* Spin and try to take lock */
  101. for (i = 0; i < MUTEX_SPINLOCK_TIMES; i++) {
  102. if (!xchg(&m->b.locked, 1))
  103. goto success;
  104. cpu_relax();
  105. }
  106. while (xchg(&m->u, 257) & 1) {
  107. ret = INLINE_SYSCALL(futex, 6, m, FUTEX_WAIT, 257, NULL, NULL, 0);
  108. if (IS_ERR(ret) &&
  109. ERRNO(ret) != EWOULDBLOCK) {
  110. #ifdef DEBUG_MUTEX
  111. printf("futex failed (err = %d)\n", ERRNO(ret));
  112. #endif
  113. ret = unix_to_pal_error(ERRNO(ret));
  114. goto out;
  115. }
  116. }
  117. success:
  118. #ifdef DEBUG_MUTEX
  119. m->owner = tid;
  120. #endif
  121. ret = 0;
  122. out:
  123. #ifdef DEBUG_MUTEX
  124. if (ret < 0)
  125. printf("mutex failed (%s, tid = %d)\n", PAL_STRERROR(ret), tid);
  126. #endif
  127. return ret;
  128. }
  129. int _DkMutexUnlock (struct mutex_handle * m)
  130. {
  131. int ret = 0, i;
  132. #ifdef DEBUG_MUTEX
  133. m->owner = 0;
  134. #endif
  135. /* Unlock, and if not contended then exit. */
  136. if ((m->u == 1) && (cmpxchg(&m->u, 1, 0) == 1)) return 0;
  137. m->b.locked = 0;
  138. barrier();
  139. /* Spin and try to take lock */
  140. for (i = 0; i < MUTEX_SPINLOCK_TIMES * 2; i++) {
  141. if (m->b.locked)
  142. goto success;
  143. cpu_relax();
  144. }
  145. m->b.contended = 0;
  146. /* We need to wake someone up */
  147. INLINE_SYSCALL(futex, 6, m, FUTEX_WAKE, 1, NULL, NULL, 0);
  148. success:
  149. ret = 0;
  150. out:
  151. return ret;
  152. }