Compare commits

..

No commits in common. "0bdbffe0ca423304e13a479c1563c444557fba1c" and "a63146a798733387ddbb6ddb9d06e694637b0207" have entirely different histories.

4 changed files with 27 additions and 34 deletions

View File

@ -21,6 +21,8 @@ namespace KernelVM
{
void init()
{
g_kernelvm_bitmap.init();
auto kernelvm_bitmap = g_kernelvm_bitmap.lock();
kernelvm_bitmap->initialize(bitmap_memory, sizeof(bitmap_memory));
kernelvm_bitmap->clear(false);

View File

@ -53,6 +53,8 @@ namespace MemoryManager
MemoryMapIterator iter;
MemoryMapEntry entry;
g_frame_bitmap.init();
const auto largest_free_entry = iter.largest_free();
expect(largest_free_entry.is_free(), "We were given a largest free memory region that isn't even free!");

View File

@ -4,24 +4,18 @@
void Spinlock::lock()
{
int expected = 0;
while (!m_lock.compare_exchange_strong(expected, 1))
int expected = -1;
while (!m_lock.compare_exchange_strong(expected, 0))
{
expected = 0;
expected = -1;
CPU::pause();
}
}
bool Spinlock::try_lock()
{
int expected = 0;
return m_lock.compare_exchange_strong(expected, 1);
}
void Spinlock::unlock()
{
int expected = 1;
if (!m_lock.compare_exchange_strong(expected, 0))
int expected = 0;
if (!m_lock.compare_exchange_strong(expected, -1))
{
kwarnln("Spinlock::unlock() called on an unlocked lock with value %d", expected);
}

View File

@ -1,6 +1,5 @@
#pragma once
#include <luna/Atomic.h>
#include <luna/Option.h>
class Spinlock
{
@ -8,41 +7,37 @@ class Spinlock
void lock();
void unlock();
bool try_lock();
bool is_locked() const
{
return m_lock.load() != 0;
return m_lock.load() != -1;
}
// Call this before use if the Spinlock is a global variable.
void init()
{
m_lock = -1;
}
private:
Atomic<int> m_lock{0};
Atomic<int> m_lock{-1};
};
template <typename T> class LockedValue
{
struct LockedValueGuard
{
LockedValueGuard(LockedValue& value_ref) : m_value_ref(&value_ref)
LockedValueGuard(LockedValue& value_ref) : m_value_ref(value_ref)
{
}
LockedValueGuard(const LockedValueGuard& other) = delete;
LockedValueGuard(LockedValueGuard&& other)
{
m_value_ref = other.m_value_ref;
other.m_value_ref = nullptr;
}
~LockedValueGuard()
{
if (m_value_ref) m_value_ref->m_lock.unlock();
m_value_ref.m_lock.unlock();
}
T& ref()
{
expect(m_value_ref, "LockedValueGuard::ref() called on a moved LockedValueGuard");
return m_value_ref->m_value;
return m_value_ref.m_value;
}
void set(const T& other)
@ -61,7 +56,7 @@ template <typename T> class LockedValue
}
private:
LockedValue* m_value_ref;
LockedValue& m_value_ref;
};
public:
@ -73,18 +68,18 @@ template <typename T> class LockedValue
{
}
// Call this before use if the LockedValue is a global variable.
void init()
{
m_lock.init();
}
LockedValueGuard lock()
{
m_lock.lock();
return {*this};
}
Option<LockedValueGuard> try_lock()
{
if (m_lock.try_lock()) { return {*this}; }
return {};
}
private:
T m_value;
Spinlock m_lock;