mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/sta-core.git
synced 2025-08-02 17:31:53 +00:00
Add mutex implementation using std::atomic_flag
This commit is contained in:
parent
3abe36ec81
commit
d7bd511c70
25
include/sta/atomic_mutex.hpp
Normal file
25
include/sta/atomic_mutex.hpp
Normal 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
19
src/atomic_mutex.cpp
Normal 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
|
Loading…
x
Reference in New Issue
Block a user