Mutex: Locks should be recursive.

This commit is contained in:
bunnei 2015-02-09 22:06:09 -05:00
parent caa58acc84
commit 1b0bf00cbc
2 changed files with 19 additions and 15 deletions

View file

@ -21,7 +21,7 @@ namespace Kernel {
*/ */
static void ResumeWaitingThread(Mutex* mutex) { static void ResumeWaitingThread(Mutex* mutex) {
// Reset mutex lock thread handle, nothing is waiting // Reset mutex lock thread handle, nothing is waiting
mutex->locked = false; mutex->lock_count = 0;
mutex->holding_thread = nullptr; mutex->holding_thread = nullptr;
// Find the next waiting thread for the mutex... // Find the next waiting thread for the mutex...
@ -44,8 +44,7 @@ Mutex::~Mutex() {}
SharedPtr<Mutex> Mutex::Create(bool initial_locked, std::string name) { SharedPtr<Mutex> Mutex::Create(bool initial_locked, std::string name) {
SharedPtr<Mutex> mutex(new Mutex); SharedPtr<Mutex> mutex(new Mutex);
mutex->initial_locked = initial_locked; mutex->lock_count = 0;
mutex->locked = false;
mutex->name = std::move(name); mutex->name = std::move(name);
mutex->holding_thread = nullptr; mutex->holding_thread = nullptr;
@ -57,7 +56,7 @@ SharedPtr<Mutex> Mutex::Create(bool initial_locked, std::string name) {
} }
bool Mutex::ShouldWait() { bool Mutex::ShouldWait() {
return locked && holding_thread != GetCurrentThread(); return lock_count > 0 && holding_thread != GetCurrentThread();;
} }
void Mutex::Acquire() { void Mutex::Acquire() {
@ -66,21 +65,27 @@ void Mutex::Acquire() {
void Mutex::Acquire(SharedPtr<Thread> thread) { void Mutex::Acquire(SharedPtr<Thread> thread) {
_assert_msg_(Kernel, !ShouldWait(), "object unavailable!"); _assert_msg_(Kernel, !ShouldWait(), "object unavailable!");
if (locked)
return;
locked = true;
// Actually "acquire" the mutex only if we don't already have it...
if (lock_count == 0) {
thread->held_mutexes.insert(this); thread->held_mutexes.insert(this);
holding_thread = std::move(thread); holding_thread = std::move(thread);
} }
void Mutex::Release() { lock_count++;
if (!locked) }
return;
void Mutex::Release() {
// Only release if the mutex is held...
if (lock_count > 0) {
lock_count--;
// Yield to the next thread only if we've fully released the mutex...
if (lock_count == 0) {
holding_thread->held_mutexes.erase(this); holding_thread->held_mutexes.erase(this);
ResumeWaitingThread(this); ResumeWaitingThread(this);
} }
}
}
} // namespace } // namespace

View file

@ -30,8 +30,7 @@ public:
static const HandleType HANDLE_TYPE = HandleType::Mutex; static const HandleType HANDLE_TYPE = HandleType::Mutex;
HandleType GetHandleType() const override { return HANDLE_TYPE; } HandleType GetHandleType() const override { return HANDLE_TYPE; }
bool initial_locked; ///< Initial lock state when mutex was created int lock_count; ///< Number of times the mutex has been acquired
bool locked; ///< Current locked state
std::string name; ///< Name of mutex (optional) std::string name; ///< Name of mutex (optional)
SharedPtr<Thread> holding_thread; ///< Thread that has acquired the mutex SharedPtr<Thread> holding_thread; ///< Thread that has acquired the mutex