1 /*
2 * File: cancel7.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 canceling a Win32 thread having created an
37 * implicit POSIX handle for it.
38 *
39 * Test Method (Validation or Falsification):
40 * - Validate return value and that POSIX handle is created and destroyed.
41 *
42 * Requirements Tested:
43 * -
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 #ifndef _UWIN
77 #include <process.h>
78 #endif
79
80 /*
81 * Create NUMTHREADS threads in addition to the Main thread.
82 */
83 enum {
84 NUMTHREADS = 4
85 };
86
87 typedef struct bag_t_ bag_t;
88 struct bag_t_ {
89 int threadnum;
90 int started;
91 /* Add more per-thread state variables here */
92 int count;
93 pthread_t self;
94 };
95
96 static bag_t threadbag[NUMTHREADS + 1];
97
98 unsigned int __stdcall
Win32thread(void * arg)99 Win32thread(void * arg)
100 {
101 int i;
102 bag_t * bag = (bag_t *) arg;
103
104 assert(bag == &threadbag[bag->threadnum]);
105 assert(bag->started == 0);
106 bag->started = 1;
107
108 assert((bag->self = pthread_self()) != 0);
109 assert(pthread_gethandle (bag->self) != NULL);
110 assert(pthread_kill(bag->self, 0) == 0);
111
112 for (i = 0; i < 100; i++)
113 {
114 Sleep(100);
115 pthread_testcancel();
116 }
117
118 return 0;
119 }
120
121 int
main()122 main()
123 {
124 int failed = 0;
125 int i;
126 HANDLE h[NUMTHREADS + 1];
127 unsigned thrAddr; /* Dummy variable to pass a valid location to _beginthreadex (Win98). */
128
129 for (i = 1; i <= NUMTHREADS; i++)
130 {
131 threadbag[i].started = 0;
132 threadbag[i].threadnum = i;
133 h[i] = (HANDLE) _beginthreadex(NULL, 0, Win32thread, (void *) &threadbag[i], 0, &thrAddr);
134 }
135
136 /*
137 * Code to control or munipulate child threads should probably go here.
138 */
139 Sleep(500);
140
141 /*
142 * Cancel all threads.
143 */
144 for (i = 1; i <= NUMTHREADS; i++)
145 {
146 assert(pthread_kill(threadbag[i].self, 0) == 0);
147 assert(pthread_cancel(threadbag[i].self) == 0);
148 }
149
150 /*
151 * Give threads time to run.
152 */
153 Sleep(NUMTHREADS * 100);
154
155 /*
156 * Standard check that all threads started.
157 */
158 for (i = 1; i <= NUMTHREADS; i++)
159 {
160 if (!threadbag[i].started)
161 {
162 failed |= !threadbag[i].started;
163 fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started);
164 }
165 }
166
167 assert(!failed);
168
169 /*
170 * Check any results here. Set "failed" and only print output on failure.
171 */
172 failed = 0;
173 for (i = 1; i <= NUMTHREADS; i++)
174 {
175 int fail = 0;
176 int result = 0;
177
178 assert(GetExitCodeThread(h[i], (LPDWORD) &result) == TRUE);
179
180 //assert(threadbag[i].self->h != NULL);
181 assert(pthread_kill(threadbag[i].self, 0) == ESRCH);
182
183 fail = (result != (int) (size_t) PTHREAD_CANCELED);
184
185 if (fail)
186 {
187 fprintf(stderr, "Thread %d: started %d: count %d\n",
188 i,
189 threadbag[i].started,
190 threadbag[i].count);
191 }
192 failed = (failed || fail);
193 }
194
195 assert(!failed);
196
197 /*
198 * Success.
199 */
200 return 0;
201 }
202
203