mutex.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2017 Richard Braun.
  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. * Mutual-exclusion locks.
  18. */
  19. #ifndef KERN_MUTEX_H
  20. #define KERN_MUTEX_H
  21. #include <assert.h>
  22. #include <errno.h>
  23. #include <stdbool.h>
  24. #include <stdint.h>
  25. #include <kern/atomic.h>
  26. #include <kern/init.h>
  27. #include <kern/macros.h>
  28. #define MUTEX_UNLOCKED 0
  29. #define MUTEX_LOCKED 1
  30. #define MUTEX_CONTENDED 2
  31. struct mutex
  32. {
  33. uint32_t state;
  34. };
  35. static inline void
  36. mutex_init (struct mutex *mutex)
  37. {
  38. mutex->state = MUTEX_UNLOCKED;
  39. }
  40. static inline bool
  41. mutex_locked (const struct mutex *mutex)
  42. {
  43. return (atomic_load_rlx (&mutex->state) != MUTEX_UNLOCKED);
  44. }
  45. static inline int
  46. mutex_trylock (struct mutex *mutex)
  47. {
  48. uint32_t state = atomic_cas_acq (&mutex->state,
  49. MUTEX_UNLOCKED, MUTEX_LOCKED);
  50. return (state != MUTEX_UNLOCKED ? EBUSY : 0);
  51. }
  52. static inline int
  53. mutex_unlock_fast (struct mutex *mutex)
  54. {
  55. uint32_t state = atomic_swap_rel (&mutex->state, MUTEX_UNLOCKED);
  56. return (state == MUTEX_CONTENDED ? EBUSY : 0);
  57. }
  58. void mutex_lock_slow (struct mutex *mutex);
  59. int mutex_timedlock_slow (struct mutex *mutex, uint64_t ticks);
  60. void mutex_unlock_slow (struct mutex *mutex);
  61. static inline void
  62. mutex_lock (struct mutex *mutex)
  63. {
  64. if (unlikely (mutex_trylock (mutex) != 0))
  65. mutex_lock_slow (mutex);
  66. }
  67. static inline int
  68. mutex_timedlock (struct mutex *mutex, uint64_t ticks)
  69. {
  70. if (likely (mutex_trylock (mutex) == 0))
  71. return (0);
  72. return (mutex_timedlock_slow (mutex, ticks));
  73. }
  74. static inline void
  75. mutex_unlock (struct mutex *mutex)
  76. {
  77. if (unlikely (mutex_unlock_fast (mutex) != 0))
  78. mutex_unlock_slow (mutex);
  79. }
  80. // Mutex guards.
  81. static inline void
  82. mutex_guard_fini (void *ptr)
  83. {
  84. mutex_unlock (*(struct mutex **)ptr);
  85. }
  86. #define MUTEX_GUARD(mtx) \
  87. CLEANUP (mutex_guard_fini) __unused _Auto UNIQ(mg) = \
  88. ({ \
  89. struct mutex *mutex_ = (mtx); \
  90. mutex_lock (mutex_); \
  91. mutex_; \
  92. })
  93. /*
  94. * This init operation provides :
  95. * - uncontended mutex locking
  96. *
  97. * Contended locking may only occur after starting the scheduler.
  98. */
  99. INIT_OP_DECLARE (mutex_setup);
  100. #endif