summaryrefslogtreecommitdiffstats
path: root/src/thread.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2022-10-20 21:08:42 -0400
committerDavid Robillard <d@drobilla.net>2022-10-20 21:17:04 -0400
commit5eb8ceef3fd92cc2dccd9efc1fd8f2baed0a492c (patch)
tree893ca237ad6e8b7432e362c85b7ab0dc40b16050 /src/thread.c
parent668ffa3febb116d20add6e9460fb1fb521a22e35 (diff)
downloadzix-5eb8ceef3fd92cc2dccd9efc1fd8f2baed0a492c.tar.gz
zix-5eb8ceef3fd92cc2dccd9efc1fd8f2baed0a492c.tar.bz2
zix-5eb8ceef3fd92cc2dccd9efc1fd8f2baed0a492c.zip
Hide thread implementation
Diffstat (limited to 'src/thread.c')
-rw-r--r--src/thread.c59
1 files changed, 59 insertions, 0 deletions
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