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,18 +566,30 @@ 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;
if (v2_len_sq(control->move) > 1) {
/* Cap movement vector magnitude at 1 */ /* Cap movement vector magnitude at 1 */
if (v2_len_sq(control->move) > 1) {
control->move = v2_norm(control->move); control->move = v2_norm(control->move);
} }
player->player_hovered_ent = cmd_ent->cmd_control_hovered_ent; if (flags & SIM_CONTROL_FLAG_TELEPORT_TEST) {
if (!(old_control.flags & SIM_CONTROL_FLAG_TELEPORT_TEST)) {
logf_info("Teleport (test)");
struct sim_ent *ent = sim_ent_from_id(world, player->player_control_ent);
if (ent->valid) {
test_teleport(ent, player->player_cursor_pos);
}
}
}
u32 flags = control->flags; /* Non-predicted cmds */
if (ctx->is_master) {
if (flags & SIM_CONTROL_FLAG_DRAG) { if (flags & SIM_CONTROL_FLAG_DRAG) {
if (!(old_control.flags & SIM_CONTROL_FLAG_DRAG)) { if (!(old_control.flags & SIM_CONTROL_FLAG_DRAG)) {
player->player_dbg_drag_start = true; player->player_dbg_drag_start = true;
@ -610,14 +622,6 @@ void sim_step(struct sim_step_ctx *ctx)
spawn_test_explosion(world, player->player_cursor_pos, 1); 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)");
struct sim_ent *ent = sim_ent_from_id(world, player->player_control_ent);
if (ent->valid) {
test_teleport(ent, player->player_cursor_pos);
}
}
} }
if (flags & SIM_CONTROL_FLAG_TILE_TEST) { if (flags & SIM_CONTROL_FLAG_TILE_TEST) {

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);
DWORD wait_res = WaitForSingleObject(handle, timeout_ms);
if (wait_res == WAIT_OBJECT_0) {
success = true;
CloseHandle(handle); CloseHandle(handle);
win32_thread_release(t);
ASSERT(res != WAIT_FAILED); }
(UNUSED)res;
} }
/* 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)