null_mutex.h 967 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. #include <atomic>
  5. #include <utility>
  6. // null, no cost dummy "mutex" and dummy "atomic" int
  7. namespace spdlog {
  8. namespace details {
  9. struct null_mutex
  10. {
  11. void lock() const {}
  12. void unlock() const {}
  13. };
  14. struct null_atomic_int
  15. {
  16. int value;
  17. null_atomic_int() = default;
  18. explicit null_atomic_int(int new_value)
  19. : value(new_value)
  20. {}
  21. int load(std::memory_order = std::memory_order_relaxed) const
  22. {
  23. return value;
  24. }
  25. void store(int new_value, std::memory_order = std::memory_order_relaxed)
  26. {
  27. value = new_value;
  28. }
  29. int exchange(int new_value, std::memory_order = std::memory_order_relaxed)
  30. {
  31. std::swap(new_value, value);
  32. return new_value; // return value before the call
  33. }
  34. };
  35. } // namespace details
  36. } // namespace spdlog