1 /*
2  * File: cleanup2.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  *      51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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   int 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 #ifdef _MSC_VER
123 #pragma inline_depth(0)
124 #endif
125   pthread_cleanup_push(increment_pop_count, (void *) &pop_count);
126 
127   sched_yield();
128 
129   pthread_cleanup_pop(1);
130 #ifdef _MSC_VER
131 #pragma inline_depth()
132 #endif
133 
134   return (void *) (size_t)result;
135 }
136 
137 int
main()138 main()
139 {
140   int failed = 0;
141   int i;
142   pthread_t t[NUMTHREADS + 1];
143 
144   InitializeCriticalSection(&pop_count.cs);
145 
146   assert((t[0] = pthread_self()).p != NULL);
147 
148   for (i = 1; i <= NUMTHREADS; i++)
149     {
150       threadbag[i].started = 0;
151       threadbag[i].threadnum = i;
152       assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0);
153     }
154 
155   /*
156    * Code to control or munipulate child threads should probably go here.
157    */
158   Sleep(1000);
159 
160   /*
161    * Standard check that all threads started.
162    */
163   for (i = 1; i <= NUMTHREADS; i++)
164     {
165       if (!threadbag[i].started)
166 	{
167 	  failed |= !threadbag[i].started;
168 	  fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started);
169 	}
170     }
171 
172   assert(!failed);
173 
174   /*
175    * Check any results here. Set "failed" and only print output on failure.
176    */
177   failed = 0;
178   for (i = 1; i <= NUMTHREADS; i++)
179     {
180       int fail = 0;
181       void* result = (void*)0;
182 
183       assert(pthread_join(t[i], &result) == 0);
184 
185       fail = ((int)(size_t)result != 0);
186 
187       if (fail)
188 	{
189 	  fprintf(stderr, "Thread %d: started %d: result: %d\n",
190 		  i,
191 		  threadbag[i].started,
192 		  (int)(size_t)result);
193 	}
194       failed = (failed || fail);
195     }
196 
197   assert(!failed);
198 
199   assert(pop_count.i == NUMTHREADS);
200 
201   DeleteCriticalSection(&pop_count.cs);
202 
203   /*
204    * Success.
205    */
206   return 0;
207 }
208 
209 #else /* defined(_MSC_VER) || defined(__cplusplus) */
210 
211 int
main()212 main()
213 {
214   return 0;
215 }
216 
217 #endif /* defined(_MSC_VER) || defined(__cplusplus) */
218