This commit is contained in:
snarmph 2026-08-30 12:17:39 +02:00
parent 93b05f3060
commit 79f30bb23b
3 changed files with 76 additions and 10 deletions

View file

@ -518,7 +518,7 @@ char char_lower(char c) {
} }
char char_upper(char c) { char char_upper(char c) {
return c <= 'a' && c >= 'z' ? c - 32 : c; return c >= 'a' && c <= 'z' ? c - 32 : c;
} }
// == INPUT STREAM ================================================= // == INPUT STREAM =================================================
@ -1614,7 +1614,6 @@ void os_log_print(const char *file, int line, os_log_level_e level, const char *
void os_log_printv(const char *file, int line, os_log_level_e level, const char *fmt, va_list args) { void os_log_printv(const char *file, int line, os_log_level_e level, const char *fmt, va_list args) {
time_t t = time(NULL); time_t t = time(NULL);
struct tm log_time = { 0 };
#define gettime(src, dst) localtime_s(&dst, &src) #define gettime(src, dst) localtime_s(&dst, &src)
log_event_t ev = { log_event_t ev = {
.level = level, .level = level,
@ -1628,6 +1627,7 @@ void os_log_printv(const char *file, int line, os_log_level_e level, const char
va_copy(ev.args, args); va_copy(ev.args, args);
#if COLLA_WIN #if COLLA_WIN
struct tm log_time = { 0 };
localtime_s(&log_time, &t); localtime_s(&log_time, &t);
ev.time = &log_time; ev.time = &log_time;
#else #else
@ -1890,7 +1890,7 @@ job_queue_t *jq_init(arena_t *arena, int worker_count) {
job_queue_t *q = alloc(arena, job_queue_t); job_queue_t *q = alloc(arena, job_queue_t);
q->mutex = os_mutex_create(); q->mutex = os_mutex_create();
q->condvar = os_cond_create(); q->condvar = os_cond_create();
q->thread_count = worker_count ? worker_count : os_get_system_info().processor_count; q->thread_count = worker_count ? (u32)worker_count : os_get_system_info().processor_count;
q->threads = alloc(arena, oshandle_t, q->thread_count); q->threads = alloc(arena, oshandle_t, q->thread_count);
for (int i = 0; i < q->thread_count; ++i) { for (int i = 0; i < q->thread_count; ++i) {
@ -2963,7 +2963,7 @@ strview_t html_closing_p_tags[] = {
}; };
bool html__closes_p_tag(strview_t tag) { bool html__closes_p_tag(strview_t tag) {
for (int i = 0; i < arrlen(html_closing_p_tags); ++i) { for (u32 i = 0; i < arrlen(html_closing_p_tags); ++i) {
if (strv_equals(html_closing_p_tags[i], tag)) { if (strv_equals(html_closing_p_tags[i], tag)) {
return true; return true;
} }
@ -3076,6 +3076,7 @@ const char *http_get_method_string(http_method_e method) {
case HTTP_HEAD: return "HEAD"; case HTTP_HEAD: return "HEAD";
case HTTP_PUT: return "PUT"; case HTTP_PUT: return "PUT";
case HTTP_DELETE: return "DELETE"; case HTTP_DELETE: return "DELETE";
default: break;
} }
return "GET"; return "GET";
} }

View file

@ -1,6 +1,10 @@
#ifndef COLLA_HEADER #ifndef COLLA_HEADER
#define COLLA_HEADER #define COLLA_HEADER
#ifdef __linux__
#define _DEFAULT_SOURCE 1
#endif
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <stdbool.h> #include <stdbool.h>
@ -16,7 +20,11 @@
#else #else
#include <uchar.h> #include <uchar.h>
#ifndef thread_local #ifndef thread_local
#ifdef __WIN32
#define thread_local __declspec(thread) #define thread_local __declspec(thread)
#else
#define thread_local _Thread_local
#endif
#endif #endif
#endif #endif

View file

@ -1,8 +1,10 @@
#include "colla.h" #include "colla.h"
#if COLLA_CLANG
#pragma clang diagnostic ignored "-Winitializer-overrides" #pragma clang diagnostic ignored "-Winitializer-overrides"
#pragma clang diagnostic ignored "-Wswitch" #pragma clang diagnostic ignored "-Wswitch"
#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#endif
#include <errno.h> #include <errno.h>
#include <linux/limits.h> #include <linux/limits.h>
@ -87,6 +89,7 @@ struct os_entity_t {
pthread_t handle; pthread_t handle;
thread_func_t *func; thread_func_t *func;
void *userdata; void *userdata;
u64 id;
} thread; } thread;
pthread_mutex_t mtx; pthread_mutex_t mtx;
pthread_cond_t cv; pthread_cond_t cv;
@ -162,6 +165,7 @@ str_t os_get_error_string(iptr error) {
os_wait_t os_wait_on_handles(oshandle_t *handles, int count, bool wait_all, u32 milliseconds) { os_wait_t os_wait_on_handles(oshandle_t *handles, int count, bool wait_all, u32 milliseconds) {
// TODO // TODO
(void)handles; (void)count; (void)wait_all; (void)milliseconds;
return (os_wait_t){0}; return (os_wait_t){0};
} }
@ -266,7 +270,9 @@ void os_file_close(oshandle_t handle) {
if (!os_handle_valid(handle)) return; if (!os_handle_valid(handle)) return;
int fd = fileno((FILE*)handle.data); int fd = fileno((FILE*)handle.data);
int res = fsync(fd); int res = fsync(fd);
warn("res: %d", res); if (res) {
warn("failed to write changes to disk");
}
fclose((FILE*)handle.data); fclose((FILE*)handle.data);
close(fd); close(fd);
} }
@ -364,6 +370,7 @@ bool os_dir_is_valid(dir_t *dir) {
} }
dir_entry_t *os_dir_next(arena_t *arena, dir_t *dir) { dir_entry_t *os_dir_next(arena_t *arena, dir_t *dir) {
(void)arena;
struct dirent *data = readdir(dir->ctx); struct dirent *data = readdir(dir->ctx);
if (!data) { if (!data) {
os_dir_close(dir); os_dir_close(dir);
@ -383,7 +390,7 @@ dir_entry_t *os_dir_next(arena_t *arena, dir_t *dir) {
// == PROCESS =================================== // == PROCESS ===================================
void os_set_env_var(arena_t scratch, strview_t key, strview_t value) { void os_set_env_var(arena_t scratch, strview_t key, strview_t value) {
str_t var = str_fmt(&scratch, "%v=%v"); str_t var = str_fmt(&scratch, "%v=%v", key, value);
putenv(var.buf); putenv(var.buf);
} }
@ -396,16 +403,19 @@ str_t os_get_env_var(arena_t *arena, strview_t key) {
os_env_t *os_get_env(arena_t *arena) { os_env_t *os_get_env(arena_t *arena) {
// TODO // TODO
(void)arena;
return NULL; return NULL;
} }
oshandle_t os_run_cmd_async(arena_t scratch, os_cmd_t *cmd, os_cmd_options_t *options) { oshandle_t os_run_cmd_async(arena_t scratch, os_cmd_t *cmd, os_cmd_options_t *options) {
// TODO // TODO
(void)scratch; (void)cmd; (void)options;
return os_handle_zero(); return os_handle_zero();
} }
bool os_process_wait(oshandle_t proc, uint time, int *out_exit) { bool os_process_wait(oshandle_t proc, uint time, int *out_exit) {
// TODO // TODO
(void)proc; (void)time; (void)out_exit;
return false; return false;
} }
@ -457,12 +467,17 @@ bool os_release(void *ptr, usize size) {
// == THREAD ==================================== // == THREAD ====================================
thread_local i64 os_thread_id = 0;
i64 os_thread_count = 0;
void *os__lin_thread_entry_point(void *ptr) { void *os__lin_thread_entry_point(void *ptr) {
os_entity_t *entity = (os_entity_t *)ptr; os_entity_t *entity = (os_entity_t *)ptr;
colla_assert(entity); colla_assert(entity);
thread_func_t *func = entity->thread.func; thread_func_t *func = entity->thread.func;
void *userdata = entity->thread.userdata; void *userdata = entity->thread.userdata;
int result = func(entity->thread.handle, userdata); u64 id = entity->thread.id;
int result = func(id, userdata);
return (void*)((iptr)result); return (void*)((iptr)result);
} }
@ -471,6 +486,8 @@ oshandle_t os_thread_launch(thread_func_t func, void *userdata) {
entity->thread.func = func; entity->thread.func = func;
entity->thread.userdata = userdata; entity->thread.userdata = userdata;
os_thread_id = atomic_inc_i64(&os_thread_count);
entity->thread.id = os_thread_id;
int result = pthread_create(&entity->thread.handle, NULL, os__lin_thread_entry_point, entity); int result = pthread_create(&entity->thread.handle, NULL, os__lin_thread_entry_point, entity);
@ -596,15 +613,55 @@ void os_cond_broadcast(oshandle_t cond) {
void os_cond_wait(oshandle_t cond, oshandle_t mutex, int milliseconds) { void os_cond_wait(oshandle_t cond, oshandle_t mutex, int milliseconds) {
os_entity_t *cond_entity = os__handle_to_entity(cond, OS_KIND_CONDITION_VARIABLE); os_entity_t *cond_entity = os__handle_to_entity(cond, OS_KIND_CONDITION_VARIABLE);
os_entity_t *mutex_entity = os__handle_to_entity(cond, OS_KIND_MUTEX); os_entity_t *mutex_entity = os__handle_to_entity(mutex, OS_KIND_MUTEX);
if (!cond_entity) return; if (!cond_entity) return;
if (!mutex_entity) return; if (!mutex_entity) return;
// TODO pthread_cond_timedwait;
(void)milliseconds;
pthread_cond_wait(&cond_entity->cv, &mutex_entity->mtx); pthread_cond_wait(&cond_entity->cv, &mutex_entity->mtx);
} }
#endif #endif
#include <linux/atmioc.h>
#if !COLLA_NO_ATOMIC
i64 atomic_set_i64(i64 *dest, i64 val) {
return __atomic_exchange_n(dest, val, __ATOMIC_SEQ_CST);
}
i64 atomic_add_i64(i64 *dest, i64 val) {
return __atomic_fetch_add(dest, val, __ATOMIC_SEQ_CST);
}
i64 atomic_and_i64(i64 *dest, i64 val) {
return __atomic_fetch_and(dest, val, __ATOMIC_SEQ_CST);
}
i64 atomic_cmp_i64(i64 *dest, i64 val, i64 cmp) {
i64 expected = cmp;
__atomic_compare_exchange_n(dest, &expected, val, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
return expected;
}
i64 atomic_inc_i64(i64 *dest) {
return __atomic_add_fetch(dest, 1, __ATOMIC_SEQ_CST);
}
i64 atomic_dec_i64(i64 *dest) {
return __atomic_sub_fetch(dest, 1, __ATOMIC_SEQ_CST);
}
i64 atomic_or_i64(i64 *dest, i64 val) {
return __atomic_fetch_or(dest, val, __ATOMIC_SEQ_CST);
}
i64 atomic_xor_i64(i64 *dest, i64 val) {
return __atomic_fetch_xor(dest, val, __ATOMIC_SEQ_CST);
}
#endif
str_t str_os_from_str16(arena_t *arena, str16_t src) { str_t str_os_from_str16(arena_t *arena, str16_t src) {
mbstate_t state = {0}; mbstate_t state = {0};