rpcsx/rpcs3/Emu/SysCalls/Callback.cpp

116 lines
1.9 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "Utilities/Log.h"
#include "Emu/Memory/Memory.h"
#include "Emu/System.h"
2015-07-01 00:25:52 +02:00
#include "Emu/IdManager.h"
2014-07-13 21:05:28 +02:00
#include "Emu/Cell/PPUThread.h"
#include "Emu/ARMv7/ARMv7Thread.h"
2014-08-23 22:40:04 +02:00
#include "Callback.h"
2015-07-04 21:23:10 +02:00
void CallbackManager::Register(check_cb_t func)
{
std::lock_guard<std::mutex> lock(m_mutex);
2015-03-16 01:21:40 +01:00
2015-07-04 21:23:10 +02:00
m_check_cb.emplace(std::move(func));
}
2015-07-04 21:23:10 +02:00
void CallbackManager::Async(async_cb_t func)
{
std::lock_guard<std::mutex> lock(m_mutex);
if (!m_cb_thread)
{
throw EXCEPTION("Callback thread not found");
}
2015-07-04 21:23:10 +02:00
m_async_cb.emplace(std::move(func));
2015-03-16 01:21:40 +01:00
m_cb_thread->cv.notify_one();
}
2015-07-04 21:23:10 +02:00
CallbackManager::check_cb_t CallbackManager::Check()
{
2015-07-04 21:23:10 +02:00
std::lock_guard<std::mutex> lock(m_mutex);
2015-03-16 01:21:40 +01:00
2015-07-04 21:23:10 +02:00
if (m_check_cb.size())
{
2015-07-04 21:23:10 +02:00
check_cb_t func = std::move(m_check_cb.front());
2014-09-11 21:18:19 +02:00
2015-07-04 21:23:10 +02:00
m_check_cb.pop();
return func;
}
2015-07-04 21:23:10 +02:00
return nullptr;
2014-09-11 21:18:19 +02:00
}
2014-09-11 21:18:19 +02:00
void CallbackManager::Init()
{
std::lock_guard<std::mutex> lock(m_mutex);
auto task = [this](CPUThread& cpu)
{
2015-03-16 01:21:40 +01:00
std::unique_lock<std::mutex> lock(m_mutex);
2015-07-04 21:23:10 +02:00
while (true)
{
2015-07-04 21:23:10 +02:00
CHECK_EMU_STATUS;
2014-09-11 21:18:19 +02:00
2015-07-08 19:45:26 +02:00
if (!lock)
{
lock.lock();
continue;
}
2014-09-11 21:18:19 +02:00
2015-07-04 21:23:10 +02:00
if (m_async_cb.size())
2014-09-11 21:18:19 +02:00
{
2015-07-04 21:23:10 +02:00
async_cb_t func = std::move(m_async_cb.front());
m_async_cb.pop();
2015-07-01 00:25:52 +02:00
if (lock) lock.unlock();
func(cpu);
2015-07-04 21:23:10 +02:00
2014-09-11 21:18:19 +02:00
continue;
}
cpu.cv.wait(lock);
}
2015-07-01 00:25:52 +02:00
};
2015-07-11 22:44:53 +02:00
if (vm::get(vm::main)->addr != 0x10000)
2015-07-01 00:25:52 +02:00
{
auto thread = Emu.GetIdManager().make_ptr<ARMv7Thread>("Callback Thread");
thread->prio = 1001;
thread->stack_size = 0x10000;
thread->custom_task = task;
2015-07-19 13:36:32 +02:00
thread->run();
2015-07-01 00:25:52 +02:00
m_cb_thread = thread;
}
else
{
auto thread = Emu.GetIdManager().make_ptr<PPUThread>("Callback Thread");
thread->prio = 1001;
thread->stack_size = 0x10000;
thread->custom_task = task;
2015-07-19 13:36:32 +02:00
thread->run();
2015-07-01 00:25:52 +02:00
m_cb_thread = thread;
}
}
2014-09-11 21:18:19 +02:00
void CallbackManager::Clear()
{
2014-09-11 21:18:19 +02:00
std::lock_guard<std::mutex> lock(m_mutex);
m_check_cb = decltype(m_check_cb){};
m_async_cb = decltype(m_async_cb){};
2015-07-01 19:09:26 +02:00
m_cb_thread.reset();
}