cancel9.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * File: cancel9.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 true asynchronous cancelation with Alert driver.
  37. *
  38. * Test Method (Validation or Falsification):
  39. * -
  40. *
  41. * Requirements Tested:
  42. * - Cancel threads, including those blocked on system recources
  43. * such as network I/O.
  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, pthread_join
  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. #include <windows.h>
  76. void *
  77. test_udp (void *arg)
  78. {
  79. struct sockaddr_in serverAddress;
  80. struct sockaddr_in clientAddress;
  81. SOCKET UDPSocket;
  82. int addr_len;
  83. int nbyte, bytes;
  84. char buffer[4096];
  85. WORD wsaVersion = MAKEWORD (2, 2);
  86. WSADATA wsaData;
  87. pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
  88. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  89. if (WSAStartup (wsaVersion, &wsaData) != 0)
  90. {
  91. return NULL;
  92. }
  93. UDPSocket = socket (AF_INET, SOCK_DGRAM, 0);
  94. if ((int)UDPSocket == -1)
  95. {
  96. printf ("Server: socket ERROR \n");
  97. exit (-1);
  98. }
  99. serverAddress.sin_family = AF_INET;
  100. serverAddress.sin_addr.s_addr = INADDR_ANY;
  101. serverAddress.sin_port = htons (9003);
  102. if (bind
  103. (UDPSocket, (struct sockaddr *) &serverAddress,
  104. sizeof (struct sockaddr_in)))
  105. {
  106. printf ("Server: ERROR can't bind UDPSocket");
  107. exit (-1);
  108. }
  109. addr_len = sizeof (struct sockaddr);
  110. nbyte = 512;
  111. bytes =
  112. recvfrom (UDPSocket, (char *) buffer, nbyte, 0,
  113. (struct sockaddr *) &clientAddress, &addr_len);
  114. closesocket (UDPSocket);
  115. WSACleanup ();
  116. return NULL;
  117. }
  118. void *
  119. test_sleep (void *arg)
  120. {
  121. pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
  122. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  123. Sleep (1000);
  124. return NULL;
  125. }
  126. void *
  127. test_wait (void *arg)
  128. {
  129. HANDLE hEvent;
  130. DWORD dwEvent;
  131. pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
  132. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  133. hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
  134. dwEvent = WaitForSingleObject (hEvent, 1000); /* WAIT_IO_COMPLETION */
  135. return NULL;
  136. }
  137. int
  138. main ()
  139. {
  140. pthread_t t;
  141. void *result;
  142. if (pthread_win32_test_features_np (PTW32_ALERTABLE_ASYNC_CANCEL))
  143. {
  144. printf ("Cancel sleeping thread.\n");
  145. assert (pthread_create (&t, NULL, test_sleep, NULL) == 0);
  146. /* Sleep for a while; then cancel */
  147. Sleep (100);
  148. assert (pthread_cancel (t) == 0);
  149. assert (pthread_join (t, &result) == 0);
  150. assert (result == PTHREAD_CANCELED && "test_sleep" != NULL);
  151. printf ("Cancel waiting thread.\n");
  152. assert (pthread_create (&t, NULL, test_wait, NULL) == 0);
  153. /* Sleep for a while; then cancel. */
  154. Sleep (100);
  155. assert (pthread_cancel (t) == 0);
  156. assert (pthread_join (t, &result) == 0);
  157. assert (result == PTHREAD_CANCELED && "test_wait");
  158. printf ("Cancel blocked thread (blocked on network I/O).\n");
  159. assert (pthread_create (&t, NULL, test_udp, NULL) == 0);
  160. /* Sleep for a while; then cancel. */
  161. Sleep (100);
  162. assert (pthread_cancel (t) == 0);
  163. assert (pthread_join (t, &result) == 0);
  164. assert (result == PTHREAD_CANCELED && "test_udp" != NULL);
  165. }
  166. else
  167. {
  168. printf ("Alertable async cancel not available.\n");
  169. }
  170. /*
  171. * Success.
  172. */
  173. return 0;
  174. }