condvar9.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * File: condvar9.c
  3. *
  4. *
  5. * --------------------------------------------------------------------------
  6. *
  7. * Pthreads-win32 - POSIX Threads Library for Win32
  8. * Copyright(C) 1998 John E. Bossom
  9. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  10. *
  11. * Contact Email: [email protected]
  12. *
  13. * The current list of contributors is contained
  14. * in the file CONTRIBUTORS included with the source
  15. * code distribution. The list can also be seen at the
  16. * following World Wide Web location:
  17. * http://sources.redhat.com/pthreads-win32/contributors.html
  18. *
  19. * This library is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU Lesser General Public
  21. * License as published by the Free Software Foundation; either
  22. * version 2 of the License, or (at your option) any later version.
  23. *
  24. * This library is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public
  30. * License along with this library in the file COPYING.LIB;
  31. * if not, write to the Free Software Foundation, Inc.,
  32. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  33. *
  34. * --------------------------------------------------------------------------
  35. *
  36. * Test Synopsis:
  37. * - Test multiple pthread_cond_broadcasts with thread cancelation.
  38. *
  39. * Test Method (Validation or Falsification):
  40. * - Validation
  41. *
  42. * Requirements Tested:
  43. * -
  44. *
  45. * Features Tested:
  46. * -
  47. *
  48. * Cases Tested:
  49. * -
  50. *
  51. * Description:
  52. * - Make NUMTHREADS threads wait on CV, cancel one, broadcast signal them,
  53. * and then repeat.
  54. *
  55. * Environment:
  56. * -
  57. *
  58. * Input:
  59. * - None.
  60. *
  61. * Output:
  62. * - File name, Line number, and failed expression on failure.
  63. * - No output on success.
  64. *
  65. * Assumptions:
  66. * -
  67. *
  68. * Pass Criteria:
  69. * - Process returns zero exit status.
  70. *
  71. * Fail Criteria:
  72. * - Process returns non-zero exit status.
  73. */
  74. #include "test.h"
  75. #include <sys/timeb.h>
  76. /*
  77. * Create NUMTHREADS threads in addition to the Main thread.
  78. */
  79. enum {
  80. NUMTHREADS = 9
  81. };
  82. typedef struct bag_t_ bag_t;
  83. struct bag_t_ {
  84. int threadnum;
  85. int started;
  86. int finished;
  87. /* Add more per-thread state variables here */
  88. };
  89. static bag_t threadbag[NUMTHREADS + 1];
  90. typedef struct cvthing_t_ cvthing_t;
  91. struct cvthing_t_ {
  92. pthread_cond_t notbusy;
  93. pthread_mutex_t lock;
  94. int shared;
  95. };
  96. static cvthing_t cvthing = {
  97. PTHREAD_COND_INITIALIZER,
  98. PTHREAD_MUTEX_INITIALIZER,
  99. 0
  100. };
  101. static pthread_mutex_t start_flag = PTHREAD_MUTEX_INITIALIZER;
  102. static struct timespec abstime = { 0, 0 };
  103. static int awoken;
  104. static void *
  105. mythread(void * arg)
  106. {
  107. bag_t * bag = (bag_t *) arg;
  108. assert(bag == &threadbag[bag->threadnum]);
  109. assert(bag->started == 0);
  110. bag->started = 1;
  111. /* Wait for the start gun */
  112. assert(pthread_mutex_lock(&start_flag) == 0);
  113. assert(pthread_mutex_unlock(&start_flag) == 0);
  114. assert(pthread_mutex_lock(&cvthing.lock) == 0);
  115. /*
  116. * pthread_cond_timedwait is a cancelation point and we're
  117. * going to cancel some threads deliberately.
  118. */
  119. #ifdef _MSC_VER
  120. #pragma inline_depth(0)
  121. #endif
  122. pthread_cleanup_push(pthread_mutex_unlock, (void *) &cvthing.lock);
  123. while (! (cvthing.shared > 0))
  124. assert(pthread_cond_timedwait(&cvthing.notbusy, &cvthing.lock, &abstime) == 0);
  125. pthread_cleanup_pop(0);
  126. #ifdef _MSC_VER
  127. #pragma inline_depth()
  128. #endif
  129. assert(cvthing.shared > 0);
  130. awoken++;
  131. bag->finished = 1;
  132. assert(pthread_mutex_unlock(&cvthing.lock) == 0);
  133. return (void *) 0;
  134. }
  135. int
  136. main()
  137. {
  138. int failed = 0;
  139. int i;
  140. int first, last;
  141. int canceledThreads = 0;
  142. pthread_t t[NUMTHREADS + 1];
  143. PTW32_STRUCT_TIMEB currSysTime;
  144. const DWORD NANOSEC_PER_MILLISEC = 1000000;
  145. assert((t[0] = pthread_self()).p != NULL);
  146. assert(cvthing.notbusy == PTHREAD_COND_INITIALIZER);
  147. assert(cvthing.lock == PTHREAD_MUTEX_INITIALIZER);
  148. PTW32_FTIME(&currSysTime);
  149. abstime.tv_sec = (long)currSysTime.time;
  150. abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
  151. abstime.tv_sec += 5;
  152. assert((t[0] = pthread_self()).p != NULL);
  153. awoken = 0;
  154. for (first = 1, last = NUMTHREADS / 2;
  155. first < NUMTHREADS;
  156. first = last + 1, last = NUMTHREADS)
  157. {
  158. int ct;
  159. assert(pthread_mutex_lock(&start_flag) == 0);
  160. for (i = first; i <= last; i++)
  161. {
  162. threadbag[i].started = threadbag[i].finished = 0;
  163. threadbag[i].threadnum = i;
  164. assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0);
  165. }
  166. /*
  167. * Code to control or munipulate child threads should probably go here.
  168. */
  169. cvthing.shared = 0;
  170. assert(pthread_mutex_unlock(&start_flag) == 0);
  171. /*
  172. * Give threads time to start.
  173. */
  174. Sleep(1000);
  175. ct = (first + last) / 2;
  176. assert(pthread_cancel(t[ct]) == 0);
  177. canceledThreads++;
  178. assert(pthread_join(t[ct], NULL) == 0);
  179. assert(pthread_mutex_lock(&cvthing.lock) == 0);
  180. cvthing.shared++;
  181. assert(pthread_mutex_unlock(&cvthing.lock) == 0);
  182. assert(pthread_cond_broadcast(&cvthing.notbusy) == 0);
  183. /*
  184. * Standard check that all threads started - and wait for them to finish.
  185. */
  186. for (i = first; i <= last; i++)
  187. {
  188. failed = !threadbag[i].started;
  189. if (failed)
  190. {
  191. fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started);
  192. }
  193. else
  194. {
  195. assert(pthread_join(t[i], NULL) == 0 || threadbag[i].finished == 0);
  196. // fprintf(stderr, "Thread %d: finished %d\n", i, threadbag[i].finished);
  197. }
  198. }
  199. }
  200. /*
  201. * Cleanup the CV.
  202. */
  203. assert(pthread_mutex_destroy(&cvthing.lock) == 0);
  204. assert(cvthing.lock == NULL);
  205. assert_e(pthread_cond_destroy(&cvthing.notbusy), ==, 0);
  206. assert(cvthing.notbusy == NULL);
  207. assert(!failed);
  208. /*
  209. * Check any results here.
  210. */
  211. assert(awoken == NUMTHREADS - canceledThreads);
  212. /*
  213. * Success.
  214. */
  215. return 0;
  216. }