OSDN Git Service

96ae390ce445b52a981f511da2b012cc58b4c681
[pf3gnuchains/pf3gnuchains3x.git] / winsup / testsuite / winsup.api / pthread / cancel1.c
1 /*
2  * File: cancel1.c
3  *
4  * Test Synopsis: Test setting cancel state and cancel type.
5  * - 
6  *
7  * Test Method (Validation or Falsification):
8  * - 
9  *
10  * Requirements Tested:
11  * - pthread_setcancelstate function
12  * - pthread_setcanceltype function
13  *
14  * Features Tested:
15  * - 
16  *
17  * Cases Tested:
18  * - 
19  *
20  * Description:
21  * - 
22  *
23  * Environment:
24  * - 
25  *
26  * Input:
27  * - None.
28  *
29  * Output:
30  * - File name, Line number, and failed expression on failure.
31  * - No output on success.
32  *
33  * Assumptions:
34  * - pthread_create, pthread_self work.
35  *
36  * Pass Criteria:
37  * - Process returns zero exit status.
38  *
39  * Fail Criteria:
40  * - Process returns non-zero exit status.
41  */
42
43 #include "test.h"
44
45 /*
46  * Create NUMTHREADS threads in addition to the Main thread.
47  */
48 enum {
49   NUMTHREADS = 10
50 };
51
52 typedef struct bag_t_ bag_t;
53 struct bag_t_ {
54   int threadnum;
55   int started;
56   /* Add more per-thread state variables here */
57 };
58
59 static bag_t threadbag[NUMTHREADS + 1];
60
61 void *
62 mythread(void * arg)
63 {
64   bag_t * bag = (bag_t *) arg;
65
66   assert(bag == &threadbag[bag->threadnum]);
67   assert(bag->started == 0);
68   bag->started = 1;
69
70   /* ... */
71   {
72     int oldstate;
73     int oldtype;
74
75     assert(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) == 0);
76     assert(oldstate == PTHREAD_CANCEL_ENABLE); /* Check default */
77     assert(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) == 0);
78     assert(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) == 0);
79     assert(pthread_setcancelstate(oldstate, &oldstate) == 0);
80     assert(oldstate == PTHREAD_CANCEL_DISABLE); /* Check setting */
81
82     assert(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype) == 0);
83     assert(oldtype == PTHREAD_CANCEL_DEFERRED); /* Check default */
84     assert(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) == 0);
85     assert(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) == 0);
86     assert(pthread_setcanceltype(oldtype, &oldtype) == 0);
87     assert(oldtype == PTHREAD_CANCEL_ASYNCHRONOUS); /* Check setting */
88   }
89
90   return 0;
91 }
92
93 int
94 main()
95 {
96   int failed = 0;
97   int i;
98   pthread_t t[NUMTHREADS + 1];
99
100   assert((t[0] = pthread_self()) != NULL);
101
102   for (i = 1; i <= NUMTHREADS; i++)
103     {
104       threadbag[i].started = 0;
105       threadbag[i].threadnum = i;
106       assert(pthread_create(&t[i], NULL, mythread, (void *) &threadbag[i]) == 0);
107     }
108
109   /*
110    * Code to control or munipulate child threads should probably go here.
111    */
112
113   /*
114    * Give threads time to run.
115    */
116   Sleep(NUMTHREADS * 1000);
117
118   /*
119    * Standard check that all threads started.
120    */
121   for (i = 1; i <= NUMTHREADS; i++)
122     { 
123       failed = !threadbag[i].started;
124
125       if (failed)
126         {
127           fprintf(stderr, "Thread %d: started %d\n", i, threadbag[i].started);
128         }
129     }
130
131   assert(!failed);
132
133   /*
134    * Check any results here. Set "failed" and only print ouput on failure.
135    */
136   for (i = 1; i <= NUMTHREADS; i++)
137     { 
138       /* ... */
139     }
140
141   assert(!failed);
142
143   /*
144    * Success.
145    */
146   return 0;
147 }