• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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
103 #ifdef __CLEANUP_C
104 __cdecl
105 #endif
increment_pop_count(void * arg)106 increment_pop_count(void * arg)
107 {
108   sharedInt_t * sI = (sharedInt_t *) arg;
109 
110   EnterCriticalSection(&sI->cs);
111   sI->i++;
112   LeaveCriticalSection(&sI->cs);
113 }
114 
115 void *
mythread(void * arg)116 mythread(void * arg)
117 {
118   intptr_t result = 0;
119   bag_t * bag = (bag_t *) arg;
120 
121   assert(bag == &threadbag[bag->threadnum]);
122   assert(bag->started == 0);
123   bag->started = 1;
124 
125   /* Set to known state and type */
126 
127   assert(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) == 0);
128 
129   assert(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) == 0);
130 
131 #ifdef _MSC_VER
132 #pragma inline_depth(0)
133 #endif
134   pthread_cleanup_push(increment_pop_count, (void *) &pop_count);
135   /*
136    * We don't have true async cancelation - it relies on the thread
137    * at least re-entering the run state at some point.
138    * We wait up to 10 seconds, waking every 0.1 seconds,
139    * for a cancelation to be applied to us.
140    */
141   for (bag->count = 0; bag->count < 100; bag->count++)
142     Sleep(100);
143 
144   pthread_cleanup_pop(0);
145 #ifdef _MSC_VER
146 #pragma inline_depth()
147 #endif
148 
149   return (void *) result;
150 }
151 
152 int
main()153 main()
154 {
155   int failed = 0;
156   int i;
157   pthread_t t[NUMTHREADS + 1];
158 
159   InitializeCriticalSection(&pop_count.cs);
160 
161   assert((t[0] = pthread_self()).p != NULL);
162 
163   for (i = 1; i <= NUMTHREADS; i++)
164     {
165       threadbag[i].started = 0;
166       threadbag[i].threadnum = i;
167       assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0);
168     }
169 
170   /*
171    * Code to control or munipulate child threads should probably go here.
172    */
173   Sleep(500);
174 
175   for (i = 1; i <= NUMTHREADS; i++)
176     {
177       assert(pthread_cancel(t[i]) == 0);
178     }
179 
180   /*
181    * Give threads time to run.
182    */
183   Sleep(NUMTHREADS * 100);
184 
185   /*
186    * Standard check that all threads started.
187    */
188   for (i = 1; i <= NUMTHREADS; i++)
189     {
190       if (!threadbag[i].started)
191 	{
192 	  failed |= !threadbag[i].started;
193 	  fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started);
194 	}
195     }
196 
197   assert(!failed);
198 
199   /*
200    * Check any results here. Set "failed" and only print output on failure.
201    */
202   failed = 0;
203   for (i = 1; i <= NUMTHREADS; i++)
204     {
205       int fail = 0;
206       intptr_t result = 0;
207 
208       assert(pthread_join(t[i], (void **) &result) == 0);
209 
210       fail = (result != (intptr_t) PTHREAD_CANCELED);
211 
212       if (fail)
213 	{
214 	  fprintf(stderr, "Thread %d: started %d: result %d\n",
215 		  i,
216 		  threadbag[i].started,
217 		  (int)result);
218 	}
219       failed = (failed || fail);
220     }
221 
222   assert(!failed);
223 
224   assert(pop_count.i == NUMTHREADS);
225 
226   DeleteCriticalSection(&pop_count.cs);
227 
228   /*
229    * Success.
230    */
231   return 0;
232 }
233 
234 #else /* defined(_MSC_VER) || defined(__cplusplus) */
235 
236 int
main()237 main()
238 {
239   return 0;
240 }
241 
242 #endif /* defined(_MSC_VER) || defined(__cplusplus) */
243