Add FreeBSD targets, get gb.h working with FreeBSD, fix odin_root_directory function for FreeBSD and a few other operating systems not yet added

This commit is contained in:
Christian Seibold
2020-09-14 11:28:41 -05:00
parent b8bebf4511
commit ac126a8cd7
3 changed files with 125 additions and 2 deletions

View File

@@ -1,3 +1,8 @@
#if defined(GB_SYSTEM_FREEBSD)
#include <sys/types.h>
#include <sys/sysctl.h>
#endif
enum TargetOsKind {
TargetOs_Invalid,
@@ -6,6 +11,7 @@ enum TargetOsKind {
TargetOs_linux,
TargetOs_essence,
TargetOs_js,
TargetOs_freebsd,
TargetOs_COUNT,
};
@@ -36,6 +42,7 @@ String target_os_names[TargetOs_COUNT] = {
str_lit("linux"),
str_lit("essence"),
str_lit("js"),
str_lit("freebsd"),
};
String target_arch_names[TargetArch_COUNT] = {
@@ -204,6 +211,23 @@ gb_global TargetMetrics target_darwin_amd64 = {
str_lit("e-m:o-i64:64-f80:128-n8:16:32:64-S128"),
};
gb_global TargetMetrics target_freebsd_386 = {
TargetOs_freebsd,
TargetArch_386,
4,
8,
str_lit("i386-unknown-freebsd"),
};
gb_global TargetMetrics target_freebsd_amd64 = {
TargetOs_freebsd,
TargetArch_386,
8,
16,
str_lit("x86_64-unknown-freebsd"),
str_lit("e-m:w-i64:64-f80:128-n8:16:32:64-S128"),
};
gb_global TargetMetrics target_essence_amd64 = {
TargetOs_essence,
TargetArch_amd64,
@@ -235,6 +259,8 @@ gb_global NamedTargetMetrics named_targets[] = {
{ str_lit("linux_amd64"), &target_linux_amd64 },
{ str_lit("windows_386"), &target_windows_386 },
{ str_lit("windows_amd64"), &target_windows_amd64 },
{ str_lit("freebsd_386"), &target_freebsd_386 },
{ str_lit("freebsd_amd64"), &target_freebsd_amd64 },
};
NamedTargetMetrics *selected_target_metrics;
@@ -476,7 +502,21 @@ String odin_root_dir(void) {
// of this compiler, it should be _good enough_.
// That said, there's no solid 100% method on Linux to get the program's
// path without checking this link. Sorry.
#if defined(GB_SYSTEM_FREEBSD)
int mib[4];
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PATHNAME;
mib[3] = -1;
len = path_buf.count;
sysctl(mib, 4, &path_buf[0], (size_t *) &len, NULL, 0);
#elif defined(GB_SYSTEM_NETBSD)
len = readlink("/proc/curproc/exe", &path_buf[0], path_buf.count);
#elif defined(GB_SYSTEM_DRAGONFLYBSD)
len = readlink("/proc/curproc/file", &path_buf[0], path_buf.count);
#else
len = readlink("/proc/self/exe", &path_buf[0], path_buf.count);
#endif
if(len == 0) {
return make_string(nullptr, 0);
}

View File

@@ -954,7 +954,7 @@ ReadDirectoryError read_directory(String path, Array<FileInfo> *fi) {
return ReadDirectory_None;
}
#elif defined(GB_SYSTEM_LINUX) || defined(GB_SYSTEM_OSX)
#elif defined(GB_SYSTEM_LINUX) || defined(GB_SYSTEM_OSX) || defined(GB_SYSTEM_FREEBSD)
#include <dirent.h>

View File

@@ -317,7 +317,7 @@ extern "C" {
#endif
#include <stdlib.h> // NOTE(bill): malloc on linux
#include <sys/mman.h>
#if !defined(GB_SYSTEM_OSX)
#if !defined(GB_SYSTEM_OSX) && !defined(__FreeBSD__)
#include <sys/sendfile.h>
#endif
#include <sys/stat.h>
@@ -342,6 +342,17 @@ extern "C" {
#include <mach/clock.h>
#endif
#if defined(GB_SYSTEM_FREEBSD)
#include <sys/sysctl.h>
#include <pthread_np.h>
#include <sys/cpuset.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#define lseek64 lseek
#define sendfile(out, in, offset, count) sendfile(out, in, offset, count, NULL, NULL, 0)
#endif
#if defined(GB_SYSTEM_UNIX)
#include <semaphore.h>
#endif
@@ -1046,6 +1057,13 @@ typedef struct gbAffinity {
isize thread_count;
isize threads_per_core;
} gbAffinity;
#elif defined(GB_SYSTEM_FREEBSD)
typedef struct gbAffinity {
b32 is_accurate;
isize core_count;
isize thread_count;
isize threads_per_core;
} gbAffinity;
#else
#error TODO(bill): Unknown system
#endif
@@ -4805,6 +4823,8 @@ void gb_thread_set_name(gbThread *t, char const *name) {
#elif defined(GB_SYSTEM_OSX)
// TODO(bill): Test if this works
pthread_setname_np(name);
#elif defined(GB_SYSTEM_FREEBSD)
pthread_set_name_np(t->posix_handle, name);
#else
// TODO(bill): Test if this works
pthread_setname_np(t->posix_handle, name);
@@ -5090,6 +5110,69 @@ isize gb_affinity_thread_count_for_core(gbAffinity *a, isize core) {
return a->threads_per_core;
}
#elif defined(GB_SYSTEM_FREEBSD)
#include <stdio.h>
void gb_affinity_init(gbAffinity *a) {
usize count = 0;
usize count_size = sizeof(count);
a->is_accurate = false;
a->thread_count = 1;
a->core_count = 1;
a->threads_per_core = 1;
if (sysctlbyname("hw.logicalcpu", &count, &count_size, NULL, 0) == 0) {
if (count > 0) {
a->thread_count = count;
// Get # of physical cores
if (sysctlbyname("hw.physicalcpu", &count, &count_size, NULL, 0) == 0) {
if (count > 0) {
a->core_count = count;
a->threads_per_core = a->thread_count / count;
if (a->threads_per_core < 1)
a->threads_per_core = 1;
else
a->is_accurate = true;
}
}
}
}
}
void gb_affinity_destroy(gbAffinity *a) {
gb_unused(a);
}
b32 gb_affinity_set(gbAffinity *a, isize core, isize thread_index) {
isize index;
pthread_t thread;
int result;
GB_ASSERT(core < a->core_count);
GB_ASSERT(thread_index < a->threads_per_core);
index = core * a->threads_per_core + thread_index;
thread = pthread_self();
cpuset_t mn;
CPU_ZERO(&mn);
CPU_SET(size_t(index), &mn);
//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 = pthread_setaffinity_np(thread, sizeof(cpuset_t), &mn);
return result == 0;
}
isize gb_affinity_thread_count_for_core(gbAffinity *a, isize core) {
GB_ASSERT(core >= 0 && core < a->core_count);
return a->threads_per_core;
}
#elif defined(GB_SYSTEM_LINUX)
// IMPORTANT TODO(bill): This gbAffinity stuff for linux needs be improved a lot!
// NOTE(zangent): I have to read /proc/cpuinfo to get the number of threads per core.