mutex.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 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. #if INCLUDE_MUTEX_TEST
  22. #undef TRACE_
  23. //#define TRACE_(x) PJ_LOG(3,x)
  24. #define TRACE_(x)
  25. /* Test witn non-recursive mutex. */
  26. static int simple_mutex_test(pj_pool_t *pool)
  27. {
  28. pj_status_t rc;
  29. pj_mutex_t *mutex;
  30. PJ_LOG(3,("", "...testing simple mutex"));
  31. /* Create mutex. */
  32. TRACE_(("", "....create mutex"));
  33. rc = pj_mutex_create( pool, "", PJ_MUTEX_SIMPLE, &mutex);
  34. if (rc != PJ_SUCCESS) {
  35. app_perror("...error: pj_mutex_create", rc);
  36. return -10;
  37. }
  38. /* Normal lock/unlock cycle. */
  39. TRACE_(("", "....lock mutex"));
  40. rc = pj_mutex_lock(mutex);
  41. if (rc != PJ_SUCCESS) {
  42. app_perror("...error: pj_mutex_lock", rc);
  43. return -20;
  44. }
  45. TRACE_(("", "....unlock mutex"));
  46. rc = pj_mutex_unlock(mutex);
  47. if (rc != PJ_SUCCESS) {
  48. app_perror("...error: pj_mutex_unlock", rc);
  49. return -30;
  50. }
  51. /* Lock again. */
  52. TRACE_(("", "....lock mutex"));
  53. rc = pj_mutex_lock(mutex);
  54. if (rc != PJ_SUCCESS) return -40;
  55. /* Try-lock should fail. It should not deadlocked. */
  56. TRACE_(("", "....trylock mutex"));
  57. rc = pj_mutex_trylock(mutex);
  58. if (rc == PJ_SUCCESS)
  59. PJ_LOG(3,("", "...info: looks like simple mutex is recursive"));
  60. /* Unlock and done. */
  61. TRACE_(("", "....unlock mutex"));
  62. rc = pj_mutex_unlock(mutex);
  63. if (rc != PJ_SUCCESS) return -50;
  64. TRACE_(("", "....destroy mutex"));
  65. rc = pj_mutex_destroy(mutex);
  66. if (rc != PJ_SUCCESS) return -60;
  67. TRACE_(("", "....done"));
  68. return PJ_SUCCESS;
  69. }
  70. /* Test with recursive mutex. */
  71. static int recursive_mutex_test(pj_pool_t *pool)
  72. {
  73. pj_status_t rc;
  74. pj_mutex_t *mutex;
  75. PJ_LOG(3,("", "...testing recursive mutex"));
  76. /* Create mutex. */
  77. TRACE_(("", "....create mutex"));
  78. rc = pj_mutex_create( pool, "", PJ_MUTEX_RECURSE, &mutex);
  79. if (rc != PJ_SUCCESS) {
  80. app_perror("...error: pj_mutex_create", rc);
  81. return -10;
  82. }
  83. /* Normal lock/unlock cycle. */
  84. TRACE_(("", "....lock mutex"));
  85. rc = pj_mutex_lock(mutex);
  86. if (rc != PJ_SUCCESS) {
  87. app_perror("...error: pj_mutex_lock", rc);
  88. return -20;
  89. }
  90. TRACE_(("", "....unlock mutex"));
  91. rc = pj_mutex_unlock(mutex);
  92. if (rc != PJ_SUCCESS) {
  93. app_perror("...error: pj_mutex_unlock", rc);
  94. return -30;
  95. }
  96. /* Lock again. */
  97. TRACE_(("", "....lock mutex"));
  98. rc = pj_mutex_lock(mutex);
  99. if (rc != PJ_SUCCESS) return -40;
  100. /* Try-lock should NOT fail. . */
  101. TRACE_(("", "....trylock mutex"));
  102. rc = pj_mutex_trylock(mutex);
  103. if (rc != PJ_SUCCESS) {
  104. app_perror("...error: recursive mutex is not recursive!", rc);
  105. return -40;
  106. }
  107. /* Locking again should not fail. */
  108. TRACE_(("", "....lock mutex"));
  109. rc = pj_mutex_lock(mutex);
  110. if (rc != PJ_SUCCESS) {
  111. app_perror("...error: recursive mutex is not recursive!", rc);
  112. return -45;
  113. }
  114. /* Unlock several times and done. */
  115. TRACE_(("", "....unlock mutex 3x"));
  116. rc = pj_mutex_unlock(mutex);
  117. if (rc != PJ_SUCCESS) return -50;
  118. rc = pj_mutex_unlock(mutex);
  119. if (rc != PJ_SUCCESS) return -51;
  120. rc = pj_mutex_unlock(mutex);
  121. if (rc != PJ_SUCCESS) return -52;
  122. TRACE_(("", "....destroy mutex"));
  123. rc = pj_mutex_destroy(mutex);
  124. if (rc != PJ_SUCCESS) return -60;
  125. TRACE_(("", "....done"));
  126. return PJ_SUCCESS;
  127. }
  128. #if PJ_HAS_SEMAPHORE
  129. static int semaphore_test(pj_pool_t *pool)
  130. {
  131. pj_sem_t *sem;
  132. pj_status_t status;
  133. PJ_LOG(3,("", "...testing semaphore"));
  134. status = pj_sem_create(pool, NULL, 0, 1, &sem);
  135. if (status != PJ_SUCCESS) {
  136. app_perror("...error: pj_sem_create()", status);
  137. return -151;
  138. }
  139. status = pj_sem_post(sem);
  140. if (status != PJ_SUCCESS) {
  141. app_perror("...error: pj_sem_post()", status);
  142. pj_sem_destroy(sem);
  143. return -153;
  144. }
  145. status = pj_sem_trywait(sem);
  146. if (status != PJ_SUCCESS) {
  147. app_perror("...error: pj_sem_trywait()", status);
  148. pj_sem_destroy(sem);
  149. return -156;
  150. }
  151. status = pj_sem_post(sem);
  152. if (status != PJ_SUCCESS) {
  153. app_perror("...error: pj_sem_post()", status);
  154. pj_sem_destroy(sem);
  155. return -159;
  156. }
  157. status = pj_sem_wait(sem);
  158. if (status != PJ_SUCCESS) {
  159. app_perror("...error: pj_sem_wait()", status);
  160. pj_sem_destroy(sem);
  161. return -161;
  162. }
  163. status = pj_sem_destroy(sem);
  164. if (status != PJ_SUCCESS) {
  165. app_perror("...error: pj_sem_destroy()", status);
  166. return -163;
  167. }
  168. return 0;
  169. }
  170. #endif /* PJ_HAS_SEMAPHORE */
  171. int mutex_test(void)
  172. {
  173. pj_pool_t *pool;
  174. int rc;
  175. pool = pj_pool_create(mem, "", 4000, 4000, NULL);
  176. rc = simple_mutex_test(pool);
  177. if (rc != 0)
  178. return rc;
  179. rc = recursive_mutex_test(pool);
  180. if (rc != 0)
  181. return rc;
  182. #if PJ_HAS_SEMAPHORE
  183. rc = semaphore_test(pool);
  184. if (rc != 0)
  185. return rc;
  186. #endif
  187. pj_pool_release(pool);
  188. return 0;
  189. }
  190. #else
  191. int dummy_mutex_test;
  192. #endif