1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \file thread_create.c
8 * \brief Implements thread creation wrappers
20 #include <yaz/xmalloc.h>
22 #include <yaz/thread_create.h>
38 void *(*routine)(void *p);
45 unsigned int __stdcall win32_routine(void *p)
47 yaz_thread_t t = (yaz_thread_t) p;
48 void *userdata = t->data;
49 t->data = t->routine(userdata);
55 yaz_thread_t yaz_thread_create(void *(*start_routine)(void *p), void *arg)
57 yaz_thread_t t = xmalloc(sizeof(*t));
59 int r = pthread_create(&t->id, 0, start_routine, arg);
67 /* we create a wrapper on windows and pass yaz_thread struct to that */
70 t->data = arg; /* use data for both input and output */
71 t->routine = start_routine;
72 ex_ret = _beginthreadex(NULL, 0, win32_routine, t, 0, &threadID);
78 t->handle = (HANDLE) ex_ret;
80 t->data = start_routine(arg);
86 void yaz_thread_join(yaz_thread_t *tp, void **value_ptr)
90 #ifdef YAZ_POSIX_THREADS
91 pthread_join((*tp)->id, value_ptr);
94 WaitForSingleObject((*tp)->handle, INFINITE);
95 CloseHandle((*tp)->handle);
98 *value_ptr = (*tp)->data;
105 void yaz_thread_detach(yaz_thread_t *tp)
109 #ifdef YAZ_POSIX_THREADS
110 pthread_detach((*tp)->id);
113 CloseHandle((*tp)->handle);
124 * c-file-style: "Stroustrup"
125 * indent-tabs-mode: nil
127 * vim: shiftwidth=4 tabstop=8 expandtab