cancel8.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * File: cancel8.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 cancelling a blocked Win32 thread having created an
  37. * implicit POSIX handle for it.
  38. *
  39. * Test Method (Validation or Falsification):
  40. * - Validate return value and that POSIX handle is created and destroyed.
  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. * - have working pthread_create, pthread_self, pthread_mutex_lock/unlock
  66. * pthread_testcancel, pthread_cancel
  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. #ifndef _UWIN
  76. #include <process.h>
  77. #endif
  78. /*
  79. * Create NUMTHREADS threads in addition to the Main thread.
  80. */
  81. enum {
  82. NUMTHREADS = 4
  83. };
  84. typedef struct bag_t_ bag_t;
  85. struct bag_t_ {
  86. int threadnum;
  87. int started;
  88. /* Add more per-thread state variables here */
  89. int count;
  90. pthread_t self;
  91. };
  92. static bag_t threadbag[NUMTHREADS + 1];
  93. pthread_cond_t CV = PTHREAD_COND_INITIALIZER;
  94. pthread_mutex_t CVLock = PTHREAD_MUTEX_INITIALIZER;
  95. #if ! defined (__MINGW32__) || defined (__MSVCRT__)
  96. unsigned __stdcall
  97. #else
  98. void
  99. #endif
  100. Win32thread(void * arg)
  101. {
  102. bag_t * bag = (bag_t *) arg;
  103. assert(bag == &threadbag[bag->threadnum]);
  104. assert(bag->started == 0);
  105. bag->started = 1;
  106. assert((bag->self = pthread_self()).p != NULL);
  107. assert(pthread_kill(bag->self, 0) == 0);
  108. assert(pthread_mutex_lock(&CVLock) == 0);
  109. pthread_cleanup_push(pthread_mutex_unlock, &CVLock);
  110. pthread_cond_wait(&CV, &CVLock);
  111. pthread_cleanup_pop(1);
  112. #if ! defined (__MINGW32__) || defined (__MSVCRT__)
  113. return 0;
  114. #endif
  115. }
  116. int
  117. main()
  118. {
  119. int failed = 0;
  120. int i;
  121. HANDLE h[NUMTHREADS + 1];
  122. unsigned thrAddr; /* Dummy variable to pass a valid location to _beginthreadex (Win98). */
  123. for (i = 1; i <= NUMTHREADS; i++)
  124. {
  125. threadbag[i].started = 0;
  126. threadbag[i].threadnum = i;
  127. #if ! defined (__MINGW32__) || defined (__MSVCRT__)
  128. h[i] = (HANDLE) _beginthreadex(NULL, 0, Win32thread, (void *) &threadbag[i], 0, &thrAddr);
  129. #else
  130. h[i] = (HANDLE) _beginthread(Win32thread, 0, (void *) &threadbag[i]);
  131. #endif
  132. }
  133. /*
  134. * Code to control or munipulate child threads should probably go here.
  135. */
  136. Sleep(500);
  137. /*
  138. * Cancel all threads.
  139. */
  140. for (i = 1; i <= NUMTHREADS; i++)
  141. {
  142. assert(pthread_kill(threadbag[i].self, 0) == 0);
  143. assert(pthread_cancel(threadbag[i].self) == 0);
  144. }
  145. /*
  146. * Give threads time to run.
  147. */
  148. Sleep(NUMTHREADS * 100);
  149. /*
  150. * Standard check that all threads started.
  151. */
  152. for (i = 1; i <= NUMTHREADS; i++)
  153. {
  154. if (!threadbag[i].started)
  155. {
  156. failed |= !threadbag[i].started;
  157. fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started);
  158. }
  159. }
  160. assert(!failed);
  161. /*
  162. * Check any results here. Set "failed" and only print output on failure.
  163. */
  164. failed = 0;
  165. for (i = 1; i <= NUMTHREADS; i++)
  166. {
  167. int fail = 0;
  168. int result = 0;
  169. #if ! defined (__MINGW32__) || defined (__MSVCRT__)
  170. assert(GetExitCodeThread(h[i], (LPDWORD) &result) == TRUE);
  171. #else
  172. /*
  173. * Can't get a result code.
  174. */
  175. result = (int)(size_t)PTHREAD_CANCELED;
  176. #endif
  177. assert(threadbag[i].self.p != NULL);
  178. assert(pthread_kill(threadbag[i].self, 0) == ESRCH);
  179. fail = (result != (int)(size_t)PTHREAD_CANCELED);
  180. if (fail)
  181. {
  182. fprintf(stderr, "Thread %d: started %d: count %d\n",
  183. i,
  184. threadbag[i].started,
  185. threadbag[i].count);
  186. }
  187. failed = (failed || fail);
  188. }
  189. assert(!failed);
  190. /*
  191. * Success.
  192. */
  193. return 0;
  194. }