add necessary Linux impls for linker

NOTE: Current OS API for semaphores doesn't work well with Linux/pthreads
way, for example, on Linux it's up to user program to allocate shared memory
and store there sem_t and detect when to release it. (Windows does this under
the hood when process that created shared semaphore exits)
This commit is contained in:
Nikita Smith
2024-11-08 11:27:32 -08:00
parent cf8c0e7442
commit e297460697
2 changed files with 87 additions and 17 deletions
+73 -4
View File
@@ -154,6 +154,22 @@ os_get_current_path(Arena *arena)
return string;
}
internal U32
os_get_process_start_time_unix(void)
{
Temp scratch = scratch_begin(0,0);
pid_t pid = getpid();
String8 path = push_str8f(scratch.arena, "/proc/%u", pid);
struct stat st;
int err = stat((char*)path.str, &st);
if(err == 0)
{
return (U32)st.st_mtime;
}
scratch_end(scratch);
return 0;
}
////////////////////////////////
//~ rjf: @os_hooks Memory Allocation (Implemented Per-OS)
@@ -453,6 +469,22 @@ os_file_path_exists(String8 path)
return result;
}
internal B32
os_folder_path_exists(String8 path)
{
Temp scratch = scratch_begin(0, 0);
B32 exists = 0;
String8 path_copy = push_str8_copy(scratch.arena, path);
DIR *handle = opendir((char*)path_copy.str);
if(handle)
{
closedir(handle);
exists = 1;
}
scratch_end(scratch);
return exists;
}
internal FileProperties
os_properties_from_file_path(String8 path)
{
@@ -1028,13 +1060,26 @@ os_condition_variable_broadcast(OS_Handle cv)
internal OS_Handle
os_semaphore_alloc(U32 initial_count, U32 max_count, String8 name)
{
NotImplemented;
OS_Handle result = {0};
if (name.size > 0) {
// TODO: we need to allocate shared memory to store sem_t
NotImplemented;
} else {
sem_t *s = mmap(0, sizeof(*s), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
AssertAlways(s != MAP_FAILED);
int err = sem_init(s, 0, initial_count);
if (err == 0) {
result.u64[0] = (U64)s;
}
}
return result;
}
internal void
os_semaphore_release(OS_Handle semaphore)
{
NotImplemented;
int err = munmap((void*)semaphore.u64[0], sizeof(sem_t));
AssertAlways(err == 0);
}
internal OS_Handle
@@ -1052,13 +1097,37 @@ os_semaphore_close(OS_Handle semaphore)
internal B32
os_semaphore_take(OS_Handle semaphore, U64 endt_us)
{
NotImplemented;
AssertAlways(endt_us == max_U64);
for (;;) {
int err = sem_wait((sem_t*)semaphore.u64[0]);
if (err == 0) {
break;
} else {
if (errno == EAGAIN) {
continue;
}
}
InvalidPath;
break;
}
return 1;
}
internal void
os_semaphore_drop(OS_Handle semaphore)
{
NotImplemented;
for (;;) {
int err = sem_post((sem_t*)semaphore.u64[0]);
if (err == 0) {
break;
} else {
if (errno == EAGAIN) {
continue;
}
}
InvalidPath;
break;
}
}
////////////////////////////////
+14 -13
View File
@@ -8,24 +8,25 @@
//~ rjf: Includes
#define _GNU_SOURCE
#include <dirent.h>
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <features.h>
#include <linux/limits.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/limits.h>
#include <time.h>
#include <dirent.h>
#include <pthread.h>
#include <sys/syscall.h>
#include <signal.h>
#include <errno.h>
#include <dlfcn.h>
#include <sys/sysinfo.h>
#include <sys/random.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
pid_t gettid(void);
int pthread_setname_np(pthread_t thread, const char *name);