From 354b4fe6dc0d0867800a44606c5e1aba79873ced Mon Sep 17 00:00:00 2001 From: Yuval Dolev Date: Sun, 19 Jan 2020 05:47:01 +0200 Subject: [PATCH] All mutexes are now refrence counted. This solves a lot of the crashes caused by deadlocks in situations where the same thread locks its own mutex multiple times before unlocking it. --- platform_mac/mac_4ed.mm | 14 ++++++++++++-- platform_mac/mac_4ed_functions.mm | 6 +++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/platform_mac/mac_4ed.mm b/platform_mac/mac_4ed.mm index 6275f6d9..2033dc50 100644 --- a/platform_mac/mac_4ed.mm +++ b/platform_mac/mac_4ed.mm @@ -67,6 +67,7 @@ #include // NOTE(yuval): Used for mmap, munmap, mprotect #include // NOTE(yuval): Used for stat #include // NOTE(yuval): Used for struct stat, pid_t +#include // NOTE(yuval): Used for PATH_MAX #include // NOTE(yuval): Used for free @@ -297,6 +298,15 @@ mac_to_object(Plat_Handle handle){ //////////////////////////////// +function void +mac_init_recursive_mutex(pthread_mutex_t *mutex){ + pthread_mutexattr_t attr; + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(mutex, &attr); +} + +//////////////////////////////// + function void mac_error_box(char *msg, b32 shutdown = true){ NSAlert *alert = [[[NSAlert alloc] init] autorelease]; @@ -1097,7 +1107,7 @@ main(int arg_count, char **args){ FCoder_App_Delegate *app_delegate = [[FCoder_App_Delegate alloc] init]; [NSApp setDelegate:app_delegate]; - pthread_mutex_init(&memory_tracker_mutex, 0); + mac_init_recursive_mutex(&memory_tracker_mutex); // NOTE(yuval): Context setup Thread_Context _tctx = {}; @@ -1124,7 +1134,7 @@ main(int arg_count, char **args){ dll_init_sentinel(&mac_vars.free_mac_objects); dll_init_sentinel(&mac_vars.timer_objects); - pthread_mutex_init(&mac_vars.thread_launch_mutex, 0); + mac_init_recursive_mutex(&mac_vars.thread_launch_mutex); pthread_cond_init(&mac_vars.thread_launch_cv, 0); // NOTE(yuval): Screen scale factor calculation diff --git a/platform_mac/mac_4ed_functions.mm b/platform_mac/mac_4ed_functions.mm index dbfc11b0..9a211136 100644 --- a/platform_mac/mac_4ed_functions.mm +++ b/platform_mac/mac_4ed_functions.mm @@ -638,7 +638,7 @@ system_thread_get_id_sig(){ function system_mutex_make_sig(){ Mac_Object *object = mac_alloc_object(MacObjectKind_Mutex); - pthread_mutex_init(&object->mutex, 0); + mac_init_recursive_mutex(&object->mutex); System_Mutex result = mac_to_plat_handle(object); return(result); @@ -846,8 +846,8 @@ system_memory_annotation_sig(){ node != 0; node = node->next){ // TODO(yuval): Fix the API so that annotations would not mess with the system memory. - // Memory_Annotation_Node *r_node = push_array(arena, Memory_Annotation_Node, 1); - Memory_Annotation_Node *r_node = (Memory_Annotation_Node*)malloc(sizeof(Memory_Annotation_Node)); + Memory_Annotation_Node *r_node = push_array(arena, Memory_Annotation_Node, 1); + //Memory_Annotation_Node *r_node = (Memory_Annotation_Node*)malloc(sizeof(Memory_Annotation_Node)); sll_queue_push(result.first, result.last, r_node); result.count += 1;