1
/* $Id: mutex.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
#if INCLUDE_MUTEX_TEST
26
//#define TRACE_(x) PJ_LOG(3,x)
29
/* Test witn non-recursive mutex. */
30
static int simple_mutex_test(pj_pool_t *pool)
35
PJ_LOG(3,("", "...testing simple mutex"));
38
TRACE_(("", "....create mutex"));
39
rc = pj_mutex_create( pool, "", PJ_MUTEX_SIMPLE, &mutex);
40
if (rc != PJ_SUCCESS) {
41
app_perror("...error: pj_mutex_create", rc);
45
/* Normal lock/unlock cycle. */
46
TRACE_(("", "....lock mutex"));
47
rc = pj_mutex_lock(mutex);
48
if (rc != PJ_SUCCESS) {
49
app_perror("...error: pj_mutex_lock", rc);
52
TRACE_(("", "....unlock mutex"));
53
rc = pj_mutex_unlock(mutex);
54
if (rc != PJ_SUCCESS) {
55
app_perror("...error: pj_mutex_unlock", rc);
60
TRACE_(("", "....lock mutex"));
61
rc = pj_mutex_lock(mutex);
62
if (rc != PJ_SUCCESS) return -40;
64
/* Try-lock should fail. It should not deadlocked. */
65
TRACE_(("", "....trylock mutex"));
66
rc = pj_mutex_trylock(mutex);
68
PJ_LOG(3,("", "...info: looks like simple mutex is recursive"));
70
/* Unlock and done. */
71
TRACE_(("", "....unlock mutex"));
72
rc = pj_mutex_unlock(mutex);
73
if (rc != PJ_SUCCESS) return -50;
75
TRACE_(("", "....destroy mutex"));
76
rc = pj_mutex_destroy(mutex);
77
if (rc != PJ_SUCCESS) return -60;
79
TRACE_(("", "....done"));
84
/* Test with recursive mutex. */
85
static int recursive_mutex_test(pj_pool_t *pool)
90
PJ_LOG(3,("", "...testing recursive mutex"));
93
TRACE_(("", "....create mutex"));
94
rc = pj_mutex_create( pool, "", PJ_MUTEX_RECURSE, &mutex);
95
if (rc != PJ_SUCCESS) {
96
app_perror("...error: pj_mutex_create", rc);
100
/* Normal lock/unlock cycle. */
101
TRACE_(("", "....lock mutex"));
102
rc = pj_mutex_lock(mutex);
103
if (rc != PJ_SUCCESS) {
104
app_perror("...error: pj_mutex_lock", rc);
107
TRACE_(("", "....unlock mutex"));
108
rc = pj_mutex_unlock(mutex);
109
if (rc != PJ_SUCCESS) {
110
app_perror("...error: pj_mutex_unlock", rc);
115
TRACE_(("", "....lock mutex"));
116
rc = pj_mutex_lock(mutex);
117
if (rc != PJ_SUCCESS) return -40;
119
/* Try-lock should NOT fail. . */
120
TRACE_(("", "....trylock mutex"));
121
rc = pj_mutex_trylock(mutex);
122
if (rc != PJ_SUCCESS) {
123
app_perror("...error: recursive mutex is not recursive!", rc);
127
/* Locking again should not fail. */
128
TRACE_(("", "....lock mutex"));
129
rc = pj_mutex_lock(mutex);
130
if (rc != PJ_SUCCESS) {
131
app_perror("...error: recursive mutex is not recursive!", rc);
135
/* Unlock several times and done. */
136
TRACE_(("", "....unlock mutex 3x"));
137
rc = pj_mutex_unlock(mutex);
138
if (rc != PJ_SUCCESS) return -50;
139
rc = pj_mutex_unlock(mutex);
140
if (rc != PJ_SUCCESS) return -51;
141
rc = pj_mutex_unlock(mutex);
142
if (rc != PJ_SUCCESS) return -52;
144
TRACE_(("", "....destroy mutex"));
145
rc = pj_mutex_destroy(mutex);
146
if (rc != PJ_SUCCESS) return -60;
148
TRACE_(("", "....done"));
153
static int semaphore_test(pj_pool_t *pool)
158
PJ_LOG(3,("", "...testing semaphore"));
160
status = pj_sem_create(pool, NULL, 0, 1, &sem);
161
if (status != PJ_SUCCESS) {
162
app_perror("...error: pj_sem_create()", status);
166
status = pj_sem_post(sem);
167
if (status != PJ_SUCCESS) {
168
app_perror("...error: pj_sem_post()", status);
173
status = pj_sem_trywait(sem);
174
if (status != PJ_SUCCESS) {
175
app_perror("...error: pj_sem_trywait()", status);
180
status = pj_sem_post(sem);
181
if (status != PJ_SUCCESS) {
182
app_perror("...error: pj_sem_post()", status);
187
status = pj_sem_wait(sem);
188
if (status != PJ_SUCCESS) {
189
app_perror("...error: pj_sem_wait()", status);
194
status = pj_sem_destroy(sem);
195
if (status != PJ_SUCCESS) {
196
app_perror("...error: pj_sem_destroy()", status);
202
#endif /* PJ_HAS_SEMAPHORE */
210
pool = pj_pool_create(mem, "", 4000, 4000, NULL);
212
rc = simple_mutex_test(pool);
216
rc = recursive_mutex_test(pool);
221
rc = semaphore_test(pool);
226
pj_pool_release(pool);
232
int dummy_mutex_test;