blob: 877712d7a973bd33b54c24c758d4a639ab24dcf2 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include "test.h"
20#include <pjlib.h>
21
22#if INCLUDE_MUTEX_TEST
23
24#undef TRACE_
25//#define TRACE_(x) PJ_LOG(3,x)
26#define TRACE_(x)
27
28/* Test witn non-recursive mutex. */
29static int simple_mutex_test(pj_pool_t *pool)
30{
31 pj_status_t rc;
32 pj_mutex_t *mutex;
33
34 PJ_LOG(3,("", "...testing simple mutex"));
35
36 /* Create mutex. */
37 TRACE_(("", "....create mutex"));
38 rc = pj_mutex_create( pool, "", PJ_MUTEX_SIMPLE, &mutex);
39 if (rc != PJ_SUCCESS) {
40 app_perror("...error: pj_mutex_create", rc);
41 return -10;
42 }
43
44 /* Normal lock/unlock cycle. */
45 TRACE_(("", "....lock mutex"));
46 rc = pj_mutex_lock(mutex);
47 if (rc != PJ_SUCCESS) {
48 app_perror("...error: pj_mutex_lock", rc);
49 return -20;
50 }
51 TRACE_(("", "....unlock mutex"));
52 rc = pj_mutex_unlock(mutex);
53 if (rc != PJ_SUCCESS) {
54 app_perror("...error: pj_mutex_unlock", rc);
55 return -30;
56 }
57
58 /* Lock again. */
59 TRACE_(("", "....lock mutex"));
60 rc = pj_mutex_lock(mutex);
61 if (rc != PJ_SUCCESS) return -40;
62
63 /* Try-lock should fail. It should not deadlocked. */
64 TRACE_(("", "....trylock mutex"));
65 rc = pj_mutex_trylock(mutex);
66 if (rc == PJ_SUCCESS)
67 PJ_LOG(3,("", "...info: looks like simple mutex is recursive"));
68
69 /* Unlock and done. */
70 TRACE_(("", "....unlock mutex"));
71 rc = pj_mutex_unlock(mutex);
72 if (rc != PJ_SUCCESS) return -50;
73
74 TRACE_(("", "....destroy mutex"));
75 rc = pj_mutex_destroy(mutex);
76 if (rc != PJ_SUCCESS) return -60;
77
78 TRACE_(("", "....done"));
79 return PJ_SUCCESS;
80}
81
82
83/* Test with recursive mutex. */
84static int recursive_mutex_test(pj_pool_t *pool)
85{
86 pj_status_t rc;
87 pj_mutex_t *mutex;
88
89 PJ_LOG(3,("", "...testing recursive mutex"));
90
91 /* Create mutex. */
92 TRACE_(("", "....create mutex"));
93 rc = pj_mutex_create( pool, "", PJ_MUTEX_RECURSE, &mutex);
94 if (rc != PJ_SUCCESS) {
95 app_perror("...error: pj_mutex_create", rc);
96 return -10;
97 }
98
99 /* Normal lock/unlock cycle. */
100 TRACE_(("", "....lock mutex"));
101 rc = pj_mutex_lock(mutex);
102 if (rc != PJ_SUCCESS) {
103 app_perror("...error: pj_mutex_lock", rc);
104 return -20;
105 }
106 TRACE_(("", "....unlock mutex"));
107 rc = pj_mutex_unlock(mutex);
108 if (rc != PJ_SUCCESS) {
109 app_perror("...error: pj_mutex_unlock", rc);
110 return -30;
111 }
112
113 /* Lock again. */
114 TRACE_(("", "....lock mutex"));
115 rc = pj_mutex_lock(mutex);
116 if (rc != PJ_SUCCESS) return -40;
117
118 /* Try-lock should NOT fail. . */
119 TRACE_(("", "....trylock mutex"));
120 rc = pj_mutex_trylock(mutex);
121 if (rc != PJ_SUCCESS) {
122 app_perror("...error: recursive mutex is not recursive!", rc);
123 return -40;
124 }
125
126 /* Locking again should not fail. */
127 TRACE_(("", "....lock mutex"));
128 rc = pj_mutex_lock(mutex);
129 if (rc != PJ_SUCCESS) {
130 app_perror("...error: recursive mutex is not recursive!", rc);
131 return -45;
132 }
133
134 /* Unlock several times and done. */
135 TRACE_(("", "....unlock mutex 3x"));
136 rc = pj_mutex_unlock(mutex);
137 if (rc != PJ_SUCCESS) return -50;
138 rc = pj_mutex_unlock(mutex);
139 if (rc != PJ_SUCCESS) return -51;
140 rc = pj_mutex_unlock(mutex);
141 if (rc != PJ_SUCCESS) return -52;
142
143 TRACE_(("", "....destroy mutex"));
144 rc = pj_mutex_destroy(mutex);
145 if (rc != PJ_SUCCESS) return -60;
146
147 TRACE_(("", "....done"));
148 return PJ_SUCCESS;
149}
150
151int mutex_test(void)
152{
153 pj_pool_t *pool;
154 int rc;
155
156 pool = pj_pool_create(mem, "", 4000, 4000, NULL);
157
158 rc = simple_mutex_test(pool);
159 if (rc != 0)
160 return rc;
161
162 rc = recursive_mutex_test(pool);
163 if (rc != 0)
164 return rc;
165
166 pj_pool_release(pool);
167
168 return 0;
169}
170
171#else
172int dummy_mutex_test;
173#endif
174