spinlock_linux.inc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2018 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // This file is a Linux-specific part of spinlock_wait.cc
  16. #include <linux/futex.h>
  17. #include <sys/syscall.h>
  18. #include <unistd.h>
  19. #include <atomic>
  20. #include <climits>
  21. #include <cstdint>
  22. #include <ctime>
  23. #include "absl/base/attributes.h"
  24. #include "absl/base/internal/errno_saver.h"
  25. // The SpinLock lockword is `std::atomic<uint32_t>`. Here we assert that
  26. // `std::atomic<uint32_t>` is bitwise equivalent of the `int` expected
  27. // by SYS_futex. We also assume that reads/writes done to the lockword
  28. // by SYS_futex have rational semantics with regard to the
  29. // std::atomic<> API. C++ provides no guarantees of these assumptions,
  30. // but they are believed to hold in practice.
  31. static_assert(sizeof(std::atomic<uint32_t>) == sizeof(int),
  32. "SpinLock lockword has the wrong size for a futex");
  33. // Some Android headers are missing these definitions even though they
  34. // support these futex operations.
  35. #ifdef __BIONIC__
  36. #ifndef SYS_futex
  37. #define SYS_futex __NR_futex
  38. #endif
  39. #ifndef FUTEX_PRIVATE_FLAG
  40. #define FUTEX_PRIVATE_FLAG 128
  41. #endif
  42. #endif
  43. #if defined(__NR_futex_time64) && !defined(SYS_futex_time64)
  44. #define SYS_futex_time64 __NR_futex_time64
  45. #endif
  46. #if defined(SYS_futex_time64) && !defined(SYS_futex)
  47. #define SYS_futex SYS_futex_time64
  48. #endif
  49. extern "C" {
  50. ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockDelay)(
  51. std::atomic<uint32_t> *w, uint32_t value, int,
  52. absl::base_internal::SchedulingMode) {
  53. absl::base_internal::ErrnoSaver errno_saver;
  54. syscall(SYS_futex, w, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, value, nullptr);
  55. }
  56. ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalSpinLockWake)(
  57. std::atomic<uint32_t> *w, bool all) {
  58. syscall(SYS_futex, w, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, all ? INT_MAX : 1, 0);
  59. }
  60. } // extern "C"