1 /*
2 * File: cleanup1.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 cleanup handler executes (when thread is not canceled).
37 *
38 * Test Method (Validation or Falsification):
39 * -
40 *
41 * Requirements Tested:
42 * -
43 *
44 * Features Tested:
45 * -
46 *
47 * Cases Tested:
48 * -
49 *
50 * Description:
51 * -
52 *
53 * Environment:
54 * -
55 *
56 * Input:
57 * - None.
58 *
59 * Output:
60 * - File name, Line number, and failed expression on failure.
61 * - No output on success.
62 *
63 * Assumptions:
64 * - have working pthread_create, pthread_self, pthread_mutex_lock/unlock
65 * pthread_testcancel, pthread_cancel, pthread_join
66 *
67 * Pass Criteria:
68 * - Process returns zero exit status.
69 *
70 * Fail Criteria:
71 * - Process returns non-zero exit status.
72 */
73
74 #if defined(_MSC_VER) || defined(__cplusplus)
75
76 #include "test.h"
77
78 /*
79 * Create NUMTHREADS threads in addition to the Main thread.
80 */
81 enum {
82 NUMTHREADS = 10
83 };
84
85 typedef struct bag_t_ bag_t;
86 struct bag_t_ {
87 int threadnum;
88 int started;
89 /* Add more per-thread state variables here */
90 int count;
91 };
92
93 static bag_t threadbag[NUMTHREADS + 1];
94
95 typedef struct {
96 int i;
97 CRITICAL_SECTION cs;
98 } sharedInt_t;
99
100 static sharedInt_t pop_count = {0, {0}};
101
102 static void
increment_pop_count(void * arg)103 increment_pop_count(void * arg)
104 {
105 sharedInt_t * sI = (sharedInt_t *) arg;
106
107 EnterCriticalSection(&sI->cs);
108 sI->i++;
109 LeaveCriticalSection(&sI->cs);
110 }
111
112 void *
mythread(void * arg)113 mythread(void * arg)
114 {
115 intptr_t result = 0;
116 bag_t * bag = (bag_t *) arg;
117
118 assert(bag == &threadbag[bag->threadnum]);
119 assert(bag->started == 0);
120 bag->started = 1;
121
122 /* Set to known state and type */
123
124 assert(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) == 0);
125
126 assert(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) == 0);
127
128 #ifdef _MSC_VER
129 #pragma inline_depth(0)
130 #endif
131 pthread_cleanup_push(increment_pop_count, (void *) &pop_count);
132
133 Sleep(100);
134
135 pthread_cleanup_pop(1);
136 #ifdef _MSC_VER
137 #pragma inline_depth()
138 #endif
139
140 return (void *) result;
141 }
142
143 int
main()144 main()
145 {
146 int failed = 0;
147 int i;
148 pthread_t t[NUMTHREADS + 1];
149
150 InitializeCriticalSection(&pop_count.cs);
151
152 assert((t[0] = pthread_self()).p != NULL);
153
154 for (i = 1; i <= NUMTHREADS; i++)
155 {
156 threadbag[i].started = 0;
157 threadbag[i].threadnum = i;
158 assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0);
159 }
160
161 /*
162 * Code to control or munipulate child threads should probably go here.
163 */
164 Sleep(500);
165
166 /*
167 * Give threads time to run.
168 */
169 Sleep(NUMTHREADS * 100);
170
171 /*
172 * Standard check that all threads started.
173 */
174 for (i = 1; i <= NUMTHREADS; i++)
175 {
176 if (!threadbag[i].started)
177 {
178 failed |= !threadbag[i].started;
179 fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started);
180 }
181 }
182
183 assert(!failed);
184
185 /*
186 * Check any results here. Set "failed" and only print output on failure.
187 */
188 failed = 0;
189 for (i = 1; i <= NUMTHREADS; i++)
190 {
191 int fail = 0;
192 intptr_t result = 0;
193
194 assert(pthread_join(t[i], (void **) &result) == 0);
195
196 fail = (result == (intptr_t) PTHREAD_CANCELED);
197
198 if (fail)
199 {
200 fprintf(stderr, "Thread %d: started %d: result %d\n",
201 i,
202 threadbag[i].started,
203 (int)result);
204 fflush(stderr);
205 }
206 failed = (failed || fail);
207 }
208
209 assert(!failed);
210
211 assert(pop_count.i == NUMTHREADS);
212
213 DeleteCriticalSection(&pop_count.cs);
214
215 /*
216 * Success.
217 */
218 return 0;
219 }
220
221 #else /* defined(_MSC_VER) || defined(__cplusplus) */
222
223 int
main()224 main()
225 {
226 return 0;
227 }
228
229 #endif /* defined(_MSC_VER) || defined(__cplusplus) */
230