Implemented all system mutex and cv handling functions.

This commit is contained in:
Yuval Dolev 2019-12-31 13:14:05 +02:00
parent cbdc6a14e0
commit 9263a4a59f
2 changed files with 74 additions and 47 deletions

View File

@ -58,6 +58,7 @@
#include <dlfcn.h> // NOTE(yuval): Used for dlopen, dlclose, dlsym #include <dlfcn.h> // NOTE(yuval): Used for dlopen, dlclose, dlsym
#include <errno.h> // NOTE(yuval): Used for errno #include <errno.h> // NOTE(yuval): Used for errno
#include <fcntl.h> // NOTE(yuval): Used for open #include <fcntl.h> // NOTE(yuval): Used for open
#include <pthread.h> // NOTE(yuval): Used for threads, mutexes, cvs
#include <unistd.h> // NOTE(yuval): Used for getcwd, read, write, getpid #include <unistd.h> // NOTE(yuval): Used for getcwd, read, write, getpid
#include <sys/stat.h> // NOTE(yuval): Used for stat #include <sys/stat.h> // NOTE(yuval): Used for stat
#include <sys/types.h> // NOTE(yuval): Used for struct stat, pid_t #include <sys/types.h> // NOTE(yuval): Used for struct stat, pid_t
@ -109,9 +110,16 @@ struct Mac_Object{
Mac_Object_Kind kind; Mac_Object_Kind kind;
union{ union{
NSTimer* timer;
struct{ struct{
NSTimer* timer; pthread_t thread;
} timer; 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 free_mac_objects;
Node timer_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* function Mac_Object*
mac_alloc_object(Mac_Object_Kind kind){ mac_alloc_object(Mac_Object_Kind kind){
Mac_Object *result = 0; Mac_Object *result = 0;
@ -210,6 +199,18 @@ mac_free_object(Mac_Object *object){
dll_insert(&mac_vars.free_mac_objects, &object->node); 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" #import "mac_4ed_functions.mm"

View File

@ -375,7 +375,7 @@ system_wake_up_timer_create_sig(){
Mac_Object *object = mac_alloc_object(MacObjectKind_Timer); Mac_Object *object = mac_alloc_object(MacObjectKind_Timer);
dll_insert(&mac_vars.timer_objects, &object->node); dll_insert(&mac_vars.timer_objects, &object->node);
object->timer.timer = nil; object->timer = nil;
Plat_Handle result = mac_to_plat_handle(object); Plat_Handle result = mac_to_plat_handle(object);
return(result); return(result);
@ -385,8 +385,8 @@ function
system_wake_up_timer_release_sig(){ system_wake_up_timer_release_sig(){
Mac_Object *object = mac_to_object(handle); Mac_Object *object = mac_to_object(handle);
if (object->kind == MacObjectKind_Timer){ if (object->kind == MacObjectKind_Timer){
if ((object->timer.timer != nil) && [object->timer.timer isValid]) { if ((object->timer != nil) && [object->timer isValid]) {
[object->timer.timer invalidate]; [object->timer invalidate];
mac_free_object(object); mac_free_object(object);
} }
} }
@ -397,7 +397,7 @@ system_wake_up_timer_set_sig(){
Mac_Object *object = mac_to_object(handle); Mac_Object *object = mac_to_object(handle);
if (object->kind == MacObjectKind_Timer){ if (object->kind == MacObjectKind_Timer){
f64 time_seconds = ((f64)time_milliseconds / 1000.0); 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 target:mac_vars.view
selector:@selector(requestDisplay) selector:@selector(requestDisplay)
userInfo:nil repeats:NO]; userInfo:nil repeats:NO];
@ -511,7 +511,7 @@ system_thread_launch_sig(){
} }
pthread_mutex_unlock(&mac_vars.thread_launch_mutex); 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); return(result);
} }
@ -533,66 +533,92 @@ system_thread_free_sig(){
function function
system_thread_get_id_sig(){ system_thread_get_id_sig(){
i32 result = (i32)pthread_getthreadid_np(); pthread_t id = pthread_self();
i32 result = *(i32*)(&id);
return(result); return(result);
} }
function function
system_mutex_make_sig(){ system_mutex_make_sig(){
System_Mutex result = {}; Mac_Object *object = mac_alloc_object(MacObjectKind_Mutex);
pthread_mutex_init(&object->mutex, 0);
NotImplemented;
System_Mutex result = mac_to_plat_handle(object);
return(result); return(result);
} }
function function
system_mutex_acquire_sig(){ system_mutex_acquire_sig(){
NotImplemented; Mac_Object *object = mac_to_object(mutex);
if (object->kind == MacObjectKind_Mutex){
pthread_mutex_lock(&object->mutex);
}
} }
function function
system_mutex_release_sig(){ system_mutex_release_sig(){
NotImplemented; Mac_Object *object = mac_to_object(mutex);
if (object->kind == MacObjectKind_Mutex){
pthread_mutex_unlock(&object->mutex);
}
} }
function function
system_mutex_free_sig(){ 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 function
system_acquire_global_frame_mutex_sig(){ system_acquire_global_frame_mutex_sig(){
NotImplemented; if (tctx->kind == ThreadKind_AsyncTasks){
system_mutex_acquire(mac_vars.global_frame_mutex);
}
} }
function function
system_release_global_frame_mutex_sig(){ system_release_global_frame_mutex_sig(){
NotImplemented; if (tctx->kind == ThreadKind_AsyncTasks){
system_mutex_release(mac_vars.global_frame_mutex);
}
} }
function function
system_condition_variable_make_sig(){ system_condition_variable_make_sig(){
System_Condition_Variable result = {}; Mac_Object *object = mac_alloc_object(MacObjectKind_CV);
pthread_cond_init(&object->cv, 0);
NotImplemented;
System_Condition_Variable result = mac_to_plat_handle(object);
return(result); return(result);
} }
function function
system_condition_variable_wait_sig(){ 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 function
system_condition_variable_signal_sig(){ system_condition_variable_signal_sig(){
NotImplemented; Mac_Object *object = mac_to_object(cv);
if (object->kind == MacObjectKind_CV){
pthread_cond_signal(&object->cv);
}
} }
function function
system_condition_variable_free_sig(){ 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);
}
} }
//////////////////////////////// ////////////////////////////////