diff --git a/meson.build b/meson.build index 031cc72cdf0bae09a5f8188e7bf4f772a0293860..480f732ba20fc552b9673890c0d80b4a51991040 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 9791739bdcb0f6034c5d537074d5d3adb1d3e768..5ac48a6a141ddd789bd84ff77a2308f8a5c7a862 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 a83d2b2a399c1e289848f11c7552201797c0c1ab..48506d509401d38c1f9f90b258e4838aaba44f72 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 422eb485afe966b0e3aa996f797bdb03da6dcf9b..0000000000000000000000000000000000000000 --- 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; -}