1 /* This test is designed to test byte memory prioritize.  */
2 
3 #include   <stdio.h>
4 #include   "tx_api.h"
5 
6 
7 /* Define the ISR dispatch.  */
8 
9 extern VOID    (*test_isr_dispatch)(void);
10 
11 
12 /* Define the external reference for the preempt disable flag.  */
13 
14 extern volatile UINT   _tx_thread_preempt_disable;
15 
16 
17 static unsigned long   thread_0_counter =  0;
18 static TX_THREAD       thread_0;
19 
20 static unsigned long   thread_1_counter =  0;
21 static TX_THREAD       thread_1;
22 
23 static unsigned long   thread_2_counter =  0;
24 static TX_THREAD       thread_2;
25 
26 static unsigned long   thread_3_counter =  0;
27 static TX_THREAD       thread_3;
28 
29 static unsigned long   thread_4_counter =  0;
30 static TX_THREAD       thread_4;
31 
32 static unsigned long   thread_5_counter =  0;
33 static TX_THREAD       thread_5;
34 
35 static unsigned long   thread_6_counter =  0;
36 static TX_THREAD       thread_6;
37 
38 static TX_BYTE_POOL    byte_pool_0;
39 static TX_BYTE_POOL    byte_pool_1;
40 
41 static int             test_status;
42 
43 
44 /* Define thread prototypes.  */
45 
46 static void    thread_0_entry(ULONG thread_input);
47 static void    thread_1_entry(ULONG thread_input);
48 static void    thread_2_entry(ULONG thread_input);
49 static void    thread_3_entry(ULONG thread_input);
50 static void    thread_4_entry(ULONG thread_input);
51 static void    thread_5_entry(ULONG thread_input);
52 static void    thread_6_entry(ULONG thread_input);
53 
54 
55 /* Prototype for test control return.  */
56 void  test_control_return(UINT status);
57 
58 
59 /* Define the ISR dispatch routine.  */
60 
test_isr(void)61 static void    test_isr(void)
62 {
63 
64     /* Determine if the test case we are looking for is present.  */
65     if ((_tx_thread_preempt_disable) && (test_status == 1))
66     {
67 
68         /* Determine if thread 3 is at the front of the suspension list.  */
69         if (byte_pool_0.tx_byte_pool_suspension_list == &thread_3)
70         {
71 
72             /* Abort the wait of thread 3.  */
73             tx_thread_wait_abort(&thread_3);
74         }
75         else
76         {
77 
78             /* Abort the wait of thread 5.  */
79             tx_thread_wait_abort(&thread_5);
80 
81             /* End the ISR processing.  */
82             test_status =  2;
83             test_isr_dispatch =  TX_NULL;
84         }
85     }
86 }
87 
88 
89 /* Define what the initial system looks like.  */
90 
91 #ifdef CTEST
test_application_define(void * first_unused_memory)92 void test_application_define(void *first_unused_memory)
93 #else
94 void    threadx_byte_memory_prioritize_application_define(void *first_unused_memory)
95 #endif
96 {
97 
98 UINT    status;
99 CHAR    *pointer;
100 
101 
102     /* Put first available memory address into a character pointer.  */
103     pointer =  (CHAR *) first_unused_memory;
104 
105     /* Put system definition stuff in here, e.g. thread creates and other assorted
106        create information.  */
107 
108     status =  tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
109             pointer, TEST_STACK_SIZE_PRINTF,
110             17, 17, 100, TX_AUTO_START);
111     pointer = pointer + TEST_STACK_SIZE_PRINTF;
112 
113     /* Check for status.  */
114     if (status != TX_SUCCESS)
115     {
116 
117         printf("Running Byte Memory Prioritize Test................................. ERROR #1\n");
118         test_control_return(1);
119     }
120 
121     status =  tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
122             pointer, TEST_STACK_SIZE_PRINTF,
123             16, 16, 100, TX_DONT_START);
124     pointer = pointer + TEST_STACK_SIZE_PRINTF;
125 
126     /* Check for status.  */
127     if (status != TX_SUCCESS)
128     {
129 
130         printf("Running Byte Memory Prioritize Test................................. ERROR #2\n");
131         test_control_return(1);
132     }
133 
134     status =  tx_thread_create(&thread_2, "thread 2", thread_2_entry, 2,
135             pointer, TEST_STACK_SIZE_PRINTF,
136             15, 15, 100, TX_DONT_START);
137     pointer = pointer + TEST_STACK_SIZE_PRINTF;
138 
139     /* Check for status.  */
140     if (status != TX_SUCCESS)
141     {
142 
143         printf("Running Byte Memory Prioritize Test................................. ERROR #3\n");
144         test_control_return(1);
145     }
146 
147     status =  tx_thread_create(&thread_3, "thread 3", thread_3_entry, 3,
148             pointer, TEST_STACK_SIZE_PRINTF,
149             3, 3, 100, TX_DONT_START);
150     pointer = pointer + TEST_STACK_SIZE_PRINTF;
151 
152     /* Check for status.  */
153     if (status != TX_SUCCESS)
154     {
155 
156         printf("Running Byte Memory Prioritize Test................................. ERROR #4\n");
157         test_control_return(1);
158     }
159 
160     status =  tx_thread_create(&thread_4, "thread 4", thread_4_entry, 4,
161             pointer, TEST_STACK_SIZE_PRINTF,
162             4, 4, 100, TX_DONT_START);
163     pointer = pointer + TEST_STACK_SIZE_PRINTF;
164 
165     /* Check for status.  */
166     if (status != TX_SUCCESS)
167     {
168 
169         printf("Running Byte Memory Prioritize Test................................. ERROR #5\n");
170         test_control_return(1);
171     }
172 
173     status =  tx_thread_create(&thread_5, "thread 5", thread_5_entry, 5,
174             pointer, TEST_STACK_SIZE_PRINTF,
175             5, 5, 100, TX_DONT_START);
176     pointer = pointer + TEST_STACK_SIZE_PRINTF;
177 
178     /* Check for status.  */
179     if (status != TX_SUCCESS)
180     {
181 
182         printf("Running Byte Memory Prioritize Test................................. ERROR #6\n");
183         test_control_return(1);
184     }
185 
186     status =  tx_thread_create(&thread_6, "thread 6", thread_6_entry, 6,
187             pointer, TEST_STACK_SIZE_PRINTF,
188             6, 6, 100, TX_DONT_START);
189     pointer = pointer + TEST_STACK_SIZE_PRINTF;
190 
191     /* Check for status.  */
192     if (status != TX_SUCCESS)
193     {
194 
195         printf("Running Byte Memory Prioritize Test................................. ERROR #7\n");
196         test_control_return(1);
197     }
198 
199     /* Create the byte_pool with one byte.  */
200     status =  tx_byte_pool_create(&byte_pool_0, "byte_pool 0", pointer, 100);
201     pointer = pointer + 100;
202 
203     /* Check for status.  */
204     if (status != TX_SUCCESS)
205     {
206 
207         printf("Running Byte Memory Prioritize Test................................. ERROR #8\n");
208         test_control_return(1);
209     }
210 }
211 
212 
213 
214 /* Define the test threads.  */
215 
thread_0_entry(ULONG thread_input)216 static void    thread_0_entry(ULONG thread_input)
217 {
218 
219 UINT    status;
220 VOID    *pointer;
221 
222 
223     /* Inform user.  */
224     printf("Running Byte Memory Prioritize Test................................. ");
225 
226     /* Allocate the one byte.  */
227     tx_byte_allocate(&byte_pool_0, &pointer, 80, TX_NO_WAIT);
228 
229 #ifndef TX_DISABLE_ERROR_CHECKING
230 
231     /* Call byte pool prioritize with a NULL pointer.  */
232     status =  tx_byte_pool_prioritize(TX_NULL);
233 
234     /* Check for error.  */
235     if (status != TX_POOL_ERROR)
236     {
237 
238         /* Byte Pool error.  */
239         printf("ERROR #9\n");
240         test_control_return(1);
241     }
242 
243     /* Call byte pool info with an non-created pool pointer.  */
244     byte_pool_1.tx_byte_pool_id =  0;
245     status =  tx_byte_pool_prioritize(&byte_pool_1);
246 
247     /* Check for error.  */
248     if (status != TX_POOL_ERROR)
249     {
250 
251         /* Byte Pool error.  */
252         printf("ERROR #10\n");
253         test_control_return(1);
254     }
255 #endif
256 
257     /* Nothing to do here, but check prioritization with no suspended threads.  */
258     status =  tx_byte_pool_prioritize(&byte_pool_0);
259 
260     /* Check status and make sure thread 1 is terminated.  */
261     if (status != TX_SUCCESS)
262     {
263 
264         /* Byte Pool error.  */
265         printf("ERROR #11\n");
266         test_control_return(1);
267     }
268 
269     tx_thread_resume(&thread_1);
270     tx_thread_resume(&thread_2);
271 
272     /* Increment the thread counter.  */
273     thread_0_counter++;
274 
275     /* Make sure thread 1 and 2 are suspended on the byte_pool.  */
276     if ((thread_1.tx_thread_state != TX_BYTE_MEMORY) || (thread_2.tx_thread_state != TX_BYTE_MEMORY) ||
277         (byte_pool_0.tx_byte_pool_suspension_list != &thread_1))
278     {
279 
280         /* Byte Pool error.  */
281         printf("ERROR #12\n");
282         test_control_return(1);
283     }
284 
285     /* Prioritize the byte pool suspension list.  */
286     status =  tx_byte_pool_prioritize(&byte_pool_0);
287 
288     /* Check status and make sure thread 2 is now at the front of the suspension list.  */
289     if ((status != TX_SUCCESS) || (byte_pool_0.tx_byte_pool_suspension_list != &thread_2))
290     {
291 
292         /* Byte Pool error.  */
293         printf("ERROR #13\n");
294         test_control_return(1);
295     }
296 
297     /* Call byte pool prioritize again to test the don't-do-anything path in tx_byte_pool_prioritize.  */
298     status +=  tx_byte_pool_prioritize(&byte_pool_0);
299 
300     /* At this point we are going to get more than 2 threads suspended.  */
301     tx_thread_resume(&thread_1);
302     tx_thread_resume(&thread_2);
303     tx_thread_resume(&thread_3);
304     tx_thread_resume(&thread_4);
305     tx_thread_resume(&thread_5);
306     tx_thread_resume(&thread_6);
307 
308     /* Prioritize the byte pool suspension list.  */
309     status =  tx_byte_pool_prioritize(&byte_pool_0);
310 
311     /* Check status and make sure thread 3 is now at the front of the suspension list.  */
312     if ((status != TX_SUCCESS) || (byte_pool_0.tx_byte_pool_suspension_list != &thread_3))
313     {
314 
315         /* Byte Pool error.  */
316         printf("ERROR #14\n");
317         test_control_return(1);
318     }
319 
320     /* Now loop to test the interrupt of the prioritize loop logic.  */
321     test_status =  1;
322     test_isr_dispatch =  test_isr;
323     do
324     {
325 
326         /* Prioritize the byte pool suspension list.  */
327         status =  tx_byte_pool_prioritize(&byte_pool_0);
328 
329         /* Check status and make sure thread 1 is terminated.  */
330         if (status != TX_SUCCESS)
331         {
332 
333             /* Block Pool error.  */
334             printf("ERROR #15\n");
335             test_control_return(1);
336         }
337 
338     } while (test_status == 1);
339 
340     /* Check status and make sure thread 3 is now at the front of the suspension list.  */
341     if ((status != TX_SUCCESS) || (byte_pool_0.tx_byte_pool_suspension_list != &thread_4))
342     {
343 
344         /* Byte Pool error.  */
345         printf("ERROR #16\n");
346         test_control_return(1);
347     }
348     else
349     {
350 
351         /* Successful test.  */
352         printf("SUCCESS!\n");
353         test_control_return(0);
354     }
355 }
356 
357 
thread_1_entry(ULONG thread_input)358 static void    thread_1_entry(ULONG thread_input)
359 {
360 UINT    status;
361 VOID    *pointer;
362 
363     /* Loop forever!  */
364     while(1)
365     {
366 
367 
368         /* Get byte from pool.  */
369         status =  tx_byte_allocate(&byte_pool_0, &pointer, 25, TX_WAIT_FOREVER);
370 
371         if (status != TX_SUCCESS)
372             break;
373 
374         /* Increment the thread counter.  */
375         thread_1_counter++;
376     }
377 }
378 
379 
thread_2_entry(ULONG thread_input)380 static void    thread_2_entry(ULONG thread_input)
381 {
382 
383 UINT    status;
384 VOID    *pointer;
385 
386 
387     /* Loop forever!  */
388     while(1)
389     {
390 
391 
392         /* Get byte from pool.  */
393         status =  tx_byte_allocate(&byte_pool_0, &pointer, 25, TX_WAIT_FOREVER);
394 
395         if (status != TX_SUCCESS)
396             break;
397 
398         /* Increment the thread counter.  */
399         thread_2_counter++;
400     }
401 }
402 
403 
thread_3_entry(ULONG thread_input)404 static void    thread_3_entry(ULONG thread_input)
405 {
406 
407 UINT    status;
408 VOID    *pointer;
409 
410 
411     /* Loop forever!  */
412     while(1)
413     {
414 
415 
416         /* Get byte from pool.  */
417         status =  tx_byte_allocate(&byte_pool_0, &pointer, 25, TX_WAIT_FOREVER);
418 
419         if (status != TX_SUCCESS)
420             break;
421 
422         /* Increment the thread counter.  */
423         thread_3_counter++;
424     }
425 }
426 
427 
thread_4_entry(ULONG thread_input)428 static void    thread_4_entry(ULONG thread_input)
429 {
430 
431 UINT    status;
432 VOID    *pointer;
433 
434 
435     /* Loop forever!  */
436     while(1)
437     {
438 
439 
440         /* Get byte from pool.  */
441         status =  tx_byte_allocate(&byte_pool_0, &pointer, 25, TX_WAIT_FOREVER);
442 
443         if (status != TX_SUCCESS)
444             break;
445 
446         /* Increment the thread counter.  */
447         thread_4_counter++;
448     }
449 }
450 
451 
thread_5_entry(ULONG thread_input)452 static void    thread_5_entry(ULONG thread_input)
453 {
454 
455 UINT    status;
456 VOID    *pointer;
457 
458 
459     /* Loop forever!  */
460     while(1)
461     {
462 
463 
464         /* Get byte from pool.  */
465         status =  tx_byte_allocate(&byte_pool_0, &pointer, 25, TX_WAIT_FOREVER);
466 
467         if (status != TX_SUCCESS)
468             break;
469 
470         /* Increment the thread counter.  */
471         thread_5_counter++;
472     }
473 }
474 
475 
thread_6_entry(ULONG thread_input)476 static void    thread_6_entry(ULONG thread_input)
477 {
478 
479 UINT    status;
480 VOID    *pointer;
481 
482 
483     /* Loop forever!  */
484     while(1)
485     {
486 
487 
488         /* Get byte from pool.  */
489         status =  tx_byte_allocate(&byte_pool_0, &pointer, 25, TX_WAIT_FOREVER);
490 
491         if (status != TX_SUCCESS)
492             break;
493 
494         /* Increment the thread counter.  */
495         thread_6_counter++;
496     }
497 }
498 
499