cancel1.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * File: cancel1.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 setting cancel state and cancel type.
  37. * -
  38. *
  39. * Test Method (Validation or Falsification):
  40. * -
  41. *
  42. * Requirements Tested:
  43. * - pthread_setcancelstate function
  44. * - pthread_setcanceltype function
  45. *
  46. * Features Tested:
  47. * -
  48. *
  49. * Cases Tested:
  50. * -
  51. *
  52. * Description:
  53. * -
  54. *
  55. * Environment:
  56. * -
  57. *
  58. * Input:
  59. * - None.
  60. *
  61. * Output:
  62. * - File name, Line number, and failed expression on failure.
  63. * - No output on success.
  64. *
  65. * Assumptions:
  66. * - pthread_create, pthread_self work.
  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. /*
  76. * Create NUMTHREADS threads in addition to the Main thread.
  77. */
  78. enum {
  79. NUMTHREADS = 2
  80. };
  81. typedef struct bag_t_ bag_t;
  82. struct bag_t_ {
  83. int threadnum;
  84. int started;
  85. /* Add more per-thread state variables here */
  86. };
  87. static bag_t threadbag[NUMTHREADS + 1];
  88. void *
  89. mythread(void * arg)
  90. {
  91. bag_t * bag = (bag_t *) arg;
  92. assert(bag == &threadbag[bag->threadnum]);
  93. assert(bag->started == 0);
  94. bag->started = 1;
  95. /* ... */
  96. {
  97. int oldstate;
  98. int oldtype;
  99. assert(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) == 0);
  100. assert(oldstate == PTHREAD_CANCEL_ENABLE); /* Check default */
  101. assert(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) == 0);
  102. assert(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) == 0);
  103. assert(pthread_setcancelstate(oldstate, &oldstate) == 0);
  104. assert(oldstate == PTHREAD_CANCEL_DISABLE); /* Check setting */
  105. assert(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype) == 0);
  106. assert(oldtype == PTHREAD_CANCEL_DEFERRED); /* Check default */
  107. assert(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) == 0);
  108. assert(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) == 0);
  109. assert(pthread_setcanceltype(oldtype, &oldtype) == 0);
  110. assert(oldtype == PTHREAD_CANCEL_ASYNCHRONOUS); /* Check setting */
  111. }
  112. return 0;
  113. }
  114. int
  115. main()
  116. {
  117. int failed = 0;
  118. int i;
  119. pthread_t t[NUMTHREADS + 1];
  120. assert((t[0] = pthread_self()).p != NULL);
  121. for (i = 1; i <= NUMTHREADS; i++)
  122. {
  123. threadbag[i].started = 0;
  124. threadbag[i].threadnum = i;
  125. assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0);
  126. }
  127. /*
  128. * Code to control or munipulate child threads should probably go here.
  129. */
  130. /*
  131. * Give threads time to run.
  132. */
  133. Sleep(NUMTHREADS * 100);
  134. /*
  135. * Standard check that all threads started.
  136. */
  137. for (i = 1; i <= NUMTHREADS; i++)
  138. {
  139. failed = !threadbag[i].started;
  140. if (failed)
  141. {
  142. fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started);
  143. }
  144. }
  145. assert(!failed);
  146. /*
  147. * Check any results here. Set "failed" and only print ouput on failure.
  148. */
  149. for (i = 1; i <= NUMTHREADS; i++)
  150. {
  151. /* ... */
  152. }
  153. assert(!failed);
  154. /*
  155. * Success.
  156. */
  157. return 0;
  158. }