lwlock.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "lwlock.hpp"
  2. #include "xatomic.hpp"
  3. #if (defined (linux) || defined (__linux) || defined (__linux__)) && \
  4. defined (__BYTE_ORDER__)
  5. #include <linux/futex.h>
  6. #include <unistd.h>
  7. #include <syscall.h>
  8. #ifndef FUTEX_PRIVATE_FLAG
  9. # define FUTEX_PRIVATE_FLAG 0
  10. #endif
  11. template <bool Wide>
  12. void* futex_ptr (void *ptr)
  13. {
  14. return (ptr);
  15. }
  16. template <>
  17. void* futex_ptr<true> (void *ptr)
  18. {
  19. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  20. return (ptr);
  21. #else
  22. return ((int *)ptr + 1);
  23. #endif
  24. }
  25. static inline void
  26. lwlock_acquire (uintptr_t *ptr)
  27. {
  28. const int MAX_SPINS = 1000;
  29. if (xrcu::xatomic_cas_bool (ptr, 0, 1))
  30. return;
  31. while (true)
  32. {
  33. for (int i = 0; i < MAX_SPINS && *ptr != 0; ++i)
  34. xrcu::xatomic_spin_nop ();
  35. if (xrcu::xatomic_swap (ptr, 2) == 0)
  36. return;
  37. syscall (SYS_futex, futex_ptr<sizeof (int) < sizeof (void *)> (ptr),
  38. (long)(FUTEX_WAIT | FUTEX_PRIVATE_FLAG), 2l, 0ul);
  39. }
  40. }
  41. static inline void
  42. lwlock_release (uintptr_t *ptr)
  43. {
  44. if (xrcu::xatomic_swap (ptr, 0) != 1)
  45. syscall (SYS_futex, futex_ptr<sizeof (int) < sizeof (void *)> (ptr),
  46. (long)(FUTEX_WAKE | FUTEX_PRIVATE_FLAG), 1ul);
  47. }
  48. #else
  49. #include <thread>
  50. #include <atomic>
  51. static inline void
  52. lwlock_acquire (uintptr_t *ptr)
  53. {
  54. const int MAX_SPINS = 1000;
  55. while (true)
  56. {
  57. if (xrcu::xatomic_cas_bool (ptr, 0, 1))
  58. return;
  59. for (int i = 0; i < MAX_SPINS && *ptr != 0; ++i)
  60. xrcu::xatomic_spin_nop ();
  61. if (xrcu::xatomic_swap (ptr, 1) == 0)
  62. break;
  63. std::this_thread::sleep_for (std::chrono::milliseconds (1));
  64. }
  65. }
  66. static inline void
  67. lwlock_release (uintptr_t *ptr)
  68. {
  69. *ptr = 0;
  70. std::atomic_thread_fence (std::memory_order_release);
  71. }
  72. #endif
  73. namespace xrcu
  74. {
  75. void lwlock::acquire ()
  76. {
  77. lwlock_acquire (&this->lock);
  78. }
  79. void lwlock::release ()
  80. {
  81. lwlock_release (&this->lock);
  82. }
  83. } // namespace xrcu