exception1.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * File: exception1.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 passing of exceptions back to the application.
  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 = 4
  80. };
  81. void *
  82. exceptionedThread(void * arg)
  83. {
  84. int dummy = 0;
  85. void* result = (void*)((int)(size_t)PTHREAD_CANCELED + 1);
  86. /* Set to async cancelable */
  87. assert(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) == 0);
  88. assert(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) == 0);
  89. Sleep(100);
  90. #if defined(_MSC_VER) && !defined(__cplusplus)
  91. __try
  92. {
  93. int zero = (int) (size_t)arg; /* Passed in from arg to avoid compiler error */
  94. int one = 1;
  95. /*
  96. * The deliberate exception condition (zero divide) is
  97. * in an "if" to avoid being optimised out.
  98. */
  99. if (dummy == one/zero)
  100. Sleep(0);
  101. }
  102. __except (EXCEPTION_EXECUTE_HANDLER)
  103. {
  104. /* Should get into here. */
  105. result = (void*)((int)(size_t)PTHREAD_CANCELED + 2);
  106. }
  107. #elif defined(__cplusplus)
  108. try
  109. {
  110. /*
  111. * I had a zero divide exception here but it
  112. * wasn't being caught by the catch(...)
  113. * below under Mingw32. That could be a problem.
  114. */
  115. throw dummy;
  116. }
  117. #if defined(PtW32CatchAll)
  118. PtW32CatchAll
  119. #else
  120. catch (...)
  121. #endif
  122. {
  123. /* Should get into here. */
  124. result = (void*)((int)(size_t)PTHREAD_CANCELED + 2);
  125. }
  126. #endif
  127. return (void *) (size_t)result;
  128. }
  129. void *
  130. canceledThread(void * arg)
  131. {
  132. void* result = (void*)((int)(size_t)PTHREAD_CANCELED + 1);
  133. int count;
  134. /* Set to async cancelable */
  135. assert(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) == 0);
  136. assert(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) == 0);
  137. #if defined(_MSC_VER) && !defined(__cplusplus)
  138. __try
  139. {
  140. /*
  141. * We wait up to 10 seconds, waking every 0.1 seconds,
  142. * for a cancelation to be applied to us.
  143. */
  144. for (count = 0; count < 100; count++)
  145. Sleep(100);
  146. }
  147. __except (EXCEPTION_EXECUTE_HANDLER)
  148. {
  149. /* Should NOT get into here. */
  150. result = (void*)((int)(size_t)PTHREAD_CANCELED + 2);
  151. }
  152. #elif defined(__cplusplus)
  153. try
  154. {
  155. /*
  156. * We wait up to 10 seconds, waking every 0.1 seconds,
  157. * for a cancelation to be applied to us.
  158. */
  159. for (count = 0; count < 100; count++)
  160. Sleep(100);
  161. }
  162. #if defined(PtW32CatchAll)
  163. PtW32CatchAll
  164. #else
  165. catch (...)
  166. #endif
  167. {
  168. /* Should NOT get into here. */
  169. result = (void*)((int)(size_t)PTHREAD_CANCELED + 2);
  170. }
  171. #endif
  172. return (void *) (size_t)result;
  173. }
  174. int
  175. main()
  176. {
  177. int failed = 0;
  178. int i;
  179. pthread_t mt;
  180. pthread_t et[NUMTHREADS];
  181. pthread_t ct[NUMTHREADS];
  182. assert((mt = pthread_self()).p != NULL);
  183. for (i = 0; i < NUMTHREADS; i++)
  184. {
  185. assert(pthread_create(&et[i], NULL, exceptionedThread, (void *) 0) == 0);
  186. assert(pthread_create(&ct[i], NULL, canceledThread, NULL) == 0);
  187. }
  188. /*
  189. * Code to control or munipulate child threads should probably go here.
  190. */
  191. Sleep(1000);
  192. for (i = 0; i < NUMTHREADS; i++)
  193. {
  194. assert(pthread_cancel(ct[i]) == 0);
  195. }
  196. /*
  197. * Give threads time to run.
  198. */
  199. Sleep(NUMTHREADS * 1000);
  200. /*
  201. * Check any results here. Set "failed" and only print output on failure.
  202. */
  203. failed = 0;
  204. for (i = 0; i < NUMTHREADS; i++)
  205. {
  206. int fail = 0;
  207. void* result = (void*)0;
  208. /* Canceled thread */
  209. assert(pthread_join(ct[i], &result) == 0);
  210. assert(!(fail = (result != PTHREAD_CANCELED)));
  211. failed = (failed || fail);
  212. /* Exceptioned thread */
  213. assert(pthread_join(et[i], &result) == 0);
  214. assert(!(fail = (result != (void*)((int)(size_t)PTHREAD_CANCELED + 2))));
  215. failed = (failed || fail);
  216. }
  217. assert(!failed);
  218. /*
  219. * Success.
  220. */
  221. return 0;
  222. }
  223. #else /* defined(_MSC_VER) || defined(__cplusplus) */
  224. #include <stdio.h>
  225. int
  226. main()
  227. {
  228. fprintf(stderr, "Test N/A for this compiler environment.\n");
  229. return 0;
  230. }
  231. #endif /* defined(_MSC_VER) || defined(__cplusplus) */