null_mutex.h 930 B

1234567891011121314151617181920212223242526272829303132333435
  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. void lock() const {}
  11. void unlock() const {}
  12. };
  13. struct null_atomic_int {
  14. int value;
  15. null_atomic_int() = default;
  16. explicit null_atomic_int(int new_value)
  17. : value(new_value) {}
  18. int load(std::memory_order = std::memory_order_relaxed) const { return value; }
  19. void store(int new_value, std::memory_order = std::memory_order_relaxed) { value = new_value; }
  20. int exchange(int new_value, std::memory_order = std::memory_order_relaxed) {
  21. std::swap(new_value, value);
  22. return new_value; // return value before the call
  23. }
  24. };
  25. } // namespace details
  26. } // namespace spdlog