Add mutex implementation using std::atomic_flag

This commit is contained in:
Henrik Stickann 2022-04-09 21:23:02 +02:00
parent 3abe36ec81
commit d7bd511c70
2 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,25 @@
#ifndef STA_ATOMIC_MUTEX_HPP
#define STA_ATOMIC_MUTEX_HPP
#include <sta/mutex.hpp>
#include <atomic>
namespace sta
{
class AtomicMutex : public Mutex
{
public:
AtomicMutex();
void acquire() override;
void release() override;
private:
std::atomic_flag lock_;
};
} // namespace sta
#endif // STA_ATOMIC_MUTEX_HPP

19
src/atomic_mutex.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <sta/atomic_mutex.hpp>
namespace sta
{
AtomicMutex::AtomicMutex()
: lock_{ATOMIC_FLAG_INIT}
{}
void AtomicMutex::acquire()
{
while (lock_.test_and_set());
}
void AtomicMutex::release()
{
lock_.clear();
}
} // namespace sta