spdlog/include/spdlog/details/null_mutex.h

50 lines
1.0 KiB
C
Raw Normal View History

2019-06-04 05:09:16 +08:00
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2015-11-29 00:24:20 +08:00
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
2014-11-01 09:20:54 +08:00
2014-10-11 02:36:32 +08:00
#pragma once
#include <atomic>
2019-09-08 01:11:58 +08:00
#include <utility>
// null, no cost dummy "mutex" and dummy "atomic" int
2014-10-11 02:36:32 +08:00
2018-03-17 18:47:46 +08:00
namespace spdlog {
namespace details {
2014-10-11 02:36:32 +08:00
struct null_mutex
{
2019-09-08 01:11:58 +08:00
void lock() const {}
void unlock() const {}
bool try_lock() const
2014-12-21 08:47:04 +08:00
{
return true;
}
2014-10-11 02:36:32 +08:00
};
struct null_atomic_int
{
2016-04-20 16:57:49 +08:00
int value;
null_atomic_int() = default;
2019-09-08 01:11:58 +08:00
explicit null_atomic_int(int new_value)
: value(new_value)
{}
2019-09-08 01:11:58 +08:00
int load(std::memory_order = std::memory_order_relaxed) const
2016-04-20 16:57:49 +08:00
{
return value;
}
2019-09-08 01:11:58 +08:00
void store(int new_value, std::memory_order = std::memory_order_relaxed)
2016-04-20 16:57:49 +08:00
{
2019-09-08 01:11:58 +08:00
value = new_value;
}
int exchange(int new_value, std::memory_order = std::memory_order_relaxed)
{
std::swap(new_value, value);
return new_value; // return value before the call
2016-04-20 16:57:49 +08:00
}
};
2018-03-17 18:47:46 +08:00
} // namespace details
} // namespace spdlog