jak-project/game/system/iop_thread.cpp

87 lines
1.7 KiB
C++
Raw Normal View History

2020-08-22 22:30:12 -04:00
#include "iop_thread.h"
#ifdef __linux__
#include <unistd.h>
#elif _WIN32
2020-08-26 21:02:24 -04:00
#include <io.h>
#endif
#include <cstring>
2020-08-22 22:30:12 -04:00
#include "SystemThread.h"
2020-08-26 01:21:33 -04:00
IOP::IOP() {}
2020-08-22 22:30:12 -04:00
void IOP::send_status(IOP_Status new_status) {
{
std::lock_guard<std::mutex> lck(iop_mutex);
status = new_status;
}
cv.notify_all();
}
void IOP::wait_for_overlord_start_cmd() {
std::unique_lock<std::mutex> lk(iop_mutex);
2020-08-26 01:21:33 -04:00
if (status != IOP_WAIT_FOR_LOAD)
return;
2020-08-22 22:30:12 -04:00
2020-08-26 01:21:33 -04:00
cv.wait(lk, [&] { return status != IOP_WAIT_FOR_LOAD; });
2020-08-22 22:30:12 -04:00
}
void IOP::wait_for_overlord_init_finish() {
std::unique_lock<std::mutex> lk(iop_mutex);
2020-08-26 01:21:33 -04:00
if (overlord_init_done)
return;
2020-08-22 22:30:12 -04:00
2020-08-26 01:21:33 -04:00
cv.wait(lk, [&] { return overlord_init_done; });
2020-08-22 22:30:12 -04:00
}
void IOP::signal_overlord_init_finish() {
std::unique_lock<std::mutex> lk(iop_mutex);
overlord_init_done = true;
cv.notify_all();
}
void IOP::reset_allocator() {
2020-08-26 01:21:33 -04:00
for (auto x : allocations) {
2020-08-22 22:30:12 -04:00
free(x);
}
allocations.clear();
}
void* IOP::iop_alloc(int size) {
void* mem = malloc(size);
memset(mem, 0xaa, size);
2020-08-22 22:30:12 -04:00
allocations.push_back(mem);
return mem;
}
void IOP::wait_run_iop() {
std::unique_lock<std::mutex> lk(iters_mutex);
2020-08-26 01:21:33 -04:00
if (iop_iters_des > iop_iters_act) {
2020-08-22 22:30:12 -04:00
iop_iters_act++;
return;
}
2020-08-26 01:21:33 -04:00
iop_run_cv.wait(lk, [&] { return iop_iters_des > iop_iters_act; });
2020-08-22 22:30:12 -04:00
iop_iters_act++;
}
void IOP::kill_from_ee() {
want_exit = true;
signal_run_iop(true);
2020-08-22 22:30:12 -04:00
}
void IOP::signal_run_iop(bool force) {
2020-08-22 22:30:12 -04:00
std::unique_lock<std::mutex> lk(iters_mutex);
if (iop_iters_act == iop_iters_des || force) {
iop_iters_des++; // todo, tune this
if (iop_iters_des - iop_iters_act > 500) {
iop_iters_des = iop_iters_act + 500;
}
iop_run_cv.notify_all();
}
2020-08-22 22:30:12 -04:00
}
IOP::~IOP() {
reset_allocator();
}