blob: 0c5076099cb60e17d2e8abdc0d117598345b52d2 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00004 *
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
Benny Prijono22668612008-08-15 14:53:18 +0000151#if PJ_HAS_SEMAPHORE
152static int semaphore_test(pj_pool_t *pool)
153{
154 pj_sem_t *sem;
155 pj_status_t status;
156
157 PJ_LOG(3,("", "...testing semaphore"));
158
159 status = pj_sem_create(pool, NULL, 0, 1, &sem);
160 if (status != PJ_SUCCESS) {
161 app_perror("...error: pj_sem_create()", status);
162 return -151;
163 }
164
165 status = pj_sem_post(sem);
166 if (status != PJ_SUCCESS) {
167 app_perror("...error: pj_sem_post()", status);
168 pj_sem_destroy(sem);
169 return -153;
170 }
171
172 status = pj_sem_trywait(sem);
173 if (status != PJ_SUCCESS) {
174 app_perror("...error: pj_sem_trywait()", status);
175 pj_sem_destroy(sem);
176 return -156;
177 }
178
179 status = pj_sem_post(sem);
180 if (status != PJ_SUCCESS) {
181 app_perror("...error: pj_sem_post()", status);
182 pj_sem_destroy(sem);
183 return -159;
184 }
185
186 status = pj_sem_wait(sem);
187 if (status != PJ_SUCCESS) {
188 app_perror("...error: pj_sem_wait()", status);
189 pj_sem_destroy(sem);
190 return -161;
191 }
192
193 status = pj_sem_destroy(sem);
194 if (status != PJ_SUCCESS) {
195 app_perror("...error: pj_sem_destroy()", status);
196 return -163;
197 }
198
199 return 0;
200}
201#endif /* PJ_HAS_SEMAPHORE */
202
203
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000204int mutex_test(void)
205{
206 pj_pool_t *pool;
207 int rc;
208
209 pool = pj_pool_create(mem, "", 4000, 4000, NULL);
210
211 rc = simple_mutex_test(pool);
212 if (rc != 0)
213 return rc;
214
215 rc = recursive_mutex_test(pool);
216 if (rc != 0)
217 return rc;
218
Benny Prijono22668612008-08-15 14:53:18 +0000219#if PJ_HAS_SEMAPHORE
220 rc = semaphore_test(pool);
221 if (rc != 0)
222 return rc;
223#endif
224
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000225 pj_pool_release(pool);
226
227 return 0;
228}
229
230#else
231int dummy_mutex_test;
232#endif
233