X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fthread_create.c;fp=src%2Fthread_create.c;h=130efed1b5f4ec80cf3304ed7aeaee5518ed32f3;hp=0000000000000000000000000000000000000000;hb=a4f3aed6537286535d1ea9962adb9ab77c528565;hpb=7993bf125aa3ae6ac06e12a92209f54a863717c8 diff --git a/src/thread_create.c b/src/thread_create.c new file mode 100644 index 0000000..130efed --- /dev/null +++ b/src/thread_create.c @@ -0,0 +1,93 @@ +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2010 Index Data + * See the file LICENSE for details. + */ + +/** + * \file thread_create.c + * \brief Implements thread creation wrappers + * + */ +#if HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include + +#if YAZ_POSIX_THREADS +#include +#endif +#ifdef WIN32 +#include +#endif + +struct yaz_thread { +#if YAZ_POSIX_THREADS + pthread_t id; +#else + void *return_data; +#ifdef WIN32 + HANDLE id; +#endif +#endif +}; + +yaz_thread_t yaz_thread_create(void *(*start_routine)(void *p), void *arg) +{ + yaz_thread_t t = xmalloc(sizeof(*t)); +#if YAZ_POSIX_THREADS + int r = pthread_create(&t->id, 0, start_routine, arg); + if (r) + { + xfree(t); + t = 0; + } +#else + t->return_data = start_routine(arg); +#endif + return t; +} + +void yaz_thread_join(yaz_thread_t *tp, void **value_ptr) +{ + if (*tp) + { +#ifdef YAZ_POSIX_THREADS + pthread_join((*tp)->id, value_ptr); +#else + if (value_ptr) + *value_ptr = (*tp)->return_data; +#endif + xfree(*tp); + *tp = 0; + } +} + +void yaz_thread_detach(yaz_thread_t *tp) +{ + if (*tp) + { +#ifdef YAZ_POSIX_THREADS + pthread_detach((*tp)->id); +#endif + xfree(*tp); + *tp = 0; + } +} + +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ +