mirror of
https://github.com/Fennix-Project/Kernel.git
synced 2025-05-25 22:14:37 +00:00
94 lines
1.7 KiB
Plaintext
94 lines
1.7 KiB
Plaintext
/*
|
|
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 <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <types.h>
|
|
|
|
#include <task.hpp>
|
|
#include <atomic>
|
|
#include <vector>
|
|
|
|
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<Tasking::TCB *> Waiting;
|
|
Tasking::TCB *Holder = nullptr;
|
|
|
|
public:
|
|
void lock();
|
|
bool try_lock();
|
|
void unlock();
|
|
|
|
mutex();
|
|
mutex(const mutex &) = delete;
|
|
~mutex();
|
|
};
|
|
|
|
template <class Mutex>
|
|
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;
|
|
};
|
|
}
|