Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <linux/futex.h>
- #include <sys/syscall.h>
- #include <sys/time.h>
- #include <unistd.h>
- #include "mutex.h"
- #include <atomic>
- void FutexWait(std::atomic<int> *value, uint64_t expected_value) {
- syscall(SYS_futex, value, FUTEX_WAIT_PRIVATE, expected_value, nullptr, nullptr, 0);
- }
- void FutexWake(std::atomic<int> *value, int count) {
- syscall(SYS_futex, value, FUTEX_WAKE_PRIVATE, count, nullptr, nullptr, 0);
- }
- void Mutex::Lock() {
- int c = 0;
- if (!val_.compare_exchange_strong(c, 1)) {
- c = val_.exchange(2);
- while (c != 0) {
- FutexWait(&val_, 2);
- c = val_.exchange(2);
- }
- }
- }
- void Mutex::Unlock() {
- if (val_.fetch_sub(1) != 1) {
- val_ = 0;
- FutexWake(&val_, 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement