cancel4.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * File: cancel4.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 cancelation does not occur in deferred
  37. * cancelation threads with no cancelation points.
  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. * - pthread_create
  66. * pthread_self
  67. * pthread_cancel
  68. * pthread_join
  69. * pthread_setcancelstate
  70. * pthread_setcanceltype
  71. *
  72. * Pass Criteria:
  73. * - Process returns zero exit status.
  74. *
  75. * Fail Criteria:
  76. * - Process returns non-zero exit status.
  77. */
  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. int count;
  91. };
  92. static bag_t threadbag[NUMTHREADS + 1];
  93. void *
  94. mythread(void * arg)
  95. {
  96. void* result = (void*)((int)(size_t)PTHREAD_CANCELED + 1);
  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. assert(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) == 0);
  104. /*
  105. * We wait up to 2 seconds, waking every 0.1 seconds,
  106. * for a cancelation to be applied to us.
  107. */
  108. for (bag->count = 0; bag->count < 20; bag->count++)
  109. Sleep(100);
  110. return result;
  111. }
  112. int
  113. main()
  114. {
  115. int failed = 0;
  116. int i;
  117. pthread_t t[NUMTHREADS + 1];
  118. assert((t[0] = pthread_self()).p != NULL);
  119. for (i = 1; i <= NUMTHREADS; i++)
  120. {
  121. threadbag[i].started = 0;
  122. threadbag[i].threadnum = i;
  123. assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0);
  124. }
  125. /*
  126. * Code to control or munipulate child threads should probably go here.
  127. */
  128. Sleep(500);
  129. for (i = 1; i <= NUMTHREADS; i++)
  130. {
  131. assert(pthread_cancel(t[i]) == 0);
  132. }
  133. /*
  134. * Give threads time to run.
  135. */
  136. Sleep(NUMTHREADS * 100);
  137. /*
  138. * Standard check that all threads started.
  139. */
  140. for (i = 1; i <= NUMTHREADS; i++)
  141. {
  142. if (!threadbag[i].started)
  143. {
  144. failed |= !threadbag[i].started;
  145. fprintf(stderr, "Thread %d: started %d\n", i, 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 indicates that async
  160. * cancelation occurred.
  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,
  168. threadbag[i].started,
  169. threadbag[i].count);
  170. }
  171. failed = (failed || fail);
  172. }
  173. assert(!failed);
  174. /*
  175. * Success.
  176. */
  177. return 0;
  178. }