| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "test.h" |
| 20 |
#include <pjlib.h> |
| 21 |
|
| 22 |
#if INCLUDE_MUTEX_TEST |
| 23 |
|
| 24 |
#undef TRACE_ |
| 25 |
|
| 26 |
#define TRACE_(x) |
| 27 |
|
| 28 |
|
| 29 |
static 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 |
|
| 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 |
|
| 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 |
|
| 59 |
TRACE_(("", "....lock mutex")); |
| 60 |
rc = pj_mutex_lock(mutex); |
| 61 |
if (rc != PJ_SUCCESS) return -40; |
| 62 |
|
| 63 |
|
| 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 |
|
| 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 |
|
| 84 |
static 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 |
|
| 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 |
|
| 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 |
|
| 114 |
TRACE_(("", "....lock mutex")); |
| 115 |
rc = pj_mutex_lock(mutex); |
| 116 |
if (rc != PJ_SUCCESS) return -40; |
| 117 |
|
| 118 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 151 |
#if PJ_HAS_SEMAPHORE |
| 152 |
static 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 |
| 202 |
|
| 203 |
|
| 204 |
int 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 |
|
| 219 |
#if PJ_HAS_SEMAPHORE |
| 220 |
rc = semaphore_test(pool); |
| 221 |
if (rc != 0) |
| 222 |
return rc; |
| 223 |
#endif |
| 224 |
|
| 225 |
pj_pool_release(pool); |
| 226 |
|
| 227 |
return 0; |
| 228 |
} |
| 229 |
|
| 230 |
#else |
| 231 |
int dummy_mutex_test; |
| 232 |
#endif |
| 233 |
|