cancel6a.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * File: cancel6a.c
  3. *
  4. *
  5. * Based upon 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. * --------------------------------------------------------------------------
  26. *
  27. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  28. * Copyright(C) 2008 Jason Schmidlapp
  29. *
  30. * Contact Email: [email protected]
  31. *
  32. *
  33. * Test Synopsis: Test double cancelation - asynchronous.
  34. * Second attempt should fail (ESRCH).
  35. *
  36. * Test Method (Validation or Falsification):
  37. * -
  38. *
  39. * Requirements Tested:
  40. * -
  41. *
  42. * Features Tested:
  43. * -
  44. *
  45. * Cases Tested:
  46. * -
  47. *
  48. * Description:
  49. * -
  50. *
  51. * Environment:
  52. * -
  53. *
  54. * Input:
  55. * - None.
  56. *
  57. * Output:
  58. * - File name, Line number, and failed expression on failure.
  59. * - No output on success.
  60. *
  61. * Assumptions:
  62. * - have working pthread_create, pthread_self, pthread_mutex_lock/unlock
  63. * pthread_testcancel, pthread_cancel, pthread_join
  64. *
  65. * Pass Criteria:
  66. * - Process returns zero exit status.
  67. *
  68. * Fail Criteria:
  69. * - Process returns non-zero exit status.
  70. */
  71. #include "test.h"
  72. #ifdef PTE_SUPPORT_ASYNC_CANCEL
  73. /*
  74. * Create NUMTHREADS threads in addition to the Main thread.
  75. */
  76. enum
  77. {
  78. NUMTHREADS = 4
  79. };
  80. typedef struct bag_t_ bag_t;
  81. struct bag_t_
  82. {
  83. int threadnum;
  84. int started;
  85. /* Add more per-thread state variables here */
  86. int count;
  87. };
  88. static bag_t threadbag[NUMTHREADS + 1];
  89. static void *
  90. mythread(void * arg)
  91. {
  92. int result = ((int)PTHREAD_CANCELED + 1);
  93. bag_t * bag = (bag_t *) arg;
  94. assert(bag == &threadbag[bag->threadnum]);
  95. assert(bag->started == 0);
  96. bag->started = 1;
  97. /* Set to known state and type */
  98. assert(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) == 0);
  99. assert(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) == 0);
  100. /*
  101. * We wait up to 10 seconds, waking every 0.1 seconds,
  102. * for a cancelation to be applied to us.
  103. */
  104. for (bag->count = 0; bag->count < 100; bag->count++)
  105. pte_osThreadSleep(100);
  106. return (void *) result;
  107. }
  108. int pthread_test_cancel6a()
  109. {
  110. int failed = 0;
  111. int i;
  112. pthread_t t[NUMTHREADS + 1];
  113. assert((t[0] = pthread_self()).p != NULL);
  114. for (i = 1; i <= NUMTHREADS; i++)
  115. {
  116. threadbag[i].started = 0;
  117. threadbag[i].threadnum = i;
  118. assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0);
  119. }
  120. /*
  121. * Code to control or munipulate child threads should probably go here.
  122. */
  123. pte_osThreadSleep(500);
  124. for (i = 1; i <= NUMTHREADS; i++)
  125. {
  126. assert(pthread_cancel(t[i]) == 0);
  127. assert(pthread_cancel(t[i]) == ESRCH);
  128. }
  129. /*
  130. * Give threads time to run.
  131. */
  132. pte_osThreadSleep(NUMTHREADS * 100);
  133. /*
  134. * Standard check that all threads started.
  135. */
  136. for (i = 1; i <= NUMTHREADS; i++)
  137. {
  138. if (!threadbag[i].started)
  139. {
  140. failed |= !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. int result = 0;
  152. /*
  153. * The thread does not contain any cancelation points, so
  154. * a return value of PTHREAD_CANCELED confirms that async
  155. * cancelation succeeded.
  156. */
  157. assert(pthread_join(t[i], (void **) &result) == 0);
  158. fail = (result != (int) PTHREAD_CANCELED);
  159. failed = (failed || fail);
  160. }
  161. assert(!failed);
  162. /*
  163. * Success.
  164. */
  165. return 0;
  166. }
  167. #else /* PTE_SUPPORT_ASYNC_CANCEL */
  168. int pthread_test_cancel6a()
  169. {
  170. printf("Test not run - async cancellation not supported\n");
  171. return 0;
  172. }
  173. #endif /* !PTE_SUPPORT_ASYNC_CANCEL */