increase run callbacks background thread polling time to ~200ms

This commit is contained in:
otavepto 2024-04-12 10:25:34 +02:00
parent 159c1d1d9e
commit ca8fb4bc35
2 changed files with 14 additions and 18 deletions

View File

@ -1,3 +1,7 @@
* increase run callbacks background thread polling time to ~200ms
---
## 2024/4/11 (2) ## 2024/4/11 (2)
* **[Clompress]** Turkish translation for the overlay * **[Clompress]** Turkish translation for the overlay

View File

@ -23,12 +23,8 @@ static std::condition_variable kill_background_thread_cv;
static bool kill_background_thread; static bool kill_background_thread;
static void background_thread(Steam_Client *client) static void background_thread(Steam_Client *client)
{ {
// duration allowed in which the thread might get a kill signal, before running its code
constexpr const static auto kill_thread_allowed_duration = std::chrono::milliseconds(100);
// max allowed time in which RunCallbacks() might not be called // max allowed time in which RunCallbacks() might not be called
constexpr const static auto max_stall_ms = 100ull; constexpr const static auto max_stall_ms = std::chrono::milliseconds(200);
static unsigned long long last_cooldown_time = 0;
// wait 1 sec // wait 1 sec
{ {
@ -43,11 +39,10 @@ static void background_thread(Steam_Client *client)
PRINT_DEBUG("starting"); PRINT_DEBUG("starting");
last_cooldown_time = (unsigned long long)std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
while (1) { while (1) {
{ {
std::unique_lock lck(kill_background_thread_mutex); std::unique_lock lck(kill_background_thread_mutex);
if (kill_background_thread || kill_background_thread_cv.wait_for(lck, kill_thread_allowed_duration) != std::cv_status::timeout) { if (kill_background_thread || kill_background_thread_cv.wait_for(lck, max_stall_ms) != std::cv_status::timeout) {
if (kill_background_thread) { if (kill_background_thread) {
PRINT_DEBUG("exit"); PRINT_DEBUG("exit");
return; return;
@ -57,19 +52,16 @@ static void background_thread(Steam_Client *client)
auto now_ms = (unsigned long long)std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); auto now_ms = (unsigned long long)std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
if (now_ms > (last_cooldown_time + max_stall_ms)) { // if our cooldown time elapsed
last_cooldown_time = now_ms;
// if our time exceeds last run time of callbacks and it wasn't processing already // if our time exceeds last run time of callbacks and it wasn't processing already
if (!client->cb_run_active && (now_ms > (client->last_cb_run + max_stall_ms))) { if (!client->cb_run_active && (now_ms >= (client->last_cb_run + max_stall_ms.count()))) {
global_mutex.lock(); global_mutex.lock();
PRINT_DEBUG("run @@@@@@@@@@@@@@@@@@@@@@@@@@@"); PRINT_DEBUG("run @@@@@@@@@@@@@@@@@@@@@@@@@@@");
client->network->Run(); // networking must run first since it receives messages use by each run_callback() client->last_cb_run = now_ms; // update the time counter just to avoid overlap
client->network->Run(); // networking must run first since it receives messages used by each run_callback()
client->run_every_runcb->run(); // call each run_callback() client->run_every_runcb->run(); // call each run_callback()
global_mutex.unlock(); global_mutex.unlock();
} }
} }
}
} }
Steam_Client::Steam_Client() Steam_Client::Steam_Client()