Get Odin to compile on Haiku

This patch makes Odin to compile on Haiku which is a good first step.
Now, all that's needed to do is to figure out how to do futexes, which
I am blaming for the program crashing.
This commit is contained in:
Slendi
2024-02-15 15:51:28 +02:00
parent c5c2a4d09d
commit c178f7199d
4 changed files with 559 additions and 464 deletions
+5
View File
@@ -83,6 +83,11 @@ OpenBSD)
LDFLAGS="$LDFLAGS -liconv" LDFLAGS="$LDFLAGS -liconv"
LDFLAGS="$LDFLAGS $($LLVM_CONFIG --libs core native --system-libs)" LDFLAGS="$LDFLAGS $($LLVM_CONFIG --libs core native --system-libs)"
;; ;;
Haiku)
CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags) -I/system/develop/headers/private/shared -I/system/develop/headers/private/kernel"
LDFLAGS="$LDFLAGS -liconv"
LDFLAGS="$LDFLAGS $($LLVM_CONFIG --libs core native --system-libs)"
;;
*) *)
error "Platform \"$OS_NAME\" unsupported" error "Platform \"$OS_NAME\" unsupported"
;; ;;
+47 -2
View File
@@ -83,6 +83,10 @@ extern "C" {
#ifndef GB_SYSTEM_OPENBSD #ifndef GB_SYSTEM_OPENBSD
#define GB_SYSTEM_OPENBSD 1 #define GB_SYSTEM_OPENBSD 1
#endif #endif
#elif defined(__HAIKU__) || defined(__haiku__)
#ifndef GB_SYSTEM_HAIKU
#define GB_SYSTEM_HAIKU 1
#endif
#else #else
#error This UNIX operating system is not supported #error This UNIX operating system is not supported
#endif #endif
@@ -206,7 +210,7 @@ extern "C" {
#endif #endif
#include <stdlib.h> // NOTE(bill): malloc on linux #include <stdlib.h> // NOTE(bill): malloc on linux
#include <sys/mman.h> #include <sys/mman.h>
#if !defined(GB_SYSTEM_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__) #if !defined(GB_SYSTEM_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__HAIKU__)
#include <sys/sendfile.h> #include <sys/sendfile.h>
#endif #endif
#include <sys/stat.h> #include <sys/stat.h>
@@ -248,6 +252,13 @@ extern "C" {
#define lseek64 lseek #define lseek64 lseek
#endif #endif
#if defined(GB_SYSTEM_HAIKU)
#include <stdio.h>
#include <pthread.h>
#include <kernel/OS.h>
#define lseek64 lseek
#endif
#if defined(GB_SYSTEM_UNIX) #if defined(GB_SYSTEM_UNIX)
#include <semaphore.h> #include <semaphore.h>
#endif #endif
@@ -801,6 +812,13 @@ typedef struct gbAffinity {
isize thread_count; isize thread_count;
isize threads_per_core; isize threads_per_core;
} gbAffinity; } gbAffinity;
#elif defined(GB_SYSTEM_HAIKU)
typedef struct gbAffinity {
b32 is_accurate;
isize core_count;
isize thread_count;
isize threads_per_core;
} gbAffinity;
#else #else
#error TODO(bill): Unknown system #error TODO(bill): Unknown system
#endif #endif
@@ -2984,6 +3002,8 @@ gb_inline u32 gb_thread_current_id(void) {
__asm__("mov %%fs:0x10,%0" : "=r"(thread_id)); __asm__("mov %%fs:0x10,%0" : "=r"(thread_id));
#elif defined(GB_SYSTEM_LINUX) #elif defined(GB_SYSTEM_LINUX)
thread_id = gettid(); thread_id = gettid();
#elif defined(GB_SYSTEM_HAIKU)
thread_id = find_thread(NULL);
#else #else
#error Unsupported architecture for gb_thread_current_id() #error Unsupported architecture for gb_thread_current_id()
#endif #endif
@@ -3184,7 +3204,9 @@ b32 gb_affinity_set(gbAffinity *a, isize core, isize thread_index) {
//info.affinity_tag = cast(integer_t)index; //info.affinity_tag = cast(integer_t)index;
//result = thread_policy_set(thread, THREAD_AFFINITY_POLICY, cast(thread_policy_t)&info, THREAD_AFFINITY_POLICY_COUNT); //result = thread_policy_set(thread, THREAD_AFFINITY_POLICY, cast(thread_policy_t)&info, THREAD_AFFINITY_POLICY_COUNT);
#if !defined(GB_SYSTEM_HAIKU)
result = pthread_setaffinity_np(thread, sizeof(cpuset_t), &mn); result = pthread_setaffinity_np(thread, sizeof(cpuset_t), &mn);
#endif
return result == 0; return result == 0;
} }
@@ -3236,6 +3258,29 @@ b32 gb_affinity_set(gbAffinity *a, isize core, isize thread_index) {
return true; return true;
} }
isize gb_affinity_thread_count_for_core(gbAffinity *a, isize core) {
GB_ASSERT(0 <= core && core < a->core_count);
return a->threads_per_core;
}
#elif defined(GB_SYSTEM_HAIKU)
#include <unistd.h>
void gb_affinity_init(gbAffinity *a) {
a->core_count = sysconf(_SC_NPROCESSORS_ONLN);
a->threads_per_core = 1;
a->is_accurate = a->core_count > 0;
a->core_count = a->is_accurate ? a->core_count : 1;
a->thread_count = a->core_count;
}
void gb_affinity_destroy(gbAffinity *a) {
gb_unused(a);
}
b32 gb_affinity_set(gbAffinity *a, isize core, isize thread_index) {
return true;
}
isize gb_affinity_thread_count_for_core(gbAffinity *a, isize core) { isize gb_affinity_thread_count_for_core(gbAffinity *a, isize core) {
GB_ASSERT(0 <= core && core < a->core_count); GB_ASSERT(0 <= core && core < a->core_count);
return a->threads_per_core; return a->threads_per_core;
@@ -5457,7 +5502,7 @@ gb_inline b32 gb_file_copy(char const *existing_filename, char const *new_filena
} }
} }
gb_free(buf); gb_mfree(buf);
close(new_fd); close(new_fd);
close(existing_fd); close(existing_fd);
+1 -1
View File
@@ -341,7 +341,7 @@ gb_internal ReadDirectoryError read_directory(String path, Array<FileInfo> *fi)
return ReadDirectory_None; return ReadDirectory_None;
} }
#elif defined(GB_SYSTEM_LINUX) || defined(GB_SYSTEM_OSX) || defined(GB_SYSTEM_FREEBSD) || defined(GB_SYSTEM_OPENBSD) #elif defined(GB_SYSTEM_LINUX) || defined(GB_SYSTEM_OSX) || defined(GB_SYSTEM_FREEBSD) || defined(GB_SYSTEM_OPENBSD) || defined(GB_SYSTEM_HAIKU)
#include <dirent.h> #include <dirent.h>
+45
View File
@@ -831,6 +831,51 @@ gb_internal void futex_wait(Futex *f, Footex val) {
WaitOnAddress(f, (void *)&val, sizeof(val), INFINITE); WaitOnAddress(f, (void *)&val, sizeof(val), INFINITE);
} while (f->load() == val); } while (f->load() == val);
} }
#elif defined(GB_SYSTEM_HAIKU)
#include <pthread.h>
#include <unordered_map>
#include <memory>
struct MutexCond {
pthread_mutex_t mutex;
pthread_cond_t cond;
};
std::unordered_map<Futex*, std::unique_ptr<MutexCond>> futex_map;
MutexCond* get_mutex_cond(Futex* f) {
if (futex_map.find(f) == futex_map.end()) {
futex_map[f] = std::make_unique<MutexCond>();
pthread_mutex_init(&futex_map[f]->mutex, NULL);
pthread_cond_init(&futex_map[f]->cond, NULL);
}
return futex_map[f].get();
}
void futex_signal(Futex *f) {
MutexCond* mc = get_mutex_cond(f);
pthread_mutex_lock(&mc->mutex);
pthread_cond_signal(&mc->cond);
pthread_mutex_unlock(&mc->mutex);
}
void futex_broadcast(Futex *f) {
MutexCond* mc = get_mutex_cond(f);
pthread_mutex_lock(&mc->mutex);
pthread_cond_broadcast(&mc->cond);
pthread_mutex_unlock(&mc->mutex);
}
void futex_wait(Futex *f, Footex val) {
MutexCond* mc = get_mutex_cond(f);
pthread_mutex_lock(&mc->mutex);
while (f->load() == val) {
pthread_cond_wait(&mc->cond, &mc->mutex);
}
pthread_mutex_unlock(&mc->mutex);
}
#endif #endif
#if defined(GB_SYSTEM_WINDOWS) #if defined(GB_SYSTEM_WINDOWS)