2018-09-25 22:34:45 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "vm.h"
|
|
|
|
|
#include "Utilities/cond.h"
|
2019-07-27 00:34:10 +02:00
|
|
|
#include "util/atomic.hpp"
|
2018-09-29 00:12:00 +02:00
|
|
|
|
2018-09-25 22:34:45 +02:00
|
|
|
namespace vm
|
|
|
|
|
{
|
|
|
|
|
// Get reservation status for further atomic update: last update timestamp
|
|
|
|
|
inline atomic_t<u64>& reservation_acquire(u32 addr, u32 size)
|
|
|
|
|
{
|
|
|
|
|
// Access reservation info: stamp and the lock bit
|
2020-04-11 10:16:28 +02:00
|
|
|
return *reinterpret_cast<atomic_t<u64>*>(g_reservations + (addr & 0xff80) / 2);
|
2018-09-25 22:34:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update reservation status
|
|
|
|
|
inline void reservation_update(u32 addr, u32 size, bool lsb = false)
|
|
|
|
|
{
|
|
|
|
|
// Update reservation info with new timestamp
|
|
|
|
|
reservation_acquire(addr, size) += 128;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get reservation sync variable
|
2019-09-19 01:57:08 +02:00
|
|
|
inline atomic_t<u64>& reservation_notifier(u32 addr, u32 size)
|
2018-09-25 22:34:45 +02:00
|
|
|
{
|
2020-04-11 10:16:28 +02:00
|
|
|
return *reinterpret_cast<atomic_t<u64>*>(g_reservations + (addr & 0xff80) / 2);
|
2018-09-25 22:34:45 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-23 15:36:32 +02:00
|
|
|
bool reservation_lock_internal(u32, atomic_t<u64>&);
|
2018-09-25 22:34:45 +02:00
|
|
|
|
|
|
|
|
inline atomic_t<u64>& reservation_lock(u32 addr, u32 size)
|
|
|
|
|
{
|
2020-05-23 15:36:32 +02:00
|
|
|
auto res = &vm::reservation_acquire(addr, size);
|
2018-09-25 22:34:45 +02:00
|
|
|
|
2020-05-23 15:36:32 +02:00
|
|
|
if (res->bts(0)) [[unlikely]]
|
2018-09-25 22:34:45 +02:00
|
|
|
{
|
2020-05-23 15:36:32 +02:00
|
|
|
static atomic_t<u64> no_lock{};
|
|
|
|
|
|
|
|
|
|
if (!reservation_lock_internal(addr, *res))
|
|
|
|
|
{
|
|
|
|
|
res = &no_lock;
|
|
|
|
|
}
|
2018-09-25 22:34:45 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-23 15:36:32 +02:00
|
|
|
return *res;
|
2018-09-25 22:34:45 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-08 19:41:15 +02:00
|
|
|
inline bool reservation_trylock(atomic_t<u64>& res, u64 rtime)
|
|
|
|
|
{
|
|
|
|
|
if (res.compare_and_swap_test(rtime, rtime | 1)) [[likely]]
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-25 22:34:45 +02:00
|
|
|
} // namespace vm
|