2020-08-22 22:30:12 -04:00
|
|
|
#include "iop_thread.h"
|
|
|
|
|
2020-09-03 22:24:50 -04:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <unistd.h>
|
|
|
|
#elif _WIN32
|
2020-08-26 21:02:24 -04:00
|
|
|
#include <io.h>
|
2020-09-03 22:24:50 -04:00
|
|
|
#endif
|
2021-12-28 15:13:46 -05:00
|
|
|
#include <cstring>
|
2020-08-22 22:30:12 -04:00
|
|
|
|
2022-06-22 23:37:46 -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);
|
2021-12-28 15:13:46 -05:00
|
|
|
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;
|
2022-05-28 20:12:33 -04:00
|
|
|
signal_run_iop(true);
|
2020-08-22 22:30:12 -04:00
|
|
|
}
|
|
|
|
|
2022-05-28 20:12:33 -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);
|
2022-05-28 20:12:33 -04:00
|
|
|
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-09-24 22:15:01 -04:00
|
|
|
}
|
2020-08-22 22:30:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
IOP::~IOP() {
|
|
|
|
reset_allocator();
|
2022-05-28 20:12:33 -04:00
|
|
|
}
|