mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-21 20:54:35 +02:00
Introduce SpinLock
This commit is contained in:
parent
5ead5b1f67
commit
4af450141b
@ -64,6 +64,7 @@ set(base_SOURCES
|
|||||||
shared-object.hpp
|
shared-object.hpp
|
||||||
singleton.hpp
|
singleton.hpp
|
||||||
socket.cpp socket.hpp
|
socket.cpp socket.hpp
|
||||||
|
spinlock.cpp spinlock.hpp
|
||||||
stacktrace.cpp stacktrace.hpp
|
stacktrace.cpp stacktrace.hpp
|
||||||
statsfunction.hpp
|
statsfunction.hpp
|
||||||
stdiostream.cpp stdiostream.hpp
|
stdiostream.cpp stdiostream.hpp
|
||||||
|
22
lib/base/spinlock.cpp
Normal file
22
lib/base/spinlock.cpp
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/* Icinga 2 | (c) 2020 Icinga GmbH | GPLv2+ */
|
||||||
|
|
||||||
|
#include "base/spinlock.hpp"
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
|
using namespace icinga;
|
||||||
|
|
||||||
|
void SpinLock::lock()
|
||||||
|
{
|
||||||
|
while (m_Locked.test_and_set(std::memory_order_acquire)) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SpinLock::try_lock()
|
||||||
|
{
|
||||||
|
return !m_Locked.test_and_set(std::memory_order_acquire);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpinLock::unlock()
|
||||||
|
{
|
||||||
|
m_Locked.clear(std::memory_order_release);
|
||||||
|
}
|
35
lib/base/spinlock.hpp
Normal file
35
lib/base/spinlock.hpp
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/* Icinga 2 | (c) 2020 Icinga GmbH | GPLv2+ */
|
||||||
|
|
||||||
|
#ifndef SPINLOCK_H
|
||||||
|
#define SPINLOCK_H
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
|
namespace icinga
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A spin lock.
|
||||||
|
*
|
||||||
|
* @ingroup base
|
||||||
|
*/
|
||||||
|
class SpinLock
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
SpinLock() = default;
|
||||||
|
SpinLock(const SpinLock&) = delete;
|
||||||
|
SpinLock& operator=(const SpinLock&) = delete;
|
||||||
|
SpinLock(SpinLock&&) = delete;
|
||||||
|
SpinLock& operator=(SpinLock&&) = delete;
|
||||||
|
|
||||||
|
void lock();
|
||||||
|
bool try_lock();
|
||||||
|
void unlock();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::atomic_flag m_Locked = ATOMIC_FLAG_INIT;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* SPINLOCK_H */
|
Loading…
x
Reference in New Issue
Block a user