cancel3.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * File: cancel3.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 asynchronous cancelation (alertable or non-alertable).
  37. *
  38. * Test Method (Validation or Falsification):
  39. * -
  40. *
  41. * Requirements Tested:
  42. * - Async cancel if thread is not blocked (i.e. voluntarily resumes if blocked).
  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. * - quserex.dll and alertdrv.sys are not available.
  67. *
  68. * Pass Criteria:
  69. * - Process returns zero exit status.
  70. *
  71. * Fail Criteria:
  72. * - Process returns non-zero exit status.
  73. */
  74. #include "test.h"
  75. /*
  76. * Create NUMTHREADS threads in addition to the Main thread.
  77. */
  78. enum
  79. {
  80. NUMTHREADS = 4
  81. };
  82. typedef struct bag_t_ bag_t;
  83. struct bag_t_
  84. {
  85. int threadnum;
  86. int started;
  87. /* Add more per-thread state variables here */
  88. int count;
  89. };
  90. static bag_t threadbag[NUMTHREADS + 1];
  91. void *
  92. mythread (void *arg)
  93. {
  94. void* result = (void*)((int)(size_t)PTHREAD_CANCELED + 1);
  95. bag_t *bag = (bag_t *) arg;
  96. assert (bag == &threadbag[bag->threadnum]);
  97. assert (bag->started == 0);
  98. bag->started = 1;
  99. /* Set to known state and type */
  100. assert (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) == 0);
  101. assert (pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL) == 0);
  102. /*
  103. * We wait up to 10 seconds, waking every 0.1 seconds,
  104. * for a cancelation to be applied to us.
  105. */
  106. for (bag->count = 0; bag->count < 100; bag->count++)
  107. Sleep (100);
  108. return result;
  109. }
  110. int
  111. main ()
  112. {
  113. int failed = 0;
  114. int i;
  115. pthread_t t[NUMTHREADS + 1];
  116. assert ((t[0] = pthread_self ()).p != NULL);
  117. for (i = 1; i <= NUMTHREADS; i++)
  118. {
  119. threadbag[i].started = 0;
  120. threadbag[i].threadnum = i;
  121. assert (pthread_create (&t[i], NULL, mythread, (void *) &threadbag[i])
  122. == 0);
  123. }
  124. /*
  125. * Code to control or munipulate child threads should probably go here.
  126. */
  127. Sleep (NUMTHREADS * 100);
  128. for (i = 1; i <= NUMTHREADS; i++)
  129. {
  130. assert (pthread_cancel (t[i]) == 0);
  131. }
  132. /*
  133. * Give threads time to complete.
  134. */
  135. Sleep (NUMTHREADS * 100);
  136. /*
  137. * Standard check that all threads started.
  138. */
  139. for (i = 1; i <= NUMTHREADS; i++)
  140. {
  141. if (!threadbag[i].started)
  142. {
  143. failed |= !threadbag[i].started;
  144. fprintf (stderr, "Thread %d: started %d\n", i,
  145. threadbag[i].started);
  146. }
  147. }
  148. assert (!failed);
  149. /*
  150. * Check any results here. Set "failed" and only print output on failure.
  151. */
  152. failed = 0;
  153. for (i = 1; i <= NUMTHREADS; i++)
  154. {
  155. int fail = 0;
  156. void* result = (void*)0;
  157. /*
  158. * The thread does not contain any cancelation points, so
  159. * a return value of PTHREAD_CANCELED confirms that async
  160. * cancelation succeeded.
  161. */
  162. assert (pthread_join (t[i], &result) == 0);
  163. fail = (result != PTHREAD_CANCELED);
  164. if (fail)
  165. {
  166. fprintf (stderr, "Thread %d: started %d: count %d\n",
  167. i, threadbag[i].started, threadbag[i].count);
  168. }
  169. failed = (failed || fail);
  170. }
  171. assert (!failed);
  172. /*
  173. * Success.
  174. */
  175. return 0;
  176. }