Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 2012 Frediano Ziglio
3 : *
4 : * This library is free software; you can redistribute it and/or
5 : * modify it under the terms of the GNU Library General Public
6 : * License as published by the Free Software Foundation; either
7 : * version 2 of the License, or (at your option) any later version.
8 : *
9 : * This library is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : * Library General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU Library General Public
15 : * License along with this library; if not, write to the
16 : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 : * Boston, MA 02111-1307, USA.
18 : */
19 :
20 : #include <config.h>
21 :
22 : #include <stdio.h>
23 :
24 : #if HAVE_STDLIB_H
25 : #include <stdlib.h>
26 : #endif /* HAVE_STDLIB_H */
27 :
28 : #ifdef HAVE_UNISTD_H
29 : #include <unistd.h>
30 : #endif
31 :
32 : #include <freetds/thread.h>
33 : #include <freetds/utils.h>
34 : #include <freetds/macros.h>
35 : #include <freetds/replacements.h>
36 :
37 : #if !defined(TDS_NO_THREADSAFE)
38 :
39 : static tds_mutex mtx = TDS_MUTEX_INITIALIZER;
40 :
41 32 : static TDS_THREAD_PROC_DECLARE(signal_proc, arg)
42 : {
43 32 : tds_condition *cond = (tds_condition *) arg;
44 :
45 : /* success */
46 32 : int res = 0;
47 :
48 32 : tds_mutex_lock(&mtx);
49 32 : if (tds_cond_signal(cond)) {
50 : /* failure */
51 0 : res = 1;
52 : }
53 32 : tds_mutex_unlock(&mtx);
54 32 : return TDS_THREAD_RESULT(res);
55 : }
56 :
57 160 : static void check(int cond, const char *msg)
58 : {
59 160 : if (cond) {
60 0 : fprintf(stderr, "%s\n", msg);
61 0 : exit(1);
62 : }
63 160 : }
64 :
65 8 : int main(void)
66 : {
67 : tds_condition cond;
68 : tds_thread th;
69 : void *res;
70 :
71 8 : check(tds_cond_init(&cond), "failed initializing condition");
72 :
73 8 : tds_mutex_lock(&mtx);
74 :
75 8 : check(tds_thread_create(&th, signal_proc, &cond) != 0, "error creating thread");
76 :
77 8 : tds_sleep_ms(100);
78 :
79 8 : check(tds_cond_wait(&cond, &mtx), "failed waiting condition");
80 :
81 8 : res = &th;
82 16 : check(tds_thread_join(th, &res) != 0, "error waiting thread");
83 :
84 8 : check(TDS_PTR2INT(res) != 0, "error signaling condition");
85 :
86 : /* under Windows mutex are recursive */
87 : #ifndef _WIN32
88 8 : check(tds_mutex_trylock(&mtx) == 0, "mutex should be locked");
89 : #endif
90 :
91 : /* check timed version */
92 :
93 8 : check(tds_cond_timedwait(&cond, &mtx, 1) != ETIMEDOUT, "should not succeed to wait condition");
94 :
95 8 : check(tds_thread_create(&th, signal_proc, &cond) != 0, "error creating thread");
96 :
97 8 : check(tds_cond_timedwait(&cond, &mtx, 1), "error on timed waiting condition");
98 :
99 8 : res = &th; /* just to avoid NULL */
100 16 : check(tds_thread_join(th, &res) != 0, "error waiting thread");
101 :
102 8 : check(TDS_PTR2INT(res) != 0, "error signaling condition");
103 :
104 8 : check(tds_thread_create(&th, signal_proc, &cond) != 0, "error creating thread");
105 :
106 8 : check(tds_cond_timedwait(&cond, &mtx, -1), "error on timed waiting condition");
107 :
108 8 : res = &th; /* just to avoid NULL */
109 16 : check(tds_thread_join(th, &res) != 0, "error waiting thread");
110 :
111 8 : check(TDS_PTR2INT(res) != 0, "error signaling condition");
112 :
113 8 : check(tds_thread_create(&th, signal_proc, &cond) != 0, "error creating thread");
114 :
115 8 : check(tds_cond_timedwait(&cond, &mtx, 0), "error on timed waiting condition");
116 :
117 8 : res = &th; /* just to avoid NULL */
118 16 : check(tds_thread_join(th, &res) != 0, "error waiting thread");
119 :
120 8 : check(TDS_PTR2INT(res) != 0, "error signaling condition");
121 :
122 8 : tds_mutex_unlock(&mtx);
123 :
124 8 : check(tds_cond_destroy(&cond), "failed destroying condition");
125 : return 0;
126 : }
127 :
128 : #else
129 :
130 : int main(void)
131 : {
132 : return 0;
133 : }
134 :
135 : #endif
136 :
|