summaryrefslogtreecommitdiff
path: root/thread/impl/simpleops-thread-impl-win32.h
diff options
context:
space:
mode:
Diffstat (limited to 'thread/impl/simpleops-thread-impl-win32.h')
-rw-r--r--thread/impl/simpleops-thread-impl-win32.h123
1 files changed, 123 insertions, 0 deletions
diff --git a/thread/impl/simpleops-thread-impl-win32.h b/thread/impl/simpleops-thread-impl-win32.h
new file mode 100644
index 0000000..ba8f126
--- /dev/null
+++ b/thread/impl/simpleops-thread-impl-win32.h
@@ -0,0 +1,123 @@
+/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
+/*
+ * Copyright 2010-2011 Andrea Canciani
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * Author(s): Andrea Canciani <ranma42@gmail.com>
+ */
+
+#ifndef SIMPLEOPS_THREAD_IMPL_WIN32_H
+#define SIMPLEOPS_THREAD_IMPL_WIN32_H
+
+#ifndef SIMPLEOPS_THREAD_IMPL_H
+#error Private header used directly
+#endif
+
+#if ! SIMPLEOPS_HAVE_THREAD_IMPL && \
+ defined(_WIN32)
+
+#define SIMPLEOPS_HAVE_THREAD_IMPL 1
+#define SIMPLEOPS_HAVE_THREAD_IMPL_WIN32 1
+
+#include "simpleops/atomic/simpleops-atomic.h"
+
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#include <process.h>
+#include <errno.h>
+
+typedef struct _simpleops_thread_win32 {
+ void *(*f) (void *);
+ void *arg, *value;
+ HANDLE handle;
+} *simpleops_thread_t;
+
+static unsigned __stdcall
+_simpleops_thread_win32_wrapper (void *arg)
+{
+ simpleops_thread_t thread;
+
+ thread = arg;
+ thread->value = thread->f (thread->arg);
+
+ _endthreadex (0);
+ return 0;
+}
+
+static simpleops_always_inline simpleops_bool_t
+simpleops_thread_create (simpleops_thread_t *thread,
+ void * (*f) (void *),
+ void *arg)
+{
+ simpleops_thread_t t;
+ uintptr_t r;
+
+ if (SIMPLEOPS_UNLIKELY (thread == NULL))
+ return FALSE;
+
+ *thread = NULL;
+
+ t = malloc (sizeof (struct _simpleops_thread_win32));
+ if (SIMPLEOPS_UNLIKELY (t == NULL))
+ return FALSE;
+
+ t->f = f;
+ t->arg = arg;
+ t->value = NULL;
+
+ r = _beginthreadex (NULL, 0, _simpleops_thread_win32_wrapper, t, 0, NULL);
+ if (SIMPLEOPS_UNLIKELY (r == 0)) {
+ free (t);
+ return FALSE;
+ }
+
+ t->handle = (HANDLE) r;
+
+ *thread = t;
+
+ return TRUE;
+}
+
+static simpleops_always_inline simpleops_bool_t
+simpleops_thread_join (simpleops_thread_t thread, void **value_ptr)
+{
+ DWORD ret;
+
+ if (SIMPLEOPS_UNLIKELY (thread == NULL))
+ return FALSE;
+
+ ret = WaitForSingleObject (thread->handle, INFINITE);
+ if (SIMPLEOPS_UNLIKELY (ret != WAIT_OBJECT_0))
+ return FALSE;
+
+ if (value_ptr != NULL)
+ *value_ptr = thread->value;
+
+ CloseHandle (thread->handle);
+ free (thread);
+
+ return TRUE;
+}
+
+#endif
+
+#endif