scratch arena thread safety

This commit is contained in:
kamkow1
2025-06-28 01:29:22 +02:00
parent 5e6291cdc6
commit db8389f7d5

46
gebs.h
View File

@ -90,6 +90,10 @@ int main(int argc, char ** argv)
#include <string.h> #include <string.h>
#include <limits.h> #include <limits.h>
#ifdef GEBS_ENABLE_PTHREAD_FEATURES
# include <pthread.h>
#endif
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
@ -440,7 +444,14 @@ void gebs_rebuild_self1_alloc(Gebs_Allocator *alloc, int argc, char ** argv,
// Scratch arena // Scratch arena
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
extern Gebs_Arena gebs_scratch_arena; typedef struct {
Gebs_Arena value;
#ifdef GEBS_ENABLE_PTHREAD_FEATURES
pthread_mutex_t lock;
#endif
} Gebs_Scratch_Arena;
extern Gebs_Scratch_Arena gebs_scratch_arena;
void gebs_scratch_arena_reset(void); void gebs_scratch_arena_reset(void);
char *gebs_fmt(const char *fmt, ...); char *gebs_fmt(const char *fmt, ...);
@ -816,22 +827,25 @@ void gebs_arena_destroy(Gebs_Arena *a)
// Scratch arena // Scratch arena
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
Gebs_Arena gebs_scratch_arena; Gebs_Scratch_Arena gebs_scratch_arena;
__attribute__((constructor)) __attribute__((constructor))
void gebs_scratch_areana_init(void) void gebs_scratch_areana_init(void)
{ {
gebs_scratch_arena = gebs_arena_get(); gebs_scratch_arena.value = gebs_arena_get();
} }
__attribute__((destructor)) __attribute__((destructor))
void gebs_scratch_areana_free(void) void gebs_scratch_areana_free(void)
{ {
gebs_arena_destroy(&gebs_scratch_arena); gebs_arena_destroy(&gebs_scratch_arena.value);
} }
char *gebs_fmt(const char *fmt, ...) char *gebs_fmt(const char *fmt, ...)
{ {
#ifdef GEBS_ENABLE_PTHREAD_FEATURES
pthread_mutex_lock(&gebs_scratch_arena.lock);
#endif
va_list list; va_list list;
va_start(list, fmt); va_start(list, fmt);
@ -840,23 +854,41 @@ char *gebs_fmt(const char *fmt, ...)
size_t size = vsnprintf(nil, 0, fmt, list_copy); size_t size = vsnprintf(nil, 0, fmt, list_copy);
va_end(list_copy); va_end(list_copy);
char *buf = gebs_malloc(&gebs_scratch_arena, size+1); char *buf = gebs_malloc(&gebs_scratch_arena.value, size+1);
vsprintf(buf, fmt, list); vsprintf(buf, fmt, list);
va_end(list); va_end(list);
#ifdef GEBS_ENABLE_PTHREAD_FEATURES
pthread_mutex_unlock(&gebs_scratch_arena.lock);
#endif
return buf; return buf;
} }
void gebs_scratch_arena_reset(void) void gebs_scratch_arena_reset(void)
{ {
gebs_arena_reset(&gebs_scratch_arena); #ifdef GEBS_ENABLE_PTHREAD_FEATURES
pthread_mutex_lock(&gebs_scratch_arena.lock);
#endif
gebs_arena_reset(&gebs_scratch_arena.value);
#ifdef GEBS_ENABLE_PTHREAD_FEATURES
pthread_mutex_unlock(&gebs_scratch_arena.lock);
#endif
} }
char *gebs_cwd(void) char *gebs_cwd(void)
{ {
char *cwd = gebs_malloc(&gebs_scratch_arena, PATH_MAX); #ifdef GEBS_ENABLE_PTHREAD_FEATURES
pthread_mutex_lock(&gebs_scratch_arena.lock);
#endif
char *cwd = gebs_malloc(&gebs_scratch_arena.value, PATH_MAX);
if (getcwd(cwd, PATH_MAX) == nil) { if (getcwd(cwd, PATH_MAX) == nil) {
#ifdef GEBS_ENABLE_PTHREAD_FEATURES
pthread_mutex_unlock(&gebs_scratch_arena.lock);
#endif
return nil; return nil;
} }
#ifdef GEBS_ENABLE_PTHREAD_FEATURES
pthread_mutex_unlock(&gebs_scratch_arena.lock);
#endif
return cwd; return cwd;
} }