condvar7.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * File: condvar7.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 pthread_cond_broadcast 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. * - Test broadcast with NUMTHREADS (=5) waiting CVs, one is canceled while waiting.
  53. *
  54. * Environment:
  55. * -
  56. *
  57. * Input:
  58. * - None.
  59. *
  60. * Output:
  61. * - File name, Line number, and failed expression on failure.
  62. * - No output on success.
  63. *
  64. * Assumptions:
  65. * -
  66. *
  67. * Pass Criteria:
  68. * - Process returns zero exit status.
  69. *
  70. * Fail Criteria:
  71. * - Process returns non-zero exit status.
  72. */
  73. #include "test.h"
  74. #include <sys/timeb.h>
  75. /*
  76. * Create NUMTHREADS threads in addition to the Main thread.
  77. */
  78. enum {
  79. NUMTHREADS = 5
  80. };
  81. typedef struct bag_t_ bag_t;
  82. struct bag_t_ {
  83. int threadnum;
  84. int started;
  85. /* Add more per-thread state variables here */
  86. };
  87. static bag_t threadbag[NUMTHREADS + 1];
  88. typedef struct cvthing_t_ cvthing_t;
  89. struct cvthing_t_ {
  90. pthread_cond_t notbusy;
  91. pthread_mutex_t lock;
  92. int shared;
  93. };
  94. static cvthing_t cvthing = {
  95. PTHREAD_COND_INITIALIZER,
  96. PTHREAD_MUTEX_INITIALIZER,
  97. 0
  98. };
  99. static pthread_mutex_t start_flag = PTHREAD_MUTEX_INITIALIZER;
  100. static struct timespec abstime = { 0, 0 };
  101. static int awoken;
  102. void *
  103. mythread(void * arg)
  104. {
  105. bag_t * bag = (bag_t *) arg;
  106. assert(bag == &threadbag[bag->threadnum]);
  107. assert(bag->started == 0);
  108. bag->started = 1;
  109. /* Wait for the start gun */
  110. assert(pthread_mutex_lock(&start_flag) == 0);
  111. assert(pthread_mutex_unlock(&start_flag) == 0);
  112. assert(pthread_mutex_lock(&cvthing.lock) == 0);
  113. #ifdef _MSC_VER
  114. #pragma inline_depth(0)
  115. #endif
  116. pthread_cleanup_push(pthread_mutex_unlock, (void *) &cvthing.lock);
  117. while (! (cvthing.shared > 0))
  118. assert(pthread_cond_timedwait(&cvthing.notbusy, &cvthing.lock, &abstime) == 0);
  119. pthread_cleanup_pop(0);
  120. #ifdef _MSC_VER
  121. #pragma inline_depth()
  122. #endif
  123. assert(cvthing.shared > 0);
  124. awoken++;
  125. assert(pthread_mutex_unlock(&cvthing.lock) == 0);
  126. return (void *) 0;
  127. }
  128. int
  129. main()
  130. {
  131. int failed = 0;
  132. int i;
  133. pthread_t t[NUMTHREADS + 1];
  134. PTW32_STRUCT_TIMEB currSysTime;
  135. const DWORD NANOSEC_PER_MILLISEC = 1000000;
  136. cvthing.shared = 0;
  137. assert((t[0] = pthread_self()).p != NULL);
  138. assert(cvthing.notbusy == PTHREAD_COND_INITIALIZER);
  139. assert(cvthing.lock == PTHREAD_MUTEX_INITIALIZER);
  140. assert(pthread_mutex_lock(&start_flag) == 0);
  141. PTW32_FTIME(&currSysTime);
  142. abstime.tv_sec = (time_t)currSysTime.time;
  143. abstime.tv_nsec = NANOSEC_PER_MILLISEC * currSysTime.millitm;
  144. abstime.tv_sec += 10;
  145. assert((t[0] = pthread_self()).p != NULL);
  146. awoken = 0;
  147. for (i = 1; i <= NUMTHREADS; i++)
  148. {
  149. threadbag[i].started = 0;
  150. threadbag[i].threadnum = i;
  151. assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0);
  152. }
  153. /*
  154. * Code to control or munipulate child threads should probably go here.
  155. */
  156. assert(pthread_mutex_unlock(&start_flag) == 0);
  157. /*
  158. * Give threads time to start.
  159. */
  160. Sleep(1000);
  161. /*
  162. * Cancel one of the threads.
  163. */
  164. assert(pthread_cancel(t[1]) == 0);
  165. assert(pthread_join(t[1], NULL) == 0);
  166. assert(pthread_mutex_lock(&cvthing.lock) == 0);
  167. cvthing.shared++;
  168. assert(pthread_mutex_unlock(&cvthing.lock) == 0);
  169. /*
  170. * Signal all remaining waiting threads.
  171. */
  172. assert(pthread_cond_broadcast(&cvthing.notbusy) == 0);
  173. /*
  174. * Wait for all threads to complete.
  175. */
  176. for (i = 2; i <= NUMTHREADS; i++)
  177. assert(pthread_join(t[i], NULL) == 0);
  178. /*
  179. * Cleanup the CV.
  180. */
  181. assert(pthread_mutex_destroy(&cvthing.lock) == 0);
  182. assert(cvthing.lock == NULL);
  183. assert(pthread_cond_destroy(&cvthing.notbusy) == 0);
  184. assert(cvthing.notbusy == NULL);
  185. /*
  186. * Standard check that all threads started.
  187. */
  188. for (i = 1; i <= NUMTHREADS; i++)
  189. {
  190. failed = !threadbag[i].started;
  191. if (failed)
  192. {
  193. fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started);
  194. }
  195. }
  196. assert(!failed);
  197. /*
  198. * Check any results here.
  199. */
  200. assert(awoken == (NUMTHREADS - 1));
  201. /*
  202. * Success.
  203. */
  204. return 0;
  205. }