From 84b0caf3794757223ec3dd03b9a7e51f81f06ae6 Mon Sep 17 00:00:00 2001 From: jacob Date: Fri, 16 May 2025 11:10:37 -0500 Subject: [PATCH] sys_thread_try_release --- src/host.c | 5 ++-- src/sim_step.c | 76 +++++++++++++++++++++++++----------------------- src/sock_win32.c | 4 +-- src/sys.h | 1 + src/sys_win32.c | 24 ++++++++++----- 5 files changed, 63 insertions(+), 47 deletions(-) diff --git a/src/host.c b/src/host.c index 93d2ecf5..cacbc287 100644 --- a/src/host.c +++ b/src/host.c @@ -209,8 +209,9 @@ void host_release(struct host *host) { atomic_i32_eval_exchange(&host->receiver_thread_shutdown_flag, 1); sock_wake(host->sock); - sys_thread_wait_release(&host->receiver_thread); - + while (!sys_thread_try_release(&host->receiver_thread, 0.001)) { + sock_wake(host->sock); + } sys_mutex_release(&host->rcv_buffer_write_mutex); sock_release(host->sock); diff --git a/src/sim_step.c b/src/sim_step.c index b6fdd580..c483469f 100644 --- a/src/sim_step.c +++ b/src/sim_step.c @@ -566,50 +566,18 @@ void sim_step(struct sim_step_ctx *ctx) struct sim_control *control = &player->player_control; *control = cmd_ent->cmd_control; { + u32 flags = control->flags; + player->player_cursor_pos = control->dbg_cursor; + player->player_hovered_ent = cmd_ent->cmd_control_hovered_ent; player->player_dbg_drag_start = false; player->player_dbg_drag_stop = false; + /* Cap movement vector magnitude at 1 */ if (v2_len_sq(control->move) > 1) { - /* Cap movement vector magnitude at 1 */ control->move = v2_norm(control->move); } - player->player_hovered_ent = cmd_ent->cmd_control_hovered_ent; - - u32 flags = control->flags; - if (flags & SIM_CONTROL_FLAG_DRAG) { - if (!(old_control.flags & SIM_CONTROL_FLAG_DRAG)) { - player->player_dbg_drag_start = true; - } - } else { - if (old_control.flags & SIM_CONTROL_FLAG_DRAG) { - player->player_dbg_drag_stop = true; - } - } - if (flags & SIM_CONTROL_FLAG_CLEAR_ALL) { - if (is_master && !(old_control.flags & SIM_CONTROL_FLAG_CLEAR_ALL)) { - test_clear_level(ctx); - } - } - if (flags & SIM_CONTROL_FLAG_SPAWN_TEST) { - if (!(old_control.flags & SIM_CONTROL_FLAG_SPAWN_TEST)) { - logf_info("Spawning (test)"); - u32 count = 1; - f32 spread = 0; - for (u32 j = 0; j < count; ++j) { - struct v2 pos = player->player_cursor_pos; - pos.y += (((f32)j / (f32)count) - 0.5) * spread; - spawn_test_entities(ctx, pos); - } - } - } - if (flags & SIM_CONTROL_FLAG_EXPLODE_TEST) { - if (!(old_control.flags & SIM_CONTROL_FLAG_EXPLODE_TEST)) { - logf_info("Explosion (test)"); - spawn_test_explosion(world, player->player_cursor_pos, 1); - } - } if (flags & SIM_CONTROL_FLAG_TELEPORT_TEST) { if (!(old_control.flags & SIM_CONTROL_FLAG_TELEPORT_TEST)) { logf_info("Teleport (test)"); @@ -620,6 +588,42 @@ void sim_step(struct sim_step_ctx *ctx) } } + /* Non-predicted cmds */ + if (ctx->is_master) { + if (flags & SIM_CONTROL_FLAG_DRAG) { + if (!(old_control.flags & SIM_CONTROL_FLAG_DRAG)) { + player->player_dbg_drag_start = true; + } + } else { + if (old_control.flags & SIM_CONTROL_FLAG_DRAG) { + player->player_dbg_drag_stop = true; + } + } + if (flags & SIM_CONTROL_FLAG_CLEAR_ALL) { + if (is_master && !(old_control.flags & SIM_CONTROL_FLAG_CLEAR_ALL)) { + test_clear_level(ctx); + } + } + if (flags & SIM_CONTROL_FLAG_SPAWN_TEST) { + if (!(old_control.flags & SIM_CONTROL_FLAG_SPAWN_TEST)) { + logf_info("Spawning (test)"); + u32 count = 1; + f32 spread = 0; + for (u32 j = 0; j < count; ++j) { + struct v2 pos = player->player_cursor_pos; + pos.y += (((f32)j / (f32)count) - 0.5) * spread; + spawn_test_entities(ctx, pos); + } + } + } + if (flags & SIM_CONTROL_FLAG_EXPLODE_TEST) { + if (!(old_control.flags & SIM_CONTROL_FLAG_EXPLODE_TEST)) { + logf_info("Explosion (test)"); + spawn_test_explosion(world, player->player_cursor_pos, 1); + } + } + } + if (flags & SIM_CONTROL_FLAG_TILE_TEST) { #if 0 if (is_master) { diff --git a/src/sock_win32.c b/src/sock_win32.c index cd6a5b53..f0d5c8c0 100644 --- a/src/sock_win32.c +++ b/src/sock_win32.c @@ -345,8 +345,8 @@ void sock_release(struct sock *sock) win32_sock_release(ws); } -/* Send an empty dummy packet to wake anyone blocking on read (dumb hack since - * winsock doesn't have eventfd). +/* Send an empty dummy packet to wake anyone blocking on read. + * This is hack since winsock doesn't have eventfd. * * TODO: Use WSAEvent and WSAWaitForMultipleEvents instead */ void sock_wake(struct sock *sock) diff --git a/src/sys.h b/src/sys.h index 73dff98f..1757e5ae 100644 --- a/src/sys.h +++ b/src/sys.h @@ -442,6 +442,7 @@ struct sys_thread sys_thread_alloc( ); void sys_thread_wait_release(struct sys_thread *thread); +b32 sys_thread_try_release(struct sys_thread *thread, f32 timeout_seconds); /* Returns false if the thread could not release in specified timeout (e.g. because it is still running) */ void sys_thread_force_release(struct sys_thread *thread); /* Gets the current executing thread's ID */ diff --git a/src/sys_win32.c b/src/sys_win32.c index 5db1f201..51d4332f 100644 --- a/src/sys_win32.c +++ b/src/sys_win32.c @@ -1976,20 +1976,30 @@ struct sys_thread sys_thread_alloc(sys_thread_entry_point_func *entry_point, voi void sys_thread_wait_release(struct sys_thread *thread) { __prof; + b32 success = sys_thread_try_release(thread, F32_INFINITY); + ASSERT(success); + (UNUSED)success; +} + +b32 sys_thread_try_release(struct sys_thread *thread, f32 timeout_seconds) +{ + __prof; + b32 success = false; struct win32_thread *t = (struct win32_thread *)thread->handle; HANDLE handle = t->handle; /* Wait for thread to stop */ if (handle) { - DWORD res = WaitForSingleObject(handle, INFINITE); - CloseHandle(handle); - - ASSERT(res != WAIT_FAILED); - (UNUSED)res; + DWORD timeout_ms = (timeout_seconds == F32_INFINITY) ? INFINITE : math_round_to_int(timeout_seconds * 1000); + DWORD wait_res = WaitForSingleObject(handle, timeout_ms); + if (wait_res == WAIT_OBJECT_0) { + success = true; + CloseHandle(handle); + win32_thread_release(t); + } } - /* Release thread struct */ - win32_thread_release(t); + return success; } void sys_thread_force_release(struct sys_thread *thread)