kernel: Add a KMutex class and use that for ATA::Controller locking
This commit is contained in:
parent
7efc6dc985
commit
d9a1e8a980
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "arch/PCI.h"
|
#include "arch/PCI.h"
|
||||||
#include "fs/devices/DeviceRegistry.h"
|
#include "fs/devices/DeviceRegistry.h"
|
||||||
|
#include "lib/KMutex.h"
|
||||||
#include <luna/Atomic.h>
|
#include <luna/Atomic.h>
|
||||||
#include <luna/SharedPtr.h>
|
#include <luna/SharedPtr.h>
|
||||||
|
|
||||||
@ -41,7 +42,7 @@ namespace ATA
|
|||||||
Channel m_primary_channel;
|
Channel m_primary_channel;
|
||||||
Channel m_secondary_channel;
|
Channel m_secondary_channel;
|
||||||
|
|
||||||
Atomic<int> m_hwlock = 0;
|
KMutex<100> m_lock;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
78
kernel/src/lib/KMutex.h
Normal file
78
kernel/src/lib/KMutex.h
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "Log.h"
|
||||||
|
#include "arch/CPU.h"
|
||||||
|
#include "thread/Scheduler.h"
|
||||||
|
#include "thread/Thread.h"
|
||||||
|
#include <luna/CircularQueue.h>
|
||||||
|
|
||||||
|
template <usize ConcurrentThreads> class KMutex
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void lock()
|
||||||
|
{
|
||||||
|
int expected = 0;
|
||||||
|
while (!m_lock.compare_exchange_strong(expected, 1))
|
||||||
|
{
|
||||||
|
expected = 0;
|
||||||
|
auto* current = Scheduler::current();
|
||||||
|
|
||||||
|
// We cannot be interrupted between these functions, otherwise we might never exit the loop
|
||||||
|
CPU::disable_interrupts();
|
||||||
|
bool ok = m_blocked_threads.try_push(current);
|
||||||
|
if (!ok) kernel_sleep(10);
|
||||||
|
else
|
||||||
|
kernel_wait_for_event();
|
||||||
|
CPU::enable_interrupts();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void unlock()
|
||||||
|
{
|
||||||
|
int expected = 1;
|
||||||
|
if (!m_lock.compare_exchange_strong(expected, 0))
|
||||||
|
{
|
||||||
|
kwarnln("KMutex::unlock() called on an unlocked lock with value %d", expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
Thread* blocked;
|
||||||
|
if (m_blocked_threads.try_pop(blocked)) blocked->wake_up();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool try_lock()
|
||||||
|
{
|
||||||
|
int expected = 0;
|
||||||
|
return m_lock.compare_exchange_strong(expected, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
CircularQueue<Thread*, ConcurrentThreads> m_blocked_threads;
|
||||||
|
Atomic<int> m_lock;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <usize ConcurrentThreads> class ScopedKMutexLock
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ScopedKMutexLock(KMutex<ConcurrentThreads>& lock)
|
||||||
|
{
|
||||||
|
m_lock = lock;
|
||||||
|
m_lock.lock();
|
||||||
|
}
|
||||||
|
|
||||||
|
~ScopedKMutexLock()
|
||||||
|
{
|
||||||
|
if (!m_taken_over) m_lock.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
ScopedKMutexLock(const ScopedKMutexLock&) = delete;
|
||||||
|
ScopedKMutexLock(ScopedKMutexLock&&) = delete;
|
||||||
|
|
||||||
|
KMutex<ConcurrentThreads>& take_over()
|
||||||
|
{
|
||||||
|
m_taken_over = true;
|
||||||
|
return m_lock;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
KMutex<ConcurrentThreads>& m_lock;
|
||||||
|
bool m_taken_over { false };
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user