test_pthread_mutex.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:5k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* ==== test_pthread_cond.c =========================================
  2.  * Copyright (c) 1993 by Chris Provenzano, proven@athena.mit.edu
  3.  *
  4.  * Description : Test pthread_cond(). Run this after test_create()
  5.  *
  6.  *  1.23 94/05/04 proven
  7.  *      -Started coding this file.
  8.  */
  9. #include <errno.h>
  10. #include <pthread.h>
  11. #include <stdio.h>
  12. #define OK 0
  13. #define NOTOK -1
  14. int contention_variable;
  15. void * thread_contention(void * arg)
  16. {
  17. pthread_mutex_t * mutex = arg;
  18. if (pthread_mutex_lock(mutex)) {
  19. printf("pthread_mutex_lock() ERRORn");
  20. pthread_exit(NULL);
  21. }
  22. if (contention_variable != 1) {
  23. printf("contention_variable != 1 ERRORn");
  24. pthread_exit(NULL);
  25. }
  26. contention_variable = 2;
  27. if (pthread_mutex_unlock(mutex)) {
  28. printf("pthread_mutex_unlock() ERRORn");
  29. pthread_exit(NULL);
  30. }
  31. pthread_exit(NULL);
  32. }
  33. int test_contention_lock(pthread_mutex_t * mutex)
  34. {
  35. pthread_t thread;
  36. printf("test_contention_lock()n");
  37. if (pthread_mutex_lock(mutex)) {
  38. printf("pthread_mutex_lock() ERRORn");
  39. return(NOTOK);
  40. }
  41. contention_variable = 0;
  42. if (pthread_create(&thread, NULL, thread_contention, mutex)) {
  43. printf("pthread_create() FAILEDn");
  44. exit(2);
  45. }
  46. pthread_yield();
  47. contention_variable = 1;
  48. if (pthread_mutex_unlock(mutex)) {
  49. printf("pthread_mutex_unlock() ERRORn");
  50. return(NOTOK);
  51. }
  52. if (pthread_mutex_lock(mutex)) {
  53. printf("pthread_mutex_lock() ERRORn");
  54. return(NOTOK);
  55. }
  56. if (contention_variable != 2) {
  57. printf("contention_variable != 2 ERRORn");
  58. return(NOTOK);
  59. }
  60. if (pthread_mutex_unlock(mutex)) {
  61. printf("pthread_mutex_unlock() ERRORn");
  62. return(NOTOK);
  63. }
  64. return(OK);
  65. }
  66. int test_nocontention_lock(pthread_mutex_t * mutex)
  67. {
  68. printf("test_nocontention_lock()n");
  69. if (pthread_mutex_lock(mutex)) {
  70. printf("pthread_mutex_lock() ERRORn");
  71. return(NOTOK);
  72. }
  73. if (pthread_mutex_unlock(mutex)) {
  74. printf("pthread_mutex_unlock() ERRORn");
  75. return(NOTOK);
  76. }
  77. return(OK);
  78. }
  79. int test_debug_double_lock(pthread_mutex_t * mutex)
  80. {
  81. printf("test_debug_double_lock()n");
  82. if (pthread_mutex_lock(mutex)) {
  83. printf("pthread_mutex_lock() ERRORn");
  84. return(NOTOK);
  85. }
  86. if (pthread_mutex_lock(mutex) != EDEADLK) {
  87. printf("double lock error not detected ERRORn");
  88. return(NOTOK);
  89. }
  90. if (pthread_mutex_unlock(mutex)) {
  91. printf("pthread_mutex_unlock() ERRORn");
  92. return(NOTOK);
  93. }
  94. return(OK);
  95. }
  96. int test_debug_double_unlock(pthread_mutex_t * mutex)
  97. {
  98. printf("test_debug_double_unlock()n");
  99. if (pthread_mutex_lock(mutex)) {
  100. printf("pthread_mutex_lock() ERRORn");
  101. return(NOTOK);
  102. }
  103. if (pthread_mutex_unlock(mutex)) {
  104. printf("pthread_mutex_unlock() ERRORn");
  105. return(NOTOK);
  106. }
  107. if (pthread_mutex_unlock(mutex) != EPERM) {
  108. printf("double unlock error not detected ERRORn");
  109. return(NOTOK);
  110. }
  111. return(OK);
  112. }
  113. int test_nocontention_trylock(pthread_mutex_t * mutex)
  114. {
  115. printf("test_nocontention_trylock()n");
  116. if (pthread_mutex_trylock(mutex)) {
  117. printf("pthread_mutex_trylock() ERRORn");
  118. return(NOTOK);
  119. }
  120. if (pthread_mutex_unlock(mutex)) {
  121. printf("pthread_mutex_unlock() ERRORn");
  122. return(NOTOK);
  123. }
  124. return(OK);
  125. }
  126. int test_mutex_static(void)
  127. {
  128. pthread_mutex_t mutex_static = PTHREAD_MUTEX_INITIALIZER;
  129. printf("test_mutex_static()n");
  130. if (test_nocontention_lock(&mutex_static) ||
  131.   test_contention_lock(&mutex_static)) {
  132. return(NOTOK);
  133. }
  134. return(OK);
  135. }
  136. int test_mutex_fast(void)
  137. {
  138. pthread_mutex_t mutex_fast; 
  139. printf("test_mutex_fast()n");
  140. if (pthread_mutex_init(&mutex_fast, NULL)) {
  141. printf("pthread_mutex_init() ERRORn");
  142. return(NOTOK);
  143. }
  144. if (test_nocontention_lock(&mutex_fast) ||
  145.   test_contention_lock(&mutex_fast)) {
  146. return(NOTOK);
  147. }
  148. if (pthread_mutex_destroy(&mutex_fast)) {
  149. printf("pthread_mutex_destroy() ERRORn");
  150. return(NOTOK);
  151. }
  152. return(OK);
  153. }
  154. int test_mutex_debug()
  155. {
  156. pthread_mutexattr_t mutex_debug_attr; 
  157. pthread_mutex_t mutex_debug; 
  158. printf("test_mutex_debug()n");
  159. pthread_mutexattr_init(&mutex_debug_attr);
  160. pthread_mutexattr_settype(&mutex_debug_attr, PTHREAD_MUTEXTYPE_DEBUG);
  161. if (pthread_mutex_init(&mutex_debug, &mutex_debug_attr)) {
  162. printf("pthread_mutex_init() ERRORn");
  163. return(NOTOK);
  164. }
  165. if (test_nocontention_lock(&mutex_debug) ||
  166.   test_contention_lock(&mutex_debug) ||
  167.   test_debug_double_lock(&mutex_debug) ||
  168.   test_debug_double_unlock(&mutex_debug)) {
  169. return(NOTOK);
  170. }
  171. if (pthread_mutex_destroy(&mutex_debug)) {
  172. printf("pthread_mutex_destroy() ERRORn");
  173. return(NOTOK);
  174. }
  175. return(OK);
  176. }
  177. main()
  178. {
  179. pthread_init();
  180. printf("test_pthread_mutex STARTn");
  181. if (test_mutex_static() || test_mutex_fast() || test_mutex_debug()) { 
  182. printf("test_pthread_mutex FAILEDn");
  183. exit(1);
  184. }
  185. printf("test_pthread_mutex PASSEDn");
  186. exit(0);
  187. }