/* This file is part of Fennix Kernel. Fennix Kernel is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Fennix Kernel is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Fennix Kernel. If not, see . */ #pragma once #include #include #include #include namespace std { struct defer_lock_t { explicit defer_lock_t() = default; }; struct try_to_lock_t { explicit try_to_lock_t() = default; }; struct adopt_lock_t { explicit adopt_lock_t() = default; }; /** * A mutex implementation. * * @note The TaskManager must be * initialized before using this class. */ class mutex { private: atomic_bool Locked = false; list Waiting; Tasking::TCB *Holder = nullptr; public: void lock(); bool try_lock(); void unlock(); mutex(); mutex(const mutex &) = delete; ~mutex(); }; template class lock_guard { private: Mutex &mutexRef; public: explicit lock_guard(Mutex &m) : mutexRef(m) { mutexRef.lock(); } lock_guard(Mutex &m, adopt_lock_t t) : mutexRef(m) { /* Do nothing */ } lock_guard(const lock_guard &) = delete; ~lock_guard() { mutexRef.unlock(); } lock_guard &operator=(const lock_guard &) = delete; }; }