cancel6d.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * File: cancel6d.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 - deferred.
  34. * Second attempt should succeed (unless the canceled thread has started
  35. * cancelation already - not tested here).
  36. *
  37. * Test Method (Validation or Falsification):
  38. * -
  39. *
  40. * Requirements Tested:
  41. * -
  42. *
  43. * Features Tested:
  44. * -
  45. *
  46. * Cases Tested:
  47. * -
  48. *
  49. * Description:
  50. * -
  51. *
  52. * Environment:
  53. * -
  54. *
  55. * Input:
  56. * - None.
  57. *
  58. * Output:
  59. * - File name, Line number, and failed expression on failure.
  60. * - No output on success.
  61. *
  62. * Assumptions:
  63. * - have working pthread_create, pthread_self, pthread_mutex_lock/unlock
  64. * pthread_testcancel, pthread_cancel, pthread_join
  65. *
  66. * Pass Criteria:
  67. * - Process returns zero exit status.
  68. *
  69. * Fail Criteria:
  70. * - Process returns non-zero exit status.
  71. */
  72. #include "test.h"
  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_DEFERRED, 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. {
  106. pte_osThreadSleep(100);
  107. pthread_testcancel();
  108. }
  109. return (void *) result;
  110. }
  111. int pthread_test_cancel6d()
  112. {
  113. int failed = 0;
  114. int i;
  115. pthread_t t[NUMTHREADS + 1];
  116. assert((t[0] = pthread_self()).p != NULL);
  117. for (i = 1; i <= NUMTHREADS; i++)
  118. {
  119. threadbag[i].started = 0;
  120. threadbag[i].threadnum = i;
  121. assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0);
  122. }
  123. /*
  124. * Code to control or munipulate child threads should probably go here.
  125. */
  126. pte_osThreadSleep(500);
  127. for (i = 1; i <= NUMTHREADS; i++)
  128. {
  129. assert(pthread_cancel(t[i]) == 0);
  130. assert(pthread_cancel(t[i]) == 0);
  131. }
  132. /*
  133. * Give threads time to run.
  134. */
  135. pte_osThreadSleep(NUMTHREADS * 100);
  136. /*
  137. * Standard check that all threads started.
  138. */
  139. for (i = 1; i <= NUMTHREADS; i++)
  140. {
  141. if (!threadbag[i].started)
  142. {
  143. failed |= !threadbag[i].started;
  144. }
  145. }
  146. assert(!failed);
  147. /*
  148. * Check any results here. Set "failed" and only print output on failure.
  149. */
  150. failed = 0;
  151. for (i = 1; i <= NUMTHREADS; i++)
  152. {
  153. int fail = 0;
  154. int result = 0;
  155. assert(pthread_join(t[i], (void **) &result) == 0);
  156. fail = (result != (int) PTHREAD_CANCELED);
  157. failed = (failed || fail);
  158. }
  159. assert(!failed);
  160. /*
  161. * Success.
  162. */
  163. return 0;
  164. }