cancel6a.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * File: cancel6a.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 - asynchronous.
  28. * Second attempt should fail (ESRCH).
  29. *
  30. * Test Method (Validation or Falsification):
  31. * -
  32. *
  33. * Requirements Tested:
  34. * -
  35. *
  36. * Features Tested:
  37. * -
  38. *
  39. * Cases Tested:
  40. * -
  41. *
  42. * Description:
  43. * -
  44. *
  45. * Environment:
  46. * -
  47. *
  48. * Input:
  49. * - None.
  50. *
  51. * Output:
  52. * - File name, Line number, and failed expression on failure.
  53. * - No output on success.
  54. *
  55. * Assumptions:
  56. * - have working pthread_create, pthread_self, pthread_mutex_lock/unlock
  57. * pthread_testcancel, pthread_cancel, pthread_join
  58. *
  59. * Pass Criteria:
  60. * - Process returns zero exit status.
  61. *
  62. * Fail Criteria:
  63. * - Process returns non-zero exit status.
  64. */
  65. #include "test.h"
  66. /*
  67. * Create NUMTHREADS threads in addition to the Main thread.
  68. */
  69. enum {
  70. NUMTHREADS = 4
  71. };
  72. typedef struct bag_t_ bag_t;
  73. struct bag_t_ {
  74. int threadnum;
  75. int started;
  76. /* Add more per-thread state variables here */
  77. int count;
  78. };
  79. static bag_t threadbag[NUMTHREADS + 1];
  80. void *
  81. mythread(void * arg)
  82. {
  83. void* result = (void*)((int)(size_t)PTHREAD_CANCELED + 1);
  84. bag_t * bag = (bag_t *) arg;
  85. assert(bag == &threadbag[bag->threadnum]);
  86. assert(bag->started == 0);
  87. bag->started = 1;
  88. /* Set to known state and type */
  89. assert(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) == 0);
  90. assert(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) == 0);
  91. /*
  92. * We wait up to 10 seconds, waking every 0.1 seconds,
  93. * for a cancelation to be applied to us.
  94. */
  95. for (bag->count = 0; bag->count < 100; bag->count++)
  96. Sleep(100);
  97. return result;
  98. }
  99. int
  100. main()
  101. {
  102. int failed = 0;
  103. int i;
  104. pthread_t t[NUMTHREADS + 1];
  105. assert((t[0] = pthread_self()).p != NULL);
  106. for (i = 1; i <= NUMTHREADS; i++)
  107. {
  108. threadbag[i].started = 0;
  109. threadbag[i].threadnum = i;
  110. assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0);
  111. }
  112. /*
  113. * Code to control or munipulate child threads should probably go here.
  114. */
  115. Sleep(500);
  116. for (i = 1; i <= NUMTHREADS; i++)
  117. {
  118. assert(pthread_cancel(t[i]) == 0);
  119. assert(pthread_cancel(t[i]) == ESRCH);
  120. }
  121. /*
  122. * Give threads time to run.
  123. */
  124. Sleep(NUMTHREADS * 100);
  125. /*
  126. * Standard check that all threads started.
  127. */
  128. for (i = 1; i <= NUMTHREADS; i++)
  129. {
  130. if (!threadbag[i].started)
  131. {
  132. failed |= !threadbag[i].started;
  133. fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started);
  134. }
  135. }
  136. assert(!failed);
  137. /*
  138. * Check any results here. Set "failed" and only print output on failure.
  139. */
  140. failed = 0;
  141. for (i = 1; i <= NUMTHREADS; i++)
  142. {
  143. int fail = 0;
  144. void* result = (void*)0;
  145. /*
  146. * The thread does not contain any cancelation points, so
  147. * a return value of PTHREAD_CANCELED confirms that async
  148. * cancelation succeeded.
  149. */
  150. assert(pthread_join(t[i], &result) == 0);
  151. fail = (result != PTHREAD_CANCELED);
  152. if (fail)
  153. {
  154. fprintf(stderr, "Thread %d: started %d: count %d\n",
  155. i,
  156. threadbag[i].started,
  157. threadbag[i].count);
  158. }
  159. failed = (failed || fail);
  160. }
  161. assert(!failed);
  162. /*
  163. * Success.
  164. */
  165. return 0;
  166. }