From 65d1432ba2c628cb9bdca5368ab44fe0fc87127e Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Sun, 14 Jun 2020 22:06:39 +0200 Subject: [PATCH] Merge mthread files --- meson.build | 3 +- meson_options.txt | 7 ++-- src/{mthread => }/mthread.c | 71 +++++++++++++++++++++++++++++++++++ src/mthread/mthread_tst.c | 75 ------------------------------------- 4 files changed, 75 insertions(+), 81 deletions(-) rename src/{mthread => }/mthread.c (85%) delete mode 100644 src/mthread/mthread_tst.c diff --git a/meson.build b/meson.build index 031cc72c..480f732b 100644 --- a/meson.build +++ b/meson.build @@ -48,8 +48,6 @@ common_sources = [ 'src/common.c' core_sources = [ 'src/mkv/write.c' , 'src/mkv/utils.c' , 'src/mkv/mkv.c' - , 'src/mthread/mthread.c' - , 'src/mthread/mthread_tst.c' , 'src/database/stickers.c' , 'src/database/open.c' , 'src/database/queue.c' @@ -62,6 +60,7 @@ core_sources = [ 'src/mkv/write.c' , 'src/net/listen.c' , 'src/net/message.c' , 'src/module/repo.c' + , 'src/mthread.c' , 'src/commands.c' , 'src/config.c' , 'src/uri.c' diff --git a/meson_options.txt b/meson_options.txt index 9791739b..5ac48a6a 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -1,4 +1,3 @@ -option('static_modules', type: 'feature', value: 'enabled' - , description: 'Build modules and link them statically with lektord') -option('module_sdl', type: 'feature', value: 'enabled', description: 'Build the sdl module') -option('static_liblektor', type: 'boolean', value: true, description: 'Link the liblektor statically') +option('static_modules', type: 'feature', value: 'enabled', description: 'Build modules and link them statically with lektord') +option('module_sdl', type: 'feature', value: 'enabled', description: 'Build the sdl module') +option('static_liblektor', type: 'boolean', value: true, description: 'Link the liblektor statically') diff --git a/src/mthread/mthread.c b/src/mthread.c similarity index 85% rename from src/mthread/mthread.c rename to src/mthread.c index a83d2b2a..48506d50 100644 --- a/src/mthread/mthread.c +++ b/src/mthread.c @@ -7,6 +7,77 @@ #define MTHREAD_LWP 1 +#if defined(i686_ARCH) || defined(x86_64_ARCH) + +static inline int +__mthread_test_and_set(mthread_tst_t *atomic) +{ + int ret; + __asm__ __volatile__("lock; xchgl %0, %1":"=r"(ret), "=m"(*atomic):"0"(1), "m"(*atomic):"memory"); + return ret; +} + +#elif defined(sparc_ARCH) +static inline int +__mthread_test_and_set(mthread_tst_t *spinlock) +{ + char ret = 0; + __asm__ __volatile__("ldstub [%0], %1": "=r"(spinlock), "=r"(ret): "0"(spinlock), "1" (ret) : "memory"); + return (unsigned) ret; +} + +#elif defined(ia64_ARCH) +static __inline__ int +__mthread_test_and_set(mthread_tst_t *atomic) +{ + int ret; + __asm__ __volatile__("xchg4 %0=%1, %2":"=r"(ret), "=m"(*atomic):"0"(1), "m"(*atomic):"memory"); + return ret; +} +#else +#define USE_GENERIC_ASM +#include <pthread.h> +static pthread_mutex_t tst_mutex = PTHREAD_MUTEX_INITIALIZER; + +static inline int +__mthread_test_and_set(mthread_tst_t *atomic) +{ + int res; + pthread_mutex_lock(&tst_mutex); + res = *atomic; + if (*atomic == 0) + *atomic = 1; + pthread_mutex_unlock(&tst_mutex); + return res; +} +#endif + +int +mthread_test_and_set(mthread_tst_t *atomic) +{ + return __mthread_test_and_set(atomic); +} + +void +mthread_spinlock_lock(mthread_tst_t *atomic) +{ +#ifdef USE_GENERIC_ASM + static pthread_mutex_t spin_tst_mutex = PTHREAD_MUTEX_INITIALIZER; + pthread_mutex_lock(&spin_tst_mutex); +#endif + while (mthread_test_and_set(atomic)) + sched_yield(); +#ifdef USE_GENERIC_ASM + pthread_mutex_unlock(&spin_tst_mutex); +#endif +} + +void +mthread_spinlock_unlock(mthread_tst_t *atomic) +{ + *atomic = 0; +} + static mthread_virtual_processor_t virtual_processors[MTHREAD_MAX_VIRUTAL_PROCESSORS]; static mthread_list_t joined_list; diff --git a/src/mthread/mthread_tst.c b/src/mthread/mthread_tst.c deleted file mode 100644 index 422eb485..00000000 --- a/src/mthread/mthread_tst.c +++ /dev/null @@ -1,75 +0,0 @@ -#define _POSIX_C_SOURCE 200809L - -#include <mthread/mthread_internal.h> -#include <sched.h> - -#if defined(i686_ARCH) || defined(x86_64_ARCH) - -static inline int -__mthread_test_and_set(mthread_tst_t *atomic) -{ - int ret; - __asm__ __volatile__("lock; xchgl %0, %1":"=r"(ret), "=m"(*atomic):"0"(1), "m"(*atomic):"memory"); - return ret; -} - -#elif defined(sparc_ARCH) -static inline int -__mthread_test_and_set(mthread_tst_t *spinlock) -{ - char ret = 0; - __asm__ __volatile__("ldstub [%0], %1": "=r"(spinlock), "=r"(ret): "0"(spinlock), "1" (ret) : "memory"); - return (unsigned) ret; -} - -#elif defined(ia64_ARCH) -static __inline__ int -__mthread_test_and_set(mthread_tst_t *atomic) -{ - int ret; - __asm__ __volatile__("xchg4 %0=%1, %2":"=r"(ret), "=m"(*atomic):"0"(1), "m"(*atomic):"memory"); - return ret; -} -#else -#define USE_GENERIC_ASM -#include <pthread.h> -static pthread_mutex_t tst_mutex = PTHREAD_MUTEX_INITIALIZER; - -static inline int -__mthread_test_and_set(mthread_tst_t *atomic) -{ - int res; - pthread_mutex_lock(&tst_mutex); - res = *atomic; - if (*atomic == 0) - *atomic = 1; - pthread_mutex_unlock(&tst_mutex); - return res; -} -#endif - -int -mthread_test_and_set(mthread_tst_t *atomic) -{ - return __mthread_test_and_set(atomic); -} - -void -mthread_spinlock_lock(mthread_tst_t *atomic) -{ -#ifdef USE_GENERIC_ASM - static pthread_mutex_t spin_tst_mutex = PTHREAD_MUTEX_INITIALIZER; - pthread_mutex_lock(&spin_tst_mutex); -#endif - while (mthread_test_and_set(atomic)) - sched_yield(); -#ifdef USE_GENERIC_ASM - pthread_mutex_unlock(&spin_tst_mutex); -#endif -} - -void -mthread_spinlock_unlock(mthread_tst_t *atomic) -{ - *atomic = 0; -} -- GitLab