pthread: minimal compat for win32
authorMarko Kreen <markokr@gmail.com>
Wed, 2 Sep 2009 13:45:32 +0000 (16:45 +0300)
committerMarko Kreen <markokr@gmail.com>
Wed, 2 Sep 2009 16:16:22 +0000 (19:16 +0300)
m4/usual.m4
usual/pthread.c [new file with mode: 0644]
usual/pthread.h [new file with mode: 0644]

index 271ae432b3ff47749028de0090a258183b708248..406f55745536073e18499fa1515fff832aadadb9 100644 (file)
@@ -103,7 +103,7 @@ AC_CHECK_HEADERS([sys/socket.h poll.h sys/poll.h sys/un.h])
 AC_CHECK_HEADERS([arpa/inet.h netinet/in.h netinet/tcp.h])
 AC_CHECK_HEADERS([sys/param.h sys/uio.h libgen.h pwd.h grp.h])
 AC_CHECK_HEADERS([sys/wait.h sys/mman.h syslog.h netdb.h])
-AC_CHECK_HEADERS([err.h])
+AC_CHECK_HEADERS([err.h pthread.h])
 dnl ucred.h may have prereqs
 AC_CHECK_HEADERS([ucred.h sys/ucred.h], [], [], [
 #ifdef HAVE_SYS_TYPES_H
diff --git a/usual/pthread.c b/usual/pthread.c
new file mode 100644 (file)
index 0000000..ca1cb75
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * Pthreads compat.
+ *
+ * Copyright (c) 2007-2009 Marko Kreen
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+
+#include <usual/pthread.h>
+
+#ifdef WIN32
+/*
+ * basic pthreads for win32.
+ */
+
+struct _w32thread {
+       void *(*fn)(void *);
+       void *arg;
+};
+
+static DWORD WINAPI w32launcher(LPVOID arg)
+{
+       struct _w32thread *info = arg;
+       info->fn(info->arg);
+       free(info);
+       return 0;
+}
+
+int pthread_create(pthread_t *t, pthread_attr_t *attr, void *(*fn)(void *), void *arg)
+{
+       struct _w32thread *info = calloc(1, sizeof(*info));
+       if (!info)
+               return -1;
+       info->fn = fn;
+       info->arg = arg;
+       *t = CreateThread(NULL, 0, w32launcher, info, 0, NULL);
+       if (*t == NULL)
+               return -1;
+       return 0;
+}
+
+int pthread_join(pthread_t *t, void **ret)
+{
+       if (WaitForSingleObject(*t, INFINITE) != WAIT_OBJECT_0)
+               return -1;
+       CloseHandle(*t);
+       return 0;
+}
+
+int pthread_mutex_init(pthread_mutex_t *lock, void *unused)
+{
+       *lock = CreateMutex(NULL, FALSE, NULL);
+       if (*lock == NULL)
+               return -1;
+       return 0;
+}
+
+int pthread_mutex_destroy(pthread_mutex_t *lock)
+{
+       if (*lock) {
+               CloseHandle(*lock);
+               *lock = NULL;
+       }
+       return 0;
+}
+
+int pthread_mutex_lock(pthread_mutex_t *lock)
+{
+       if (WaitForSingleObject(*lock, INFINITE) != WAIT_OBJECT_0)
+               return -1;
+       return 0;
+}
+
+int pthread_mutex_unlock(pthread_mutex_t *lock)
+{
+       if (!ReleaseMutex(*lock))
+               return -1;
+       return 0;
+}
+
+#endif /* win32 */
+
diff --git a/usual/pthread.h b/usual/pthread.h
new file mode 100644 (file)
index 0000000..fab7f3b
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Pthreads compat.
+ *
+ * Copyright (c) 2007-2009 Marko Kreen
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef _USUAL_PTHREAD_H_
+#define _USUAL_PTHREAD_H_
+
+#include <usual/base.h>
+
+#ifdef HAVE_PTHREAD_H
+#include <pthread.h>
+#endif
+
+#ifdef WIN32
+
+#define pthread_create(a,b,c,d)                compat_pthread_create(a,b,c,d)
+#define pthread_mutex_init(a,b)                compat_pthread_mutex_init(a,b)
+#define pthread_mutex_destroy(a)       compat_pthread_mutex_destroy(a)
+#define pthread_mutex_lock(a)          compat_pthread_mutex_lock(a)
+#define pthread_mutex_unlock(a)                compat_pthread_mutex_unlock(a)
+#define pthread_join(a,b)              compat_pthread_join(a,b)
+
+typedef HANDLE pthread_t;
+typedef HANDLE pthread_mutex_t;
+typedef int pthread_attr_t;
+
+int pthread_create(pthread_t *t, pthread_attr_t *attr, void *(*fn)(void *), void *arg);
+int pthread_mutex_init(pthread_mutex_t *lock, void *unused);
+int pthread_mutex_destroy(pthread_mutex_t *lock);
+int pthread_mutex_lock(pthread_mutex_t *lock);
+int pthread_mutex_unlock(pthread_mutex_t *lock);
+int pthread_join(pthread_t *t, void **ret);
+
+#endif /* WIN32 */
+
+#endif