X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=test%2Ftest_mutex.c;h=2327b8ea44d22eecf019f5e0d0824b1c67c80ca3;hp=9cef383402546fe61c901d5fa439bab22fd77068;hb=077c4e7bb226de0f6414674e01a838498a49867b;hpb=e45adb79ab73280e963c849927707089d4031040 diff --git a/test/test_mutex.c b/test/test_mutex.c index 9cef383..2327b8e 100644 --- a/test/test_mutex.c +++ b/test/test_mutex.c @@ -1,15 +1,26 @@ /* This file is part of the YAZ toolkit. - * Copyright (C) 1995-2010 Index Data + * Copyright (C) Index Data * See the file LICENSE for details. */ +#if HAVE_CONFIG_H +#include +#endif #include #include #include +#include +#if HAVE_SYS_TIME_H #include +#endif +#ifdef WIN32 +#include +#endif + #include #include +#include static void tst_mutex(void) { @@ -37,8 +48,7 @@ static void tst_cond(void) { YAZ_MUTEX p = 0; YAZ_COND c; - struct timespec abstime; - struct timeval tval; + struct timeval abstime; int r; yaz_mutex_create(&p); @@ -47,31 +57,63 @@ static void tst_cond(void) return; yaz_cond_create(&c); - YAZ_CHECK(c); - if (!c) - return; + if (c) + { + r = yaz_gettimeofday(&abstime); + YAZ_CHECK_EQ(r, 0); - r = gettimeofday(&tval, 0); - YAZ_CHECK_EQ(r, 0); - - abstime.tv_sec = tval.tv_sec + 1; /* wait 2 seconds */ - abstime.tv_nsec = tval.tv_usec * 1000; - - r = yaz_cond_wait(c, p, &abstime); - YAZ_CHECK(r != 0); + abstime.tv_sec += 1; /* wait 1 second */ + r = yaz_cond_wait(c, p, &abstime); + YAZ_CHECK(r != 0); + + } yaz_cond_destroy(&c); YAZ_CHECK(c == 0); yaz_mutex_destroy(&p); 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 mydata0 = 42; + int mydata1= 42; + yaz_thread_t t[2]; + + t[0] = yaz_thread_create(my_handler, &mydata0); + YAZ_CHECK(t[0]); + t[1] = yaz_thread_create(my_handler, &mydata1); + YAZ_CHECK(t[1]); + + return_data = 0; + yaz_thread_join(&t[0], &return_data); + YAZ_CHECK(!t[0]); + YAZ_CHECK(return_data == &mydata0); + + return_data = 0; + yaz_thread_join(&t[1], &return_data); + YAZ_CHECK(!t[1]); + YAZ_CHECK(return_data == &mydata1); + + YAZ_CHECK_EQ(mydata0, 43); + YAZ_CHECK_EQ(mydata1, 43); +} + int main (int argc, char **argv) { YAZ_CHECK_INIT(argc, argv); YAZ_CHECK_LOG(); tst_mutex(); tst_cond(); + tst_create_thread(); YAZ_CHECK_TERM; }