1 /* This test is designed to test the semaphore information services.  */
2 
3 #include   <stdio.h>
4 #include   "tx_api.h"
5 #include   "tx_semaphore.h"
6 
7 
8 static unsigned long   thread_0_counter =  0;
9 static TX_THREAD       thread_0;
10 
11 static TX_SEMAPHORE    semaphore_0;
12 static TX_SEMAPHORE    semaphore_1;
13 static TX_SEMAPHORE    semaphore_2;
14 
15 
16 /* Define thread prototypes.  */
17 
18 static void    thread_0_entry(ULONG thread_input);
19 
20 
21 UINT        _tx_semaphore_performance_info_get(TX_SEMAPHORE *semaphore_ptr, ULONG *puts, ULONG *gets,
22                     ULONG *suspensions, ULONG *timeouts);
23 
24 
25 /* Prototype for test control return.  */
26 
27 void  test_control_return(UINT status);
28 
29 
30 /* Define what the initial system looks like.  */
31 
32 #ifdef CTEST
test_application_define(void * first_unused_memory)33 void test_application_define(void *first_unused_memory)
34 #else
35 void    threadx_semaphore_information_application_define(void *first_unused_memory)
36 #endif
37 {
38 
39 UINT    status;
40 CHAR    *pointer;
41 
42     /* Put first available memory address into a character pointer.  */
43     pointer =  (CHAR *) first_unused_memory;
44 
45     /* Put system definition stuff in here, e.g. thread creates and other assorted
46        create information.  */
47 
48     status =  tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
49             pointer, TEST_STACK_SIZE_PRINTF,
50             16, 16, 100, TX_AUTO_START);
51     pointer = pointer + TEST_STACK_SIZE_PRINTF;
52 
53     /* Check for status.  */
54     if (status != TX_SUCCESS)
55     {
56 
57         printf("Running Semaphore Information Test.................................. ERROR #1\n");
58         test_control_return(1);
59     }
60 
61     /* Create a semaphore with an initial count of 1.  */
62     status =  tx_semaphore_create(&semaphore_0, "semaphore 0", 1);
63 
64     /* Check for status.  */
65     if (status != TX_SUCCESS)
66     {
67 
68         printf("Running Semaphore Information Test.................................. ERROR #2\n");
69         test_control_return(1);
70     }
71 
72     /* Create a semaphore with an initial count of 0.  */
73     status =  tx_semaphore_create(&semaphore_1, "semaphore 1", 1);
74 
75     /* Check for status.  */
76     if (status != TX_SUCCESS)
77     {
78 
79         printf("Running Semaphore Information Test.................................. ERROR #3\n");
80         test_control_return(1);
81     }
82 }
83 
84 
85 
86 /* Define the test threads.  */
87 
thread_0_entry(ULONG thread_input)88 static void    thread_0_entry(ULONG thread_input)
89 {
90 
91 UINT            status;
92 CHAR            *name;
93 ULONG           current_value;
94 TX_THREAD       *first_suspended;
95 ULONG           suspended_count;
96 TX_SEMAPHORE    *next_semaphore;
97 ULONG           puts;
98 ULONG           gets;
99 ULONG           suspensions;
100 ULONG           timeouts;
101 
102 
103     /* Inform user.  */
104     printf("Running Semaphore Information Test.................................. ");
105 
106     /* Increment thread 0 counter.  */
107     thread_0_counter++;
108 
109 #ifndef TX_DISABLE_ERROR_CHECKING
110 
111     /* Attempt to get semaphore information from a non-semaphore.  */
112     status =   tx_semaphore_info_get(TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL);
113 
114     /* Check status.  */
115     if (status != TX_SEMAPHORE_ERROR)
116     {
117 
118         /* Semaphore error.  */
119         printf("ERROR #4\n");
120         test_control_return(1);
121     }
122 
123     /* Attempt to get semaphore information from a non-created semaphore.  */
124     semaphore_2.tx_semaphore_id =  0;
125     status =   tx_semaphore_info_get(&semaphore_2, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL);
126 
127     /* Check status.  */
128     if (status != TX_SEMAPHORE_ERROR)
129     {
130 
131         /* Semaphore error.  */
132         printf("ERROR #5\n");
133         test_control_return(1);
134     }
135 #endif
136 
137     /* Attempt to get from semaphore with an instance.  Should be successful! */
138     status =  tx_semaphore_get(&semaphore_0, TX_NO_WAIT);
139 
140     /* Check status.  */
141     if (status != TX_SUCCESS)
142     {
143 
144         /* Semaphore error.  */
145         printf("ERROR #6\n");
146         test_control_return(1);
147     }
148 
149     /* Attempt to get from semaphore without an instance.  Should be unsuccessful.  */
150     status =  tx_semaphore_get(&semaphore_0, TX_NO_WAIT);
151 
152     /* Check status.  */
153     if (status != TX_NO_INSTANCE)
154     {
155 
156         /* Semaphore error.  */
157         printf("ERROR #7\n");
158         test_control_return(1);
159     }
160 
161     /* Put to semaphore that has an instance already.  Should now be 2!  */
162     status =  tx_semaphore_put(&semaphore_1);
163 
164     /* Check status.  */
165     if ((status != TX_SUCCESS) || (semaphore_1.tx_semaphore_count != 2))
166     {
167 
168         /* Semaphore error.  */
169         printf("ERROR #8\n");
170         test_control_return(1);
171     }
172 
173 
174     /* Attempt to get from semaphore with an instance.  Should be successful! */
175     status =  tx_semaphore_get(&semaphore_1, TX_NO_WAIT);
176 
177     /* Check status.  */
178     if (status != TX_SUCCESS)
179     {
180 
181         /* Semaphore error.  */
182         printf("ERROR #9\n");
183         test_control_return(1);
184     }
185 
186     /* Attempt to get from semaphore with an instance.  Should be successful.  */
187     status =  tx_semaphore_get(&semaphore_1, TX_NO_WAIT);
188 
189     /* Check status.  */
190     if (status != TX_SUCCESS)
191     {
192 
193         /* Semaphore error.  */
194         printf("ERROR #10\n");
195         test_control_return(1);
196     }
197 
198     /* Get semaphore information.  */
199     status =   tx_semaphore_info_get(&semaphore_0, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL);
200     status +=  tx_semaphore_info_get(&semaphore_0, &name, &current_value, &first_suspended, &suspended_count, &next_semaphore);
201 
202     /* Check status.  */
203     if ((status != TX_SUCCESS) || (current_value != semaphore_0.tx_semaphore_count) ||
204         (first_suspended != semaphore_0.tx_semaphore_suspension_list) || (suspended_count != semaphore_0.tx_semaphore_suspended_count) ||
205         (next_semaphore != semaphore_0.tx_semaphore_created_next))
206     {
207 
208         /* Semaphore error.  */
209         printf("ERROR #11\n");
210         test_control_return(1);
211     }
212 
213 #ifdef TX_SEMAPHORE_ENABLE_PERFORMANCE_INFO
214 
215     /* Get semaphore performance information with NULL pointer.  */
216     status =  _tx_semaphore_performance_info_get(TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL);
217 
218     /* Check for error.  */
219     if (status != TX_PTR_ERROR)
220     {
221 
222         /* Semaphore error.  */
223         printf("ERROR #12\n");
224         test_control_return(1);
225     }
226 
227     /* Get semaphore performance information.  */
228     status =  tx_semaphore_performance_info_get(&semaphore_0, TX_NULL, TX_NULL, TX_NULL, TX_NULL);
229     status += tx_semaphore_performance_info_get(&semaphore_0, &puts, &gets, &suspensions, &timeouts);
230 
231     /* Check status.  */
232     if ((status != TX_SUCCESS) || (puts != semaphore_0.tx_semaphore_performance_put_count) || (gets != semaphore_0.tx_semaphore_performance_get_count) ||
233         (suspensions != semaphore_0.tx_semaphore_performance_suspension_count) || (timeouts != semaphore_0.tx_semaphore_performance_timeout_count))
234     {
235 
236         /* Semaphore error.  */
237         printf("ERROR #13\n");
238         test_control_return(1);
239     }
240 
241     /* Get semaphore system performance information.  */
242     status =  tx_semaphore_performance_system_info_get(TX_NULL, TX_NULL, TX_NULL, TX_NULL);
243     status += tx_semaphore_performance_system_info_get(&puts, &gets, &suspensions, &timeouts);
244 
245     /* Check status.  */
246     if ((status != TX_SUCCESS) || (puts != _tx_semaphore_performance_put_count) || (gets != _tx_semaphore_performance_get_count) ||
247         (suspensions != _tx_semaphore_performance_suspension_count) || (timeouts != _tx_semaphore_performance_timeout_count))
248     {
249 
250         /* Semaphore error.  */
251         printf("ERROR #14\n");
252         test_control_return(1);
253     }
254 
255 #else
256 
257     /* Get semaphore performance information.  */
258     status =  tx_semaphore_performance_info_get(&semaphore_0, &puts, &gets, &suspensions, &timeouts);
259 
260     /* Check for error.  */
261     if (status != TX_FEATURE_NOT_ENABLED)
262     {
263 
264         /* Semaphore error.  */
265         printf("ERROR #15\n");
266         test_control_return(1);
267     }
268 
269     /* Get semaphore performance information.  */
270     status =  tx_semaphore_performance_info_get(TX_NULL, &puts, &gets, &suspensions, &timeouts);
271 
272     /* Check for error.  */
273     if (status != TX_FEATURE_NOT_ENABLED)
274     {
275 
276         /* Semaphore error.  */
277         printf("ERROR #16\n");
278         test_control_return(1);
279     }
280 
281     /* Get semaphore performance information.  */
282     status =  tx_semaphore_performance_info_get(TX_NULL, TX_NULL, &gets, &suspensions, &timeouts);
283 
284     /* Check for error.  */
285     if (status != TX_FEATURE_NOT_ENABLED)
286     {
287 
288         /* Semaphore error.  */
289         printf("ERROR #17\n");
290         test_control_return(1);
291     }
292 
293     /* Get semaphore performance information.  */
294     status =  tx_semaphore_performance_info_get(TX_NULL, TX_NULL, TX_NULL, &suspensions, &timeouts);
295 
296     /* Check for error.  */
297     if (status != TX_FEATURE_NOT_ENABLED)
298     {
299 
300         /* Semaphore error.  */
301         printf("ERROR #18\n");
302         test_control_return(1);
303     }
304 
305     /* Get semaphore performance information.  */
306     status =  tx_semaphore_performance_info_get(TX_NULL, TX_NULL, TX_NULL, TX_NULL, &timeouts);
307 
308     /* Check for error.  */
309     if (status != TX_FEATURE_NOT_ENABLED)
310     {
311 
312         /* Semaphore error.  */
313         printf("ERROR #19\n");
314         test_control_return(1);
315     }
316 
317     /* Get semaphore performance information.  */
318     status =  tx_semaphore_performance_info_get(TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL);
319 
320     /* Check for error.  */
321     if (status != TX_FEATURE_NOT_ENABLED)
322     {
323 
324         /* Semaphore error.  */
325         printf("ERROR #20\n");
326         test_control_return(1);
327     }
328 
329     /* Get semaphore system performance information.  */
330     status =  tx_semaphore_performance_system_info_get(&puts, &gets, &suspensions, &timeouts);
331 
332     /* Check for error.  */
333     if (status != TX_FEATURE_NOT_ENABLED)
334     {
335 
336         /* Semaphore error.  */
337         printf("ERROR #21\n");
338         test_control_return(1);
339     }
340 
341     /* Get semaphore system performance information.  */
342     status =  tx_semaphore_performance_system_info_get(TX_NULL, &gets, &suspensions, &timeouts);
343 
344     /* Check for error.  */
345     if (status != TX_FEATURE_NOT_ENABLED)
346     {
347 
348         /* Semaphore error.  */
349         printf("ERROR #22\n");
350         test_control_return(1);
351     }
352 
353     /* Get semaphore system performance information.  */
354     status =  tx_semaphore_performance_system_info_get(TX_NULL, TX_NULL, &suspensions, &timeouts);
355 
356     /* Check for error.  */
357     if (status != TX_FEATURE_NOT_ENABLED)
358     {
359 
360         /* Semaphore error.  */
361         printf("ERROR #23\n");
362         test_control_return(1);
363     }
364 
365     /* Get semaphore system performance information.  */
366     status =  tx_semaphore_performance_system_info_get(TX_NULL, TX_NULL, TX_NULL, &timeouts);
367 
368     /* Check for error.  */
369     if (status != TX_FEATURE_NOT_ENABLED)
370     {
371 
372         /* Semaphore error.  */
373         printf("ERROR #24\n");
374         test_control_return(1);
375     }
376 
377     /* Get semaphore system performance information.  */
378     status =  tx_semaphore_performance_system_info_get(TX_NULL, TX_NULL, TX_NULL, TX_NULL);
379 
380     /* Check for error.  */
381     if (status != TX_FEATURE_NOT_ENABLED)
382     {
383 
384         /* Semaphore error.  */
385         printf("ERROR #25\n");
386         test_control_return(1);
387     }
388 #endif
389 
390     /* Delete semaphores.  */
391     status =  tx_semaphore_delete(&semaphore_0);
392 
393     /* Check status.  */
394     if (status != TX_SUCCESS)
395     {
396 
397         /* Semaphore error.  */
398         printf("ERROR #26\n");
399         test_control_return(1);
400     }
401 
402     status =  tx_semaphore_delete(&semaphore_1);
403     /* Check status.  */
404     if (status != TX_SUCCESS)
405     {
406 
407         /* Semaphore error.  */
408         printf("ERROR #27\n");
409         test_control_return(1);
410     }
411     else
412     {
413 
414         /* Successful test.  */
415         printf("SUCCESS!\n");
416         test_control_return(0);
417     }
418 }
419