2017-01-24 20:19:52 +00:00
|
|
|
#include "mutex.h"
|
2017-10-05 12:09:08 +00:00
|
|
|
|
2020-12-18 06:47:08 +00:00
|
|
|
#include "util/asm.hpp"
|
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
void shared_mutex::imp_lock_shared(u32 val)
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2020-12-09 07:47:45 +00:00
|
|
|
ensure(val < c_err); // "shared_mutex underflow"
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2022-08-07 14:48:14 +00:00
|
|
|
// Try to steal the notification bit
|
|
|
|
if (val & c_sig && m_value.compare_exchange(val, val - c_sig + 1))
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2022-08-07 14:48:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2022-08-07 14:48:14 +00:00
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
{
|
2018-09-21 17:17:16 +00:00
|
|
|
if (try_lock_shared())
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2017-02-15 15:07:42 +00:00
|
|
|
return;
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
2022-08-07 14:48:14 +00:00
|
|
|
|
|
|
|
const u32 old = m_value;
|
|
|
|
|
|
|
|
if (old & c_sig && m_value.compare_and_swap_test(old, old - c_sig + 1))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
busy_wait();
|
2017-02-15 15:07:42 +00:00
|
|
|
}
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
// Acquire writer lock and downgrade
|
|
|
|
const u32 old = m_value.fetch_add(c_one);
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
if (old == 0)
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2018-09-21 17:17:16 +00:00
|
|
|
lock_downgrade();
|
|
|
|
return;
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
2018-03-11 16:28:32 +00:00
|
|
|
|
2020-12-09 07:47:45 +00:00
|
|
|
ensure((old % c_sig) + c_one < c_sig); // "shared_mutex overflow"
|
2018-09-21 17:17:16 +00:00
|
|
|
imp_wait();
|
|
|
|
lock_downgrade();
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
void shared_mutex::imp_unlock_shared(u32 old)
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2020-12-09 07:47:45 +00:00
|
|
|
ensure(old - 1 < c_err); // "shared_mutex underflow"
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2017-02-15 15:07:42 +00:00
|
|
|
// Check reader count, notify the writer if necessary
|
2018-09-21 17:17:16 +00:00
|
|
|
if ((old - 1) % c_one == 0)
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2018-10-01 17:03:40 +00:00
|
|
|
imp_signal();
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
void shared_mutex::imp_wait()
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2019-07-27 14:08:45 +00:00
|
|
|
while (true)
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2019-07-27 14:08:45 +00:00
|
|
|
const auto [old, ok] = m_value.fetch_op([](u32& value)
|
2017-02-24 15:48:53 +00:00
|
|
|
{
|
2019-07-27 14:08:45 +00:00
|
|
|
if (value >= c_sig)
|
|
|
|
{
|
|
|
|
value -= c_sig;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (ok)
|
|
|
|
{
|
|
|
|
break;
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
2017-02-15 15:07:42 +00:00
|
|
|
|
2023-07-31 20:57:26 +00:00
|
|
|
m_value.wait(old);
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 17:03:40 +00:00
|
|
|
void shared_mutex::imp_signal()
|
|
|
|
{
|
|
|
|
m_value += c_sig;
|
2023-07-31 20:57:26 +00:00
|
|
|
m_value.notify_one();
|
2018-10-01 17:03:40 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
void shared_mutex::imp_lock(u32 val)
|
2017-02-15 15:07:42 +00:00
|
|
|
{
|
2020-12-09 07:47:45 +00:00
|
|
|
ensure(val < c_err); // "shared_mutex underflow"
|
2017-02-15 15:07:42 +00:00
|
|
|
|
2022-08-07 14:48:14 +00:00
|
|
|
// Try to steal the notification bit
|
|
|
|
if (val & c_sig && m_value.compare_exchange(val, val - c_sig + c_one))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-15 15:07:42 +00:00
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
{
|
|
|
|
busy_wait();
|
|
|
|
|
2022-08-07 14:48:14 +00:00
|
|
|
const u32 old = m_value;
|
|
|
|
|
|
|
|
if (!old && try_lock())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old & c_sig && m_value.compare_and_swap_test(old, old - c_sig + c_one))
|
2017-02-15 15:07:42 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
const u32 old = m_value.fetch_add(c_one);
|
|
|
|
|
|
|
|
if (old == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-09 07:47:45 +00:00
|
|
|
ensure((old % c_sig) + c_one < c_sig); // "shared_mutex overflow"
|
2018-09-21 17:17:16 +00:00
|
|
|
imp_wait();
|
2017-02-15 15:07:42 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
void shared_mutex::imp_unlock(u32 old)
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2020-12-09 07:47:45 +00:00
|
|
|
ensure(old - c_one < c_err); // "shared_mutex underflow"
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2017-02-15 15:07:42 +00:00
|
|
|
// 1) Notify the next writer if necessary
|
2018-09-21 17:17:16 +00:00
|
|
|
// 2) Notify all readers otherwise if necessary (currently indistinguishable from writers)
|
|
|
|
if (old - c_one)
|
2017-02-15 15:07:42 +00:00
|
|
|
{
|
2018-10-01 17:03:40 +00:00
|
|
|
imp_signal();
|
2017-02-15 15:07:42 +00:00
|
|
|
}
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void shared_mutex::imp_lock_upgrade()
|
|
|
|
{
|
2018-09-21 17:17:16 +00:00
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
{
|
|
|
|
busy_wait();
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
if (try_lock_upgrade())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
// Convert to writer lock
|
|
|
|
const u32 old = m_value.fetch_add(c_one - 1);
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2020-12-09 07:47:45 +00:00
|
|
|
ensure((old % c_sig) + c_one - 1 < c_sig); // "shared_mutex overflow"
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
if (old % c_one == 1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2018-09-21 17:17:16 +00:00
|
|
|
imp_wait();
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
2018-10-01 17:03:40 +00:00
|
|
|
|
|
|
|
void shared_mutex::imp_lock_unlock()
|
|
|
|
{
|
|
|
|
u32 _max = 1;
|
|
|
|
|
|
|
|
for (int i = 0; i < 30; i++)
|
|
|
|
{
|
|
|
|
const u32 val = m_value;
|
|
|
|
|
|
|
|
if (val % c_one == 0 && (val / c_one < _max || val >= c_sig))
|
|
|
|
{
|
|
|
|
// Return if have cought a state where:
|
|
|
|
// 1) Mutex is free
|
|
|
|
// 2) Total number of waiters decreased since last check
|
|
|
|
// 3) Signal bit is set (if used on the platform)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_max = val / c_one;
|
|
|
|
|
|
|
|
busy_wait(1500);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock and unlock
|
|
|
|
if (!m_value.fetch_add(c_one))
|
|
|
|
{
|
|
|
|
unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
imp_wait();
|
|
|
|
unlock();
|
|
|
|
}
|