Improve vm::reader_lock

Add upgrade() method
This commit is contained in:
Nekotekina 2018-08-18 21:14:52 +03:00
parent 6ec4a88eb5
commit e8d144f399
2 changed files with 21 additions and 8 deletions

View file

@ -155,7 +155,6 @@ namespace vm
} }
reader_lock::reader_lock() reader_lock::reader_lock()
: locked(true)
{ {
auto cpu = get_current_cpu_thread(); auto cpu = get_current_cpu_thread();
@ -175,12 +174,27 @@ namespace vm
reader_lock::~reader_lock() reader_lock::~reader_lock()
{ {
if (locked) if (m_upgraded)
{
g_mutex.unlock();
}
else
{ {
g_mutex.unlock_shared(); g_mutex.unlock_shared();
} }
} }
void reader_lock::upgrade()
{
if (m_upgraded)
{
return;
}
g_mutex.lock_upgrade();
m_upgraded = true;
}
writer_lock::writer_lock(int full) writer_lock::writer_lock(int full)
: locked(true) : locked(true)
{ {
@ -844,15 +858,13 @@ namespace vm
{ {
if (location == vm::user64k || location == vm::user1m) if (location == vm::user64k || location == vm::user1m)
{ {
g_mutex.lock_upgrade(); lock.upgrade();
if (!loc) if (!loc)
{ {
// Deferred allocation // Deferred allocation
loc = _find_map(0x10000000, 0x10000000, location == vm::user64k ? 0x201 : 0x401); loc = _find_map(0x10000000, 0x10000000, location == vm::user64k ? 0x201 : 0x401);
} }
g_mutex.lock_degrade();
} }
} }

View file

@ -64,15 +64,16 @@ namespace vm
void temporary_unlock(cpu_thread& cpu) noexcept; void temporary_unlock(cpu_thread& cpu) noexcept;
void temporary_unlock() noexcept; void temporary_unlock() noexcept;
struct reader_lock final class reader_lock final
{ {
const bool locked; bool m_upgraded = false;
public:
reader_lock(const reader_lock&) = delete; reader_lock(const reader_lock&) = delete;
reader_lock(); reader_lock();
~reader_lock(); ~reader_lock();
explicit operator bool() const { return locked; } void upgrade();
}; };
struct writer_lock final struct writer_lock final