Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <twist/ed/stdlike/atomic.hpp>
- #include <twist/ed/wait/sys.hpp>
- #include <cstdlib>
- namespace stdlike {
- class Mutex {
- public:
- Mutex() = default;
- Mutex(Mutex&) = delete;
- void Lock() {
- while (locked_threads_count.fetch_add(1) != 0) {
- twist::ed::Wait(locked_threads_count, locked_threads_count.load());
- locked_threads_count.fetch_sub(1);
- }
- }
- void Unlock() {
- auto wake_key = twist::ed::PrepareWake(locked_threads_count);
- auto prev_atom = locked_threads_count.fetch_sub(1);
- // Если между этими строчками придёт поток, то всё будет ок, тк он захватится
- if (prev_atom > 1) {
- twist::ed::WakeOne(wake_key);
- }
- }
- private:
- twist::ed::stdlike::atomic<uint32_t> locked_threads_count{0};
- };
- } // namespace stdlike
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement