From ec3bf6061d371c4fba90b0a4d60e1911d449e99e Mon Sep 17 00:00:00 2001 From: Slendi Date: Sat, 9 Aug 2025 08:39:55 +0300 Subject: [PATCH] Remove pthreads Signed-off-by: Slendi --- CMakeLists.txt | 11 ------- build.sh | 10 ------ src/dcfg.c | 86 +++++++++++--------------------------------------- 3 files changed, 18 insertions(+), 89 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d9b73da..70f28a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,6 @@ cmake_minimum_required(VERSION 3.10) project(DCFG C) -option(DCFG_PTHREAD_SUPPORT "Enable pthreads support" ON) option(DCFG_POSIX_SUPPORT "Enable POSIX support" ON) option(DCFG_BUILD_PROGRAMS "Build DCFG example programs" ON) option(DCFG_ASAN "Enable AddressSanitizer" OFF) @@ -37,12 +36,6 @@ add_library(${PROJECT_NAME}_static STATIC ${SRC_DIR}/dcfg.c) set_target_properties(${PROJECT_NAME}_static PROPERTIES OUTPUT_NAME "dcfg") foreach(TARGET ${PROJECT_NAME}_shared ${PROJECT_NAME}_static) - if(DCFG_PTHREAD_SUPPORT) - find_package(Threads REQUIRED) - target_link_libraries(${TARGET} PRIVATE Threads::Threads) - target_compile_definitions(${TARGET} PRIVATE DCFG_PTHREAD_SUPPORT) - endif() - if(DCFG_POSIX_SUPPORT) target_compile_definitions(${TARGET} PRIVATE DCFG_POSIX_SUPPORT) endif() @@ -77,10 +70,6 @@ if(DCFG_BUILD_PROGRAMS) target_include_directories(${PROG_NAME} PRIVATE ${INCLUDE_DIR}) target_link_libraries(${PROG_NAME} PRIVATE ${PROJECT_NAME}_static) - if(DCFG_PTHREAD_SUPPORT) - target_link_libraries(${PROG_NAME} PRIVATE Threads::Threads) - endif() - if(MSVC) target_compile_options(${PROG_NAME} PRIVATE /W4 /permissive-) else() diff --git a/build.sh b/build.sh index 5bba400..b9ec060 100755 --- a/build.sh +++ b/build.sh @@ -6,7 +6,6 @@ BUILD_DIR="BUILD" INSTALL_DIR=$PWD/install CFLAGS="-Wall -Wextra -pedantic -Werror -Wno-newline-eof -Wno-language-extension-token -Wno-unused-command-line-argument" BUILD_SHARED=1 -PTHREAD_SUPPORT=1 POSIX_SUPPORT=1 BUILD_PROGRAMS=1 SRC_DIR="src" @@ -18,7 +17,6 @@ for arg in "$@"; do case $arg in --debug) CFLAGS="$CFLAGS -g -O0" ;; --release) CFLAGS="$CFLAGS -O2" ;; - --no-pthread) PTHREAD_SUPPORT=0 ;; --no-posix) POSIX_SUPPORT=0 ;; --no-programs) BUILD_PROGRAMS=0 ;; --clean) rm -rf "$BUILD_DIR" "$INSTALL_DIR"; echo "Cleaned."; exit 0 ;; @@ -48,14 +46,6 @@ else CFLAGS="$CFLAGS -std=c99" fi -if [ "$PTHREAD_SUPPORT" -eq 1 ]; then - CFLAGS="$CFLAGS -DDCFG_PTHREAD_SUPPORT=1" - if [ "$OS" = "SunOS" ]; then - LIBS="$LIBS -mt" - else - LIBS="$LIBS -lpthread" - fi -fi if [ "$POSIX_SUPPORT" -eq 1 ]; then CFLAGS="$CFLAGS -DDCFG_POSIX_SUPPORT=1" fi diff --git a/src/dcfg.c b/src/dcfg.c index b38929c..ecd1464 100644 --- a/src/dcfg.c +++ b/src/dcfg.c @@ -42,33 +42,6 @@ # define _POSIX_C_SOURCE 0L #endif -#if DCFG_PTHREAD_SUPPORT -# ifdef _POSIX_C_SOURCE -# undef _POSIX_C_SOURCE -# endif -# define _POSIX_C_SOURCE 200809L -# include -extern int pthread_mutexattr_settype(pthread_mutexattr_t *, int); -#else -# if defined __USE_POSIX199506 || defined __USE_UNIX98 -# else -typedef struct { - int unused; -} pthread_mutex_t; -typedef struct { - int unused; -} pthread_mutexattr_t; -# define PTHREAD_MUTEX_RECURSIVE_NP 0 -# endif - -static void pthread_mutex_init(pthread_mutex_t *m, void *data) { (void)m; (void)data; } -static void pthread_mutex_destroy(pthread_mutex_t *m) { (void)m; } -static void pthread_mutex_lock(pthread_mutex_t *m) { (void)m; } -static void pthread_mutex_unlock(pthread_mutex_t *m) { (void)m; } -void pthread_mutexattr_init(pthread_mutexattr_t *attr); -static void pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) { (void)attr; (void)type; } -#endif - #include #include #include @@ -79,7 +52,7 @@ static void pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) { (vo #include #include -int64_t dcfg_strtoll(const char *s, char **end, int base) +int64_t dcfg_strtoll(char const *s, char **end, int base) { char const *p = s; while (isspace((unsigned char)*p)) @@ -360,8 +333,6 @@ static void environment_destroy(Environment *env, bool destroy_values) } struct dcfg_Instance { - pthread_mutex_t mtx; - dcfg_AllocFn alloc; dcfg_FreeFn free; dcfg_RealpathFn realpath; @@ -382,10 +353,21 @@ struct dcfg_Instance { static void *alloc(size_t size) { return calloc(1, size); } +#ifdef __linux__ +# include +#else +# ifndef PATH_MAX +# define PATH_MAX 4096 +# endif +#endif + #if DCFG_POSIX_SUPPORT -static char *realpath_(char const *s) { +static char *realpath_(char const *s) +{ char buf[PATH_MAX]; - return strdup(realpath(s, buf)); + char *ss = malloc(strlen(realpath(s, buf)) + 1); + strcpy(ss, s); + return ss; } void *fopen_(char const *f, char const *a) { return fopen(f, a); } int fseek_(void *f, size_t p, int o) { return fseek(f, p, o); } @@ -399,12 +381,6 @@ dcfg_Version dcfg_get_version(void) | ((uint64_t)VERSION_PATCH & 0xFFFFFFFFULL); } -#if __FreeBSD__ -#define MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE -#else -#define MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE_NP -#endif - dcfg_Instance *dcfg_make_instance(dcfg_InstanceCreateInfo const *create_info) { assert(create_info); @@ -414,11 +390,6 @@ dcfg_Instance *dcfg_make_instance(dcfg_InstanceCreateInfo const *create_info) return NULL; } - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); - pthread_mutexattr_settype(&attr, MUTEX_RECURSIVE); - pthread_mutex_init(&instance->mtx, &attr); - instance->alloc = create_info->alloc; instance->free = create_info->free; if (!instance->alloc) { @@ -482,13 +453,6 @@ void dcfg_destroy_instance(dcfg_Instance *instance) } vector_free(instance->sourcev); - pthread_mutex_lock(&instance->mtx); - { // De-init other instance things - } - pthread_mutex_unlock(&instance->mtx); - - pthread_mutex_destroy(&instance->mtx); - free(instance); } @@ -498,15 +462,10 @@ char const *dcfg_last_error(dcfg_Instance *instance) char const *ret = NULL; - pthread_mutex_lock(&instance->mtx); - { - if (!instance->last_error[0]) { - pthread_mutex_unlock(&instance->mtx); - return NULL; - } - ret = instance->last_error; + if (!instance->last_error[0]) { + return NULL; } - pthread_mutex_unlock(&instance->mtx); + ret = instance->last_error; return ret; } @@ -1997,16 +1956,13 @@ bool dcfg_Value_evaluate_in_env( bool dcfg_call_function( dcfg_Value *fn, dcfg_Value **args, size_t argc, dcfg_Value **out_value) { - pthread_mutex_lock(&fn->instance->mtx); if (fn->v.f.is_builtin) { *out_value = fn->v.f.v.bi(args, argc); - pthread_mutex_unlock(&fn->instance->mtx); return *out_value != NULL; } Environment frame; if (!environment_create(&frame, fn->v.f.v.f.closure)) { - pthread_mutex_unlock(&fn->instance->mtx); return false; } @@ -2014,7 +1970,6 @@ bool dcfg_call_function( if (argc != nform) { strcpy(fn->instance->last_error, "Invalid argument count"); environment_destroy(&frame, false); - pthread_mutex_unlock(&fn->instance->mtx); return false; } for (size_t i = 0; i < nform; i++) { @@ -2024,18 +1979,13 @@ bool dcfg_call_function( bool ok = dcfg_Value_evaluate_in_env(fn->v.f.v.f.body, &frame, out_value); environment_destroy(&frame, false); - pthread_mutex_unlock(&fn->instance->mtx); return ok; } bool dcfg_Value_evaluate(dcfg_Value *value, dcfg_Value **out_value) { bool ret; - pthread_mutex_lock(&value->instance->mtx); - { - ret = dcfg_Value_evaluate_in_env(value, NULL, out_value); - } - pthread_mutex_unlock(&value->instance->mtx); + ret = dcfg_Value_evaluate_in_env(value, NULL, out_value); return ret; }