1 /*
2 * File: cancel1.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 setting cancel state and cancel type.
37 * -
38 *
39 * Test Method (Validation or Falsification):
40 * -
41 *
42 * Requirements Tested:
43 * - pthread_setcancelstate function
44 * - pthread_setcanceltype function
45 *
46 * Features Tested:
47 * -
48 *
49 * Cases Tested:
50 * -
51 *
52 * Description:
53 * -
54 *
55 * Environment:
56 * -
57 *
58 * Input:
59 * - None.
60 *
61 * Output:
62 * - File name, Line number, and failed expression on failure.
63 * - No output on success.
64 *
65 * Assumptions:
66 * - pthread_create, pthread_self work.
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 NUMTHREADS = 2
82 };
83
84 typedef struct bag_t_ bag_t;
85 struct bag_t_ {
86 int threadnum;
87 int started;
88 /* Add more per-thread state variables here */
89 };
90
91 static bag_t threadbag[NUMTHREADS + 1];
92
93 void *
mythread(void * arg)94 mythread(void * arg)
95 {
96 bag_t * bag = (bag_t *) arg;
97
98 assert(bag == &threadbag[bag->threadnum]);
99 assert(bag->started == 0);
100 bag->started = 1;
101
102 /* ... */
103 {
104 int oldstate;
105 int oldtype;
106
107 assert(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) == 0);
108 assert(oldstate == PTHREAD_CANCEL_ENABLE); /* Check default */
109 assert(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) == 0);
110 assert(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) == 0);
111 assert(pthread_setcancelstate(oldstate, &oldstate) == 0);
112 assert(oldstate == PTHREAD_CANCEL_DISABLE); /* Check setting */
113
114 assert(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype) == 0);
115 assert(oldtype == PTHREAD_CANCEL_DEFERRED); /* Check default */
116 assert(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) == 0);
117 assert(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) == 0);
118 assert(pthread_setcanceltype(oldtype, &oldtype) == 0);
119 assert(oldtype == PTHREAD_CANCEL_ASYNCHRONOUS); /* Check setting */
120 }
121
122 return 0;
123 }
124
125 int
main()126 main()
127 {
128 int failed = 0;
129 int i;
130 pthread_t t[NUMTHREADS + 1];
131
132 assert((t[0] = pthread_self()) != 0);
133 assert(pthread_gethandle (t[0]) != NULL);
134
135 for (i = 1; i <= NUMTHREADS; i++)
136 {
137 threadbag[i].started = 0;
138 threadbag[i].threadnum = i;
139 assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0);
140 }
141
142 /*
143 * Code to control or munipulate child threads should probably go here.
144 */
145
146 /*
147 * Give threads time to run.
148 */
149 Sleep(NUMTHREADS * 1000);
150
151 /*
152 * Standard check that all threads started.
153 */
154 for (i = 1; i <= NUMTHREADS; i++)
155 {
156 failed = !threadbag[i].started;
157
158 if (failed)
159 {
160 fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started);
161 }
162 }
163
164 assert(!failed);
165
166 /*
167 * Check any results here. Set "failed" and only print ouput on failure.
168 */
169 for (i = 1; i <= NUMTHREADS; i++)
170 {
171 /* ... */
172 }
173
174 assert(!failed);
175
176 /*
177 * Success.
178 */
179 return 0;
180 }
181