1 /* Simple nested-signaling test. */
2
3 #include "pthread.h"
4
5 #define DEMO_STACK_SIZE 2048
6 #define DEMO_BYTE_POOL_SIZE 9120
7
8
9 /* Define the POSIX pthread object control blocks ... */
10
11 pthread_t pthread_0;
12
13
14 /* Define pthread attributes objects */
15
16
17 pthread_attr_t ptattr0;
18
19
20 /* Define the counters used in this test application... */
21
22 ULONG pthread_0_counter;
23 ULONG pthread_0_signal_counter15;
24 ULONG pthread_0_signal_counter14;
25 ULONG pthread_0_signal_counter13;
26
27
28 /* Define pthread function prototypes. */
29
30 VOID *pthread_0_entry(VOID *);
31
32
33 /* Define signal handlers. */
34
35 VOID pthread_0_signal_handler15(int);
36 VOID pthread_0_signal_handler14(int);
37 VOID pthread_0_signal_handler13(int);
38
39 ULONG free_memory[192*1024 / sizeof(ULONG)];
40 /* Define main entry point. */
41
main()42 INT main()
43 {
44
45 /* Enter the ThreadX kernel. */
46 tx_kernel_enter();
47 }
48
49
50 /* Define what the initial system looks like. */
tx_application_define(VOID * first_unused_memory)51 VOID tx_application_define(VOID *first_unused_memory)
52 {
53
54 VOID* storage_ptr;
55
56
57 struct sched_param param;
58
59
60 /* Init POSIX Wrapper */
61 storage_ptr = (VOID*) posix_initialize((VOID* )free_memory);
62
63 /* Put system definition stuff in here, e.g. pthread creates and other assoerted
64 create information. */
65
66 /* Create pthread attributes. */
67 pthread_attr_init(&ptattr0);
68
69 /* Create a sched_param structure */
70 memset(¶m, 0, sizeof(param));
71
72 /* Now create all pthreads , firstly modify respective ptheread
73 attribute with desired priority and stack start address and then create the pthread */
74
75 /* Create pthread 0. */
76 param.sched_priority = 10;
77 pthread_attr_setschedparam(&ptattr0, ¶m);
78 pthread_attr_setstackaddr(&ptattr0, storage_ptr );
79 storage_ptr = (int *) storage_ptr + DEMO_STACK_SIZE;
80 pthread_create (&pthread_0, &ptattr0,pthread_0_entry,NULL);
81 }
82
83
error_handler(void)84 VOID error_handler(void)
85 {
86
87 while(1)
88 {
89 }
90 }
91
92
93 /* Define the signal handlers. */
94
95
pthread_0_signal_handler13(int signo)96 VOID pthread_0_signal_handler13(int signo)
97 {
98
99 /* Check for pthread self call not pthread 0. The signal handler should appear to be
100 called from pthread 0. */
101 if (pthread_self() != pthread_0)
102 {
103
104 /* Call error handler. */
105 error_handler();
106 }
107
108 /* Check for proper signal. */
109 if (signo != 13)
110 {
111
112 /* Call error handler. */
113 error_handler();
114 }
115
116 /* Just increment the signal counter for this test. */
117 pthread_0_signal_counter13++;
118 }
119
120
pthread_0_signal_handler14(int signo)121 VOID pthread_0_signal_handler14(int signo)
122 {
123
124 /* Check for pthread self call not pthread 0. The signal handler should appear to be
125 called from pthread 0. */
126 if (pthread_self() != pthread_0)
127 {
128
129 /* Call error handler. */
130 error_handler();
131 }
132
133 /* Check for proper signal. */
134 if (signo != 14)
135 {
136
137 /* Call error handler. */
138 error_handler();
139 }
140
141 /* Just increment the signal counter for this test. */
142 pthread_0_signal_counter14++;
143
144 /* Raise another signal for nesting test. */
145 pthread_kill(pthread_0, 13);
146 }
147
148
pthread_0_signal_handler15(int signo)149 VOID pthread_0_signal_handler15(int signo)
150 {
151
152 /* Check for pthread self call not pthread 0. The signal handler should appear to be
153 called from pthread 0. */
154 if (pthread_self() != pthread_0)
155 {
156
157 /* Call error handler. */
158 error_handler();
159 }
160
161 /* Check for proper signal. */
162 if (signo != 15)
163 {
164
165 /* Call error handler. */
166 error_handler();
167 }
168
169 /* Just increment the signal counter for this test. */
170 pthread_0_signal_counter15++;
171
172 /* Raise another signal for nesting test. */
173 pthread_kill(pthread_0, 14);
174 }
175
176
177 /* Define the test pthreads */
178 INT pt0_status=0;
179
180
181 /* Self signal test. */
182
pthread_0_entry(VOID * pthread0_input)183 VOID *pthread_0_entry(VOID *pthread0_input)
184 {
185
186 /* Register the signal handlers. */
187 pt0_status = signal(15, pthread_0_signal_handler15);
188
189 /* Check for error. */
190 if (pt0_status)
191 error_handler();
192
193 pt0_status = signal(14, pthread_0_signal_handler14);
194
195 /* Check for error. */
196 if (pt0_status)
197 error_handler();
198
199 pt0_status = signal(13, pthread_0_signal_handler13);
200
201 /* Check for error. */
202 if (pt0_status)
203 error_handler();
204
205 /* This pthread simply sits in while-forever-sleep loop */
206 while(1)
207 {
208 /* Increment the pthread counter.*/
209 pthread_0_counter++;
210
211 /* Raise the signal. */
212 pt0_status = pthread_kill(pthread_0, 15);
213
214 /* Check for errors. */
215 if ((pt0_status) ||
216 (pthread_0_counter != pthread_0_signal_counter15) ||
217 (pthread_0_counter != pthread_0_signal_counter14) ||
218 (pthread_0_counter != pthread_0_signal_counter13))
219 {
220
221 error_handler();
222 break;
223 }
224 }
225
226 return(&pt0_status);
227 }
228
229