From 4b42966ff15276496cb774b9bf57aca6090958b8 Mon Sep 17 00:00:00 2001 From: Christian Brendlin Date: Sat, 23 Aug 2025 18:29:05 +0200 Subject: [PATCH 1/6] fix: add thread_id.h for portable thread IDs (musl/glibc/windows) --- common/thread_id.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 common/thread_id.h diff --git a/common/thread_id.h b/common/thread_id.h new file mode 100644 index 00000000..8cecc560 --- /dev/null +++ b/common/thread_id.h @@ -0,0 +1,19 @@ +#pragma once + +#include + +#if defined(_WIN32) + #include + static inline uint32_t thread_id32(void) { + return (uint32_t)GetCurrentThreadId(); + } +#else + #include + #include + + static inline uint32_t thread_id32(void) { + uintptr_t p = (uintptr_t)pthread_self(); + // Mix high/low bits; good enough for IDs/logging. + return (uint32_t)(p ^ (p >> 32)); + } +#endif From 03de308474c8d5917f4314882df220915ac8232b Mon Sep 17 00:00:00 2001 From: Christian Brendlin Date: Sat, 23 Aug 2025 18:32:38 +0200 Subject: [PATCH 2/6] fix: remove static on tb__linker_is_library_new to fix compilation error on musl --- linker/linker.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linker/linker.c b/linker/linker.c index 9cee653e..49f73eb5 100644 --- a/linker/linker.c +++ b/linker/linker.c @@ -228,7 +228,7 @@ void tb_linker_append_module(TB_Linker* l, TB_Module* m) { #endif } -static bool tb__linker_is_library_new(TB_Linker* l, const char* file_name) { +bool tb__linker_is_library_new(TB_Linker* l, const char* file_name) { if (!linker_thread_init) { linker_thread_init = true; tb_arena_create(&linker_perm_arena, "LinkerPerm"); From b34447a788fddb019a1b2cf97eb2054dc3696e90 Mon Sep 17 00:00:00 2001 From: Christian Brendlin Date: Sat, 23 Aug 2025 18:34:02 +0200 Subject: [PATCH 3/6] replace (failing on musl) pthread_self() with portable added thread_id32() --- common/perf.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/common/perf.c b/common/perf.c index fd600643..e6d0de74 100644 --- a/common/perf.c +++ b/common/perf.c @@ -15,6 +15,8 @@ #include "spall.h" #endif +#include "thread_id.h" + #if defined(_AMD64_) || defined(__amd64__) static double rdtsc_freq; static uint64_t timer_start; @@ -165,7 +167,7 @@ void cuikperf_thread_start(void) { #if _WIN32 uint32_t tid = GetCurrentThreadId(); #else - uint32_t tid = pthread_self(); + uint32_t tid = thread_id32(); #endif if (profiling) { @@ -201,7 +203,7 @@ void cuikperf_region_start(const char* label, const char* extra) { #if _WIN32 uint32_t tid = GetCurrentThreadId(); #else - uint32_t tid = pthread_self(); + uint32_t tid = thread_id32(); #endif spall_buffer_begin_args(&ctx, &muh_buffer, label, strlen(label), extra, extra ? strlen(extra) : 0, nanos, tid, 0); @@ -217,7 +219,7 @@ void cuikperf_region_start2(const char* label, size_t extra_len, const char* ext #if _WIN32 uint32_t tid = GetCurrentThreadId(); #else - uint32_t tid = pthread_self(); + uint32_t tid = thread_id32(); #endif spall_buffer_begin_args(&ctx, &muh_buffer, label, strlen(label), extra, extra_len, nanos, tid, 0); @@ -233,7 +235,7 @@ void cuikperf_region_end(void) { #if _WIN32 uint32_t tid = GetCurrentThreadId(); #else - uint32_t tid = pthread_self(); + uint32_t tid = thread_id32(); #endif spall_buffer_end_ex(&ctx, &muh_buffer, nanos, tid, 0); From 6de279757b95f46f02954314a6e3b4a4f2b053ca Mon Sep 17 00:00:00 2001 From: Christian Brendlin Date: Sat, 23 Aug 2025 18:34:24 +0200 Subject: [PATCH 4/6] cleanup --- common/thread_id.h | 1 - 1 file changed, 1 deletion(-) diff --git a/common/thread_id.h b/common/thread_id.h index 8cecc560..4d63bdb3 100644 --- a/common/thread_id.h +++ b/common/thread_id.h @@ -13,7 +13,6 @@ static inline uint32_t thread_id32(void) { uintptr_t p = (uintptr_t)pthread_self(); - // Mix high/low bits; good enough for IDs/logging. return (uint32_t)(p ^ (p >> 32)); } #endif From 175874e3514401860a810a061e067a8f96d2632c Mon Sep 17 00:00:00 2001 From: Christian Brendlin Date: Sat, 23 Aug 2025 18:35:22 +0200 Subject: [PATCH 5/6] add pthread.h import to fix crash on musl systems --- common/pool.c | 1 + 1 file changed, 1 insertion(+) diff --git a/common/pool.c b/common/pool.c index acc9bd9f..6ec2d398 100644 --- a/common/pool.c +++ b/common/pool.c @@ -23,6 +23,7 @@ // https://github.com/colrdavidson/workpool/blob/main/pool.h #include "pool.h" #include +#include #ifdef ENABLE_TRACING #include "spall_native_auto.h" From 0a2fa271597fab6e22f4d6a93f8b8f1df6ca81ce Mon Sep 17 00:00:00 2001 From: Christian Brendlin Date: Sat, 23 Aug 2025 18:36:02 +0200 Subject: [PATCH 6/6] replace __debugbreak() with DEBUGBREAK() macro --- cuik_c/driver/driver_arg_parse.h | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/cuik_c/driver/driver_arg_parse.h b/cuik_c/driver/driver_arg_parse.h index 4a72fde5..c5b0ef92 100644 --- a/cuik_c/driver/driver_arg_parse.h +++ b/cuik_c/driver/driver_arg_parse.h @@ -12,6 +12,33 @@ static once_flag arg_global_init = ONCE_FLAG_INIT; static bool arg_global_init; #endif +#ifndef DEBUGBREAK + #ifdef _WIN32 + #if defined(_MSC_VER) || defined(__clang__) + #include + #define DEBUGBREAK() __debugbreak() + #else + #include + #define DEBUGBREAK() DebugBreak() + #endif + #else + #if defined(__has_builtin) + #if __has_builtin(__builtin_debugtrap) + #define DEBUGBREAK() __builtin_debugtrap() + #elif __has_builtin(__builtin_trap) + #define DEBUGBREAK() __builtin_trap() + #else + #include + #define DEBUGBREAK() raise(SIGTRAP) + #endif + #else + #include + #define DEBUGBREAK() raise(SIGTRAP) + #endif + #endif +#endif + + typedef struct { const char* key; void* val; @@ -243,7 +270,7 @@ static void print_help(void) { for (int j = len; j < round_up; j++) printf(" "); printf("%s\n", options[i].desc); } - __debugbreak(); + DEBUGBREAK(); /*size_t split = 24; for (int i = 1; i < ARG_DESC_COUNT; i++) {