cancel2.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * File: cancel2.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. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  33. *
  34. * --------------------------------------------------------------------------
  35. *
  36. * Test Synopsis: Test SEH or C++ cancel exception handling within
  37. * application exception blocks.
  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. /*
  75. * Don't know how to identify if we are using SEH so it's only C++ for now
  76. */
  77. #if defined(__cplusplus)
  78. #include "test.h"
  79. /*
  80. * Create NUMTHREADS threads in addition to the Main thread.
  81. */
  82. enum {
  83. NUMTHREADS = 4
  84. };
  85. typedef struct bag_t_ bag_t;
  86. struct bag_t_ {
  87. int threadnum;
  88. int started;
  89. /* Add more per-thread state variables here */
  90. };
  91. static bag_t threadbag[NUMTHREADS + 1];
  92. static pthread_mutex_t waitLock = PTHREAD_MUTEX_INITIALIZER;
  93. void *
  94. mythread(void * arg)
  95. {
  96. int result = 0;
  97. bag_t * bag = (bag_t *) arg;
  98. assert(bag == &threadbag[bag->threadnum]);
  99. assert(bag->started == 0);
  100. bag->started = 1;
  101. /* Set to known state and type */
  102. assert(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) == 0);
  103. switch (bag->threadnum % 2)
  104. {
  105. case 0:
  106. assert(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) == 0);
  107. result = 0;
  108. break;
  109. case 1:
  110. assert(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) == 0);
  111. result = 1;
  112. break;
  113. }
  114. #if !defined(__cplusplus)
  115. __try
  116. #else
  117. try
  118. #endif
  119. {
  120. /* Wait for go from main */
  121. assert(pthread_mutex_lock(&waitLock) == 0);
  122. assert(pthread_mutex_unlock(&waitLock) == 0);
  123. sched_yield();
  124. for (;;)
  125. {
  126. pthread_testcancel();
  127. }
  128. }
  129. #if !defined(__cplusplus)
  130. __except(EXCEPTION_EXECUTE_HANDLER)
  131. #else
  132. #if defined(PtW32CatchAll)
  133. PtW32CatchAll
  134. #else
  135. catch(...)
  136. #endif
  137. #endif
  138. {
  139. /*
  140. * Should not get into here.
  141. */
  142. result += 100;
  143. }
  144. /*
  145. * Should not get to here either.
  146. */
  147. result += 1000;
  148. return (void *) (size_t)result;
  149. }
  150. int
  151. main()
  152. {
  153. int failed = 0;
  154. int i;
  155. pthread_t t[NUMTHREADS + 1];
  156. assert((t[0] = pthread_self()).p != NULL);
  157. assert(pthread_mutex_lock(&waitLock) == 0);
  158. for (i = 1; i <= NUMTHREADS; i++)
  159. {
  160. threadbag[i].started = 0;
  161. threadbag[i].threadnum = i;
  162. assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0);
  163. }
  164. /*
  165. * Code to control or munipulate child threads should probably go here.
  166. */
  167. Sleep(500);
  168. assert(pthread_mutex_unlock(&waitLock) == 0);
  169. Sleep(500);
  170. for (i = 1; i <= NUMTHREADS; i++)
  171. {
  172. assert(pthread_cancel(t[i]) == 0);
  173. }
  174. /*
  175. * Give threads time to run.
  176. */
  177. Sleep(NUMTHREADS * 100);
  178. /*
  179. * Standard check that all threads started.
  180. */
  181. for (i = 1; i <= NUMTHREADS; i++)
  182. {
  183. if (!threadbag[i].started)
  184. {
  185. failed |= !threadbag[i].started;
  186. fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started);
  187. }
  188. }
  189. assert(!failed);
  190. /*
  191. * Check any results here. Set "failed" and only print output on failure.
  192. */
  193. failed = 0;
  194. for (i = 1; i <= NUMTHREADS; i++)
  195. {
  196. int fail = 0;
  197. void* result = (void*)0;
  198. assert(pthread_join(t[i], &result) == 0);
  199. fail = ((int)(size_t)result != (int) PTHREAD_CANCELED);
  200. if (fail)
  201. {
  202. fprintf(stderr, "Thread %d: started %d: location %d: cancel type %s\n",
  203. i,
  204. threadbag[i].started,
  205. (int)(size_t)result,
  206. (((int)(size_t)result % 2) == 0) ? "ASYNCHRONOUS" : "DEFERRED");
  207. }
  208. failed |= fail;
  209. }
  210. assert(!failed);
  211. /*
  212. * Success.
  213. */
  214. return 0;
  215. }
  216. #else /* defined(__cplusplus) */
  217. #include <stdio.h>
  218. int
  219. main()
  220. {
  221. fprintf(stderr, "Test N/A for this compiler environment.\n");
  222. return 0;
  223. }
  224. #endif /* defined(__cplusplus) */