diff options
author | EuAndreh <eu@euandre.org> | 2025-01-16 18:41:39 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2025-01-16 18:41:39 -0300 |
commit | c341b2bdb0259ea00c08a33a0e1d71d678039725 (patch) | |
tree | acae3853f8a2bacf2de53783ca1d59faa19c602d | |
parent | Makefile: keep symlinks in "install" target (diff) | |
download | pindaiba-c341b2bdb0259ea00c08a33a0e1d71d678039725.tar.gz pindaiba-c341b2bdb0259ea00c08a33a0e1d71d678039725.tar.xz |
src/trace.c: Replace thread_local with pthreads
-rw-r--r-- | src/trace.c | 41 | ||||
-rw-r--r-- | src/trace.h | 4 | ||||
-rw-r--r-- | tests/trace.c | 12 |
3 files changed, 39 insertions, 18 deletions
diff --git a/src/trace.c b/src/trace.c index 335ee43..d21a792 100644 --- a/src/trace.c +++ b/src/trace.c @@ -2,25 +2,36 @@ #include <assert.h> #include <errno.h> +#include <pthread.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <threads.h> #include "logerr.h" #include "trace.h" -static thread_local enum TraceLevel -LEVEL = TraceLevel_INFO; +static pthread_key_t +LEVEL_KEY; -static thread_local FILE * -STREAM = NULL; +static pthread_key_t +STREAM_KEY; +static pthread_once_t +ONCE = PTHREAD_ONCE_INIT; + +static void +init(void) { + assert(pthread_key_create(&LEVEL_KEY, NULL) == 0); + assert(pthread_key_create(&STREAM_KEY, NULL) == 0); + assert(pthread_setspecific(LEVEL_KEY, (void *)TraceLevel_INFO) == 0); + assert(pthread_setspecific(LEVEL_KEY, stderr) == 0); +} + void vftracef( const char *const file, @@ -31,7 +42,10 @@ vftracef( const char *restrict format, va_list args ) { - if (level > LEVEL) { + assert(pthread_once(&ONCE, init) == 0); + + const enum TraceLevel current_level = (enum TraceLevel)pthread_getspecific(LEVEL_KEY); + if (level > current_level) { return; } @@ -73,23 +87,22 @@ tracef( const char *restrict format, ... ) { - if (STREAM == NULL) { - STREAM = stderr; - } + assert(pthread_once(&ONCE, init) == 0); + va_list args; va_start(args, format); - vftracef(file, function, lineno, STREAM, level, format, args); + vftracef(file, function, lineno, pthread_getspecific(STREAM_KEY), level, format, args); va_end(args); } -void +int trace_set_stream(FILE *stream) { - STREAM = stream; + return pthread_setspecific(STREAM_KEY, stream); } -void +int trace_set_level(const enum TraceLevel level) { assert(level >= TraceLevel_NONE); assert(level <= TraceLevel_DEBUG); - LEVEL = level; + return pthread_setspecific(LEVEL_KEY, (void *)level); } diff --git a/src/trace.h b/src/trace.h index b81e616..be3f6ee 100644 --- a/src/trace.h +++ b/src/trace.h @@ -48,8 +48,8 @@ tracef( ... ); -void +int trace_set_stream(FILE *stream); -void +int trace_set_level(const enum TraceLevel level); diff --git a/tests/trace.c b/tests/trace.c index aa10c81..5b59a46 100644 --- a/tests/trace.c +++ b/tests/trace.c @@ -19,7 +19,7 @@ test_ftracef(void) { FILE *file = NULL; char *str = NULL; - const enum TraceLevel orig = LEVEL; + const enum TraceLevel orig = (enum TraceLevel)pthread_getspecific(LEVEL_KEY); trace_set_level(TraceLevel_INFO); { @@ -191,7 +191,15 @@ int main(void) { int rc = EXIT_FAILURE; - LEVEL = TraceLevel_DEBUG; + if (pthread_once(&ONCE, init)) { + perror("pthread_once()"); + goto out; + } + + if (trace_set_level(TraceLevel_DEBUG)) { + perror("trace_set_level()"); + goto out; + } if (test_ftracef()) { perror("test_ftracef()"); |