lwlock.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Definitions for the RCU API.
  2. This file is part of xrcu.
  3. xrcu is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #include "lwlock.hpp"
  14. #include "xatomic.hpp"
  15. #if (defined (linux) || defined (__linux) || defined (__linux__)) && \
  16. defined (__BYTE_ORDER__)
  17. #include <linux/futex.h>
  18. #include <unistd.h>
  19. #include <syscall.h>
  20. #ifndef FUTEX_PRIVATE_FLAG
  21. # define FUTEX_PRIVATE_FLAG 0
  22. #endif
  23. template <bool Wide>
  24. void* futex_ptr (void *ptr)
  25. {
  26. return (ptr);
  27. }
  28. template <>
  29. void* futex_ptr<true> (void *ptr)
  30. {
  31. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  32. return (ptr);
  33. #else
  34. return ((int *)ptr + 1);
  35. #endif
  36. }
  37. static inline void
  38. lwlock_acquire (uintptr_t *ptr)
  39. {
  40. const int MAX_SPINS = 1000;
  41. if (xrcu::xatomic_cas_bool (ptr, 0, 1))
  42. return;
  43. while (true)
  44. {
  45. for (int i = 0; i < MAX_SPINS && *ptr != 0; ++i)
  46. xrcu::xatomic_spin_nop ();
  47. if (xrcu::xatomic_swap (ptr, 2) == 0)
  48. return;
  49. syscall (SYS_futex, futex_ptr<sizeof (int) < sizeof (void *)> (ptr),
  50. (long)(FUTEX_WAIT | FUTEX_PRIVATE_FLAG), 2l, 0ul);
  51. }
  52. }
  53. static inline void
  54. lwlock_release (uintptr_t *ptr)
  55. {
  56. if (xrcu::xatomic_swap (ptr, 0) != 1)
  57. syscall (SYS_futex, futex_ptr<sizeof (int) < sizeof (void *)> (ptr),
  58. (long)(FUTEX_WAKE | FUTEX_PRIVATE_FLAG), 1ul);
  59. }
  60. #else
  61. #include <thread>
  62. #include <atomic>
  63. static inline void
  64. lwlock_acquire (uintptr_t *ptr)
  65. {
  66. const int MAX_SPINS = 1000;
  67. while (true)
  68. {
  69. if (xrcu::xatomic_cas_bool (ptr, 0, 1))
  70. return;
  71. for (int i = 0; i < MAX_SPINS && *ptr != 0; ++i)
  72. xrcu::xatomic_spin_nop ();
  73. if (xrcu::xatomic_swap (ptr, 1) == 0)
  74. break;
  75. std::this_thread::sleep_for (std::chrono::milliseconds (1));
  76. }
  77. }
  78. static inline void
  79. lwlock_release (uintptr_t *ptr)
  80. {
  81. *ptr = 0;
  82. std::atomic_thread_fence (std::memory_order_release);
  83. }
  84. #endif
  85. namespace xrcu
  86. {
  87. void lwlock::acquire ()
  88. {
  89. lwlock_acquire (&this->lock);
  90. }
  91. void lwlock::release ()
  92. {
  93. lwlock_release (&this->lock);
  94. }
  95. } // namespace xrcu