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: rpj@callisto.canberra.edu.au
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
75 #include "test.h"
76 #include <windows.h>
77
78
79 void *
test_udp(void * arg)80 test_udp (void *arg)
81 {
82 struct sockaddr_in serverAddress;
83 struct sockaddr_in clientAddress;
84 SOCKET UDPSocket;
85 int addr_len;
86 int nbyte, bytes;
87 char buffer[4096];
88 WORD wsaVersion = MAKEWORD (2, 2);
89 WSADATA wsaData;
90
91 pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
92 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
93
94 if (WSAStartup (wsaVersion, &wsaData) != 0)
95 {
96 return NULL;
97 }
98
99 UDPSocket = socket (AF_INET, SOCK_DGRAM, 0);
100 if ((int)UDPSocket == -1)
101 {
102 printf ("Server: socket ERROR \n");
103 exit (-1);
104 }
105
106 serverAddress.sin_family = AF_INET;
107 serverAddress.sin_addr.s_addr = INADDR_ANY;
108 serverAddress.sin_port = htons (9003);
109
110 if (bind
111 (UDPSocket, (struct sockaddr *) &serverAddress,
112 sizeof (struct sockaddr_in)))
113 {
114 printf ("Server: ERROR can't bind UDPSocket");
115 exit (-1);
116 }
117
118 addr_len = sizeof (struct sockaddr);
119
120 nbyte = 512;
121
122 bytes =
123 recvfrom (UDPSocket, (char *) buffer, nbyte, 0,
124 (struct sockaddr *) &clientAddress, &addr_len);
125
126 closesocket (UDPSocket);
127 WSACleanup ();
128
129 return NULL;
130 }
131
132
133 void *
test_sleep(void * arg)134 test_sleep (void *arg)
135 {
136 pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
137 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
138
139 Sleep (1000);
140 return NULL;
141
142 }
143
144 void *
test_wait(void * arg)145 test_wait (void *arg)
146 {
147 HANDLE hEvent;
148 DWORD dwEvent;
149
150 pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
151 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
152
153 hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
154
155 dwEvent = WaitForSingleObject (hEvent, 1000); /* WAIT_IO_COMPLETION */
156
157 return NULL;
158 }
159
160
161 int
main()162 main ()
163 {
164 pthread_t t;
165 void *result;
166
167 if (1 || pthread_win32_test_features_np (PTW32_ALERTABLE_ASYNC_CANCEL))
168 {
169 printf ("Cancel sleeping thread.\n");
170 assert (pthread_create (&t, NULL, test_sleep, NULL) == 0);
171 /* Sleep for a while; then cancel */
172 Sleep (100);
173 assert (pthread_cancel (t) == 0);
174 assert (pthread_join (t, &result) == 0);
175 assert (result == PTHREAD_CANCELED && "test_sleep" != NULL);
176
177 printf ("Cancel waiting thread.\n");
178 assert (pthread_create (&t, NULL, test_wait, NULL) == 0);
179 /* Sleep for a while; then cancel. */
180 Sleep (100);
181 assert (pthread_cancel (t) == 0);
182 assert (pthread_join (t, &result) == 0);
183 assert (result == PTHREAD_CANCELED && "test_wait");
184
185 printf ("Cancel blocked thread (blocked on network I/O).\n");
186 assert (pthread_create (&t, NULL, test_udp, NULL) == 0);
187 /* Sleep for a while; then cancel. */
188 Sleep (100);
189 assert (pthread_cancel (t) == 0);
190 assert (pthread_join (t, &result) == 0);
191 assert (result == PTHREAD_CANCELED && "test_udp" != NULL);
192 }
193 else
194 {
195 printf ("Alertable async cancel not available.\n");
196 }
197
198 /*
199 * Success.
200 */
201 return 0;
202 }
203