From 023447aef718ffcf30ae4e71fab54d9d066fd9ed Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Tue, 18 Jul 2017 16:17:57 -0400 Subject: [PATCH] moved threading wrappers into headers --- 4ed_defines.h | 2 + meta/4ed_build.cpp | 32 ++++---- platform_all/4ed_shared_thread_constants.h | 31 +++++++ platform_all/4ed_work_queues.cpp | 6 +- platform_linux/linux_4ed.cpp | 84 ++----------------- platform_linux/linux_threading_wrapper.h | 94 ++++++++++++++++++++++ platform_win32/win32_4ed.cpp | 76 +---------------- platform_win32/win32_threading_wrapper.h | 92 +++++++++++++++++++++ 8 files changed, 245 insertions(+), 172 deletions(-) create mode 100644 platform_all/4ed_shared_thread_constants.h create mode 100644 platform_linux/linux_threading_wrapper.h create mode 100644 platform_win32/win32_threading_wrapper.h diff --git a/4ed_defines.h b/4ed_defines.h index 0a80c4a3..26b5459a 100644 --- a/4ed_defines.h +++ b/4ed_defines.h @@ -66,6 +66,8 @@ typedef double f64; #define Max(a,b) (((a)>(b))?(a):(b)) #define Min(a,b) (((a)<(b))?(a):(b)) +#define FixSize(s) struct{ u8 __size_fixer__[s]; } + inline i32 ceil32(f32 v){ return(((v)>0)?( (v == (i32)(v))?((i32)(v)):((i32)((v)+1.f)) ):( ((i32)(v)) )); } diff --git a/meta/4ed_build.cpp b/meta/4ed_build.cpp index a14eed1d..b114acb8 100644 --- a/meta/4ed_build.cpp +++ b/meta/4ed_build.cpp @@ -327,22 +327,6 @@ build(u32 flags, u32 arch, char *code_path, char **code_files, char *out_path, c # error gcc options not set for this platform #endif -internal i32 -get_freetype_include(char *out, u32 max){ - i32 size = 0; -#if defined(IS_LINUX) - char freetype_include[512]; - FILE *file = popen("pkg-config --cflags freetype2", "r"); - if (file != 0){ - fgets(freetype_include, sizeof(freetype_include), file); - size = strlen(freetype_include); - freetype_include[size-1] = 0; - pclose(file); - } -#endif - return(size); -} - internal void build(u32 flags, u32 arch, char *code_path, char **code_files, char *out_path, char *out_file, char **defines, char **exports, char **inc_folders){ Build_Line line; @@ -507,6 +491,22 @@ do_buildsuper(char *cdir, char *file, u32 arch){ END_TIME_SECTION("build custom"); } +internal i32 +get_freetype_include(char *out, u32 max){ + i32 size = 0; +#if defined(IS_LINUX) + char freetype_include[512]; + FILE *file = popen("pkg-config --cflags freetype2", "r"); + if (file != 0){ + fgets(freetype_include, sizeof(freetype_include), file); + size = strlen(freetype_include); + freetype_include[size-1] = 0; + pclose(file); + } +#endif + return(size); +} + internal void build_main(char *cdir, b32 update_local_theme, u32 flags, u32 arch){ char *dir = fm_str(BUILD_DIR); diff --git a/platform_all/4ed_shared_thread_constants.h b/platform_all/4ed_shared_thread_constants.h new file mode 100644 index 00000000..3b739a4c --- /dev/null +++ b/platform_all/4ed_shared_thread_constants.h @@ -0,0 +1,31 @@ +/* + * Mr. 4th Dimention - Allen Webster + * + * 17.07.2017 + * + * Cross platform threading constants + * + */ + +// TOP + +#if !defined(FRED_SHARED_THREADING_CONSTANTS_H) +#define FRED_SHARED_THREADING_CONSTANTS_H + +#define CORE_COUNT 8 + +#define THREAD_TYPE_SIZE 32 +#define MUTEX_TYPE_SIZE 40 +#define CONDITION_VARIABLE_TYPE_SIZE 32 +#define SEMAPHORE_TYPE_SIZE 32 + +#define AssertThreadSizes() \ +Assert( sizeof(Thread) == THREAD_TYPE_SIZE ); \ +Assert( sizeof(Mutex) == MUTEX_TYPE_SIZE ); \ +Assert(sizeof(Condition_Variable) == CONDITION_VARIABLE_TYPE_SIZE); \ +Assert( sizeof(Semaphore) == SEMAPHORE_TYPE_SIZE ) + +#endif + +// BOTTOM + diff --git a/platform_all/4ed_work_queues.cpp b/platform_all/4ed_work_queues.cpp index 4116a80d..85f61d65 100644 --- a/platform_all/4ed_work_queues.cpp +++ b/platform_all/4ed_work_queues.cpp @@ -9,10 +9,6 @@ // TOP -#if !defined(CORE_COUNT) -#define CORE_COUNT 8 -#endif - enum CV_ID{ CANCEL_CV0, CANCEL_CV1, @@ -374,6 +370,8 @@ INTERNAL_Sys_Get_Thread_States_Sig(system_internal_get_thread_states){ internal void system_init_threaded_work_system(){ + AssertThreadSizes(); + u32 core_count = CORE_COUNT; i32 thread_system_memory_size = core_count*(sizeof(Thread_Context) + sizeof(Thread_Memory)); void *thread_system_memory = system_memory_allocate(thread_system_memory_size); diff --git a/platform_linux/linux_4ed.cpp b/platform_linux/linux_4ed.cpp index f561d626..ba30a6b1 100644 --- a/platform_linux/linux_4ed.cpp +++ b/platform_linux/linux_4ed.cpp @@ -76,6 +76,9 @@ #include #include +#include "4ed_shared_thread_constants.h" +#include "linux_threading_wrapper.h" + // // Linux macros // @@ -234,77 +237,6 @@ system_schedule_step(){ //////////////////////////////// -#define PLAT_THREAD_SIG(n) void* n(void *ptr) -typedef PLAT_THREAD_SIG(Thread_Function); - -struct Thread{ - pthread_t t; -}; - -struct Mutex{ - pthread_mutex_t crit; -}; - -struct Condition_Variable{ - pthread_cond_t cv; -}; - -struct Semaphore{ - sem_t s; -}; - -internal void -system_init_and_launch_thread(Thread *t, Thread_Function *proc, void *ptr){ - pthread_create(&t->t, 0, proc, ptr); -} - -internal void -system_init_lock(Mutex *m){ - pthread_mutex_init(&m->crit, NULL); -} - -internal void -system_acquire_lock(Mutex *m){ - pthread_mutex_lock(&m->crit); -} - -internal void -system_release_lock(Mutex *m){ - pthread_mutex_unlock(&m->crit); -} - -internal void -system_init_cv(Condition_Variable *cv){ - pthread_cond_init(&cv->cv, NULL); -} - -internal void -system_wait_cv(Condition_Variable *cv, Mutex *m){ - pthread_cond_wait(&cv->cv, &m->crit); -} - -internal void -system_signal_cv(Condition_Variable *cv, Mutex *m){ - pthread_cond_signal(&cv->cv); -} - -internal void -system_init_semaphore(Semaphore *s, u32 count){ - sem_init(&s->s, 0, 0); -} - -internal void -system_wait_on_semaphore(Semaphore *s){ - sem_wait(&s->s); -} - -internal void -system_release_semaphore(Semaphore *s){ - sem_post(&s->s); -} - -//////////////////////////////// - internal Sys_Show_Mouse_Cursor_Sig(system_show_mouse_cursor){ linuxvars.hide_cursor = !show; @@ -314,7 +246,7 @@ Sys_Show_Mouse_Cursor_Sig(system_show_mouse_cursor){ internal Sys_Toggle_Fullscreen_Sig(system_toggle_fullscreen){ b32 success = true; - LinuxToggleFullscreen(linuxvars.XDisplay, linuxvars.XWindow); + LinuxSetWMState(linuxvars.XDisplay, linuxvars.XWindow, linuxvars.atom__NET_WM_STATE_FULLSCREEN, 0, 2); return(success); } @@ -1146,12 +1078,6 @@ LinuxMaximizeWindow(Display* d, Window w, b32 maximize) LinuxSetWMState(d, w, linuxvars.atom__NET_WM_STATE_MAXIMIZED_HORZ, linuxvars.atom__NET_WM_STATE_MAXIMIZED_VERT, maximize != 0); } -internal void -LinuxToggleFullscreen(Display* d, Window w) -{ - LinuxSetWMState(d, w, linuxvars.atom__NET_WM_STATE_FULLSCREEN, 0, 2); -} - #include "linux_icon.h" internal void LinuxSetIcon(Display* d, Window w) @@ -1585,7 +1511,7 @@ LinuxX11WindowInit(int argc, char** argv, int* window_width, int* window_height) LinuxMaximizeWindow(linuxvars.XDisplay, linuxvars.XWindow, 1); } else if (linuxvars.settings.fullscreen_window){ - LinuxToggleFullscreen(linuxvars.XDisplay, linuxvars.XWindow); + system_toggle_fullscreen(); } XSync(linuxvars.XDisplay, False); diff --git a/platform_linux/linux_threading_wrapper.h b/platform_linux/linux_threading_wrapper.h new file mode 100644 index 00000000..95b27001 --- /dev/null +++ b/platform_linux/linux_threading_wrapper.h @@ -0,0 +1,94 @@ +/* + * Mr. 4th Dimention - Allen Webster + * + * 17.07.2017 + * + * Linux threading wrapper + * + */ + +// TODO(allen): Can this just be placed in UNIX? If so do that. + +// TOP + +#if !defined(LINUX_THREADING_WRAPPER) +#define LINUX_THREADING_WRAPPER + +#define PLAT_THREAD_SIG(n) void* n(void *ptr) +typedef PLAT_THREAD_SIG(Thread_Function); + +union Thread{ + pthread_t t; + FixSize(THREAD_TYPE_SIZE); +}; + +union Mutex{ + pthread_mutex_t crit; + FixSize(MUTEX_TYPE_SIZE); +}; + +union Condition_Variable{ + pthread_cond_t cv; + FixSize(CONDITION_VARIABLE_TYPE_SIZE); +}; + +union Semaphore{ + sem_t s; + FixSize(SEMAPHORE_TYPE_SIZE); +}; + + +internal void +system_init_and_launch_thread(Thread *t, Thread_Function *proc, void *ptr){ + pthread_create(&t->t, 0, proc, ptr); +} + +internal void +system_init_lock(Mutex *m){ + pthread_mutex_init(&m->crit, NULL); +} + +internal void +system_acquire_lock(Mutex *m){ + pthread_mutex_lock(&m->crit); +} + +internal void +system_release_lock(Mutex *m){ + pthread_mutex_unlock(&m->crit); +} + +internal void +system_init_cv(Condition_Variable *cv){ + pthread_cond_init(&cv->cv, NULL); +} + +internal void +system_wait_cv(Condition_Variable *cv, Mutex *m){ + pthread_cond_wait(&cv->cv, &m->crit); +} + +internal void +system_signal_cv(Condition_Variable *cv, Mutex *m){ + pthread_cond_signal(&cv->cv); +} + +internal void +system_init_semaphore(Semaphore *s, u32 count){ + sem_init(&s->s, 0, 0); +} + +internal void +system_wait_on_semaphore(Semaphore *s){ + sem_wait(&s->s); +} + +internal void +system_release_semaphore(Semaphore *s){ + sem_post(&s->s); +} + +#endif + +// BOTTOM + diff --git a/platform_win32/win32_4ed.cpp b/platform_win32/win32_4ed.cpp index 81ebe80d..9be69940 100644 --- a/platform_win32/win32_4ed.cpp +++ b/platform_win32/win32_4ed.cpp @@ -80,6 +80,9 @@ #include "4ed_font_interface_to_os.h" #include "4ed_system_shared.h" +#include "4ed_shared_thread_constants.h" +#include "win32_threading_wrapper.h" + // // Win32_Vars structs // @@ -194,79 +197,6 @@ system_schedule_step(){ PostMessage(win32vars.window_handle, WM_4coder_ANIMATE, 0, 0); } -//////////////////////////////// - -#define PLAT_THREAD_SIG(n) DWORD CALL_CONVENTION n(LPVOID ptr) -typedef PLAT_THREAD_SIG(Thread_Function); - -struct Thread{ - HANDLE t; -}; - -struct Mutex{ - CRITICAL_SECTION crit; -}; - -struct Condition_Variable{ - CONDITION_VARIABLE cv; -}; - -struct Semaphore{ - HANDLE s; -}; - -internal void -system_init_and_launch_thread(Thread *t, Thread_Function *proc, void *ptr){ - t->t = CreateThread(0, 0, proc, ptr, 0, 0); -} - -internal void -system_init_lock(Mutex *m){ - InitializeCriticalSection(&m->crit); -} - -internal void -system_acquire_lock(Mutex *m){ - EnterCriticalSection(&m->crit); -} - -internal void -system_release_lock(Mutex *m){ - LeaveCriticalSection(&m->crit); -} - -internal void -system_init_cv(Condition_Variable *cv){ - InitializeConditionVariable(&cv->cv); -} - -internal void -system_wait_cv(Condition_Variable *cv, Mutex *lock){ - SleepConditionVariableCS(&cv->cv, &lock->crit, INFINITE); -} - -internal void -system_signal_cv(Condition_Variable *cv, Mutex *lock){ - WakeConditionVariable(&cv->cv); -} - -internal void -system_init_semaphore(Semaphore *s, u32 max){ - s->s = CreateSemaphore(0, 0, max, 0); -} - -internal void -system_wait_on_semaphore(Semaphore *s){ - WaitForSingleObject(s->s, INFINITE); -} - -internal void -system_release_semaphore(Semaphore *s){ - ReleaseSemaphore(s->s, 1, 0); -} - -//////////////////////////////// - // // 4ed path // diff --git a/platform_win32/win32_threading_wrapper.h b/platform_win32/win32_threading_wrapper.h new file mode 100644 index 00000000..bbec5642 --- /dev/null +++ b/platform_win32/win32_threading_wrapper.h @@ -0,0 +1,92 @@ +/* + * Mr. 4th Dimention - Allen Webster + * + * 17.07.2017 + * + * Win32 threading wrapper + * + */ + +// TOP + +#if !defined(WIN32_THREADING_WRAPPER) +#define WIN32_THREADING_WRAPPER + +#define PLAT_THREAD_SIG(n) DWORD CALL_CONVENTION n(LPVOID ptr) +typedef PLAT_THREAD_SIG(Thread_Function); + +union Thread{ + HANDLE t; + FixSize(THREAD_TYPE_SIZE); +}; + +union Mutex{ + CRITICAL_SECTION crit; + FixSize(MUTEX_TYPE_SIZE); +}; + +union Condition_Variable{ + CONDITION_VARIABLE cv; + FixSize(CONDITION_VARIABLE_TYPE_SIZE); +}; + +union Semaphore{ + HANDLE s; + FixSize(SEMAPHORE_TYPE_SIZE); +}; + + +internal void +system_init_and_launch_thread(Thread *t, Thread_Function *proc, void *ptr){ + t->t = CreateThread(0, 0, proc, ptr, 0, 0); +} + +internal void +system_init_lock(Mutex *m){ + InitializeCriticalSection(&m->crit); +} + +internal void +system_acquire_lock(Mutex *m){ + EnterCriticalSection(&m->crit); +} + +internal void +system_release_lock(Mutex *m){ + LeaveCriticalSection(&m->crit); +} + +internal void +system_init_cv(Condition_Variable *cv){ + InitializeConditionVariable(&cv->cv); +} + +internal void +system_wait_cv(Condition_Variable *cv, Mutex *lock){ + SleepConditionVariableCS(&cv->cv, &lock->crit, INFINITE); +} + +internal void +system_signal_cv(Condition_Variable *cv, Mutex *lock){ + WakeConditionVariable(&cv->cv); +} + +internal void +system_init_semaphore(Semaphore *s, u32 max){ + s->s = CreateSemaphore(0, 0, max, 0); +} + +internal void +system_wait_on_semaphore(Semaphore *s){ + WaitForSingleObject(s->s, INFINITE); +} + +internal void +system_release_semaphore(Semaphore *s){ + ReleaseSemaphore(s->s, 1, 0); +} + +#endif + +// BOTTOM +