Use pthread mutex

This commit is contained in:
Ginger Bill
2017-08-01 13:49:12 +01:00
parent 1775e80b41
commit 2db971eedd
2 changed files with 25 additions and 68 deletions

View File

@@ -930,19 +930,13 @@ GB_DEF void gb_semaphore_wait (gbSemaphore *s);
// Mutex
// TODO(bill): Should this be replaced with a CRITICAL_SECTION on win32 or is the better?
typedef struct gbMutex {
#if defined(GB_SYSTEM_WINDOWS)
typedef struct gbMutex {
CRITICAL_SECTION win32_critical_section;
} gbMutex;
#else
typedef struct gbMutex {
gbSemaphore semaphore;
gbAtomic32 counter;
gbAtomic32 owner;
i32 recursion;
} gbMutex;
pthread_mutex_t pthread_mutex;
#endif
} gbMutex;
GB_DEF void gb_mutex_init (gbMutex *m);
GB_DEF void gb_mutex_destroy (gbMutex *m);
@@ -4608,12 +4602,7 @@ gb_inline void gb_mutex_init(gbMutex *m) {
#if defined(GB_SYSTEM_WINDOWS)
InitializeCriticalSection(&m->win32_critical_section);
#else
// NOTE(bill): THIS IS FUCKING AWESOME THAT THIS "MUTEX" IS FAST AND RECURSIVE TOO!
// NOTE(bill): WHO THE FUCK NEEDS A NORMAL MUTEX NOW?!?!?!?!
gb_atomic32_store(&m->counter, 0);
gb_atomic32_store(&m->owner, gb_thread_current_id());
gb_semaphore_init(&m->semaphore);
m->recursion = 0;
pthread_mutex_init(&m->pthread_mutex, NULL);
#endif
}
@@ -4621,7 +4610,7 @@ gb_inline void gb_mutex_destroy(gbMutex *m) {
#if defined(GB_SYSTEM_WINDOWS)
DeleteCriticalSection(&m->win32_critical_section);
#else
gb_semaphore_destroy(&m->semaphore);
pthread_mutex_destroy(&m->pthread_mutex);
#endif
}
@@ -4629,15 +4618,7 @@ gb_inline void gb_mutex_lock(gbMutex *m) {
#if defined(GB_SYSTEM_WINDOWS)
EnterCriticalSection(&m->win32_critical_section);
#else
i32 thread_id = cast(i32)gb_thread_current_id();
if (gb_atomic32_fetch_add(&m->counter, 1) > 0) {
if (thread_id != gb_atomic32_load(&m->owner)) {
gb_semaphore_wait(&m->semaphore);
}
}
gb_atomic32_store(&m->owner, thread_id);
m->recursion++;
pthread_mutex_lock(&m->pthread_mutex);
#endif
}
@@ -4645,22 +4626,7 @@ gb_inline b32 gb_mutex_try_lock(gbMutex *m) {
#if defined(GB_SYSTEM_WINDOWS)
return TryEnterCriticalSection(&m->win32_critical_section) != 0;
#else
i32 thread_id = cast(i32)gb_thread_current_id();
if (gb_atomic32_load(&m->owner) == thread_id) {
gb_atomic32_fetch_add(&m->counter, 1);
} else {
i32 expected = 0;
if (gb_atomic32_load(&m->counter) != 0) {
return false;
}
if (!gb_atomic32_compare_exchange(&m->counter, expected, 1)) {
return false;
}
gb_atomic32_store(&m->owner, thread_id);
}
m->recursion++;
return true;
return pthread_mutex_trylock(&m->pthread_mutex) == 0;
#endif
}
@@ -4668,19 +4634,7 @@ gb_inline void gb_mutex_unlock(gbMutex *m) {
#if defined(GB_SYSTEM_WINDOWS)
LeaveCriticalSection(&m->win32_critical_section);
#else
i32 recursion;
i32 thread_id = cast(i32)gb_thread_current_id();
recursion = --m->recursion;
if (recursion == 0) {
gb_atomic32_store(&m->owner, thread_id);
}
if (gb_atomic32_fetch_add(&m->counter, -1) > 1) {
if (recursion == 0) {
gb_semaphore_release(&m->semaphore);
}
}
pthread_mutex_unlock(&m->pthread_mutex);
#endif
}

View File

@@ -83,7 +83,8 @@ struct Parser {
gbAtomic32 import_index;
isize total_token_count;
isize total_line_count;
gbMutex mutex;
gbMutex file_add_mutex;
gbMutex file_decl_mutex;
};
enum ProcTag {
@@ -4812,7 +4813,8 @@ void destroy_ast_file(AstFile *f) {
bool init_parser(Parser *p) {
array_init(&p->files, heap_allocator());
array_init(&p->imports, heap_allocator());
gb_mutex_init(&p->mutex);
gb_mutex_init(&p->file_add_mutex);
gb_mutex_init(&p->file_decl_mutex);
return true;
}
@@ -4828,14 +4830,12 @@ void destroy_parser(Parser *p) {
#endif
array_free(&p->files);
array_free(&p->imports);
gb_mutex_destroy(&p->mutex);
gb_mutex_destroy(&p->file_add_mutex);
gb_mutex_destroy(&p->file_decl_mutex);
}
// NOTE(bill): Returns true if it's added
bool try_add_import_path(Parser *p, String path, String rel_path, TokenPos pos) {
gb_mutex_lock(&p->mutex);
defer (gb_mutex_unlock(&p->mutex));
if (build_context.generate_docs) {
return false;
}
@@ -4923,6 +4923,7 @@ void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, Array<AstNod
String file_str = id->relpath.string;
gbAllocator allocator = heap_allocator(); // TODO(bill): Change this allocator
String import_file = {};
String rel_path = {};
if (!is_import_path_valid(file_str)) {
if (id->is_import) {
@@ -4936,7 +4937,11 @@ void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, Array<AstNod
}
String rel_path = get_fullpath_relative(allocator, base_dir, file_str);
gb_mutex_lock(&p->file_decl_mutex);
defer (gb_mutex_unlock(&p->file_decl_mutex));
rel_path = get_fullpath_relative(allocator, base_dir, file_str);
import_file = rel_path;
if (!gb_file_exists(cast(char *)rel_path.text)) { // NOTE(bill): This should be null terminated
String abs_path = get_fullpath_core(allocator, file_str);
@@ -5039,13 +5044,11 @@ ParseFileError parse_import(Parser *p, ImportedFile imported_file) {
}
parse_file(p, &file);
{
gb_mutex_lock(&p->mutex);
file.id = imported_file.index;
array_add(&p->files, file);
p->total_line_count += file.tokenizer.line_count;
gb_mutex_unlock(&p->mutex);
}
gb_mutex_lock(&p->file_add_mutex);
file.id = imported_file.index;
array_add(&p->files, file);
p->total_line_count += file.tokenizer.line_count;
gb_mutex_unlock(&p->file_add_mutex);
return ParseFile_None;