From 3d9845a3760b4b7ca18a45cbd47e68467232669f Mon Sep 17 00:00:00 2001 From: chr Date: Sat, 11 Jan 2020 22:07:01 -0800 Subject: [PATCH] More linux platform layer --- platform_linux/linux_4ed.cpp | 104 ++++++++++++++++++++++++++++++++++- 1 file changed, 102 insertions(+), 2 deletions(-) diff --git a/platform_linux/linux_4ed.cpp b/platform_linux/linux_4ed.cpp index d7551f4f..15e8bf08 100644 --- a/platform_linux/linux_4ed.cpp +++ b/platform_linux/linux_4ed.cpp @@ -74,6 +74,8 @@ struct Linux_Vars { int epoll; Node free_linux_objects; + + System_Mutex global_frame_mutex; }; enum { @@ -83,6 +85,8 @@ enum { typedef i32 Linux_Object_Kind; enum { LinuxObjectKind_Thread = 1, + LinuxObjectKind_Mutex = 2, + LinuxObjectKind_ConditionVariable = 3, }; struct Linux_Object { @@ -94,6 +98,8 @@ struct Linux_Object { Thread_Function* proc; void* ptr; } thread; + pthread_mutex_t mutex; + pthread_cond_t condition_variable; }; }; @@ -610,11 +616,95 @@ system_thread_join_sig(){ internal system_thread_free_sig(){ Linux_Object* object = *(Linux_Object**)&thread; - if (object->kind == LinuxObjectKind_Thread) { - linux_free_object(object); + Assert(object->kind == LinuxObjectKind_Thread); + linux_free_object(object); +} + +internal +system_thread_get_id_sig(){ + pthread_t tid = pthread_self(); + Assert(tid <= (u64)max_i32); + return (i32)tid; +} + +internal +system_acquire_global_frame_mutex_sig(){ + if (tctx->kind == ThreadKind_AsyncTasks){ + system_mutex_acquire(linuxvars.global_frame_mutex); } } +internal +system_release_global_frame_mutex_sig(){ + if (tctx->kind == ThreadKind_AsyncTasks){ + system_mutex_release(linuxvars.global_frame_mutex); + } +} + +internal +system_mutex_make_sig(){ + System_Mutex result = {}; + Linux_Object* object = linux_alloc_object(LinuxObjectKind_Mutex); + pthread_mutex_init(&object->mutex, NULL); + *(Linux_Object**)&result = object; + return result; +} + +internal +system_mutex_acquire_sig(){ + Linux_Object* object = *(Linux_Object**)&mutex; + Assert(object->kind == LinuxObjectKind_Mutex); + pthread_mutex_lock(&object->mutex); +} + +internal +system_mutex_release_sig(){ + Linux_Object* object = *(Linux_Object**)&mutex; + Assert(object->kind == LinuxObjectKind_Mutex); + pthread_mutex_unlock(&object->mutex); +} + +internal +system_mutex_free_sig(){ + Linux_Object* object = *(Linux_Object**)&mutex; + Assert(object->kind == LinuxObjectKind_Mutex); + pthread_mutex_destroy(&object->mutex); + linux_free_object(object); +} + +internal +system_condition_variable_make_sig(){ + System_Condition_Variable result = {}; + Linux_Object* object = linux_alloc_object(LinuxObjectKind_ConditionVariable); + pthread_cond_init(&object->condition_variable, NULL); + *(Linux_Object**)&result = object; + return result; +} + +internal +system_condition_variable_wait_sig(){ + Linux_Object* cv_object = *(Linux_Object**)&cv; + Linux_Object* mutex_object = *(Linux_Object**)&mutex; + Assert(cv_object->kind == LinuxObjectKind_ConditionVariable); + Assert(mutex_object->kind == LinuxObjectKind_Mutex); + pthread_cond_wait(&cv_object->condition_variable, &mutex_object->mutex); +} + +internal +system_condition_variable_signal_sig(){ + Linux_Object* object = *(Linux_Object**)&cv; + Assert(object->kind == LinuxObjectKind_ConditionVariable); + pthread_cond_signal(&object->condition_variable); +} + +internal +system_condition_variable_free_sig(){ + Linux_Object* object = *(Linux_Object**)&cv; + Assert(object->kind == LinuxObjectKind_ConditionVariable); + pthread_cond_destroy(&object->condition_variable); + linux_free_object(object); +} + internal system_memory_allocate_sig(){ void* result = mmap( @@ -623,6 +713,16 @@ system_memory_allocate_sig(){ return result; } +internal +system_memory_set_protection_sig(){ + int protect = 0; + MovFlag(flags, MemProtect_Read, protect, PROT_READ); + MovFlag(flags, MemProtect_Write, protect, PROT_WRITE); + MovFlag(flags, MemProtect_Execute, protect, PROT_EXEC); + int result = mprotect(ptr, size, protect); + return result == 0; +} + internal system_memory_free_sig(){ munmap(ptr, size);