cancel6d.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * File: cancel6d.c
  3. *
  4. *
  5. * Pthreads-win32 - POSIX Threads Library for Win32
  6. * Copyright (C) 1998 Ben Elliston and Ross Johnson
  7. * Copyright (C) 1999,2000,2001 Ross Johnson
  8. *
  9. * Contact Email: [email protected]
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. *
  25. * --------------------------------------------------------------------------
  26. *
  27. * Test Synopsis: Test double cancelation - deferred.
  28. * Second attempt should succeed (unless the canceled thread has started
  29. * cancelation already - not tested here).
  30. *
  31. * Test Method (Validation or Falsification):
  32. * -
  33. *
  34. * Requirements Tested:
  35. * -
  36. *
  37. * Features Tested:
  38. * -
  39. *
  40. * Cases Tested:
  41. * -
  42. *
  43. * Description:
  44. * -
  45. *
  46. * Environment:
  47. * -
  48. *
  49. * Input:
  50. * - None.
  51. *
  52. * Output:
  53. * - File name, Line number, and failed expression on failure.
  54. * - No output on success.
  55. *
  56. * Assumptions:
  57. * - have working pthread_create, pthread_self, pthread_mutex_lock/unlock
  58. * pthread_testcancel, pthread_cancel, pthread_join
  59. *
  60. * Pass Criteria:
  61. * - Process returns zero exit status.
  62. *
  63. * Fail Criteria:
  64. * - Process returns non-zero exit status.
  65. */
  66. #include "test.h"
  67. /*
  68. * Create NUMTHREADS threads in addition to the Main thread.
  69. */
  70. enum {
  71. NUMTHREADS = 4
  72. };
  73. typedef struct bag_t_ bag_t;
  74. struct bag_t_ {
  75. int threadnum;
  76. int started;
  77. /* Add more per-thread state variables here */
  78. int count;
  79. };
  80. static bag_t threadbag[NUMTHREADS + 1];
  81. void *
  82. mythread(void * arg)
  83. {
  84. void* result = (void*)((int)(size_t)PTHREAD_CANCELED + 1);
  85. bag_t * bag = (bag_t *) arg;
  86. assert(bag == &threadbag[bag->threadnum]);
  87. assert(bag->started == 0);
  88. bag->started = 1;
  89. /* Set to known state and type */
  90. assert(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) == 0);
  91. assert(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) == 0);
  92. /*
  93. * We wait up to 10 seconds, waking every 0.1 seconds,
  94. * for a cancelation to be applied to us.
  95. */
  96. for (bag->count = 0; bag->count < 100; bag->count++)
  97. {
  98. Sleep(100);
  99. pthread_testcancel();
  100. }
  101. return result;
  102. }
  103. int
  104. main()
  105. {
  106. int failed = 0;
  107. int i;
  108. pthread_t t[NUMTHREADS + 1];
  109. assert((t[0] = pthread_self()).p != NULL);
  110. for (i = 1; i <= NUMTHREADS; i++)
  111. {
  112. threadbag[i].started = 0;
  113. threadbag[i].threadnum = i;
  114. assert(pthread_create(&t[i], NULL, mythread, (void *)(size_t) &threadbag[i]) == 0);
  115. }
  116. /*
  117. * Code to control or munipulate child threads should probably go here.
  118. */
  119. Sleep(500);
  120. for (i = 1; i <= NUMTHREADS; i++)
  121. {
  122. assert(pthread_cancel(t[i]) == 0);
  123. if (pthread_cancel(t[i]) != 0)
  124. {
  125. printf("Second cancelation failed but this is expected sometimes.\n");
  126. }
  127. }
  128. /*
  129. * Give threads time to run.
  130. */
  131. Sleep(NUMTHREADS * 100);
  132. /*
  133. * Standard check that all threads started.
  134. */
  135. for (i = 1; i <= NUMTHREADS; i++)
  136. {
  137. if (!threadbag[i].started)
  138. {
  139. failed |= !threadbag[i].started;
  140. fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started);
  141. }
  142. }
  143. assert(!failed);
  144. /*
  145. * Check any results here. Set "failed" and only print output on failure.
  146. */
  147. failed = 0;
  148. for (i = 1; i <= NUMTHREADS; i++)
  149. {
  150. int fail = 0;
  151. void* result = (void*)0;
  152. assert(pthread_join(t[i], &result) == 0);
  153. fail = (result != PTHREAD_CANCELED);
  154. if (fail)
  155. {
  156. fprintf(stderr, "Thread %d: started %d: count %d\n",
  157. i,
  158. threadbag[i].started,
  159. threadbag[i].count);
  160. }
  161. failed = (failed || fail);
  162. }
  163. assert(!failed);
  164. /*
  165. * Success.
  166. */
  167. return 0;
  168. }