From 79f30bb23bef7049a91fc07eef092a6f02dc385a Mon Sep 17 00:00:00 2001 From: snarmph Date: Sun, 30 Aug 2026 12:17:39 +0200 Subject: [PATCH] . --- colla.c | 9 ++++---- colla.h | 12 ++++++++-- colla_lin.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 76 insertions(+), 10 deletions(-) diff --git a/colla.c b/colla.c index cebb9c6..d368db9 100644 --- a/colla.c +++ b/colla.c @@ -518,7 +518,7 @@ char char_lower(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 ================================================= @@ -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) { time_t t = time(NULL); - struct tm log_time = { 0 }; #define gettime(src, dst) localtime_s(&dst, &src) log_event_t ev = { .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); #if COLLA_WIN + struct tm log_time = { 0 }; localtime_s(&log_time, &t); ev.time = &log_time; #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); q->mutex = os_mutex_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); 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) { - 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)) { return true; } @@ -3076,6 +3076,7 @@ const char *http_get_method_string(http_method_e method) { case HTTP_HEAD: return "HEAD"; case HTTP_PUT: return "PUT"; case HTTP_DELETE: return "DELETE"; + default: break; } return "GET"; } diff --git a/colla.h b/colla.h index 434e5f7..cac1a73 100644 --- a/colla.h +++ b/colla.h @@ -1,6 +1,10 @@ #ifndef COLLA_HEADER #define COLLA_HEADER +#ifdef __linux__ + #define _DEFAULT_SOURCE 1 +#endif + #include #include #include @@ -13,10 +17,14 @@ #ifndef thread_local #define thread_local #endif -#else +#else #include #ifndef thread_local - #define thread_local __declspec(thread) + #ifdef __WIN32 + #define thread_local __declspec(thread) + #else + #define thread_local _Thread_local + #endif #endif #endif diff --git a/colla_lin.c b/colla_lin.c index 6803c81..8974fb7 100644 --- a/colla_lin.c +++ b/colla_lin.c @@ -1,8 +1,10 @@ #include "colla.h" +#if COLLA_CLANG #pragma clang diagnostic ignored "-Winitializer-overrides" #pragma clang diagnostic ignored "-Wswitch" #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" +#endif #include #include @@ -87,6 +89,7 @@ struct os_entity_t { pthread_t handle; thread_func_t *func; void *userdata; + u64 id; } thread; pthread_mutex_t mtx; 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) { // TODO + (void)handles; (void)count; (void)wait_all; (void)milliseconds; return (os_wait_t){0}; } @@ -266,7 +270,9 @@ void os_file_close(oshandle_t handle) { if (!os_handle_valid(handle)) return; int fd = fileno((FILE*)handle.data); int res = fsync(fd); - warn("res: %d", res); + if (res) { + warn("failed to write changes to disk"); + } fclose((FILE*)handle.data); 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) { + (void)arena; struct dirent *data = readdir(dir->ctx); if (!data) { os_dir_close(dir); @@ -383,7 +390,7 @@ dir_entry_t *os_dir_next(arena_t *arena, dir_t *dir) { // == PROCESS =================================== 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); } @@ -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) { // TODO + (void)arena; return NULL; } oshandle_t os_run_cmd_async(arena_t scratch, os_cmd_t *cmd, os_cmd_options_t *options) { // TODO + (void)scratch; (void)cmd; (void)options; return os_handle_zero(); } bool os_process_wait(oshandle_t proc, uint time, int *out_exit) { // TODO + (void)proc; (void)time; (void)out_exit; return false; } @@ -457,12 +467,17 @@ bool os_release(void *ptr, usize size) { // == THREAD ==================================== +thread_local i64 os_thread_id = 0; +i64 os_thread_count = 0; + void *os__lin_thread_entry_point(void *ptr) { os_entity_t *entity = (os_entity_t *)ptr; colla_assert(entity); thread_func_t *func = entity->thread.func; 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); } @@ -471,6 +486,8 @@ oshandle_t os_thread_launch(thread_func_t func, void *userdata) { entity->thread.func = func; 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); @@ -596,15 +613,55 @@ void os_cond_broadcast(oshandle_t cond) { 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 *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 (!mutex_entity) return; + // TODO pthread_cond_timedwait; + (void)milliseconds; pthread_cond_wait(&cond_entity->cv, &mutex_entity->mtx); } #endif +#include + +#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) { mbstate_t state = {0};