Skip to content
This repository has been archived by the owner on Apr 10, 2021. It is now read-only.

Commit

Permalink
better unsuspension mechanics
Browse files Browse the repository at this point in the history
  • Loading branch information
1fbff5f83b23d39d38b1dfcb4cac8d9b committed Nov 24, 2019
1 parent 37e366e commit 0716ec5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion byond-extools/src/core/various_testing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void init_testing()
{
Core::enable_profiling();
debugger_initialize();
bool find_unknowns = true;
bool find_unknowns = false;
if (find_unknowns)
{
std::ofstream log("unknown_opcodes.txt");
Expand Down
23 changes: 10 additions & 13 deletions byond-extools/src/tffi/tffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "../dmdism/disassembly.h"
#include "../dmdism/opcodes.h"

#include <condition_variable>
#include <mutex>

typedef const char* (byond_ffi_func)(int, const char**);

std::map<float, SuspendedProc*> suspended_procs;
Expand All @@ -12,14 +15,19 @@ std::uint32_t result_string_id = 0;
std::uint32_t completed_string_id = 0;
std::uint32_t internal_id_string_id = 0;

std::condition_variable unsuspend_ready_cv;
std::mutex unsuspend_ready_mutex;

void tffi_suspend(ExecutionContext* ctx)
{
ctx->current_opcode++;
SuspendedProc* proc = Suspend(ctx, 0);
proc->time_to_resume = 0x7FFFFF;
StartTiming(proc);
float promise_id = ctx->constants->args[1].valuef;
std::lock_guard<std::mutex> lk(unsuspend_ready_mutex);
suspended_procs[promise_id] = proc;
unsuspend_ready_cv.notify_all();
ctx->current_opcode--;
}

Expand Down Expand Up @@ -48,19 +56,8 @@ void ffi_thread(byond_ffi_func* proc, int promise_id, int n_args, std::vector<st
SetVariable( 0x21, promise_id , result_string_id, { 0x06, (int)Core::GetString(res) });
SetVariable( 0x21, promise_id , completed_string_id, { 0x2A, 1 });
float internal_id = GetVariable( 0x21, promise_id , internal_id_string_id).valuef;
while (true)
{
if (suspended_procs.find(internal_id) != suspended_procs.end())
{
break;
}
#ifdef _WIN32
Sleep(1);
#else
usleep(1000);
#endif
//TODO: some kind of conditional variable or WaitForObject?
}
std::unique_lock<std::mutex> lk(unsuspend_ready_mutex);
unsuspend_ready_cv.wait(lk, [internal_id] { return suspended_procs.find(internal_id) != suspended_procs.end(); });
suspended_procs[internal_id]->time_to_resume = 1;
suspended_procs.erase(internal_id);
}
Expand Down

0 comments on commit 0716ec5

Please sign in to comment.