Simple test of yaz_cond_wait
[yaz-moved-to-github.git] / test / test_mutex.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5
6 #include <stdlib.h>
7 #include <stdio.h>
8
9 #include <yaz/mutex.h>
10 #include <sys/time.h>
11 #include <yaz/test.h>
12 #include <yaz/log.h>
13
14 static void tst_mutex(void)
15 {
16     YAZ_MUTEX p = 0;
17
18     yaz_mutex_create(&p);
19     YAZ_CHECK(p);
20     yaz_mutex_enter(p);
21     yaz_mutex_leave(p);
22     yaz_mutex_destroy(&p);
23     YAZ_CHECK(p == 0);
24
25     yaz_mutex_create(&p);
26     YAZ_CHECK(p);
27     yaz_mutex_set_name(p, YLOG_LOG, "mymutex");
28     yaz_mutex_enter(p);
29     yaz_mutex_leave(p);
30     yaz_mutex_destroy(&p);
31     YAZ_CHECK(p == 0);
32
33     yaz_mutex_destroy(&p); /* OK to "destroy" NULL handle */
34 }
35
36 static void tst_cond(void)
37 {
38     YAZ_MUTEX p = 0;
39     YAZ_COND c;
40     struct timespec abstime;
41     struct timeval tval;
42     int r;
43
44     yaz_mutex_create(&p);
45     YAZ_CHECK(p);
46     if (!p)
47         return;
48
49     yaz_cond_create(&c);
50     YAZ_CHECK(c);
51     if (!c)
52         return;
53
54     r = gettimeofday(&tval, 0);
55     YAZ_CHECK_EQ(r, 0);
56     
57     abstime.tv_sec = tval.tv_sec + 1; /* wait 2 seconds */
58     abstime.tv_nsec = tval.tv_usec * 1000;
59     
60     r = yaz_cond_wait(c, p, &abstime);
61     YAZ_CHECK(r != 0);
62
63     yaz_cond_destroy(&c);
64     YAZ_CHECK(c == 0);
65     yaz_mutex_destroy(&p);
66     YAZ_CHECK(p == 0);
67 }
68
69 int main (int argc, char **argv)
70 {
71     YAZ_CHECK_INIT(argc, argv);
72     YAZ_CHECK_LOG();
73     tst_mutex();
74     tst_cond();
75     YAZ_CHECK_TERM;
76 }
77
78 /*
79  * Local variables:
80  * c-basic-offset: 4
81  * c-file-style: "Stroustrup"
82  * indent-tabs-mode: nil
83  * End:
84  * vim: shiftwidth=4 tabstop=8 expandtab
85  */
86