summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/zix/thread.h65
-rw-r--r--meson.build1
-rw-r--r--src/thread.c59
3 files changed, 72 insertions, 53 deletions
diff --git a/include/zix/thread.h b/include/zix/thread.h
index 173cc39..af036b1 100644
--- a/include/zix/thread.h
+++ b/include/zix/thread.h
@@ -4,6 +4,7 @@
#ifndef ZIX_THREAD_H
#define ZIX_THREAD_H
+#include "zix/attributes.h"
#include "zix/common.h"
#ifdef _WIN32
@@ -12,7 +13,6 @@
# include <pthread.h>
#endif
-#include <errno.h>
#include <stddef.h>
#ifdef __cplusplus
@@ -26,18 +26,21 @@ extern "C" {
*/
#ifdef _WIN32
-# define ZIX_THREAD_RESULT 0 ///< Result returned from a thread function
-# define ZIX_THREAD_FUNC __stdcall ///< Thread function attribute
+
+# define ZIX_THREAD_RESULT 0
+# define ZIX_THREAD_FUNC __stdcall
typedef HANDLE ZixThread;
typedef DWORD ZixThreadResult;
#else
+
# define ZIX_THREAD_RESULT NULL ///< Result returned from a thread function
# define ZIX_THREAD_FUNC ///< Thread function attribute
-typedef pthread_t ZixThread;
-typedef void* ZixThreadResult;
+typedef pthread_t ZixThread; ///< A thread
+typedef void* ZixThreadResult; ///< Thread function return type
+
#endif
/**
@@ -59,62 +62,18 @@ typedef ZixThreadResult(ZIX_THREAD_FUNC* ZixThreadFunc)(void*);
The thread will immediately be launched, calling `function` with `arg`
as the only parameter.
*/
-static inline ZixStatus
+ZIX_API
+ZixStatus
zix_thread_create(ZixThread* thread,
size_t stack_size,
ZixThreadFunc function,
void* arg);
/// Join `thread` (block until `thread` exits)
-static inline ZixStatus
+ZIX_API
+ZixStatus
zix_thread_join(ZixThread thread);
-#ifdef _WIN32
-
-static inline ZixStatus
-zix_thread_create(ZixThread* thread,
- size_t stack_size,
- ZixThreadFunc function,
- void* arg)
-{
- *thread = CreateThread(NULL, stack_size, function, arg, 0, NULL);
- return *thread ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR;
-}
-
-static inline ZixStatus
-zix_thread_join(ZixThread thread)
-{
- return (WaitForSingleObject(thread, INFINITE) == WAIT_OBJECT_0)
- ? ZIX_STATUS_SUCCESS
- : ZIX_STATUS_ERROR;
-}
-
-#else /* !defined(_WIN32) */
-
-static inline ZixStatus
-zix_thread_create(ZixThread* thread,
- size_t stack_size,
- ZixThreadFunc function,
- void* arg)
-{
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_attr_setstacksize(&attr, stack_size);
-
- const int ret = pthread_create(thread, NULL, function, arg);
-
- pthread_attr_destroy(&attr);
- return zix_errno_status(ret);
-}
-
-static inline ZixStatus
-zix_thread_join(ZixThread thread)
-{
- return pthread_join(thread, NULL) ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
-}
-
-#endif
-
/**
@}
*/
diff --git a/meson.build b/meson.build
index 613e2fb..d5e228a 100644
--- a/meson.build
+++ b/meson.build
@@ -115,6 +115,7 @@ sources = files(
'src/hash.c',
'src/ring.c',
'src/status.c',
+ 'src/thread.c',
'src/tree.c',
)
diff --git a/src/thread.c b/src/thread.c
new file mode 100644
index 0000000..72e4d46
--- /dev/null
+++ b/src/thread.c
@@ -0,0 +1,59 @@
+// Copyright 2012-2020 David Robillard <d@drobilla.net>
+// SPDX-License-Identifier: ISC
+
+#include "zix/thread.h"
+#include "zix/common.h"
+
+#ifdef _WIN32
+# include <windows.h>
+#else
+# include <pthread.h>
+#endif
+
+#include <stddef.h>
+
+#ifdef _WIN32
+
+ZixStatus
+zix_thread_create(ZixThread* thread,
+ size_t stack_size,
+ ZixThreadFunc function,
+ void* arg)
+{
+ *thread = CreateThread(NULL, stack_size, function, arg, 0, NULL);
+ return *thread ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR;
+}
+
+ZixStatus
+zix_thread_join(ZixThread thread)
+{
+ return (WaitForSingleObject(thread, INFINITE) == WAIT_OBJECT_0)
+ ? ZIX_STATUS_SUCCESS
+ : ZIX_STATUS_ERROR;
+}
+
+#else // !defined(_WIN32)
+
+ZixStatus
+zix_thread_create(ZixThread* thread,
+ size_t stack_size,
+ ZixThreadFunc function,
+ void* arg)
+{
+ pthread_attr_t attr;
+ pthread_attr_init(&attr);
+ pthread_attr_setstacksize(&attr, stack_size);
+
+ const int ret = pthread_create(thread, NULL, function, arg);
+
+ pthread_attr_destroy(&attr);
+ return zix_errno_status(ret);
+}
+
+ZixStatus
+zix_thread_join(ZixThread thread)
+{
+ return pthread_join(thread, NULL) ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
+}
+
+#endif