db_mutex.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #define MUTEX_SPINLOCK_TIMES 20
  35. int _DkMutexLockTimeout (struct mutex_handle * mut, int timeout)
  36. {
  37. int i, c = 0;
  38. if (timeout == -1)
  39. return -_DkMutexLock(mut);
  40. struct atomic_int * m = &mut->value;
  41. /* Spin and try to take lock */
  42. for (i = 0 ; i < MUTEX_SPINLOCK_TIMES ; i++)
  43. {
  44. c = atomic_dec_and_test(m);
  45. if (c)
  46. goto success;
  47. cpu_relax();
  48. }
  49. /* The lock is now contended */
  50. int ret;
  51. if (timeout == 0) {
  52. ret = c ? 0 : -PAL_ERROR_TRYAGAIN;
  53. goto out;
  54. }
  55. unsigned long waittime = timeout;
  56. while (!c) {
  57. int val = atomic_read(m);
  58. if (val == 1)
  59. goto again;
  60. ret = ocall_futex((int *) &m->counter, FUTEX_WAIT, 2, timeout ? &waittime : NULL);
  61. if (ret < 0)
  62. goto out;
  63. again:
  64. /* Upon wakeup, we still need to check whether mutex is unlocked or
  65. * someone else took it.
  66. * If c==0 upon return from xchg (i.e., the older value of m==0), we
  67. * will exit the loop. Else, we sleep again (through a futex call).
  68. */
  69. c = atomic_dec_and_test(m);
  70. }
  71. success:
  72. ret = 0;
  73. out:
  74. return ret;
  75. }
  76. int _DkMutexLock (struct mutex_handle * mut)
  77. {
  78. int i, c = 0;
  79. int ret;
  80. struct atomic_int * m = &mut->value;
  81. /* Spin and try to take lock */
  82. for (i = 0; i < MUTEX_SPINLOCK_TIMES; i++) {
  83. c = atomic_dec_and_test(m);
  84. if (c)
  85. goto success;
  86. cpu_relax();
  87. }
  88. /* The lock is now contended */
  89. while (!c) {
  90. int val = atomic_read(m);
  91. if (val == 1)
  92. goto again;
  93. ret = ocall_futex((int *) &m->counter, FUTEX_WAIT, 2, NULL);
  94. if (ret < 0)
  95. goto out;
  96. again:
  97. /* Upon wakeup, we still need to check whether mutex is unlocked or
  98. * someone else took it.
  99. * If c==0 upon return from xchg (i.e., the older value of m==0), we
  100. * will exit the loop. Else, we sleep again (through a futex call).
  101. */
  102. c = atomic_dec_and_test(m);
  103. }
  104. success:
  105. ret = 0;
  106. out:
  107. return ret;
  108. }
  109. int _DkMutexUnlock (struct mutex_handle * mut)
  110. {
  111. int ret = 0;
  112. int must_wake = 0;
  113. struct atomic_int * m = &mut->value;
  114. /* Unlock, and if not contended then exit. */
  115. if (atomic_read(m) < 0)
  116. must_wake = 1;
  117. atomic_set(m, 1);
  118. if (must_wake) {
  119. /* We need to wake someone up */
  120. ret = ocall_futex((int *) &m->counter, FUTEX_WAKE, 1, NULL);
  121. if (ret < 0)
  122. goto out;
  123. }
  124. if (ret < 0) {
  125. ret = -PAL_ERROR_TRYAGAIN;
  126. goto out;
  127. }
  128. ret = 0;
  129. out:
  130. return ret;
  131. }