14 lines
428 B
C++
14 lines
428 B
C++
#pragma once
|
|
#include <atomic>
|
|
#include <sched.h>
|
|
namespace kcp {
|
|
class Mutex {
|
|
std::atomic_flag flag = ATOMIC_FLAG_INIT;
|
|
public:
|
|
void Acquire() { while (flag.test_and_set(std::memory_order_acquire)) sched_yield(); }
|
|
bool TryAcquire() { return !flag.test_and_set(std::memory_order_acquire); }
|
|
void Release() { flag.clear(std::memory_order_release); }
|
|
};
|
|
using Spinlock = Mutex;
|
|
}
|