sys_thread_try_release

This commit is contained in:
jacob 2025-05-16 11:10:37 -05:00
parent 0b3767df3a
commit 84b0caf379
5 changed files with 63 additions and 47 deletions

View File

@ -209,8 +209,9 @@ void host_release(struct host *host)
{ {
atomic_i32_eval_exchange(&host->receiver_thread_shutdown_flag, 1); atomic_i32_eval_exchange(&host->receiver_thread_shutdown_flag, 1);
sock_wake(host->sock); 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); sys_mutex_release(&host->rcv_buffer_write_mutex);
sock_release(host->sock); sock_release(host->sock);

View File

@ -566,50 +566,18 @@ void sim_step(struct sim_step_ctx *ctx)
struct sim_control *control = &player->player_control; struct sim_control *control = &player->player_control;
*control = cmd_ent->cmd_control; *control = cmd_ent->cmd_control;
{ {
u32 flags = control->flags;
player->player_cursor_pos = control->dbg_cursor; 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_start = false;
player->player_dbg_drag_stop = false; player->player_dbg_drag_stop = false;
/* Cap movement vector magnitude at 1 */
if (v2_len_sq(control->move) > 1) { if (v2_len_sq(control->move) > 1) {
/* Cap movement vector magnitude at 1 */
control->move = v2_norm(control->move); 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 (flags & SIM_CONTROL_FLAG_TELEPORT_TEST) {
if (!(old_control.flags & SIM_CONTROL_FLAG_TELEPORT_TEST)) { if (!(old_control.flags & SIM_CONTROL_FLAG_TELEPORT_TEST)) {
logf_info("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 (flags & SIM_CONTROL_FLAG_TILE_TEST) {
#if 0 #if 0
if (is_master) { if (is_master) {

View File

@ -345,8 +345,8 @@ void sock_release(struct sock *sock)
win32_sock_release(ws); win32_sock_release(ws);
} }
/* Send an empty dummy packet to wake anyone blocking on read (dumb hack since /* Send an empty dummy packet to wake anyone blocking on read.
* winsock doesn't have eventfd). * This is hack since winsock doesn't have eventfd.
* *
* TODO: Use WSAEvent and WSAWaitForMultipleEvents instead */ * TODO: Use WSAEvent and WSAWaitForMultipleEvents instead */
void sock_wake(struct sock *sock) void sock_wake(struct sock *sock)

View File

@ -442,6 +442,7 @@ struct sys_thread sys_thread_alloc(
); );
void sys_thread_wait_release(struct sys_thread *thread); 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); void sys_thread_force_release(struct sys_thread *thread);
/* Gets the current executing thread's ID */ /* Gets the current executing thread's ID */

View File

@ -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) void sys_thread_wait_release(struct sys_thread *thread)
{ {
__prof; __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; struct win32_thread *t = (struct win32_thread *)thread->handle;
HANDLE handle = t->handle; HANDLE handle = t->handle;
/* Wait for thread to stop */ /* Wait for thread to stop */
if (handle) { if (handle) {
DWORD res = WaitForSingleObject(handle, INFINITE); DWORD timeout_ms = (timeout_seconds == F32_INFINITY) ? INFINITE : math_round_to_int(timeout_seconds * 1000);
CloseHandle(handle); DWORD wait_res = WaitForSingleObject(handle, timeout_ms);
if (wait_res == WAIT_OBJECT_0) {
ASSERT(res != WAIT_FAILED); success = true;
(UNUSED)res; CloseHandle(handle);
win32_thread_release(t);
}
} }
/* Release thread struct */ return success;
win32_thread_release(t);
} }
void sys_thread_force_release(struct sys_thread *thread) void sys_thread_force_release(struct sys_thread *thread)