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 <freetds/utils/test_base.h>
21 :
22 : #include <stdio.h>
23 :
24 : #if HAVE_STDLIB_H
25 : #include <stdlib.h>
26 : #endif /* HAVE_STDLIB_H */
27 :
28 : #include <freetds/thread.h>
29 : #include <freetds/macros.h>
30 :
31 : #if !defined(TDS_NO_THREADSAFE)
32 :
33 : static tds_mutex mtx = TDS_MUTEX_INITIALIZER;
34 :
35 : static
36 30 : TDS_THREAD_PROC_DECLARE(trylock_proc, arg)
37 : {
38 30 : tds_mutex *mtx = (tds_mutex *) arg;
39 :
40 30 : if (!tds_mutex_trylock(mtx)) {
41 : /* got mutex, failure as should be locked */
42 : return TDS_THREAD_RESULT(1);
43 : }
44 : /* success */
45 30 : return TDS_THREAD_RESULT(0);
46 : }
47 :
48 : static void
49 30 : test(tds_mutex *mtx)
50 : {
51 : tds_thread th;
52 : void *res;
53 :
54 30 : if (tds_mutex_trylock(mtx)) {
55 0 : fprintf(stderr, "mutex should be unlocked\n");
56 0 : exit(1);
57 : }
58 : /* check mutex are not recursive, even on Windows */
59 30 : if (!tds_mutex_trylock(mtx)) {
60 0 : fprintf(stderr, "mutex should be locked\n");
61 0 : exit(1);
62 : }
63 :
64 30 : if (tds_thread_create(&th, trylock_proc, mtx) != 0) {
65 0 : fprintf(stderr, "error creating thread\n");
66 0 : exit(1);
67 : }
68 :
69 60 : if (tds_thread_join(th, &res) != 0) {
70 0 : fprintf(stderr, "error waiting thread\n");
71 0 : exit(1);
72 : }
73 :
74 30 : if (TDS_PTR2INT(res) != 0) {
75 0 : fprintf(stderr, "mutex should be locked inside thread\n");
76 0 : exit(1);
77 : }
78 :
79 30 : tds_mutex_unlock(mtx);
80 :
81 : /* now trylock after the unlock should succeed */
82 30 : if (tds_mutex_trylock(mtx)) {
83 0 : fprintf(stderr, "mutex should be unlocked\n");
84 0 : exit(1);
85 : }
86 :
87 30 : tds_mutex_unlock(mtx);
88 30 : }
89 :
90 10 : TEST_MAIN()
91 : {
92 : tds_mutex local;
93 :
94 10 : test(&mtx);
95 :
96 : /* try allocating it */
97 10 : if (tds_mutex_init(&local)) {
98 0 : fprintf(stderr, "error creating mutex\n");
99 0 : exit(1);
100 : }
101 10 : test(&local);
102 10 : tds_mutex_free(&local);
103 :
104 : /* try again */
105 10 : if (tds_mutex_init(&local)) {
106 0 : fprintf(stderr, "error creating mutex\n");
107 0 : exit(1);
108 : }
109 10 : test(&local);
110 10 : tds_mutex_free(&local);
111 :
112 10 : return 0;
113 : }
114 :
115 : #else
116 :
117 : TEST_MAIN()
118 : {
119 : return 0;
120 : }
121 :
122 : #endif
|