From a4f3aed6537286535d1ea9962adb9ab77c528565 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Wed, 19 May 2010 13:43:01 +0200 Subject: [PATCH] Add yaz_thread_{create,join,detach} These are simple wrappers for POSIX threads for now --- include/yaz/Makefile.am | 2 +- include/yaz/thread_create.h | 73 +++++++++++++++++++++++++++++++++ src/Makefile.am | 2 +- src/thread_create.c | 93 +++++++++++++++++++++++++++++++++++++++++++ test/test_mutex.c | 33 +++++++++++++++ 5 files changed, 201 insertions(+), 2 deletions(-) create mode 100644 include/yaz/thread_create.h create mode 100644 src/thread_create.c diff --git a/include/yaz/Makefile.am b/include/yaz/Makefile.am index 72e5896..28dd159 100644 --- a/include/yaz/Makefile.am +++ b/include/yaz/Makefile.am @@ -20,7 +20,7 @@ pkginclude_HEADERS= backend.h ccl.h ccl_xml.h cql.h rpn2cql.h comstack.h \ z-univ.h z-oclcui.h zes-expi.h zes-exps.h zes-order.h zes-pquery.h \ zes-psched.h zes-admin.h zes-pset.h zes-update.h zes-update0.h \ zoom.h z-charneg.h charneg.h soap.h srw.h zgdu.h matchstr.h json.h \ - file_glob.h dirent.h thread_id.h gettimeofday.h shptr.h + file_glob.h dirent.h thread_id.h gettimeofday.h shptr.h thread_create.h EXTRA_DIST = yaz-version.h.in diff --git a/include/yaz/thread_create.h b/include/yaz/thread_create.h new file mode 100644 index 0000000..d93bc03 --- /dev/null +++ b/include/yaz/thread_create.h @@ -0,0 +1,73 @@ +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2010 Index Data. + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Index Data nor the names of its contributors + * may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * \file thread_create.h + * \brief Implements thread creation wrappers + */ +#ifndef YAZ_THREAD_CREATE_H +#define YAZ_THREAD_CREATE_H + +#include +#include +#include + +YAZ_BEGIN_CDECL + +/** \brief Thread Identifier opaque pointer */ +typedef struct yaz_thread *yaz_thread_t; + +/** \brief create thread + \param start_routine thread handler + \param arg user data to be passed to handler + \returns thread_id identifier if successful; NULL on failure + */ +YAZ_EXPORT yaz_thread_t yaz_thread_create(void *(*start_routine)(void *p), void *arg); + +/** \brief join thread + \param tp thread_id reference .. Will be 0 upon completion + \param value_ptr ref pointer to routine result (0 if not needed) +*/ +YAZ_EXPORT void yaz_thread_join(yaz_thread_t *tp, void **value_ptr); + +/** \brief detach thread + \param tp thread_id reference .. Will be 0 upon completion +*/ +void yaz_thread_detach(yaz_thread_t *tp); + +YAZ_END_CDECL + +#endif +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ + diff --git a/src/Makefile.am b/src/Makefile.am index 46e2e50..cfa34cb 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -103,7 +103,7 @@ libyaz_la_SOURCES=version.c options.c log.c \ iconv_encode_marc8.c iconv_encode_iso_8859_1.c iconv_encode_wchar.c \ iconv_decode_marc8.c iconv_decode_iso5426.c iconv_decode_danmarc.c sc.c \ json.c xml_include.c file_glob.c dirent.c mutex-p.h mutex.c condvar.c \ - thread_id.c gettimeofday.c + thread_id.c gettimeofday.c thread_create.c libyaz_la_LDFLAGS=-version-info $(YAZ_VERSION_INFO) 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 + */ + diff --git a/test/test_mutex.c b/test/test_mutex.c index 52853b1..6a0b9f1 100644 --- a/test/test_mutex.c +++ b/test/test_mutex.c @@ -7,6 +7,7 @@ #include #include +#include #if HAVE_SYS_TIME_H #include #endif @@ -71,12 +72,44 @@ static void tst_cond(void) YAZ_CHECK(p == 0); } +static void *my_handler(void *arg) +{ + int *mydata = (int*) arg; + (*mydata)++; + return mydata; +} + +static void tst_create_thread(void) +{ + void *return_data; + int mydata = 42; + yaz_thread_t t[2]; + + t[0] = yaz_thread_create(my_handler, &mydata); + YAZ_CHECK(t[0]); + t[1] = yaz_thread_create(my_handler, &mydata); + YAZ_CHECK(t[1]); + + return_data = 0; + yaz_thread_join(&t[0], &return_data); + YAZ_CHECK(!t[0]); + YAZ_CHECK(return_data == &mydata); + + return_data = 0; + yaz_thread_join(&t[1], &return_data); + YAZ_CHECK(!t[1]); + YAZ_CHECK(return_data == &mydata); + + YAZ_CHECK_EQ(mydata, 44); +} + int main (int argc, char **argv) { YAZ_CHECK_INIT(argc, argv); YAZ_CHECK_LOG(); tst_mutex(); tst_cond(); + tst_create_thread(); YAZ_CHECK_TERM; } -- 1.7.10.4