HazptrRec.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2018-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <folly/synchronization/Hazptr-fwd.h>
  18. #include <folly/concurrency/CacheLocality.h>
  19. #include <atomic>
  20. namespace folly {
  21. /**
  22. * hazptr_rec:
  23. *
  24. * Contains the actual hazard pointer.
  25. */
  26. template <template <typename> class Atom>
  27. class alignas(hardware_destructive_interference_size) hazptr_rec {
  28. Atom<const void*> hazptr_{nullptr}; // the hazard pointer
  29. hazptr_domain<Atom>* domain_;
  30. hazptr_rec* next_;
  31. Atom<bool> active_{false};
  32. friend class hazptr_domain<Atom>;
  33. friend class hazptr_holder<Atom>;
  34. friend class hazptr_tc_entry<Atom>;
  35. const void* hazptr() const noexcept {
  36. return hazptr_.load(std::memory_order_acquire);
  37. }
  38. FOLLY_ALWAYS_INLINE void reset_hazptr(const void* p = nullptr) noexcept {
  39. hazptr_.store(p, std::memory_order_release);
  40. }
  41. bool active() const noexcept {
  42. return active_.load(std::memory_order_acquire);
  43. }
  44. void set_active() noexcept {
  45. active_.store(true, std::memory_order_relaxed);
  46. }
  47. bool try_acquire() noexcept {
  48. bool a = active();
  49. return !a &&
  50. active_.compare_exchange_strong(
  51. a, true, std::memory_order_release, std::memory_order_relaxed);
  52. }
  53. void release() noexcept {
  54. active_.store(false, std::memory_order_release);
  55. }
  56. hazptr_rec<Atom>* next() {
  57. return next_;
  58. }
  59. void set_next(hazptr_rec<Atom>* rec) {
  60. next_ = rec;
  61. }
  62. FOLLY_ALWAYS_INLINE hazptr_domain<Atom>* domain() {
  63. return domain_;
  64. }
  65. void set_domain(hazptr_domain<Atom>* dom) {
  66. domain_ = dom;
  67. }
  68. }; // hazptr_rec
  69. } // namespace folly