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.

This commit is contained in:
Yuval Dolev 2020-01-19 05:47:01 +02:00
parent b255da9d00
commit 354b4fe6dc
2 changed files with 15 additions and 5 deletions

View File

@ -67,6 +67,7 @@
#include <sys/mman.h> // NOTE(yuval): Used for mmap, munmap, mprotect #include <sys/mman.h> // NOTE(yuval): Used for mmap, munmap, mprotect
#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
#include <sys/syslimits.h> // NOTE(yuval): Used for PATH_MAX
#include <stdlib.h> // NOTE(yuval): Used for free #include <stdlib.h> // 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 function void
mac_error_box(char *msg, b32 shutdown = true){ mac_error_box(char *msg, b32 shutdown = true){
NSAlert *alert = [[[NSAlert alloc] init] autorelease]; 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]; FCoder_App_Delegate *app_delegate = [[FCoder_App_Delegate alloc] init];
[NSApp setDelegate:app_delegate]; [NSApp setDelegate:app_delegate];
pthread_mutex_init(&memory_tracker_mutex, 0); mac_init_recursive_mutex(&memory_tracker_mutex);
// NOTE(yuval): Context setup // NOTE(yuval): Context setup
Thread_Context _tctx = {}; 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.free_mac_objects);
dll_init_sentinel(&mac_vars.timer_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); pthread_cond_init(&mac_vars.thread_launch_cv, 0);
// NOTE(yuval): Screen scale factor calculation // NOTE(yuval): Screen scale factor calculation

View File

@ -638,7 +638,7 @@ system_thread_get_id_sig(){
function function
system_mutex_make_sig(){ system_mutex_make_sig(){
Mac_Object *object = mac_alloc_object(MacObjectKind_Mutex); 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); System_Mutex result = mac_to_plat_handle(object);
return(result); return(result);
@ -846,8 +846,8 @@ system_memory_annotation_sig(){
node != 0; node != 0;
node = node->next){ node = node->next){
// TODO(yuval): Fix the API so that annotations would not mess with the system memory. // 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 = 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 = (Memory_Annotation_Node*)malloc(sizeof(Memory_Annotation_Node));
sll_queue_push(result.first, result.last, r_node); sll_queue_push(result.first, result.last, r_node);
result.count += 1; result.count += 1;