From 9263a4a59f1a5b89eac71056017a5228e3a17454 Mon Sep 17 00:00:00 2001 From: Yuval Dolev Date: Tue, 31 Dec 2019 13:14:05 +0200 Subject: [PATCH] Implemented all system mutex and cv handling functions. --- platform_mac/mac_4ed.mm | 55 +++++++++++++------------- platform_mac/mac_4ed_functions.mm | 66 +++++++++++++++++++++---------- 2 files changed, 74 insertions(+), 47 deletions(-) diff --git a/platform_mac/mac_4ed.mm b/platform_mac/mac_4ed.mm index f21d9d46..0de68e77 100644 --- a/platform_mac/mac_4ed.mm +++ b/platform_mac/mac_4ed.mm @@ -58,6 +58,7 @@ #include // NOTE(yuval): Used for dlopen, dlclose, dlsym #include // NOTE(yuval): Used for errno #include // NOTE(yuval): Used for open +#include // NOTE(yuval): Used for threads, mutexes, cvs #include // NOTE(yuval): Used for getcwd, read, write, getpid #include // NOTE(yuval): Used for stat #include // NOTE(yuval): Used for struct stat, pid_t @@ -109,9 +110,16 @@ struct Mac_Object{ Mac_Object_Kind kind; union{ + NSTimer* timer; + struct{ - NSTimer* timer; - } timer; + pthread_t thread; + Thread_Function *proc; + void *ptr; + } thread; + + pthread_mutex_t mutex; + pthread_cond_t cv; }; }; @@ -130,6 +138,12 @@ struct Mac_Vars { Node free_mac_objects; Node timer_objects; + + pthread_mutex_t thread_launch_mutex; + pthread_cond_t thread_launch_cv; + b32 waiting_for_launch; + + System_Mutex global_frame_mutex; }; //////////////////////////////// @@ -139,31 +153,6 @@ global Render_Target target; //////////////////////////////// -function inline Plat_Handle -mac_to_plat_handle(Mac_Object *object){ - Plat_Handle result = *(Plat_Handle*)(&object); - return(result); -} - - -function inline System_Thread -mac_to_system_thread(Mac_Object *object){ - System_Thread result = *(System_Thread*)(&object); - return(result); -} - -function inline Mac_Object* -mac_to_object(Plat_Handle handle){ - Mac_Object *result = *(Mac_Object**)(&handle); - return(result); -} - -function inline Mac_Object* -mac_to_object(System_Thread thread){ - Mac_Object *result = *(Mac_Object**)(&thread); - return(result); -} - function Mac_Object* mac_alloc_object(Mac_Object_Kind kind){ Mac_Object *result = 0; @@ -210,6 +199,18 @@ mac_free_object(Mac_Object *object){ dll_insert(&mac_vars.free_mac_objects, &object->node); } +function inline Plat_Handle +mac_to_plat_handle(Mac_Object *object){ + Plat_Handle result = *(Plat_Handle*)(&object); + return(result); +} + +function inline Mac_Object* +mac_to_object(Plat_Handle handle){ + Mac_Object *result = *(Mac_Object**)(&handle); + return(result); +} + //////////////////////////////// #import "mac_4ed_functions.mm" diff --git a/platform_mac/mac_4ed_functions.mm b/platform_mac/mac_4ed_functions.mm index d099c2d8..082d3094 100644 --- a/platform_mac/mac_4ed_functions.mm +++ b/platform_mac/mac_4ed_functions.mm @@ -375,7 +375,7 @@ system_wake_up_timer_create_sig(){ Mac_Object *object = mac_alloc_object(MacObjectKind_Timer); dll_insert(&mac_vars.timer_objects, &object->node); - object->timer.timer = nil; + object->timer = nil; Plat_Handle result = mac_to_plat_handle(object); return(result); @@ -385,8 +385,8 @@ function system_wake_up_timer_release_sig(){ Mac_Object *object = mac_to_object(handle); if (object->kind == MacObjectKind_Timer){ - if ((object->timer.timer != nil) && [object->timer.timer isValid]) { - [object->timer.timer invalidate]; + if ((object->timer != nil) && [object->timer isValid]) { + [object->timer invalidate]; mac_free_object(object); } } @@ -397,7 +397,7 @@ system_wake_up_timer_set_sig(){ Mac_Object *object = mac_to_object(handle); if (object->kind == MacObjectKind_Timer){ f64 time_seconds = ((f64)time_milliseconds / 1000.0); - object->timer.timer = [NSTimer scheduledTimerWithTimeInterval:time_seconds + object->timer = [NSTimer scheduledTimerWithTimeInterval:time_seconds target:mac_vars.view selector:@selector(requestDisplay) userInfo:nil repeats:NO]; @@ -511,7 +511,7 @@ system_thread_launch_sig(){ } pthread_mutex_unlock(&mac_vars.thread_launch_mutex); - System_Thread result = mac_to_system_thread(object); + System_Thread result = mac_to_plat_handle(object); return(result); } @@ -533,66 +533,92 @@ system_thread_free_sig(){ function system_thread_get_id_sig(){ - i32 result = (i32)pthread_getthreadid_np(); + pthread_t id = pthread_self(); + i32 result = *(i32*)(&id); return(result); } function system_mutex_make_sig(){ - System_Mutex result = {}; - - NotImplemented; + Mac_Object *object = mac_alloc_object(MacObjectKind_Mutex); + pthread_mutex_init(&object->mutex, 0); + System_Mutex result = mac_to_plat_handle(object); return(result); } function system_mutex_acquire_sig(){ - NotImplemented; + Mac_Object *object = mac_to_object(mutex); + if (object->kind == MacObjectKind_Mutex){ + pthread_mutex_lock(&object->mutex); + } } function system_mutex_release_sig(){ - NotImplemented; + Mac_Object *object = mac_to_object(mutex); + if (object->kind == MacObjectKind_Mutex){ + pthread_mutex_unlock(&object->mutex); + } } function system_mutex_free_sig(){ - NotImplemented; + Mac_Object *object = mac_to_object(mutex); + if (object->kind == MacObjectKind_Mutex){ + pthread_mutex_destroy(&object->mutex); + mac_free_object(object); + } } function system_acquire_global_frame_mutex_sig(){ - NotImplemented; + if (tctx->kind == ThreadKind_AsyncTasks){ + system_mutex_acquire(mac_vars.global_frame_mutex); + } } function system_release_global_frame_mutex_sig(){ - NotImplemented; + if (tctx->kind == ThreadKind_AsyncTasks){ + system_mutex_release(mac_vars.global_frame_mutex); + } } function system_condition_variable_make_sig(){ - System_Condition_Variable result = {}; - - NotImplemented; + Mac_Object *object = mac_alloc_object(MacObjectKind_CV); + pthread_cond_init(&object->cv, 0); + System_Condition_Variable result = mac_to_plat_handle(object); return(result); } function system_condition_variable_wait_sig(){ - NotImplemented; + Mac_Object *object_cv = mac_to_object(cv); + Mac_Object *object_mutex = mac_to_object(mutex); + if ((object_cv->kind == MacObjectKind_CV) && (object_mutex->kind == MacObjectKind_Mutex)){ + pthread_cond_wait(&object_cv->cv, &object_mutex->mutex); + } } function system_condition_variable_signal_sig(){ - NotImplemented; + Mac_Object *object = mac_to_object(cv); + if (object->kind == MacObjectKind_CV){ + pthread_cond_signal(&object->cv); + } } function system_condition_variable_free_sig(){ - NotImplemented; + Mac_Object *object = mac_to_object(cv); + if (object->kind == MacObjectKind_CV){ + pthread_cond_destroy(&object->cv); + mac_free_object(object); + } } ////////////////////////////////