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