From b025cf269d2e07e179be4a0e34d936862dc640c2 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 2 Jan 2025 20:29:55 -0500 Subject: Use `holding` blocks for mutexed data instead of lambdas --- stdlib/arrays.h | 4 ++-- stdlib/datatypes.h | 5 +++++ stdlib/pointers.c | 6 ++---- stdlib/stdlib.c | 30 ------------------------------ stdlib/stdlib.h | 2 -- stdlib/threads.c | 4 ++-- stdlib/tomo.h | 1 + stdlib/types.h | 4 ++-- 8 files changed, 14 insertions(+), 42 deletions(-) (limited to 'stdlib') diff --git a/stdlib/arrays.h b/stdlib/arrays.h index cf1f088b..53681dc7 100644 --- a/stdlib/arrays.h +++ b/stdlib/arrays.h @@ -59,12 +59,12 @@ #define ARRAY_DECREF(arr) (arr).data_refcount -= ((arr).data_refcount < ARRAY_MAX_DATA_REFCOUNT) #define ARRAY_COPY(arr) ({ ARRAY_INCREF(arr); arr; }) -#define Array$insert_value(arr, item_expr, index, padded_item_size) ({ __typeof(item_expr) item = item_expr; Array$insert(arr, &item, index, padded_item_size); }) +#define Array$insert_value(arr, item_expr, index, padded_item_size) Array$insert(arr, (__typeof(item_expr)[1]){item_expr}, index, padded_item_size) void Array$insert(Array_t *arr, const void *item, Int_t index, int64_t padded_item_size); void Array$insert_all(Array_t *arr, Array_t to_insert, Int_t index, int64_t padded_item_size); void Array$remove_at(Array_t *arr, Int_t index, Int_t count, int64_t padded_item_size); void Array$remove_item(Array_t *arr, void *item, Int_t max_removals, const TypeInfo_t *type); -#define Array$remove_item_value(arr, item_expr, max, type) ({ __typeof(item_expr) item = item_expr; Array$remove_item(arr, &item, max, type); }) +#define Array$remove_item_value(arr, item_expr, max, type) Array$remove_item(arr, (__typeof(item_expr)[1]){item_expr}, max, type) #define Array$pop(arr_expr, index_expr, item_type, nonnone_var, nonnone_expr, none_expr, padded_item_size) ({ \ Array_t *arr = arr_expr; \ diff --git a/stdlib/datatypes.h b/stdlib/datatypes.h index 9c3cabc2..11ae130e 100644 --- a/stdlib/datatypes.h +++ b/stdlib/datatypes.h @@ -89,4 +89,9 @@ typedef struct timeval Moment_t; typedef struct RNGState_t* RNG_t; +typedef struct MutexedData_s { + pthread_mutex_t mutex; + void *data; +} *MutexedData_t; + // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/stdlib/pointers.c b/stdlib/pointers.c index 40348965..76e882ec 100644 --- a/stdlib/pointers.c +++ b/stdlib/pointers.c @@ -18,12 +18,10 @@ public Text_t Pointer$as_text(const void *x, bool colorize, const TypeInfo_t *ty auto ptr_info = type->PointerInfo; if (!x) { Text_t typename = generic_as_text(NULL, false, ptr_info.pointed); - Text_t text; if (colorize) - text = Text$concat(Text("\x1b[34;1m"), Text$from_str(ptr_info.sigil), typename, Text("\x1b[m")); + return Text$concat(Text("\x1b[34;1m"), Text$from_str(ptr_info.sigil), typename, Text("\x1b[m")); else - text = Text$concat(Text$from_str(ptr_info.sigil), typename); - return text; + return Text$concat(Text$from_str(ptr_info.sigil), typename); } const void *ptr = *(const void**)x; if (!ptr) { diff --git a/stdlib/stdlib.c b/stdlib/stdlib.c index 0266e788..cb9d2213 100644 --- a/stdlib/stdlib.c +++ b/stdlib/stdlib.c @@ -621,34 +621,4 @@ public void sleep_num(double seconds) nanosleep(&ts, NULL); } -static void use_mutexed(Closure_t fn, void *userdata) -{ - void (*call)(void*, void*) = fn.fn; - struct data_t { - pthread_mutex_t mutex; - char item[0] __attribute__ ((aligned (8))); - }; - pthread_mutex_t *mutex = &((struct data_t*)userdata)->mutex; - pthread_mutex_lock(mutex); - void *mutexed_item = (void*)((struct data_t*)userdata)->item; - call(mutexed_item, fn.userdata); - pthread_mutex_unlock(mutex); -} - -public Closure_t _mutexed(const void *item, size_t size) -{ - struct data_t { - pthread_mutex_t mutex; - char item[size] __attribute__ ((aligned (8))); - }; - struct data_t *userdata = GC_MALLOC(sizeof(struct data_t)); - - pthread_mutex_init(&userdata->mutex, NULL); - memcpy(userdata->item, item, size); - return (Closure_t){ - .fn=(void*)use_mutexed, - .userdata=(void*)userdata, - }; -} - // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/stdlib/stdlib.h b/stdlib/stdlib.h index c68442a6..7c9bd7e1 100644 --- a/stdlib/stdlib.h +++ b/stdlib/stdlib.h @@ -42,7 +42,5 @@ Closure_t spawn(Closure_t fn); bool pop_flag(char **argv, int *i, const char *flag, Text_t *result); void print_stack_trace(FILE *out, int start, int stop); void sleep_num(double seconds); -public Closure_t _mutexed(const void *item, size_t size); -#define mutexed(item) _mutexed((__typeof(item)[1]){item}, sizeof(item)) // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/stdlib/threads.c b/stdlib/threads.c index c5690111..efd23546 100644 --- a/stdlib/threads.c +++ b/stdlib/threads.c @@ -34,8 +34,8 @@ static void *run_thread(Closure_t *closure) public Thread_t Thread$new(Closure_t fn) { Thread_t thread = new(pthread_t); - Closure_t *doop = new(Closure_t, .fn=fn.fn, .userdata=fn.userdata); - pthread_create(thread, NULL, (void*)run_thread, doop); + Closure_t *closure = new(Closure_t, .fn=fn.fn, .userdata=fn.userdata); + pthread_create(thread, NULL, (void*)run_thread, closure); return thread; } diff --git a/stdlib/tomo.h b/stdlib/tomo.h index 10ac3668..7ed25412 100644 --- a/stdlib/tomo.h +++ b/stdlib/tomo.h @@ -18,6 +18,7 @@ #include "memory.h" #include "metamethods.h" #include "moments.h" +#include "mutexeddata.h" #include "nums.h" #include "optionals.h" #include "paths.h" diff --git a/stdlib/types.h b/stdlib/types.h index 36508393..2c9abcfc 100644 --- a/stdlib/types.h +++ b/stdlib/types.h @@ -30,7 +30,7 @@ struct TypeInfo_s { metamethods_t metamethods; struct { // Anonymous tagged union for convenience enum { OpaqueInfo, StructInfo, EnumInfo, PointerInfo, TextInfo, ArrayInfo, TableInfo, FunctionInfo, - OptionalInfo, TypeInfoInfo } tag; + OptionalInfo, MutexedDataInfo, TypeInfoInfo } tag; union { struct {} OpaqueInfo; struct { @@ -54,7 +54,7 @@ struct TypeInfo_s { } TypeInfoInfo; struct { const TypeInfo_t *type; - } OptionalInfo; + } OptionalInfo, MutexedDataInfo; struct { const char *name; int num_tags; -- cgit v1.2.3