condvar9.c 6.4 KB

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