From 9692e15c1e7d9ed0f4b6e8bd13165b9344c70179 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Mon, 21 Oct 2024 06:34:29 +0200 Subject: [PATCH] Fix thread context switches --- src/windows-emulator/windows_emulator.cpp | 40 ++++------------------- 1 file changed, 6 insertions(+), 34 deletions(-) diff --git a/src/windows-emulator/windows_emulator.cpp b/src/windows-emulator/windows_emulator.cpp index 0b9ddd68..fb57c1c3 100644 --- a/src/windows-emulator/windows_emulator.cpp +++ b/src/windows-emulator/windows_emulator.cpp @@ -4,7 +4,7 @@ #include -constexpr auto MAX_INSTRUCTIONS_PER_TIME_SLICE = 100; +constexpr auto MAX_INSTRUCTIONS_PER_TIME_SLICE = 10000; namespace { @@ -426,37 +426,12 @@ namespace } context.active_thread = &thread; + + thread.restore(emu); thread.setup_if_necessary(emu, context); } - void cleanup_threads(process_context& context) - { - while (true) - { - bool has_changed = false; - for (auto i = context.threads.begin(); i != context.threads.end(); ++i) - { - if (i->second.exit_status.has_value()) - { - if (&i->second == context.active_thread) - { - context.active_thread = nullptr; - } - - context.threads.erase(i); - has_changed = true; - break; - } - } - - if (!has_changed) - { - break; - } - } - } - void switch_to_thread(x64_emulator& emu, process_context& context, const handle thread_handle) { auto* thread = context.threads.get(thread_handle); @@ -680,19 +655,16 @@ void windows_emulator::setup_hooks() [&](const uint64_t address, const size_t, const uint64_t) { auto& process = this->process(); + auto& thread = this->current_thread(); ++process.executed_instructions; - - auto& thread = this->current_thread(); - if (thread.executed_instructions == MAX_INSTRUCTIONS_PER_TIME_SLICE) + const auto thread_insts = ++thread.executed_instructions; + if (thread_insts % MAX_INSTRUCTIONS_PER_TIME_SLICE == 0) { this->switch_thread = true; this->emu().stop(); } - ++thread.executed_instructions; - thread.executed_instructions %= MAX_INSTRUCTIONS_PER_TIME_SLICE; - process.previous_ip = process.current_ip; process.current_ip = this->emu().read_instruction_pointer();