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); 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" diff --git a/common/thread_id.h b/common/thread_id.h new file mode 100644 index 00000000..4d63bdb3 --- /dev/null +++ b/common/thread_id.h @@ -0,0 +1,18 @@ +#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(); + return (uint32_t)(p ^ (p >> 32)); + } +#endif 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++) { 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");