1 /* This test is designed to test suspension timeout on a memory byte pool. */
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_BYTE_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_byte_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 Byte 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 Byte Memory Suspension Timeout Test......................... ERROR #2\n");
70 test_control_return(1);
71 }
72
73
74 status = tx_thread_create(&thread_2, "thread 2", thread_2_entry, 2,
75 pointer, TEST_STACK_SIZE_PRINTF,
76 17, 17, 100, TX_AUTO_START);
77 pointer = pointer + TEST_STACK_SIZE_PRINTF;
78
79 /* Check status. */
80 if (status != TX_SUCCESS)
81 {
82
83 printf("Running Byte Memory Suspension Timeout Test......................... ERROR #3\n");
84 test_control_return(1);
85 }
86
87 /* Create byte pool 0. */
88 status = tx_byte_pool_create(&pool_0, "pool 0", pointer, 108);
89 pointer = pointer + 108;
90
91 /* Check status. */
92 if (status != TX_SUCCESS)
93 {
94
95 printf("Running Byte Memory Suspension Timeout Test......................... ERROR #4\n");
96 test_control_return(1);
97 }
98 }
99
100
101
102 /* Define the test threads. */
103
thread_0_entry(ULONG thread_input)104 static void thread_0_entry(ULONG thread_input)
105 {
106
107 UINT status;
108 CHAR *pointer;
109
110
111 /* Inform user. */
112 printf("Running Byte Memory Suspension Timeout Test......................... ");
113
114 /* Increment the thread counter. */
115 thread_0_counter++;
116
117 /* Allocate memory from the pool. Only one block of this size will fit. */
118 status = tx_byte_allocate(&pool_0, (VOID **) &pointer, 60, TX_NO_WAIT);
119
120 /* Check status. */
121 if ((status != TX_SUCCESS) || (thread_1_counter != 0))
122 {
123
124 /* Byte memory error. */
125 printf("ERROR #5\n");
126 test_control_return(1);
127 }
128
129 /* Sleep to allow the other thread to suspend and timeout on the memory
130 pool once. */
131 tx_thread_sleep(64);
132
133 /* Check the counter of the other thread. */
134 if ((thread_1_counter != 6) || (thread_2_counter != 3))
135 {
136
137 /* Block memory error. */
138 printf("ERROR #6\n");
139 test_control_return(1);
140 }
141 else
142 {
143
144 /* Successful test. */
145 printf("SUCCESS!\n");
146 test_control_return(0);
147 }
148 }
149
150
thread_1_entry(ULONG thread_input)151 static void thread_1_entry(ULONG thread_input)
152 {
153
154 UINT status;
155 CHAR *pointer;
156
157 while(1)
158 {
159
160 /* Allocate memory from the pool - with timeout. */
161 status = tx_byte_allocate(&pool_0, (VOID **) &pointer, 60, 10);
162
163 /* Check status. */
164 if (status != TX_NO_MEMORY)
165 return;
166
167 /* Increment the thread counter. */
168 thread_1_counter++;
169 }
170 }
171
172
thread_2_entry(ULONG thread_input)173 static void thread_2_entry(ULONG thread_input)
174 {
175
176 UINT status;
177 CHAR *pointer;
178
179
180 /* Delay so we get some single suspension timeouts as well. */
181 tx_thread_sleep(32);
182
183 while(1)
184 {
185
186 /* Allocate memory from the pool - with timeout. */
187 status = tx_byte_allocate(&pool_0, (VOID **) &pointer, 60, 10);
188
189 /* Check status. */
190 if (status != TX_NO_MEMORY)
191 return;
192
193 /* Increment the thread counter. */
194 thread_2_counter++;
195 }
196 }
197