db_mutex.c 4.2 KB

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