Spinlock+LockedValue: Remove init() functions

Let's set the default (unlocked) value of Spinlock's underlying atomic to 0, so even if the constructor is not called it stays like that.
This commit is contained in:
apio 2022-12-18 20:36:15 +01:00
parent a63146a798
commit 283e641ece
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 7 additions and 23 deletions

View File

@ -21,8 +21,6 @@ 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,8 +53,6 @@ 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,18 +4,18 @@
void Spinlock::lock()
{
int expected = -1;
while (!m_lock.compare_exchange_strong(expected, 0))
int expected = 0;
while (!m_lock.compare_exchange_strong(expected, 1))
{
expected = -1;
expected = 0;
CPU::pause();
}
}
void Spinlock::unlock()
{
int expected = 0;
if (!m_lock.compare_exchange_strong(expected, -1))
int expected = 1;
if (!m_lock.compare_exchange_strong(expected, 0))
{
kwarnln("Spinlock::unlock() called on an unlocked lock with value %d", expected);
}

View File

@ -9,17 +9,11 @@ class Spinlock
bool is_locked() const
{
return m_lock.load() != -1;
}
// Call this before use if the Spinlock is a global variable.
void init()
{
m_lock = -1;
return m_lock.load() != 0;
}
private:
Atomic<int> m_lock{-1};
Atomic<int> m_lock{0};
};
template <typename T> class LockedValue
@ -68,12 +62,6 @@ 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();