1 /* This test is designed to test the mutex information services.  */
2 
3 #include   <stdio.h>
4 #include   "tx_api.h"
5 #include   "tx_mutex.h"
6 
7 
8 static unsigned long   thread_0_counter =  0;
9 static unsigned long   thread_1_counter =  0;
10 static TX_THREAD       thread_0;
11 static TX_THREAD       thread_1;
12 
13 static TX_MUTEX        mutex_0;
14 static TX_MUTEX        mutex_1;
15 static TX_MUTEX        mutex_2;
16 static TX_MUTEX        mutex_3;
17 static TX_MUTEX        mutex_4;
18 
19 
20 /* Define thread prototypes.  */
21 
22 static void    thread_0_entry(ULONG thread_input);
23 static void    thread_1_entry(ULONG thread_input);
24 
25 
26 /* Prototype for test control return.  */
27 
28 void  test_control_return(UINT status);
29 
30 
31 UINT  _tx_mutex_performance_info_get(TX_MUTEX *mutex_ptr, ULONG *puts, ULONG *gets,
32                     ULONG *suspensions, ULONG *timeouts, ULONG *inversions, ULONG *inheritances);
33 
34 
35 /* Define what the initial system looks like.  */
36 
37 #ifdef CTEST
test_application_define(void * first_unused_memory)38 void test_application_define(void *first_unused_memory)
39 #else
40 void    threadx_mutex_information_application_define(void *first_unused_memory)
41 #endif
42 {
43 
44 UINT    status;
45 CHAR    *pointer;
46 
47     /* Put first available memory address into a character pointer.  */
48     pointer =  (CHAR *) first_unused_memory;
49 
50     /* Put system definition stuff in here, e.g. thread creates and other assorted
51        create information.  */
52 
53     status =  tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
54             pointer, TEST_STACK_SIZE_PRINTF,
55             16, 16, TX_NO_TIME_SLICE, TX_AUTO_START);
56     pointer = pointer + TEST_STACK_SIZE_PRINTF;
57 
58 
59     status =  tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
60             pointer, TEST_STACK_SIZE_PRINTF,
61             16, 16, TX_NO_TIME_SLICE, TX_AUTO_START);
62     pointer = pointer + TEST_STACK_SIZE_PRINTF;
63 
64 
65     /* Check for status.  */
66     if (status != TX_SUCCESS)
67     {
68 
69         printf("Running Mutex Information Test...................................... ERROR #1\n");
70         test_control_return(1);
71     }
72 
73     /* Create a mutex.  */
74     status =  tx_mutex_create(&mutex_0, "mutex 0", TX_NO_INHERIT);
75 
76     /* Check for status.  */
77     if (status != TX_SUCCESS)
78     {
79 
80         printf("Running Mutex Information Test...................................... ERROR #2\n");
81         test_control_return(1);
82     }
83 
84     /* Create another mutex.  */
85     status =  tx_mutex_create(&mutex_1, "mutex 1", TX_NO_INHERIT);
86 
87     /* Check for status.  */
88     if (status != TX_SUCCESS)
89     {
90 
91         printf("Running Mutex Information Test...................................... ERROR #3\n");
92         test_control_return(1);
93     }
94 
95     /* Create another mutex.  */
96     status =  tx_mutex_create(&mutex_2, "mutex 2", TX_INHERIT);
97 
98     /* Check for status.  */
99     if (status != TX_SUCCESS)
100     {
101 
102         printf("Running Mutex Information Test...................................... ERROR #4\n");
103         test_control_return(1);
104     }
105 
106     /* Create another mutex.  */
107     status =  tx_mutex_create(&mutex_3, "mutex 3", TX_INHERIT);
108 
109     /* Check for status.  */
110     if (status != TX_SUCCESS)
111     {
112 
113         printf("Running Mutex Information Test...................................... ERROR #5\n");
114         test_control_return(1);
115     }
116 }
117 
118 
119 
120 /* Define the test threads.  */
121 
thread_0_entry(ULONG thread_input)122 static void    thread_0_entry(ULONG thread_input)
123 {
124 
125 UINT        status;
126 CHAR        *name;
127 ULONG       count;
128 TX_THREAD   *owner;
129 TX_THREAD   *first_suspended;
130 ULONG       suspended_count;
131 TX_MUTEX    *next_mutex;
132 ULONG       puts;
133 ULONG       gets;
134 ULONG       suspensions;
135 ULONG       timeouts;
136 ULONG       inversions;
137 ULONG       inheritances;
138 
139 
140     /* Inform user.  */
141     printf("Running Mutex Information Test...................................... ");
142 
143     /* Increment thread 0 counter.  */
144     thread_0_counter++;
145 
146     /* Attempt to get from mutex that is available.  Should be successful!  */
147     status =  tx_mutex_get(&mutex_0, TX_NO_WAIT);
148 
149     /* Check status.  */
150     if (status != TX_SUCCESS)
151     {
152 
153         /* Mutex error.  */
154         printf("ERROR #6\n");
155         test_control_return(1);
156     }
157 
158     /* Attempt to get the same mutex again.  Should be successful!  */
159     status =  tx_mutex_get(&mutex_0, TX_NO_WAIT);
160 
161     /* Check status.  */
162     if (status != TX_SUCCESS)
163     {
164 
165         /* Mutex error.  */
166         printf("ERROR #7\n");
167         test_control_return(1);
168     }
169 
170     /* Put the mutex. */
171     status =  tx_mutex_put(&mutex_0);
172 
173     /* Check status.  */
174     if ((status != TX_SUCCESS) || (mutex_0.tx_mutex_ownership_count != 1))
175     {
176 
177         /* Mutex error.  */
178         printf("ERROR #8\n");
179         test_control_return(1);
180     }
181 
182     /* Put the mutex again.  Should be successful! */
183     status =  tx_mutex_put(&mutex_0);
184 
185     /* Check status.  */
186     if (status != TX_SUCCESS)
187     {
188 
189         /* Mutex error.  */
190         printf("ERROR #9\n");
191         test_control_return(1);
192     }
193 
194     /* Relinquish to allow other thread to get the mutex.  */
195     tx_thread_relinquish();
196 
197     /* Attempt to get the mutex.  Should be unsuccessful.  */
198     status =  tx_mutex_get(&mutex_1, TX_NO_WAIT);
199 
200     /* Check status.  */
201     if (status != TX_NOT_AVAILABLE)
202     {
203 
204         /* Mutex error.  */
205         printf("ERROR #10\n");
206         test_control_return(1);
207     }
208 
209     /* Relinquish again so that the other thread can release it.  */
210     tx_thread_relinquish();
211 
212     /* Delete mutex.  */
213     status =  tx_mutex_delete(&mutex_0);
214 
215     /* Check status.  */
216     if (status != TX_SUCCESS)
217     {
218 
219         /* Mutex error.  */
220         printf("ERROR #11\n");
221         test_control_return(1);
222     }
223 
224     status =  tx_mutex_delete(&mutex_1);
225 
226     /* Check status.  */
227     if (status != TX_SUCCESS)
228     {
229 
230         /* Mutex error.  */
231         printf("ERROR #12\n");
232         test_control_return(1);
233     }
234 
235     /* Attempt to get a priority inheritance mutex.  */
236     status =  tx_mutex_get(&mutex_2, TX_NO_WAIT);
237 
238     /* Check status.  */
239     if (status != TX_SUCCESS)
240     {
241 
242         /* Mutex error.  */
243         printf("ERROR #13\n");
244         test_control_return(1);
245     }
246 
247     /* Attempt to get another priority inheritance mutex.  */
248     status =  tx_mutex_get(&mutex_3, TX_NO_WAIT);
249 
250     /* Check status.  */
251     if (status != TX_SUCCESS)
252     {
253 
254         /* Mutex error.  */
255         printf("ERROR #14\n");
256         test_control_return(1);
257     }
258 
259 #ifndef TX_DISABLE_ERROR_CHECKING
260 
261     /* Attempt to get mutex info with a NULL pointer.  */
262     status =  tx_mutex_info_get(TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL);
263 
264     /* Check status.  */
265     if (status != TX_MUTEX_ERROR)
266     {
267 
268         /* Mutex error.  */
269         printf("ERROR #15\n");
270         test_control_return(1);
271     }
272 
273     /* Attempt to get mutex info from a non-created mutex.  */
274     mutex_4.tx_mutex_id =  0;
275     status =  tx_mutex_info_get(&mutex_4, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL);
276 
277     /* Check status.  */
278     if (status != TX_MUTEX_ERROR)
279     {
280 
281         /* Mutex error.  */
282         printf("ERROR #16\n");
283         test_control_return(1);
284     }
285 #endif
286 
287     /* Get mutex information.  */
288     status =  tx_mutex_info_get(&mutex_2, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL);
289     status += tx_mutex_info_get(&mutex_2, &name, &count, &owner, &first_suspended, &suspended_count, &next_mutex);
290 
291     /* Check status.  */
292     if ((status != TX_SUCCESS) || (count != mutex_2.tx_mutex_ownership_count) || (owner != mutex_2.tx_mutex_owner) ||
293         (first_suspended != mutex_2.tx_mutex_suspension_list) || (suspended_count != mutex_2.tx_mutex_suspended_count) || (next_mutex != mutex_2.tx_mutex_created_next))
294     {
295 
296         /* Mutex error.  */
297         printf("ERROR #17\n");
298         test_control_return(1);
299     }
300 
301 #ifdef TX_MUTEX_ENABLE_PERFORMANCE_INFO
302 
303     /* Call with NULL pointer.   */
304     status =  _tx_mutex_performance_info_get(TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL);
305 
306     /* Check status.  */
307     if (status != TX_PTR_ERROR)
308     {
309 
310         /* Mutex error.  */
311         printf("ERROR #18\n");
312         test_control_return(1);
313     }
314 
315     /* Now get the performance inforamtion.  */
316     status =  tx_mutex_performance_info_get(&mutex_2, &puts, &gets, &suspensions, &timeouts, &inversions, &inheritances);
317 
318     /* Check status.  */
319     if ((status != TX_SUCCESS) || (puts != mutex_2.tx_mutex_performance_put_count) || (gets != mutex_2.tx_mutex_performance_get_count) ||
320         (suspensions != mutex_2.tx_mutex_performance_suspension_count) || (timeouts != mutex_2.tx_mutex_performance_timeout_count) ||
321         (inversions != mutex_2.tx_mutex_performance_priority_inversion_count) || (inheritances != mutex_2.tx_mutex_performance__priority_inheritance_count))
322     {
323 
324         /* Mutex error.  */
325         printf("ERROR #19\n");
326         test_control_return(1);
327     }
328 
329     /* Now get the system performance inforamtion.  */
330     status =  tx_mutex_performance_system_info_get(&puts, &gets, &suspensions, &timeouts, &inversions, &inheritances);
331 
332     /* Check status.  */
333     if ((status != TX_SUCCESS) || (puts != _tx_mutex_performance_put_count) || (gets != _tx_mutex_performance_get_count) ||
334         (suspensions != _tx_mutex_performance_suspension_count) || (timeouts != _tx_mutex_performance_timeout_count) ||
335         (inversions != _tx_mutex_performance_priority_inversion_count) || (inheritances != _tx_mutex_performance__priority_inheritance_count))
336     {
337 
338         /* Mutex error.  */
339         printf("ERROR #20\n");
340         test_control_return(1);
341     }
342 
343 #else
344 
345     /* Now get the performance inforamtion.  */
346     status =  tx_mutex_performance_info_get(&mutex_2, &puts, &gets, &suspensions, &timeouts, &inversions, &inheritances);
347 
348     /* Check status.  */
349     if (status != TX_FEATURE_NOT_ENABLED)
350     {
351 
352         /* Mutex error.  */
353         printf("ERROR #21\n");
354         test_control_return(1);
355     }
356 
357     /* Now get the performance inforamtion.  */
358     status =  tx_mutex_performance_info_get(TX_NULL, &puts, &gets, &suspensions, &timeouts, &inversions, &inheritances);
359 
360     /* Check status.  */
361     if (status != TX_FEATURE_NOT_ENABLED)
362     {
363 
364         /* Mutex error.  */
365         printf("ERROR #22\n");
366         test_control_return(1);
367     }
368 
369     /* Now get the performance inforamtion.  */
370     status =  tx_mutex_performance_info_get(TX_NULL, TX_NULL, &gets, &suspensions, &timeouts, &inversions, &inheritances);
371 
372     /* Check status.  */
373     if (status != TX_FEATURE_NOT_ENABLED)
374     {
375 
376         /* Mutex error.  */
377         printf("ERROR #23\n");
378         test_control_return(1);
379     }
380 
381     /* Now get the performance inforamtion.  */
382     status =  tx_mutex_performance_info_get(TX_NULL, TX_NULL, TX_NULL, &suspensions, &timeouts, &inversions, &inheritances);
383 
384     /* Check status.  */
385     if (status != TX_FEATURE_NOT_ENABLED)
386     {
387 
388         /* Mutex error.  */
389         printf("ERROR #24\n");
390         test_control_return(1);
391     }
392 
393     /* Now get the performance inforamtion.  */
394     status =  tx_mutex_performance_info_get(TX_NULL, TX_NULL, TX_NULL, TX_NULL, &timeouts, &inversions, &inheritances);
395 
396     /* Check status.  */
397     if (status != TX_FEATURE_NOT_ENABLED)
398     {
399 
400         /* Mutex error.  */
401         printf("ERROR #25\n");
402         test_control_return(1);
403     }
404 
405     /* Now get the performance inforamtion.  */
406     status =  tx_mutex_performance_info_get(TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL, &inversions, &inheritances);
407 
408     /* Check status.  */
409     if (status != TX_FEATURE_NOT_ENABLED)
410     {
411 
412         /* Mutex error.  */
413         printf("ERROR #26\n");
414         test_control_return(1);
415     }
416 
417     /* Now get the performance inforamtion.  */
418     status =  tx_mutex_performance_info_get(TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL, &inheritances);
419 
420     /* Check status.  */
421     if (status != TX_FEATURE_NOT_ENABLED)
422     {
423 
424         /* Mutex error.  */
425         printf("ERROR #27\n");
426         test_control_return(1);
427     }
428 
429     /* Now get the performance inforamtion.  */
430     status =  tx_mutex_performance_info_get(TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL);
431 
432     /* Check status.  */
433     if (status != TX_FEATURE_NOT_ENABLED)
434     {
435 
436         /* Mutex error.  */
437         printf("ERROR #28\n");
438         test_control_return(1);
439     }
440 
441     /* Now get the system performance inforamtion.  */
442     status =  tx_mutex_performance_system_info_get(&puts, &gets, &suspensions, &timeouts, &inversions, &inheritances);
443 
444     /* Check status.  */
445     if (status != TX_FEATURE_NOT_ENABLED)
446     {
447 
448         /* Mutex error.  */
449         printf("ERROR #29\n");
450         test_control_return(1);
451     }
452 
453     /* Now get the system performance inforamtion.  */
454     status =  tx_mutex_performance_system_info_get(TX_NULL, &gets, &suspensions, &timeouts, &inversions, &inheritances);
455 
456     /* Check status.  */
457     if (status != TX_FEATURE_NOT_ENABLED)
458     {
459 
460         /* Mutex error.  */
461         printf("ERROR #30\n");
462         test_control_return(1);
463     }
464 
465     /* Now get the system performance inforamtion.  */
466     status =  tx_mutex_performance_system_info_get(TX_NULL, TX_NULL, &suspensions, &timeouts, &inversions, &inheritances);
467 
468     /* Check status.  */
469     if (status != TX_FEATURE_NOT_ENABLED)
470     {
471 
472         /* Mutex error.  */
473         printf("ERROR #31\n");
474         test_control_return(1);
475     }
476 
477     /* Now get the system performance inforamtion.  */
478     status =  tx_mutex_performance_system_info_get(TX_NULL, TX_NULL, TX_NULL, &timeouts, &inversions, &inheritances);
479 
480     /* Check status.  */
481     if (status != TX_FEATURE_NOT_ENABLED)
482     {
483 
484         /* Mutex error.  */
485         printf("ERROR #32\n");
486         test_control_return(1);
487     }
488 
489     /* Now get the system performance inforamtion.  */
490     status =  tx_mutex_performance_system_info_get(TX_NULL, TX_NULL, TX_NULL, TX_NULL, &inversions, &inheritances);
491 
492     /* Check status.  */
493     if (status != TX_FEATURE_NOT_ENABLED)
494     {
495 
496         /* Mutex error.  */
497         printf("ERROR #33\n");
498         test_control_return(1);
499     }
500 
501     /* Now get the system performance inforamtion.  */
502     status =  tx_mutex_performance_system_info_get(TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL, &inheritances);
503 
504     /* Check status.  */
505     if (status != TX_FEATURE_NOT_ENABLED)
506     {
507 
508         /* Mutex error.  */
509         printf("ERROR #34\n");
510         test_control_return(1);
511     }
512 
513     /* Now get the system performance inforamtion.  */
514     status =  tx_mutex_performance_system_info_get(TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL);
515 
516     /* Check status.  */
517     if (status != TX_FEATURE_NOT_ENABLED)
518     {
519 
520         /* Mutex error.  */
521         printf("ERROR #35\n");
522         test_control_return(1);
523     }
524 
525 #endif
526 
527     /* Delete mutex.  */
528     status =  tx_mutex_delete(&mutex_2);
529 
530     /* Check status.  */
531     if (status != TX_SUCCESS)
532     {
533 
534         /* Mutex error.  */
535         printf("ERROR #36\n");
536         test_control_return(1);
537     }
538 
539     status =  tx_mutex_delete(&mutex_3);
540 
541     /* Check status.  */
542     if (status != TX_SUCCESS)
543     {
544 
545         /* Mutex error.  */
546         printf("ERROR #37\n");
547         test_control_return(1);
548     }
549     else
550     {
551 
552         /* Successful test.  */
553         printf("SUCCESS!\n");
554         test_control_return(0);
555     }
556 }
557 
thread_1_entry(ULONG thread_input)558 static void    thread_1_entry(ULONG thread_input)
559 {
560 
561 UINT    status;
562 
563     /* Increment thread 1 counter.  */
564     thread_1_counter++;
565 
566     /* Attempt to get from mutex that is available.  Should be successful!  */
567     status =  tx_mutex_get(&mutex_1, TX_NO_WAIT);
568 
569     /* Check status.  */
570     if (status != TX_SUCCESS)
571     {
572 
573         /* Mutex error.  */
574         printf("ERROR #38\n");
575         test_control_return(1);
576     }
577 
578     /* Let other thread run again.  */
579     tx_thread_relinquish();
580 
581     /* Release mutex!  */
582     status =  tx_mutex_put(&mutex_1);
583 
584     /* Check status.  */
585     if (status != TX_SUCCESS)
586     {
587 
588         /* Mutex error.  */
589         printf("ERROR #39\n");
590         test_control_return(1);
591     }
592 }
593