• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * File: cancel3.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 asynchronous cancelation (alertable or non-alertable).
37  *
38  * Test Method (Validation or Falsification):
39  * -
40  *
41  * Requirements Tested:
42  * - Async cancel if thread is not blocked (i.e. voluntarily resumes if blocked).
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  * - quserex.dll and alertdrv.sys are not available.
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 
77 /*
78  * Create NUMTHREADS threads in addition to the Main thread.
79  */
80 enum
81 {
82   NUMTHREADS = 4
83 };
84 
85 typedef struct bag_t_ bag_t;
86 struct bag_t_
87 {
88   int threadnum;
89   int started;
90   /* Add more per-thread state variables here */
91   int count;
92 };
93 
94 static bag_t threadbag[NUMTHREADS + 1];
95 
96 void *
mythread(void * arg)97 mythread (void *arg)
98 {
99   intptr_t result = (((intptr_t) PTHREAD_CANCELED) + 1);
100   bag_t *bag = (bag_t *) arg;
101 
102   assert (bag == &threadbag[bag->threadnum]);
103   assert (bag->started == 0);
104   bag->started = 1;
105 
106   /* Set to known state and type */
107 
108   assert (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) == 0);
109 
110   assert (pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL) == 0);
111 
112   /*
113    * We wait up to 10 seconds, waking every 0.1 seconds,
114    * for a cancelation to be applied to us.
115    */
116   for (bag->count = 0; bag->count < 100; bag->count++)
117     Sleep (100);
118 
119   return (void *) (size_t) result;
120 }
121 
122 int
main()123 main ()
124 {
125   int failed = 0;
126   int i;
127   pthread_t t[NUMTHREADS + 1];
128 
129   assert ((t[0] = pthread_self ()) != 0);
130   assert(pthread_gethandle (t[0]) != NULL);
131 
132   for (i = 1; i <= NUMTHREADS; i++)
133     {
134       threadbag[i].started = 0;
135       threadbag[i].threadnum = i;
136       assert (pthread_create (&t[i], NULL, mythread, (void *) &threadbag[i])
137 	      == 0);
138     }
139 
140   /*
141    * Code to control or munipulate child threads should probably go here.
142    */
143   Sleep (500);
144 
145   for (i = 1; i <= NUMTHREADS; i++)
146     {
147       assert (pthread_cancel (t[i]) == 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,
164 		   threadbag[i].started);
165 	}
166     }
167 
168   assert (!failed);
169 
170   /*
171    * Check any results here. Set "failed" and only print output on failure.
172    */
173   failed = 0;
174   for (i = 1; i <= NUMTHREADS; i++)
175     {
176       int fail = 0;
177       intptr_t result = 0;
178 
179       /*
180        * The thread does not contain any cancelation points, so
181        * a return value of PTHREAD_CANCELED confirms that async
182        * cancelation succeeded.
183        */
184       assert (pthread_join (t[i], (void **) &result) == 0);
185 
186       fail = (result != (intptr_t) PTHREAD_CANCELED);
187 
188       if (fail)
189 	{
190 	  fprintf (stderr, "Thread %d: started %d: count %d\n",
191 		   i, threadbag[i].started, threadbag[i].count);
192 	}
193       failed = (failed || fail);
194     }
195 
196   assert (!failed);
197 
198   /*
199    * Success.
200    */
201   return 0;
202 }
203