mutex_adaptive_i.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2017 Agustina Arzille.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program 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. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef KERN_MUTEX_ADAPTIVE_I_H
  18. #define KERN_MUTEX_ADAPTIVE_I_H
  19. #ifndef KERN_MUTEX_H
  20. #error "don't include <kern/mutex/mutex_adaptive_i.h> directly," \
  21. " use <kern/mutex.h> instead"
  22. #endif
  23. #include <assert.h>
  24. #include <errno.h>
  25. #include <stdbool.h>
  26. #include <stdint.h>
  27. #include <kern/atomic.h>
  28. #include <kern/init.h>
  29. #include <kern/macros.h>
  30. #include <kern/mutex_types.h>
  31. #include <kern/thread.h>
  32. /*
  33. * Mutex flags.
  34. *
  35. * The "contended" flag indicates that threads are waiting for the mutex
  36. * to be unlocked, potentially spinning on the owner. It forces threads
  37. * trying to lock the mutex as well as the owner to take the slow path.
  38. */
  39. #define MUTEX_ADAPTIVE_CONTENDED 0x1UL
  40. static inline void
  41. mutex_adaptive_init(struct mutex *mutex)
  42. {
  43. mutex->owner = 0;
  44. }
  45. static inline bool
  46. mutex_adaptive_locked(const struct mutex *mutex)
  47. {
  48. uintptr_t owner;
  49. owner = atomic_load(&mutex->owner, ATOMIC_RELAXED);
  50. return (owner != 0);
  51. }
  52. static inline int
  53. mutex_adaptive_lock_fast(struct mutex *mutex)
  54. {
  55. uintptr_t owner;
  56. owner = atomic_cas(&mutex->owner, 0,
  57. (uintptr_t)thread_self(), ATOMIC_ACQUIRE);
  58. if (unlikely(owner != 0)) {
  59. return EBUSY;
  60. }
  61. return 0;
  62. }
  63. static inline int
  64. mutex_adaptive_unlock_fast(struct mutex *mutex)
  65. {
  66. uintptr_t owner;
  67. owner = atomic_cas(&mutex->owner, (uintptr_t)thread_self(),
  68. 0, ATOMIC_RELEASE);
  69. if (unlikely(owner & MUTEX_ADAPTIVE_CONTENDED)) {
  70. return EBUSY;
  71. }
  72. return 0;
  73. }
  74. void mutex_adaptive_lock_slow(struct mutex *mutex);
  75. int mutex_adaptive_timedlock_slow(struct mutex *mutex, uint64_t ticks);
  76. void mutex_adaptive_unlock_slow(struct mutex *mutex);
  77. /*
  78. * Interface exported to the public mutex header.
  79. */
  80. #define mutex_impl_init mutex_adaptive_init
  81. #define mutex_impl_locked mutex_adaptive_locked
  82. static inline int
  83. mutex_impl_trylock(struct mutex *mutex)
  84. {
  85. return mutex_adaptive_lock_fast(mutex);
  86. }
  87. static inline void
  88. mutex_impl_lock(struct mutex *mutex)
  89. {
  90. int error;
  91. error = mutex_adaptive_lock_fast(mutex);
  92. if (unlikely(error)) {
  93. mutex_adaptive_lock_slow(mutex);
  94. }
  95. }
  96. static inline int
  97. mutex_impl_timedlock(struct mutex *mutex, uint64_t ticks)
  98. {
  99. int error;
  100. error = mutex_adaptive_lock_fast(mutex);
  101. if (unlikely(error)) {
  102. error = mutex_adaptive_timedlock_slow(mutex, ticks);
  103. }
  104. return error;
  105. }
  106. static inline void
  107. mutex_impl_unlock(struct mutex *mutex)
  108. {
  109. int error;
  110. error = mutex_adaptive_unlock_fast(mutex);
  111. if (unlikely(error)) {
  112. mutex_adaptive_unlock_slow(mutex);
  113. }
  114. }
  115. /*
  116. * Mutex init operations. See kern/mutex.h.
  117. */
  118. #define mutex_impl_bootstrap mutex_adaptive_bootstrap
  119. INIT_OP_DECLARE(mutex_adaptive_bootstrap);
  120. #define mutex_impl_setup mutex_adaptive_setup
  121. INIT_OP_DECLARE(mutex_adaptive_setup);
  122. #endif /* KERN_MUTEX_ADAPTIVE_I_H */