TCPD libs only used in libyaz's LIBADD
[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 #if HAVE_SYS_TIME_H
11 #include <sys/time.h>
12 #endif
13 #ifdef WIN32
14 #include <windows.h>
15 #endif
16
17 #include <yaz/test.h>
18 #include <yaz/log.h>
19 #include <yaz/gettimeofday.h>
20
21 static void tst_mutex(void)
22 {
23     YAZ_MUTEX p = 0;
24
25     yaz_mutex_create(&p);
26     YAZ_CHECK(p);
27     yaz_mutex_enter(p);
28     yaz_mutex_leave(p);
29     yaz_mutex_destroy(&p);
30     YAZ_CHECK(p == 0);
31
32     yaz_mutex_create(&p);
33     YAZ_CHECK(p);
34     yaz_mutex_set_name(p, YLOG_LOG, "mymutex");
35     yaz_mutex_enter(p);
36     yaz_mutex_leave(p);
37     yaz_mutex_destroy(&p);
38     YAZ_CHECK(p == 0);
39
40     yaz_mutex_destroy(&p); /* OK to "destroy" NULL handle */
41 }
42
43 static void tst_cond(void)
44 {
45     YAZ_MUTEX p = 0;
46     YAZ_COND c;
47     struct timeval abstime;
48     int r;
49
50     yaz_mutex_create(&p);
51     YAZ_CHECK(p);
52     if (!p)
53         return;
54
55     yaz_cond_create(&c);
56     YAZ_CHECK(c);
57     if (!c)
58         return;
59
60     r = yaz_gettimeofday(&abstime);
61     YAZ_CHECK_EQ(r, 0);
62     
63     abstime.tv_sec += 1; /* wait 1 second */
64     
65     r = yaz_cond_wait(c, p, &abstime);
66     YAZ_CHECK(r != 0);
67
68     yaz_cond_destroy(&c);
69     YAZ_CHECK(c == 0);
70     yaz_mutex_destroy(&p);
71     YAZ_CHECK(p == 0);
72 }
73
74 int main (int argc, char **argv)
75 {
76     YAZ_CHECK_INIT(argc, argv);
77     YAZ_CHECK_LOG();
78     tst_mutex();
79     tst_cond();
80     YAZ_CHECK_TERM;
81 }
82
83 /*
84  * Local variables:
85  * c-basic-offset: 4
86  * c-file-style: "Stroustrup"
87  * indent-tabs-mode: nil
88  * End:
89  * vim: shiftwidth=4 tabstop=8 expandtab
90  */
91