exception1.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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. #include "test.h"
  74. #if defined(__cplusplus) && defined(PTE_SUPPORT_ASYNC_CANCEL)
  75. /*
  76. * Create NUMTHREADS threads in addition to the Main thread.
  77. */
  78. enum {
  79. NUMTHREADS = 4
  80. };
  81. static void *
  82. exceptionedThread(void * arg)
  83. {
  84. int dummy = 0;
  85. int result = ((int)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. pte_osThreadSleep(100);
  90. try
  91. {
  92. /*
  93. * I had a zero divide exception here but it
  94. * wasn't being caught by the catch(...)
  95. * below under Mingw32. That could be a problem.
  96. */
  97. throw dummy;
  98. }
  99. catch (...)
  100. {
  101. /* Should get into here. */
  102. result = ((int)PTHREAD_CANCELED + 2);
  103. }
  104. return (void *) result;
  105. }
  106. static void *
  107. canceledThread(void * arg)
  108. {
  109. int result = ((int)PTHREAD_CANCELED + 1);
  110. int count;
  111. /* Set to async cancelable */
  112. assert(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) == 0);
  113. assert(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) == 0);
  114. try
  115. {
  116. /*
  117. * We wait up to 10 seconds, waking every 0.1 seconds,
  118. * for a cancelation to be applied to us.
  119. */
  120. for (count = 0; count < 100; count++)
  121. pte_osThreadSleep(100);
  122. }
  123. catch (...)
  124. {
  125. /* Should NOT get into here. */
  126. result = ((int)PTHREAD_CANCELED + 2);
  127. }
  128. return (void *) result;
  129. }
  130. int
  131. pthread_test_exception1()
  132. {
  133. int failed = 0;
  134. int i;
  135. pthread_t mt;
  136. pthread_t et[NUMTHREADS];
  137. pthread_t ct[NUMTHREADS];
  138. assert((mt = pthread_self()).p != NULL);
  139. for (i = 0; i < NUMTHREADS; i++)
  140. {
  141. assert(pthread_create(&et[i], NULL, exceptionedThread, (void *) 0) == 0);
  142. assert(pthread_create(&ct[i], NULL, canceledThread, NULL) == 0);
  143. }
  144. /*
  145. * Code to control or munipulate child threads should probably go here.
  146. */
  147. pte_osThreadSleep(1000);
  148. for (i = 0; i < NUMTHREADS; i++)
  149. {
  150. assert(pthread_cancel(ct[i]) == 0);
  151. }
  152. /*
  153. * Give threads time to run.
  154. */
  155. pte_osThreadSleep(NUMTHREADS * 1000);
  156. /*
  157. * Check any results here. Set "failed" and only print output on failure.
  158. */
  159. failed = 0;
  160. for (i = 0; i < NUMTHREADS; i++)
  161. {
  162. int fail = 0;
  163. int result = 0;
  164. /* Canceled thread */
  165. assert(pthread_join(ct[i], (void **) &result) == 0);
  166. assert(!(fail = (result != (int) PTHREAD_CANCELED)));
  167. failed = (failed || fail);
  168. /* Exceptioned thread */
  169. assert(pthread_join(et[i], (void **) &result) == 0);
  170. assert(!(fail = (result != ((int) PTHREAD_CANCELED + 2))));
  171. failed = (failed || fail);
  172. }
  173. assert(!failed);
  174. /*
  175. * Success.
  176. */
  177. return 0;
  178. }
  179. #else
  180. int
  181. pthread_test_exception1()
  182. {
  183. printf("Test N/A for this compiler environment.\n");
  184. return 0;
  185. }
  186. #endif