mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-01-29 18:32:47 +00:00
waiter_reg_t, cellSyncBarrier improved
This commit is contained in:
parent
3c2e0fff07
commit
48eb66383b
@ -235,7 +235,9 @@ bool waiter_is_stopped(const char* func_name, u64 signal_id)
|
||||
return false;
|
||||
}
|
||||
|
||||
void waiter_register(u64 signal_id, NamedThreadBase* thread)
|
||||
waiter_reg_t::waiter_reg_t(u64 signal_id)
|
||||
: signal_id(signal_id)
|
||||
, thread(GetCurrentNamedThread())
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_waiter_map.m_mutex);
|
||||
|
||||
@ -243,12 +245,12 @@ void waiter_register(u64 signal_id, NamedThreadBase* thread)
|
||||
g_waiter_map.m_waiters.push_back({ signal_id, thread });
|
||||
}
|
||||
|
||||
void waiter_unregister(u64 signal_id, NamedThreadBase* thread)
|
||||
waiter_reg_t::~waiter_reg_t()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_waiter_map.m_mutex);
|
||||
|
||||
// remove waiter
|
||||
for (s32 i = g_waiter_map.m_waiters.size() - 1; i >= 0; i--)
|
||||
for (size_t i = g_waiter_map.m_waiters.size() - 1; i >= 0; i--)
|
||||
{
|
||||
if (g_waiter_map.m_waiters[i].signal_id == signal_id && g_waiter_map.m_waiters[i].thread == thread)
|
||||
{
|
||||
|
@ -73,27 +73,32 @@ public:
|
||||
|
||||
// for internal use (checks if Emu is stopped)
|
||||
bool waiter_is_stopped(const char* func_name, u64 signal_id);
|
||||
// for internal use
|
||||
void waiter_register(u64 signal_id, NamedThreadBase* thread);
|
||||
// for internal use
|
||||
void waiter_unregister(u64 signal_id, NamedThreadBase* thread);
|
||||
|
||||
// wait until waiter_func() returns true, signal_id is arbitrary number
|
||||
struct waiter_reg_t
|
||||
{
|
||||
const u64 signal_id;
|
||||
NamedThreadBase* const thread;
|
||||
|
||||
waiter_reg_t(u64 signal_id);
|
||||
~waiter_reg_t();
|
||||
};
|
||||
|
||||
// wait until waiter_func() returns true, signal_id is an arbitrary number
|
||||
template<typename WT> static __forceinline void waiter_op(const char* func_name, u64 signal_id, const WT waiter_func)
|
||||
{
|
||||
// check condition
|
||||
if (waiter_func()) return;
|
||||
|
||||
NamedThreadBase* thread = GetCurrentNamedThread();
|
||||
waiter_register(signal_id, thread);
|
||||
// register waiter
|
||||
waiter_reg_t waiter(signal_id);
|
||||
|
||||
while (true)
|
||||
{
|
||||
thread->WaitForAnySignal(1);
|
||||
// wait for 1 ms or until signal arrived
|
||||
waiter.thread->WaitForAnySignal(1);
|
||||
if (waiter_is_stopped(func_name, signal_id)) break;
|
||||
if (waiter_func()) break;
|
||||
}
|
||||
|
||||
waiter_unregister(signal_id, thread);
|
||||
}
|
||||
|
||||
// signal all threads waiting on waiter_op() with the same signal_id (signaling only hints those threads that corresponding conditions are *probably* met)
|
||||
|
@ -174,15 +174,12 @@ s32 cellSyncBarrierNotify(vm::ptr<CellSyncBarrier> barrier)
|
||||
return CELL_SYNC_ERROR_ALIGN;
|
||||
}
|
||||
|
||||
while (barrier->data.atomic_op_sync(CELL_OK, syncBarrierTryNotifyOp))
|
||||
waiter_op(__FUNCTION__, barrier.addr(), [barrier]()
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1)); // hack
|
||||
if (Emu.IsStopped())
|
||||
{
|
||||
cellSync->Warning("cellSyncBarrierNotify(barrier_addr=0x%x) aborted", barrier.addr());
|
||||
return CELL_OK;
|
||||
}
|
||||
}
|
||||
return barrier->data.atomic_op_sync(CELL_OK, syncBarrierTryNotifyOp) == CELL_OK;
|
||||
});
|
||||
|
||||
waiter_signal(barrier.addr() ^ 1);
|
||||
return CELL_OK;
|
||||
}
|
||||
|
||||
@ -199,7 +196,13 @@ s32 cellSyncBarrierTryNotify(vm::ptr<CellSyncBarrier> barrier)
|
||||
return CELL_SYNC_ERROR_ALIGN;
|
||||
}
|
||||
|
||||
return barrier->data.atomic_op_sync(CELL_OK, syncBarrierTryNotifyOp);
|
||||
if (s32 res = barrier->data.atomic_op_sync(CELL_OK, syncBarrierTryNotifyOp))
|
||||
{
|
||||
return res;
|
||||
}
|
||||
|
||||
waiter_signal(barrier.addr() ^ 1);
|
||||
return CELL_OK;
|
||||
}
|
||||
|
||||
s32 syncBarrierTryWaitOp(CellSyncBarrier::data_t& barrier)
|
||||
@ -233,15 +236,12 @@ s32 cellSyncBarrierWait(vm::ptr<CellSyncBarrier> barrier)
|
||||
return CELL_SYNC_ERROR_ALIGN;
|
||||
}
|
||||
|
||||
while (barrier->data.atomic_op_sync(CELL_OK, syncBarrierTryWaitOp))
|
||||
waiter_op(__FUNCTION__, barrier.addr() ^ 1, [barrier]()
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1)); // hack
|
||||
if (Emu.IsStopped())
|
||||
{
|
||||
cellSync->Warning("cellSyncBarrierWait(barrier_addr=0x%x) aborted", barrier.addr());
|
||||
return CELL_OK;
|
||||
}
|
||||
}
|
||||
return barrier->data.atomic_op_sync(CELL_OK, syncBarrierTryWaitOp) == CELL_OK;
|
||||
});
|
||||
|
||||
waiter_signal(barrier.addr());
|
||||
return CELL_OK;
|
||||
}
|
||||
|
||||
@ -258,7 +258,13 @@ s32 cellSyncBarrierTryWait(vm::ptr<CellSyncBarrier> barrier)
|
||||
return CELL_SYNC_ERROR_ALIGN;
|
||||
}
|
||||
|
||||
return barrier->data.atomic_op_sync(CELL_OK, syncBarrierTryWaitOp);
|
||||
if (s32 res = barrier->data.atomic_op_sync(CELL_OK, syncBarrierTryWaitOp))
|
||||
{
|
||||
return res;
|
||||
}
|
||||
|
||||
waiter_signal(barrier.addr());
|
||||
return CELL_OK;
|
||||
}
|
||||
|
||||
s32 syncRwmInitialize(vm::ptr<CellSyncRwm> rwm, vm::ptr<void> buffer, u32 buffer_size)
|
||||
|
Loading…
x
Reference in New Issue
Block a user