cleanup3.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * File: cleanup3.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 does not execute (when thread is
  37. * not canceled).
  38. *
  39. * Test Method (Validation or Falsification):
  40. * -
  41. *
  42. * Requirements Tested:
  43. * -
  44. *
  45. * Features Tested:
  46. * -
  47. *
  48. * Cases Tested:
  49. * -
  50. *
  51. * Description:
  52. * -
  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. * - have working pthread_create, pthread_self, pthread_mutex_lock/unlock
  66. * pthread_testcancel, pthread_cancel, pthread_join
  67. *
  68. * Pass Criteria:
  69. * - Process returns zero exit status.
  70. *
  71. * Fail Criteria:
  72. * - Process returns non-zero exit status.
  73. */
  74. #if defined(_MSC_VER) || defined(__cplusplus)
  75. #include "test.h"
  76. /*
  77. * Create NUMTHREADS threads in addition to the Main thread.
  78. */
  79. enum {
  80. NUMTHREADS = 10
  81. };
  82. typedef struct bag_t_ bag_t;
  83. struct bag_t_ {
  84. int threadnum;
  85. int started;
  86. /* Add more per-thread state variables here */
  87. int count;
  88. };
  89. static bag_t threadbag[NUMTHREADS + 1];
  90. typedef struct {
  91. int i;
  92. CRITICAL_SECTION cs;
  93. } sharedInt_t;
  94. static sharedInt_t pop_count = {0, {0}};
  95. static void
  96. increment_pop_count(void * arg)
  97. {
  98. sharedInt_t * sI = (sharedInt_t *) arg;
  99. EnterCriticalSection(&sI->cs);
  100. sI->i++;
  101. LeaveCriticalSection(&sI->cs);
  102. }
  103. void *
  104. mythread(void * arg)
  105. {
  106. int result = 0;
  107. bag_t * bag = (bag_t *) arg;
  108. assert(bag == &threadbag[bag->threadnum]);
  109. assert(bag->started == 0);
  110. bag->started = 1;
  111. #ifdef _MSC_VER
  112. #pragma inline_depth(0)
  113. #endif
  114. pthread_cleanup_push(increment_pop_count, (void *) &pop_count);
  115. sched_yield();
  116. EnterCriticalSection(&pop_count.cs);
  117. pop_count.i--;
  118. LeaveCriticalSection(&pop_count.cs);
  119. pthread_cleanup_pop(0);
  120. #ifdef _MSC_VER
  121. #pragma inline_depth()
  122. #endif
  123. return (void *) (size_t)result;
  124. }
  125. int
  126. main()
  127. {
  128. int failed = 0;
  129. int i;
  130. pthread_t t[NUMTHREADS + 1];
  131. InitializeCriticalSection(&pop_count.cs);
  132. assert((t[0] = pthread_self()).p != NULL);
  133. for (i = 1; i <= NUMTHREADS; i++)
  134. {
  135. threadbag[i].started = 0;
  136. threadbag[i].threadnum = i;
  137. assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0);
  138. }
  139. /*
  140. * Code to control or munipulate child threads should probably go here.
  141. */
  142. Sleep(1000);
  143. /*
  144. * Standard check that all threads started.
  145. */
  146. for (i = 1; i <= NUMTHREADS; i++)
  147. {
  148. if (!threadbag[i].started)
  149. {
  150. failed |= !threadbag[i].started;
  151. fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started);
  152. }
  153. }
  154. assert(!failed);
  155. /*
  156. * Check any results here. Set "failed" and only print output on failure.
  157. */
  158. failed = 0;
  159. for (i = 1; i <= NUMTHREADS; i++)
  160. {
  161. int fail = 0;
  162. void* result = (void*)0;
  163. assert(pthread_join(t[i], &result) == 0);
  164. fail = ((int)(size_t)result != 0);
  165. if (fail)
  166. {
  167. fprintf(stderr, "Thread %d: started %d: result: %d\n",
  168. i,
  169. threadbag[i].started,
  170. (int)(size_t)result);
  171. }
  172. failed = (failed || fail);
  173. }
  174. assert(!failed);
  175. assert(pop_count.i == -(NUMTHREADS));
  176. DeleteCriticalSection(&pop_count.cs);
  177. /*
  178. * Success.
  179. */
  180. return 0;
  181. }
  182. #else /* defined(_MSC_VER) || defined(__cplusplus) */
  183. int
  184. main()
  185. {
  186. return 0;
  187. }
  188. #endif /* defined(_MSC_VER) || defined(__cplusplus) */