diff options
Diffstat (limited to 'source/kit')
-rw-r--r-- | source/kit/CMakeLists.txt | 11 | ||||
-rw-r--r-- | source/kit/c11/CMakeLists.txt | 6 | ||||
-rw-r--r-- | source/kit/c11/impl/CMakeLists.txt | 7 | ||||
-rw-r--r-- | source/kit/c11/impl/threads_win32.h | 21 | ||||
-rw-r--r-- | source/kit/mersenne_twister_64.c | 2 | ||||
-rw-r--r-- | source/kit/threads.h (renamed from source/kit/c11/threads.h) | 119 | ||||
-rw-r--r-- | source/kit/threads.posix.c (renamed from source/kit/c11/impl/threads_posix.c) | 38 | ||||
-rw-r--r-- | source/kit/threads.win32.c (renamed from source/kit/c11/impl/threads_win32.c) | 60 | ||||
-rw-r--r-- | source/kit/threads.win32.h | 25 | ||||
-rw-r--r-- | source/kit/threads_tls_callback.win32.cpp (renamed from source/kit/c11/impl/threads_win32_tls_callback.cpp) | 7 | ||||
-rw-r--r-- | source/kit/time.c (renamed from source/kit/c11/impl/time.c) | 2 | ||||
-rw-r--r-- | source/kit/time.h (renamed from source/kit/c11/time.h) | 6 |
12 files changed, 154 insertions, 150 deletions
diff --git a/source/kit/CMakeLists.txt b/source/kit/CMakeLists.txt index 397b404..86c39dc 100644 --- a/source/kit/CMakeLists.txt +++ b/source/kit/CMakeLists.txt @@ -1,18 +1,21 @@ target_sources( ${KIT_LIBRARY} PRIVATE - atomic.c input_buffer.c input_stream.c lower_bound.c - string_ref.c async_function.c allocator.c array_ref.c dynamic_array.c - mersenne_twister_64.c + atomic.c input_buffer.c threads.win32.c time.c + threads.posix.c input_stream.c lower_bound.c string_ref.c + async_function.c allocator.c array_ref.c dynamic_array.c + mersenne_twister_64.c threads_tls_callback.win32.cpp PUBLIC + $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/time.h> $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/atomic.h> $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/allocator.h> $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/string_ref.h> + $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/threads.h> $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/dynamic_array.h> $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/async_function.h> + $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/threads.win32.h> $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/input_stream.h> $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/input_buffer.h> $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/lower_bound.h> $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/array_ref.h> $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/mersenne_twister_64.h>) -add_subdirectory(c11) diff --git a/source/kit/c11/CMakeLists.txt b/source/kit/c11/CMakeLists.txt deleted file mode 100644 index 205b256..0000000 --- a/source/kit/c11/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -target_sources( - ${KIT_LIBRARY} - PUBLIC - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/time.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/threads.h>) -add_subdirectory(impl) diff --git a/source/kit/c11/impl/CMakeLists.txt b/source/kit/c11/impl/CMakeLists.txt deleted file mode 100644 index c64b895..0000000 --- a/source/kit/c11/impl/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -target_sources( - ${KIT_LIBRARY} - PRIVATE - time.c threads_posix.c threads_win32.c - threads_win32_tls_callback.cpp - PUBLIC - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/threads_win32.h>) diff --git a/source/kit/c11/impl/threads_win32.h b/source/kit/c11/impl/threads_win32.h deleted file mode 100644 index 6adf5d2..0000000 --- a/source/kit/c11/impl/threads_win32.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright 2022 Yonggang Luo - * SPDX-License-Identifier: MIT - */ - -#ifndef KIT_C11_IMPL_THREADS_WIN32_H_ -#define KIT_C11_IMPL_THREADS_WIN32_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef KIT_HAVE_WINDOWS -void __threads_win32_tls_callback(void); -#endif - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/source/kit/mersenne_twister_64.c b/source/kit/mersenne_twister_64.c index 4be4313..9ab39d3 100644 --- a/source/kit/mersenne_twister_64.c +++ b/source/kit/mersenne_twister_64.c @@ -1,6 +1,6 @@ #include "mersenne_twister_64.h" -#include <time.h> +#include "time.h" void kit_mt64_init(kit_mt64_state_t *state, uint64_t seed) { state->mt[0] = seed; diff --git a/source/kit/c11/threads.h b/source/kit/threads.h index 0b73183..d98978d 100644 --- a/source/kit/c11/threads.h +++ b/source/kit/threads.h @@ -31,71 +31,72 @@ * DEALINGS IN THE SOFTWARE. */ -#ifndef KIT_C11_THREADS_H_INCLUDED_ -#define KIT_C11_THREADS_H_INCLUDED_ +#ifndef KIT_THREADS_H +#define KIT_THREADS_H -#include "time.h" +#ifndef KIT_DISABLE_SYSTEM_THREADS +# include "time.h" -#include <errno.h> -#include <limits.h> -#include <stdlib.h> +# include <errno.h> +# include <limits.h> +# include <stdlib.h> -#ifdef _MSC_VER -# define _Noreturn __declspec(noreturn) -#endif +# ifdef _MSC_VER +# define _Noreturn __declspec(noreturn) +# endif -#if defined(_WIN32) && !defined(__CYGWIN__) -# include <io.h> /* close */ -# include <process.h> /* _exit */ -#elif defined(KIT_HAVE_PTHREAD) -# include <pthread.h> -# include <unistd.h> /* close, _exit */ -#else -# error Not supported on this platform. -#endif +# if defined(_WIN32) && !defined(__CYGWIN__) +# include <io.h> /* close */ +# include <process.h> /* _exit */ +# elif defined(KIT_HAVE_PTHREAD) +# include <pthread.h> +# include <unistd.h> /* close, _exit */ +# else +# error Not supported on this platform. +# endif /*---------------------------- macros ---------------------------*/ -#ifndef _Thread_local -# if defined(__cplusplus) +# ifndef _Thread_local +# if defined(__cplusplus) /* C++11 doesn't need `_Thread_local` keyword or macro */ -# elif !defined(__STDC_NO_THREADS__) +# elif !defined(__STDC_NO_THREADS__) /* threads are optional in C11, _Thread_local present in this * condition */ -# elif defined(_MSC_VER) -# define _Thread_local __declspec(thread) -# elif defined(__GNUC__) -# define _Thread_local __thread -# else +# elif defined(_MSC_VER) +# define _Thread_local __declspec(thread) +# elif defined(__GNUC__) +# define _Thread_local __thread +# else /* Leave _Thread_local undefined so that use of _Thread_local would * not promote to a non-thread-local global variable */ +# endif # endif -#endif -#if !defined(__cplusplus) +# if !defined(__cplusplus) /* * C11 thread_local() macro * C++11 and above already have thread_local keyword */ -# ifndef thread_local -# if _MSC_VER -# define thread_local __declspec(thread) -# else -# define thread_local _Thread_local +# ifndef thread_local +# if _MSC_VER +# define thread_local __declspec(thread) +# else +# define thread_local _Thread_local +# endif # endif # endif -#endif -#ifdef __cplusplus +# ifdef __cplusplus extern "C" { -#endif +# endif /*---------------------------- types ----------------------------*/ typedef void (*tss_dtor_t)(void *); typedef int (*thrd_start_t)(void *); -#if defined(_WIN32) && !defined(__CYGWIN__) +# if defined(_WIN32) && !defined(__CYGWIN__) typedef struct { void *Ptr; } cnd_t; @@ -117,29 +118,29 @@ typedef struct { volatile uintptr_t status; } once_flag; // FIXME: temporary non-standard hack to ease transition -# define KIT_MTX_INITIALIZER_NP_ \ - { (void *) -1, -1, 0, 0, 0, 0 } -# define ONCE_FLAG_INIT \ - { 0 } -# define TSS_DTOR_ITERATIONS 1 -#elif defined(KIT_HAVE_PTHREAD) +# define KIT_MTX_INITIALIZER_NP_ \ + { (void *) -1, -1, 0, 0, 0, 0 } +# define ONCE_FLAG_INIT \ + { 0 } +# define TSS_DTOR_ITERATIONS 1 +# elif defined(KIT_HAVE_PTHREAD) typedef pthread_cond_t cnd_t; typedef pthread_t thrd_t; typedef pthread_key_t tss_t; typedef pthread_mutex_t mtx_t; typedef pthread_once_t once_flag; // FIXME: temporary non-standard hack to ease transition -# define KIT_MTX_INITIALIZER_NP_ PTHREAD_MUTEX_INITIALIZER -# define ONCE_FLAG_INIT PTHREAD_ONCE_INIT -# ifdef PTHREAD_DESTRUCTOR_ITERATIONS -# define TSS_DTOR_ITERATIONS PTHREAD_DESTRUCTOR_ITERATIONS +# define KIT_MTX_INITIALIZER_NP_ PTHREAD_MUTEX_INITIALIZER +# define ONCE_FLAG_INIT PTHREAD_ONCE_INIT +# ifdef PTHREAD_DESTRUCTOR_ITERATIONS +# define TSS_DTOR_ITERATIONS PTHREAD_DESTRUCTOR_ITERATIONS +# else +# define TSS_DTOR_ITERATIONS \ + 1 // assume TSS dtor MAY be called at least once. +# endif # else -# define TSS_DTOR_ITERATIONS \ - 1 // assume TSS dtor MAY be called at least once. +# error Not supported on this platform. # endif -#else -# error Not supported on this platform. -#endif /*-------------------- enumeration constants --------------------*/ enum { @@ -177,11 +178,11 @@ int thrd_create(thrd_t *, thrd_start_t, void *); thrd_t thrd_current(void); int thrd_detach(thrd_t); int thrd_equal(thrd_t, thrd_t); -#if defined(__cplusplus) +# if defined(__cplusplus) [[ noreturn ]] -#else +# else _Noreturn -#endif +# endif void thrd_exit(int); int thrd_join(thrd_t, int *); int thrd_sleep(const struct timespec *, struct timespec *); @@ -191,8 +192,10 @@ void tss_delete(tss_t); void *tss_get(tss_t); int tss_set(tss_t, void *); -#ifdef __cplusplus +# ifdef __cplusplus } -#endif +# endif + +#endif /* KIT_DISABLE_SYSTEM_THREADS */ -#endif /* KIT_C11_THREADS_H_INCLUDED_ */ +#endif /* KIT_THREADS_H */ diff --git a/source/kit/c11/impl/threads_posix.c b/source/kit/threads.posix.c index ebf411c..20ef1ae 100644 --- a/source/kit/c11/impl/threads_posix.c +++ b/source/kit/threads.posix.c @@ -29,17 +29,18 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ -#ifndef KIT_HAVE_WINDOWS +#ifndef KIT_DISABLE_SYSTEM_THREADS +# ifdef KIT_HAVE_PTHREAD -# include <assert.h> -# include <errno.h> -# include <limits.h> -# include <sched.h> -# include <stdint.h> /* for intptr_t */ -# include <stdlib.h> -# include <unistd.h> +# include <assert.h> +# include <errno.h> +# include <limits.h> +# include <sched.h> +# include <stdint.h> /* for intptr_t */ +# include <stdlib.h> +# include <unistd.h> -# include "../threads.h" +# include "threads.h" /* Configuration macro: @@ -48,10 +49,10 @@ Configuration macro: Use pthread_mutex_timedlock() for `mtx_timedlock()' Otherwise use mtx_trylock() + *busy loop* emulation. */ -# if !defined(__CYGWIN__) && !defined(__APPLE__) && \ - !defined(__NetBSD__) -# define EMULATED_THREADS_USE_NATIVE_TIMEDLOCK -# endif +# if !defined(__CYGWIN__) && !defined(__APPLE__) && \ + !defined(__NetBSD__) +# define EMULATED_THREADS_USE_NATIVE_TIMEDLOCK +# endif /*---------------------------- types ----------------------------*/ @@ -149,7 +150,7 @@ void mtx_destroy(mtx_t *mtx) { * Thus the linker will be happy and things don't clash when building * with -O1 or greater. */ -# if defined(HAVE_FUNC_ATTRIBUTE_WEAK) && !defined(__CYGWIN__) +# if defined(HAVE_FUNC_ATTRIBUTE_WEAK) && !defined(__CYGWIN__) __attribute__((weak)) int pthread_mutexattr_init( pthread_mutexattr_t *attr); @@ -158,7 +159,7 @@ __attribute__((weak)) int pthread_mutexattr_settype( __attribute__((weak)) int pthread_mutexattr_destroy( pthread_mutexattr_t *attr); -# endif +# endif // 7.25.4.2 int mtx_init(mtx_t *mtx, int type) { @@ -193,13 +194,13 @@ int mtx_timedlock(mtx_t *mtx, const struct timespec *ts) { assert(ts != NULL); { -# ifdef EMULATED_THREADS_USE_NATIVE_TIMEDLOCK +# ifdef EMULATED_THREADS_USE_NATIVE_TIMEDLOCK int rt; rt = pthread_mutex_timedlock(mtx, ts); if (rt == 0) return thrd_success; return (rt == ETIMEDOUT) ? thrd_timedout : thrd_error; -# else +# else time_t expire = time(NULL); expire += ts->tv_sec; while (mtx_trylock(mtx) != thrd_success) { @@ -210,7 +211,7 @@ int mtx_timedlock(mtx_t *mtx, const struct timespec *ts) { thrd_yield(); } return thrd_success; -# endif +# endif } } @@ -310,4 +311,5 @@ int tss_set(tss_t key, void *val) { : thrd_error; } +# endif #endif diff --git a/source/kit/c11/impl/threads_win32.c b/source/kit/threads.win32.c index b94d20f..d425a27 100644 --- a/source/kit/c11/impl/threads_win32.c +++ b/source/kit/threads.win32.c @@ -29,23 +29,24 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ -#ifdef KIT_HAVE_WINDOWS +#ifndef KIT_DISABLE_SYSTEM_THREADS +# if defined(_WIN32) && !defined(__CYGWIN__) -# include <assert.h> -# include <errno.h> -# include <limits.h> -# include <process.h> // MSVCRT -# include <stdbool.h> -# include <stdlib.h> +# include <assert.h> +# include <errno.h> +# include <limits.h> +# include <process.h> // MSVCRT +# include <stdbool.h> +# include <stdlib.h> -# include "../threads.h" +# include "threads.h" -# include "threads_win32.h" +# include "threads_win32.h" -# ifndef WIN32_LEAN_AND_MEAN -# define WIN32_LEAN_AND_MEAN 1 -# endif -# include <windows.h> +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN 1 +# endif +# include <windows.h> /* Configuration macro: @@ -59,20 +60,20 @@ Configuration macro: Max registerable TSS dtor number. */ -# if _WIN32_WINNT >= 0x0600 +# if _WIN32_WINNT >= 0x0600 // Prefer native WindowsAPI on newer environment. -# if !defined(__MINGW32__) -# define EMULATED_THREADS_USE_NATIVE_CALL_ONCE +# if !defined(__MINGW32__) +# define EMULATED_THREADS_USE_NATIVE_CALL_ONCE +# endif # endif -# endif -# define EMULATED_THREADS_TSS_DTOR_SLOTNUM \ - 64 // see TLS_MINIMUM_AVAILABLE +# define EMULATED_THREADS_TSS_DTOR_SLOTNUM \ + 64 // see TLS_MINIMUM_AVAILABLE // check configuration -# if defined(EMULATED_THREADS_USE_NATIVE_CALL_ONCE) && \ - (_WIN32_WINNT < 0x0600) -# error EMULATED_THREADS_USE_NATIVE_CALL_ONCE requires _WIN32_WINNT>=0x0600 -# endif +# if defined(EMULATED_THREADS_USE_NATIVE_CALL_ONCE) && \ + (_WIN32_WINNT < 0x0600) +# error EMULATED_THREADS_USE_NATIVE_CALL_ONCE requires _WIN32_WINNT>=0x0600 +# endif static_assert(sizeof(cnd_t) == sizeof(CONDITION_VARIABLE), "The size of cnd_t must equal to CONDITION_VARIABLE"); @@ -131,7 +132,7 @@ static DWORD impl_abs2relmsec(const struct timespec *abs_time) { return rel_ms; } -# ifdef EMULATED_THREADS_USE_NATIVE_CALL_ONCE +# ifdef EMULATED_THREADS_USE_NATIVE_CALL_ONCE struct impl_call_once_param { void (*func)(void); }; @@ -145,7 +146,7 @@ static BOOL CALLBACK impl_call_once_callback(PINIT_ONCE InitOnce, ((void) Context); // suppress warning return TRUE; } -# endif // ifdef EMULATED_THREADS_USE_NATIVE_CALL_ONCE +# endif // ifdef EMULATED_THREADS_USE_NATIVE_CALL_ONCE static struct impl_tss_dtor_entry { tss_t key; @@ -180,14 +181,14 @@ static void impl_tss_dtor_invoke(void) { // 7.25.2.1 void call_once(once_flag *flag, void (*func)(void)) { assert(flag && func); -# ifdef EMULATED_THREADS_USE_NATIVE_CALL_ONCE +# ifdef EMULATED_THREADS_USE_NATIVE_CALL_ONCE { struct impl_call_once_param param; param.func = func; InitOnceExecuteOnce((PINIT_ONCE) flag, impl_call_once_callback, (PVOID) ¶m, NULL); } -# else +# else if (InterlockedCompareExchangePointer( (PVOID volatile *) &flag->status, (PVOID) 1, (PVOID) 0) == 0) { @@ -200,7 +201,7 @@ void call_once(once_flag *flag, void (*func)(void)) { thrd_yield(); } } -# endif +# endif } /*------------- 7.25.3 Condition variable functions -------------*/ @@ -457,4 +458,5 @@ int tss_set(tss_t key, void *val) { return TlsSetValue(key, val) ? thrd_success : thrd_error; } -#endif +# endif +#endif /* KIT_DISABLE_SYSTEM_THREADS */ diff --git a/source/kit/threads.win32.h b/source/kit/threads.win32.h new file mode 100644 index 0000000..e8e9abf --- /dev/null +++ b/source/kit/threads.win32.h @@ -0,0 +1,25 @@ +/* + * Copyright 2022 Yonggang Luo + * SPDX-License-Identifier: MIT + */ + +#ifndef KIT_THREADS_WIN32_H +#define KIT_THREADS_WIN32_H + +#ifndef KIT_DISABLE_SYSTEM_THREADS + +# ifdef __cplusplus +extern "C" { +# endif + +# if defined(_WIN32) && !defined(__CYGWIN__) +void __threads_win32_tls_callback(void); +# endif + +# ifdef __cplusplus +} +# endif + +#endif + +#endif diff --git a/source/kit/c11/impl/threads_win32_tls_callback.cpp b/source/kit/threads_tls_callback.win32.cpp index 27874c0..3cc3cc6 100644 --- a/source/kit/c11/impl/threads_win32_tls_callback.cpp +++ b/source/kit/threads_tls_callback.win32.cpp @@ -3,9 +3,10 @@ * SPDX-License-Identifier: MIT */ -#include "threads_win32.h" +#ifndef KIT_DISABLE_SYSTEM_THREADS +# if defined(_WIN32) && !defined(__CYGWIN__) -#ifdef KIT_HAVE_WINDOWS +# include "threads.win32.h" struct tls_callback { tls_callback() { } @@ -13,6 +14,8 @@ struct tls_callback { __threads_win32_tls_callback(); } }; + static thread_local tls_callback tls_callback_instance; +# endif #endif diff --git a/source/kit/c11/impl/time.c b/source/kit/time.c index 777b976..bcb3819 100644 --- a/source/kit/c11/impl/time.c +++ b/source/kit/time.c @@ -81,4 +81,4 @@ int timespec_get(struct timespec *ts, int base) { } # endif -#endif /* KIT_HAVE_TIMESPEC_GET */ +#endif diff --git a/source/kit/c11/time.h b/source/kit/time.h index 7467a33..dbf8883 100644 --- a/source/kit/c11/time.h +++ b/source/kit/time.h @@ -5,8 +5,8 @@ * C11 <time.h> emulation library */ -#ifndef KIT_C11_TIME_H_INCLUDED_ -#define KIT_C11_TIME_H_INCLUDED_ +#ifndef KIT_TIME_H +#define KIT_TIME_H #include <time.h> @@ -59,4 +59,4 @@ int timespec_get(struct timespec *ts, int base); } #endif -#endif /* KIT_C11_TIME_H_INCLUDED_ */ +#endif /* KIT_TIME_H */ |