From 04f4c3a1807248bfc0f8753841068954ee7e0ae4 Mon Sep 17 00:00:00 2001 From: jacob Date: Tue, 17 Jun 2025 23:06:42 -0500 Subject: [PATCH] convert sys thread & watch structs to opaque ptrs --- src/app.c | 16 +++++----- src/host.c | 2 +- src/host.h | 2 +- src/playback_wasapi.c | 4 +-- src/resource.c | 14 ++++----- src/sprite.c | 4 +-- src/sys.h | 18 ++---------- src/sys_win32.c | 68 +++++++++++++++++++------------------------ src/user.c | 8 ++--- src/work.c | 4 +-- 10 files changed, 60 insertions(+), 80 deletions(-) diff --git a/src/app.c b/src/app.c index c0d1623e..05e7e147 100644 --- a/src/app.c +++ b/src/app.c @@ -275,7 +275,7 @@ void app_entry_point(struct string args_str) } /* Create window */ - struct sys_window window = sys_window_alloc(); + struct sys_window *window = sys_window_alloc(); /* Read window settings from file */ { @@ -310,10 +310,10 @@ void app_entry_point(struct string args_str) window_settings = *res; } else { logf_info("Settings file not found, loading default"); - window_settings = default_window_settings(&window); + window_settings = default_window_settings(window); } string_copy_to_string(STRING_FROM_ARRAY(window_settings.title), LIT(WINDOW_TITLE)); - sys_window_update_settings(&window, &window_settings); + sys_window_update_settings(window, &window_settings); arena_temp_end(temp); } @@ -323,7 +323,7 @@ void app_entry_point(struct string args_str) struct host_startup_receipt host_sr = host_startup(&sock_sr); struct resource_startup_receipt resource_sr = resource_startup(); struct work_startup_receipt work_sr = work_startup(worker_count); - struct gpu_startup_receipt gpu_sr = gpu_startup(&work_sr, &window); + struct gpu_startup_receipt gpu_sr = gpu_startup(&work_sr, window); struct asset_cache_startup_receipt asset_cache_sr = asset_cache_startup(&work_sr); struct ttf_startup_receipt ttf_sr = ttf_startup(); struct font_startup_receipt font_sr = font_startup(&work_sr, &gpu_sr, &asset_cache_sr, &ttf_sr, &resource_sr); @@ -332,14 +332,14 @@ void app_entry_point(struct string args_str) struct sound_startup_receipt sound_sr = sound_startup(&work_sr, &asset_cache_sr, &resource_sr); struct draw_startup_receipt draw_sr = draw_startup(&gpu_sr, &font_sr); struct sim_startup_receipt sim_sr = sim_startup(); - struct user_startup_receipt user_sr = user_startup(&work_sr, &gpu_sr, &font_sr, &sprite_sr, &draw_sr, &asset_cache_sr, &sound_sr, &mixer_sr, &host_sr, &sim_sr, connect_address, &window); + struct user_startup_receipt user_sr = user_startup(&work_sr, &gpu_sr, &font_sr, &sprite_sr, &draw_sr, &asset_cache_sr, &sound_sr, &mixer_sr, &host_sr, &sim_sr, connect_address, window); struct playback_startup_receipt playback_sr = playback_startup(&mixer_sr); (UNUSED)user_sr; (UNUSED)playback_sr; /* Show window */ - sys_window_show(&window); + sys_window_show(window); /* Wait for app_exit() */ sync_flag_wait(&G.exit_sf); @@ -364,7 +364,7 @@ void app_entry_point(struct string args_str) struct string window_settings_path = app_write_path_cat(temp.arena, settings_file_name); - struct sys_window_settings settings = sys_window_get_settings(&window); + struct sys_window_settings settings = sys_window_get_settings(window); struct string str = settings_serialize(temp.arena, &settings); logf_info("Serialized window settings: %F", FMT_STR(str)); @@ -377,7 +377,7 @@ void app_entry_point(struct string args_str) arena_temp_end(temp); } - sys_window_release(&window); + sys_window_release(window); logf_info("Program exited normally"); scratch_end(scratch); diff --git a/src/host.c b/src/host.c index 43259fa1..3a767fee 100644 --- a/src/host.c +++ b/src/host.c @@ -209,7 +209,7 @@ void host_release(struct host *host) { atomic_i32_eval_exchange(&host->receiver_thread_shutdown_flag, 1); sock_wake(host->sock); - while (!sys_thread_try_release(&host->receiver_thread, 0.001f)) { + while (!sys_thread_try_release(host->receiver_thread, 0.001f)) { sock_wake(host->sock); } sys_mutex_release(&host->rcv_buffer_write_mutex); diff --git a/src/host.h b/src/host.h index 738506cb..3af3c106 100644 --- a/src/host.h +++ b/src/host.h @@ -98,7 +98,7 @@ struct host { u64 bytes_sent; struct atomic_i32 receiver_thread_shutdown_flag; - struct sys_thread receiver_thread; + struct sys_thread *receiver_thread; }; /* ========================== * diff --git a/src/playback_wasapi.c b/src/playback_wasapi.c index 9e7d66c6..48e49d0f 100644 --- a/src/playback_wasapi.c +++ b/src/playback_wasapi.c @@ -38,7 +38,7 @@ struct wasapi_buffer { GLOBAL struct { struct atomic_i32 playback_thread_shutdown; - struct sys_thread playback_thread; + struct sys_thread *playback_thread; IAudioClient *client; HANDLE event; IAudioRenderClient *playback; @@ -70,7 +70,7 @@ INTERNAL APP_EXIT_CALLBACK_FUNC_DEF(playback_shutdown) { __prof; atomic_i32_eval_exchange(&G.playback_thread_shutdown, true); - sys_thread_wait_release(&G.playback_thread); + sys_thread_wait_release(G.playback_thread); } /* ========================== * diff --git a/src/resource.c b/src/resource.c index 0c89e79c..b4ab9f2d 100644 --- a/src/resource.c +++ b/src/resource.c @@ -23,10 +23,10 @@ GLOBAL struct { #endif #if RESOURCE_RELOADING - struct sys_thread resource_watch_monitor_thread; - struct sys_thread resource_watch_dispatch_thread; + struct sys_thread *resource_watch_monitor_thread; + struct sys_thread *resource_watch_dispatch_thread; - struct sys_watch watch; + struct sys_watch *watch; struct atomic_i32 watch_shutdown; struct sys_mutex watch_dispatcher_mutex; @@ -163,10 +163,10 @@ INTERNAL APP_EXIT_CALLBACK_FUNC_DEF(resource_shutdown) atomic_i32_eval_exchange(&G.watch_shutdown, 1); sys_condition_variable_broadcast(&G.watch_dispatcher_cv); - sys_watch_wake(&G.watch); + sys_watch_wake(G.watch); - sys_thread_wait_release(&G.resource_watch_dispatch_thread); - sys_thread_wait_release(&G.resource_watch_monitor_thread); + sys_thread_wait_release(G.resource_watch_dispatch_thread); + sys_thread_wait_release(G.resource_watch_monitor_thread); } void resource_register_watch_callback(resource_watch_callback *callback) @@ -189,7 +189,7 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(resource_watch_monitor_thread_entry_poi while (!atomic_i32_eval(&G.watch_shutdown)) { struct arena_temp temp = arena_temp_begin(scratch.arena); - struct sys_watch_info_list res = sys_watch_wait(temp.arena, &G.watch); + struct sys_watch_info_list res = sys_watch_wait(temp.arena, G.watch); if (res.first && !atomic_i32_eval(&G.watch_shutdown)) { struct sys_lock lock = sys_mutex_lock_e(&G.watch_dispatcher_mutex); { diff --git a/src/sprite.c b/src/sprite.c index fb9a3ab7..6085ef8f 100644 --- a/src/sprite.c +++ b/src/sprite.c @@ -157,7 +157,7 @@ GLOBAL struct { struct sys_mutex evictor_mutex; struct sys_condition_variable evictor_cv; - struct sys_thread evictor_thread; + struct sys_thread *evictor_thread; } G = ZI, DEBUG_ALIAS(G, G_sprite); /* ========================== * @@ -277,7 +277,7 @@ INTERNAL APP_EXIT_CALLBACK_FUNC_DEF(sprite_shutdown) sys_condition_variable_broadcast(&G.evictor_cv); sys_mutex_unlock(&lock); } - sys_thread_wait_release(&G.evictor_thread); + sys_thread_wait_release(G.evictor_thread); } /* ========================== * diff --git a/src/sys.h b/src/sys.h index 5f2849fb..24890aac 100644 --- a/src/sys.h +++ b/src/sys.h @@ -272,10 +272,6 @@ enum sys_watch_info_kind { }; -struct sys_watch { - u64 handle; -}; - struct sys_watch_info { enum sys_watch_info_kind kind; struct string name; @@ -288,7 +284,7 @@ struct sys_watch_info_list { struct sys_watch_info *last; }; -struct sys_watch sys_watch_alloc(struct string path); +struct sys_watch *sys_watch_alloc(struct string path); void sys_watch_release(struct sys_watch *dw); struct sys_watch_info_list sys_watch_wait(struct arena *arena, struct sys_watch *dw); void sys_watch_wake(struct sys_watch *dw); @@ -338,11 +334,7 @@ struct sys_window_settings { i32 floating_height; }; -struct sys_window { - u64 handle; -}; - -struct sys_window sys_window_alloc(void); +struct sys_window *sys_window_alloc(void); void sys_window_release(struct sys_window *sys_window); void sys_window_register_event_callback(struct sys_window *sys_window, sys_window_event_callback_func *func); @@ -432,12 +424,8 @@ struct thread_local_store *sys_thread_get_thread_local_store(void); #define SYS_THREAD_ENTRY_POINT_FUNC_DEF(name, arg_name) void name(void *arg_name) typedef SYS_THREAD_ENTRY_POINT_FUNC_DEF(sys_thread_entry_point_func, data); -struct sys_thread { - u64 handle; -}; - /* Creates a new thread running in the supplied `entry_point` */ -struct sys_thread sys_thread_alloc( +struct sys_thread *sys_thread_alloc( sys_thread_entry_point_func *entry_point, void *thread_data, /* Passed as arg to `entry_point` */ struct string thread_name diff --git a/src/sys_win32.c b/src/sys_win32.c index 7a16957d..39662080 100644 --- a/src/sys_win32.c +++ b/src/sys_win32.c @@ -84,7 +84,7 @@ struct win32_window { struct rect cursor_clip_bounds; struct atomic_i32 event_thread_shutdown; - struct sys_thread event_thread; + struct sys_thread *event_thread; struct sys_mutex event_callbacks_mutex; sys_window_event_callback_func *event_callbacks[SYS_WINDOW_EVENT_LISTENERS_MAX]; @@ -686,7 +686,7 @@ struct win32_watch { u8 results_buff[KILOBYTE(64)]; }; -struct sys_watch sys_watch_alloc(struct string dir_path) +struct sys_watch *sys_watch_alloc(struct string dir_path) { struct arena_temp scratch = scratch_begin_no_conflict(); @@ -719,15 +719,12 @@ struct sys_watch sys_watch_alloc(struct string dir_path) w32_watch->wake_handle = CreateEventW(NULL, FALSE, FALSE, NULL); scratch_end(scratch); - - struct sys_watch watch = ZI; - watch.handle = (u64)w32_watch; - return watch; + return (struct sys_watch *)w32_watch; } void sys_watch_release(struct sys_watch *dw) { - struct win32_watch *w32_watch = (struct win32_watch *)dw->handle; + struct win32_watch *w32_watch = (struct win32_watch *)dw; CloseHandle(w32_watch->dir_handle); CloseHandle(w32_watch->wake_handle); @@ -742,7 +739,7 @@ void sys_watch_release(struct sys_watch *dw) struct sys_watch_info_list sys_watch_wait(struct arena *arena, struct sys_watch *dw) { __prof; - struct win32_watch *w32_watch = (struct win32_watch *)dw->handle; + struct win32_watch *w32_watch = (struct win32_watch *)dw; struct sys_watch_info_list list = ZI; DWORD filter = FILE_NOTIFY_CHANGE_FILE_NAME | @@ -849,7 +846,7 @@ struct sys_watch_info_list sys_watch_wait(struct arena *arena, struct sys_watch void sys_watch_wake(struct sys_watch *dw) { - struct win32_watch *w32_watch = (struct win32_watch *)dw->handle; + struct win32_watch *w32_watch = (struct win32_watch *)dw; SetEvent(w32_watch->wake_handle); } @@ -1050,7 +1047,7 @@ INTERNAL void win32_window_release(struct win32_window *window) /* Stop window thread */ atomic_i32_eval_exchange(&window->event_thread_shutdown, 1); win32_window_wake(window); - sys_thread_wait_release(&window->event_thread); + sys_thread_wait_release(window->event_thread); /* Release mutexes */ sys_mutex_release(&window->event_callbacks_mutex); @@ -1414,25 +1411,22 @@ INTERNAL LRESULT CALLBACK win32_window_proc(HWND hwnd, UINT msg, WPARAM wparam, return result; } -struct sys_window sys_window_alloc(void) +struct sys_window *sys_window_alloc(void) { __prof; - struct sys_window w = { - .handle = (u64)win32_window_alloc() - }; - return w; + return (struct sys_window *)win32_window_alloc(); } void sys_window_release(struct sys_window *sys_window) { __prof; - struct win32_window *window = (struct win32_window *)sys_window->handle; + struct win32_window *window = (struct win32_window *)sys_window; win32_window_release(window); } void sys_window_register_event_callback(struct sys_window *sys_window, sys_window_event_callback_func *func) { - struct win32_window *window = (struct win32_window *)sys_window->handle; + struct win32_window *window = (struct win32_window *)sys_window; struct sys_lock lock = sys_mutex_lock_e(&window->event_callbacks_mutex); { if (window->event_callbacks_count + 1 > ARRAY_COUNT(window->event_callbacks)) { @@ -1446,7 +1440,7 @@ void sys_window_register_event_callback(struct sys_window *sys_window, sys_windo void sys_window_unregister_event_callback(struct sys_window *sys_window, sys_window_event_callback_func *func) { - struct win32_window *window = (struct win32_window *)sys_window->handle; + struct win32_window *window = (struct win32_window *)sys_window; struct sys_lock lock = sys_mutex_lock_e(&window->event_callbacks_mutex); { @@ -1469,7 +1463,7 @@ void sys_window_unregister_event_callback(struct sys_window *sys_window, sys_win void sys_window_update_settings(struct sys_window *sys_window, struct sys_window_settings *settings) { __prof; - struct win32_window *window = (struct win32_window *)sys_window->handle; + struct win32_window *window = (struct win32_window *)sys_window; struct sys_lock lock = sys_mutex_lock_e(&window->settings_mutex); { win32_update_window_from_settings(window, settings); @@ -1481,13 +1475,13 @@ void sys_window_update_settings(struct sys_window *sys_window, struct sys_window struct sys_window_settings sys_window_get_settings(struct sys_window *sys_window) { - struct win32_window *window = (struct win32_window *)sys_window->handle; + struct win32_window *window = (struct win32_window *)sys_window; return window->settings; } void sys_window_show(struct sys_window *sys_window) { - struct win32_window *window = (struct win32_window *)sys_window->handle; + struct win32_window *window = (struct win32_window *)sys_window; HWND hwnd = window->hwnd; struct sys_lock lock = sys_mutex_lock_e(&window->settings_mutex); { @@ -1507,25 +1501,25 @@ void sys_window_show(struct sys_window *sys_window) struct v2 sys_window_get_size(struct sys_window *sys_window) { - struct win32_window *window = (struct win32_window *)sys_window->handle; + struct win32_window *window = (struct win32_window *)sys_window; return V2((f32)window->width, (f32)window->height); } struct v2 sys_window_get_monitor_size(struct sys_window *sys_window) { - struct win32_window *window = (struct win32_window *)sys_window->handle; + struct win32_window *window = (struct win32_window *)sys_window; return V2((f32)window->monitor_width, (f32)window->monitor_height); } u64 sys_window_get_internal_handle(struct sys_window *sys_window) { - struct win32_window *window = (struct win32_window *)sys_window->handle; + struct win32_window *window = (struct win32_window *)sys_window; return (u64)window->hwnd; } void sys_window_cursor_set_pos(struct sys_window *sys_window, struct v2 pos) { - struct win32_window *window = (struct win32_window *)sys_window->handle; + struct win32_window *window = (struct win32_window *)sys_window; window->cursor_set_position = pos; window->cursor_set_flags |= WIN32_WINDOW_CURSOR_SET_FLAG_POSITION; win32_window_wake(window); @@ -1533,21 +1527,21 @@ void sys_window_cursor_set_pos(struct sys_window *sys_window, struct v2 pos) void sys_window_cursor_show(struct sys_window *sys_window) { - struct win32_window *window = (struct win32_window *)sys_window->handle; + struct win32_window *window = (struct win32_window *)sys_window; window->cursor_set_flags |= WIN32_WINDOW_CURSOR_SET_FLAG_SHOW; win32_window_wake(window); } void sys_window_cursor_hide(struct sys_window *sys_window) { - struct win32_window *window = (struct win32_window *)sys_window->handle; + struct win32_window *window = (struct win32_window *)sys_window; window->cursor_set_flags |= WIN32_WINDOW_CURSOR_SET_FLAG_HIDE; win32_window_wake(window); } void sys_window_cursor_enable_clip(struct sys_window *sys_window, struct rect bounds) { - struct win32_window *window = (struct win32_window *)sys_window->handle; + struct win32_window *window = (struct win32_window *)sys_window; window->cursor_clip_bounds = bounds; window->cursor_set_flags |= WIN32_WINDOW_CURSOR_SET_FLAG_ENABLE_CLIP; win32_window_wake(window); @@ -1555,7 +1549,7 @@ void sys_window_cursor_enable_clip(struct sys_window *sys_window, struct rect bo void sys_window_cursor_disable_clip(struct sys_window *sys_window) { - struct win32_window *window = (struct win32_window *)sys_window->handle; + struct win32_window *window = (struct win32_window *)sys_window; window->cursor_set_flags |= WIN32_WINDOW_CURSOR_SET_FLAG_DISABLE_CLIP; win32_window_wake(window); } @@ -1915,14 +1909,13 @@ INTERNAL DWORD WINAPI win32_thread_proc(LPVOID vt) return 0; } -struct sys_thread sys_thread_alloc(sys_thread_entry_point_func *entry_point, void *thread_data, struct string thread_name) +struct sys_thread *sys_thread_alloc(sys_thread_entry_point_func *entry_point, void *thread_data, struct string thread_name) { __prof; struct arena_temp scratch = scratch_begin_no_conflict(); ASSERT(entry_point != NULL); logf_info("Creating thread \"%F\"", FMT_STR(thread_name)); - struct sys_thread res = ZI; /* Allocate thread object */ struct win32_thread *t = win32_thread_alloc(); @@ -1955,9 +1948,8 @@ struct sys_thread sys_thread_alloc(sys_thread_entry_point_func *entry_point, voi sys_panic(LIT("Failed to create thread")); } - res.handle = (u64)t; scratch_end(scratch); - return res; + return (struct sys_thread *)t; } void sys_thread_wait_release(struct sys_thread *thread) @@ -1972,7 +1964,7 @@ 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 = t->handle; /* Wait for thread to stop */ @@ -1992,7 +1984,7 @@ b32 sys_thread_try_release(struct sys_thread *thread, f32 timeout_seconds) void sys_thread_force_release(struct sys_thread *thread) { __prof; - struct win32_thread *t = (struct win32_thread *)thread->handle; + struct win32_thread *t = (struct win32_thread *)thread; HANDLE handle = t->handle; /* Wait for thread to stop */ @@ -2409,13 +2401,13 @@ int CALLBACK wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev_instance, /* Call app thread and wait for return */ { /* Start app thread */ - struct sys_thread app_thread = sys_thread_alloc(&win32_app_thread_entry_point, NULL, LIT("[P0] App thread")); + struct sys_thread *app_thread = sys_thread_alloc(&win32_app_thread_entry_point, NULL, LIT("[P0] App thread")); /* Get app thread handle */ HANDLE app_thread_handle = 0; struct sys_lock lock = sys_mutex_lock_s(&G.threads_mutex); { - struct win32_thread *wt = (struct win32_thread *)app_thread.handle; + struct win32_thread *wt = (struct win32_thread *)app_thread; app_thread_handle = wt->handle; } sys_mutex_unlock(&lock); @@ -2428,7 +2420,7 @@ int CALLBACK wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev_instance, }; DWORD res = WaitForMultipleObjects(ARRAY_COUNT(wait_handles), wait_handles, false, INFINITE); if (res == WAIT_OBJECT_0) { - sys_thread_force_release(&app_thread); + sys_thread_force_release(app_thread); } ASSERT(res != WAIT_FAILED); (UNUSED)res; diff --git a/src/user.c b/src/user.c index 6f16cb5f..90a91581 100644 --- a/src/user.c +++ b/src/user.c @@ -50,10 +50,10 @@ struct console_log { GLOBAL struct { struct atomic_i32 user_thread_shutdown; - struct sys_thread user_thread; + struct sys_thread *user_thread; struct atomic_i32 local_sim_thread_shutdown; - struct sys_thread local_sim_thread; + struct sys_thread *local_sim_thread; struct sim_ctx *local_sim_ctx; struct arena *arena; @@ -281,7 +281,7 @@ INTERNAL APP_EXIT_CALLBACK_FUNC_DEF(user_shutdown) sys_window_unregister_event_callback(G.window, &window_event_callback); atomic_i32_eval_exchange(&G.user_thread_shutdown, true); - sys_thread_wait_release(&G.user_thread); + sys_thread_wait_release(G.user_thread); #if 0 if (G.local_sim_ctx) { @@ -291,7 +291,7 @@ INTERNAL APP_EXIT_CALLBACK_FUNC_DEF(user_shutdown) } #else atomic_i32_eval_exchange(&G.local_sim_thread_shutdown, true); - sys_thread_wait_release(&G.local_sim_thread); + sys_thread_wait_release(G.local_sim_thread); #endif } diff --git a/src/work.c b/src/work.c index 0318d44f..ef7d2f17 100644 --- a/src/work.c +++ b/src/work.c @@ -25,7 +25,7 @@ */ struct worker { - struct sys_thread thread; + struct sys_thread *thread; struct worker *next; }; @@ -154,7 +154,7 @@ INTERNAL APP_EXIT_CALLBACK_FUNC_DEF(work_shutdown) sys_mutex_unlock(&lock); for (struct worker *worker = G.worker_head; worker; worker = worker->next) { - sys_thread_wait_release(&worker->thread); + sys_thread_wait_release(worker->thread); } }