1 /* This test is designed to test timeouts on suspension on memory block pools.  */
2 
3 #include   <stdio.h>
4 #include   "tx_api.h"
5 
6 static unsigned long   thread_0_counter =  0;
7 static TX_THREAD       thread_0;
8 static unsigned long   thread_1_counter =  0;
9 static TX_THREAD       thread_1;
10 static unsigned long   thread_2_counter =  0;
11 static TX_THREAD       thread_2;
12 
13 
14 static TX_BLOCK_POOL   pool_0;
15 
16 
17 /* Define thread prototypes.  */
18 
19 static void    thread_0_entry(ULONG thread_input);
20 static void    thread_1_entry(ULONG thread_input);
21 static void    thread_2_entry(ULONG thread_input);
22 
23 
24 /* Prototype for test control return.  */
25 void  test_control_return(UINT status);
26 
27 
28 /* Define what the initial system looks like.  */
29 
30 #ifdef CTEST
test_application_define(void * first_unused_memory)31 void test_application_define(void *first_unused_memory)
32 #else
33 void    threadx_block_memory_suspension_timeout_application_define(void *first_unused_memory)
34 #endif
35 {
36 
37 UINT    status;
38 CHAR    *pointer;
39 
40 
41     /* Put first available memory address into a character pointer.  */
42     pointer =  (CHAR *) first_unused_memory;
43 
44     /* Put system definition stuff in here, e.g. thread creates and other assorted
45        create information.  */
46 
47     status =  tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
48             pointer, TEST_STACK_SIZE_PRINTF,
49             17, 17, 100, TX_AUTO_START);
50     pointer = pointer + TEST_STACK_SIZE_PRINTF;
51 
52     /* Check status.  */
53     if (status != TX_SUCCESS)
54     {
55 
56         printf("Running Block Memory Suspension Timeout Test........................ ERROR #1\n");
57         test_control_return(1);
58     }
59 
60     status =  tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
61             pointer, TEST_STACK_SIZE_PRINTF,
62             17, 17, 100, TX_AUTO_START);
63     pointer = pointer + TEST_STACK_SIZE_PRINTF;
64 
65     /* Check status.  */
66     if (status != TX_SUCCESS)
67     {
68 
69         printf("Running Block Memory Suspension Timeout Test........................ ERROR #2\n");
70         test_control_return(1);
71     }
72 
73     status =  tx_thread_create(&thread_2, "thread 2", thread_2_entry, 2,
74             pointer, TEST_STACK_SIZE_PRINTF,
75             17, 17, 100, TX_AUTO_START);
76     pointer = pointer + TEST_STACK_SIZE_PRINTF;
77 
78     /* Check status.  */
79     if (status != TX_SUCCESS)
80     {
81 
82         printf("Running Block Memory Suspension Timeout Test........................ ERROR #3\n");
83         test_control_return(1);
84     }
85 
86     /* Create block pool.  */
87     status =  tx_block_pool_create(&pool_0, "pool 0", 100, pointer, 320);
88     pointer = pointer + 320;
89 
90     /* Check status.  */
91     if (status != TX_SUCCESS)
92     {
93 
94         printf("Running Block Memory Suspension Timeout Test........................ ERROR #4\n");
95         test_control_return(1);
96     }
97 }
98 
99 
100 
101 /* Define the test threads.  */
102 
thread_0_entry(ULONG thread_input)103 static void    thread_0_entry(ULONG thread_input)
104 {
105 
106 UINT    status;
107 CHAR    *pointer_1;
108 CHAR    *pointer_2;
109 CHAR    *pointer_3;
110 
111 
112     /* Inform user.  */
113     printf("Running Block Memory Suspension Timeout Test........................ ");
114 
115     /* Allocate all blocks from the pool.  */
116     status =  tx_block_allocate(&pool_0, (VOID **) &pointer_1, TX_NO_WAIT);
117     status += tx_block_allocate(&pool_0, (VOID **) &pointer_2, TX_NO_WAIT);
118     status += tx_block_allocate(&pool_0, (VOID **) &pointer_3, TX_NO_WAIT);
119 
120     /* Check status.  */
121     if (status != TX_SUCCESS)
122     {
123 
124         /* Block memory error.  */
125         printf("ERROR #5\n");
126         test_control_return(1);
127     }
128 
129     /* Set all the memory of the blocks.  */
130     TX_MEMSET(pointer_1, (CHAR) 0xEF, 100);
131 
132     /* Set all the memory of the blocks.  */
133     TX_MEMSET(pointer_2, (CHAR) 0xEF, 100);
134 
135     /* Set all the memory of the blocks.  */
136     TX_MEMSET(pointer_3, (CHAR) 0xEF, 100);
137 
138     /* Sleep for 64 ticks to allow the other thread 6 timeouts on the block
139        pool.  */
140     tx_thread_sleep(64);
141 
142     /* Incrment the run counter.  */
143     thread_0_counter++;
144 
145     /* Check the counter of the other thread.  */
146     if ((thread_1_counter != 6) || (thread_2_counter != 3))
147     {
148 
149         /* Block memory error.  */
150         printf("ERROR #6\n");
151         test_control_return(1);
152     }
153     else
154     {
155 
156         /* Successful test.  */
157         printf("SUCCESS!\n");
158         test_control_return(0);
159     }
160 }
161 
162 
thread_1_entry(ULONG thread_input)163 static void    thread_1_entry(ULONG thread_input)
164 {
165 
166 UINT    status;
167 CHAR    *pointer_1;
168 
169 
170     /* Attempt to get a block from the pool.  */
171     while(1)
172     {
173 
174         /* Allocate a block from the pool.  */
175         status =  tx_block_allocate(&pool_0, (VOID **) &pointer_1, 10);
176 
177         /* Determine if the allocate was successful.  */
178         if (status != TX_NO_MEMORY)
179             return;
180 
181         /* Increment the thread counter.  */
182         thread_1_counter++;
183     }
184 }
185 
186 
thread_2_entry(ULONG thread_input)187 static void    thread_2_entry(ULONG thread_input)
188 {
189 
190 UINT    status;
191 CHAR    *pointer_1;
192 
193 
194     /* Delay so we get some single suspension timeouts as well.  */
195     tx_thread_sleep(32);
196 
197     /* Attempt to get a block from the pool.  */
198     while(1)
199     {
200 
201         /* Allocate a block from the pool.  */
202         status =  tx_block_allocate(&pool_0, (VOID **) &pointer_1, 10);
203 
204         /* Determine if the allocate was successful.  */
205         if (status != TX_NO_MEMORY)
206             return;
207 
208         /* Increment the thread counter.  */
209         thread_2_counter++;
210     }
211 }
212 
213