cleanup1.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * File: cleanup1.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: Test cleanup handler executes (when thread is canceled).
  37. *
  38. * Test Method (Validation or Falsification):
  39. * -
  40. *
  41. * Requirements Tested:
  42. * -
  43. *
  44. * Features Tested:
  45. * -
  46. *
  47. * Cases Tested:
  48. * -
  49. *
  50. * Description:
  51. * -
  52. *
  53. * Environment:
  54. * -
  55. *
  56. * Input:
  57. * - None.
  58. *
  59. * Output:
  60. * - File name, Line number, and failed expression on failure.
  61. * - No output on success.
  62. *
  63. * Assumptions:
  64. * - have working pthread_create, pthread_self, pthread_mutex_lock/unlock
  65. * pthread_testcancel, pthread_cancel, pthread_join
  66. *
  67. * Pass Criteria:
  68. * - Process returns zero exit status.
  69. *
  70. * Fail Criteria:
  71. * - Process returns non-zero exit status.
  72. */
  73. #if defined(_MSC_VER) || defined(__cplusplus)
  74. #include "test.h"
  75. /*
  76. * Create NUMTHREADS threads in addition to the Main thread.
  77. */
  78. enum {
  79. NUMTHREADS = 10
  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. int count;
  87. };
  88. static bag_t threadbag[NUMTHREADS + 1];
  89. typedef struct {
  90. int i;
  91. CRITICAL_SECTION cs;
  92. } sharedInt_t;
  93. static sharedInt_t pop_count = {0, {0}};
  94. static void
  95. #ifdef __CLEANUP_C
  96. __cdecl
  97. #endif
  98. increment_pop_count(void * arg)
  99. {
  100. sharedInt_t * sI = (sharedInt_t *) arg;
  101. EnterCriticalSection(&sI->cs);
  102. sI->i++;
  103. LeaveCriticalSection(&sI->cs);
  104. }
  105. void *
  106. mythread(void * arg)
  107. {
  108. int result = 0;
  109. bag_t * bag = (bag_t *) arg;
  110. assert(bag == &threadbag[bag->threadnum]);
  111. assert(bag->started == 0);
  112. bag->started = 1;
  113. /* Set to known state and type */
  114. assert(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) == 0);
  115. assert(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) == 0);
  116. #ifdef _MSC_VER
  117. #pragma inline_depth(0)
  118. #endif
  119. pthread_cleanup_push(increment_pop_count, (void *) &pop_count);
  120. /*
  121. * We don't have true async cancelation - it relies on the thread
  122. * at least re-entering the run state at some point.
  123. * We wait up to 10 seconds, waking every 0.1 seconds,
  124. * for a cancelation to be applied to us.
  125. */
  126. for (bag->count = 0; bag->count < 100; bag->count++)
  127. Sleep(100);
  128. pthread_cleanup_pop(0);
  129. #ifdef _MSC_VER
  130. #pragma inline_depth()
  131. #endif
  132. return (void *) (size_t)result;
  133. }
  134. int
  135. main()
  136. {
  137. int failed = 0;
  138. int i;
  139. pthread_t t[NUMTHREADS + 1];
  140. InitializeCriticalSection(&pop_count.cs);
  141. assert((t[0] = pthread_self()).p != NULL);
  142. for (i = 1; i <= NUMTHREADS; i++)
  143. {
  144. threadbag[i].started = 0;
  145. threadbag[i].threadnum = i;
  146. assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0);
  147. }
  148. /*
  149. * Code to control or munipulate child threads should probably go here.
  150. */
  151. Sleep(500);
  152. for (i = 1; i <= NUMTHREADS; i++)
  153. {
  154. assert(pthread_cancel(t[i]) == 0);
  155. }
  156. /*
  157. * Give threads time to run.
  158. */
  159. Sleep(NUMTHREADS * 100);
  160. /*
  161. * Standard check that all threads started.
  162. */
  163. for (i = 1; i <= NUMTHREADS; i++)
  164. {
  165. if (!threadbag[i].started)
  166. {
  167. failed |= !threadbag[i].started;
  168. fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started);
  169. }
  170. }
  171. assert(!failed);
  172. /*
  173. * Check any results here. Set "failed" and only print output on failure.
  174. */
  175. failed = 0;
  176. for (i = 1; i <= NUMTHREADS; i++)
  177. {
  178. int fail = 0;
  179. void* result = (void*)0;
  180. assert(pthread_join(t[i], &result) == 0);
  181. fail = ((int)(size_t)result != (int) PTHREAD_CANCELED);
  182. if (fail)
  183. {
  184. fprintf(stderr, "Thread %d: started %d: result %d\n",
  185. i,
  186. threadbag[i].started,
  187. (int)(size_t)result);
  188. }
  189. failed = (failed || fail);
  190. }
  191. assert(!failed);
  192. assert(pop_count.i == NUMTHREADS);
  193. DeleteCriticalSection(&pop_count.cs);
  194. /*
  195. * Success.
  196. */
  197. return 0;
  198. }
  199. #else /* defined(_MSC_VER) || defined(__cplusplus) */
  200. int
  201. main()
  202. {
  203. return 0;
  204. }
  205. #endif /* defined(_MSC_VER) || defined(__cplusplus) */