]> err.no Git - yubikey-personalization/commitdiff
make the thread test work on windows
authorKlas Lindfors <klas@yubico.com>
Thu, 20 Sep 2012 08:35:05 +0000 (10:35 +0200)
committerKlas Lindfors <klas@yubico.com>
Thu, 20 Sep 2012 08:35:05 +0000 (10:35 +0200)
tests/test_threaded_calls.c

index 0ed1c748f33c37acbbcf1707199a63e86940dd3d..5e1156820b7cf7c2970ff2da8689138cf127fce9 100644 (file)
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <pthread.h>
 #include <unistd.h>
 #include <assert.h>
 
+#ifdef _WIN32
+#include <windows.h>
+#include <errno.h>
+#define ALLOC_THREADS(size) HANDLE *threads = malloc(sizeof(HANDLE) * times)
+#define spawn_thread(thread, attr, start_routine, arg) thread = CreateThread(attr, 0, start_routine, arg, 0, NULL)
+#define join_thread(thread, retval) WaitForSingleObject(thread, INFINITE)
+#else
+#include <pthread.h>
+#define ALLOC_THREADS(size) pthread_t *threads = malloc(sizeof(pthread_t) * times)
+#define spawn_thread(thread, attr, start_routine, arg) pthread_create(&thread, attr, start_routine, arg)
+#define join_thread(thread, retval) pthread_join(thread, retval)
+#endif
+#define FREE_THREADS free(threads)
+
 #include <ykpers.h>
 
 void *start_thread(void *arg)
@@ -61,14 +74,14 @@ void _test_threaded_calls()
 {
        int times = 5;
        int i;
-       pthread_t *threads = malloc(sizeof(pthread_t) * times);
+       ALLOC_THREADS(times);
 
        for(i = 0; i < times; i++) {
-               pthread_create(&threads[i], NULL, start_thread, NULL);
-               pthread_join(threads[i], NULL);
+               spawn_thread(threads[i], NULL, start_thread, NULL);
+               join_thread(threads[i], NULL);
        }
 
-       free(threads);
+       FREE_THREADS;
 }
 
 int main(void)