1 /* This is a small demo of the high-performance ThreadX kernel. It includes examples of eight
2 threads of different priorities, using a message queue, semaphore, mutex, event flags group,
3 byte pool, and block pool. */
4
5 #include "tx_api.h"
6
7 #define DEMO_STACK_SIZE 1024
8 #define DEMO_BYTE_POOL_SIZE 9120
9 #define DEMO_BLOCK_POOL_SIZE 100
10 #define DEMO_QUEUE_SIZE 100
11
12
13 /* Define the ThreadX object control blocks... */
14
15 TX_THREAD thread_0;
16 TX_THREAD thread_1;
17 TX_THREAD thread_2;
18 TX_THREAD thread_3;
19 TX_THREAD thread_4;
20 TX_THREAD thread_5;
21 TX_THREAD thread_6;
22 TX_THREAD thread_7;
23 TX_QUEUE queue_0;
24 TX_SEMAPHORE semaphore_0;
25 TX_MUTEX mutex_0;
26 TX_EVENT_FLAGS_GROUP event_flags_0;
27 TX_BYTE_POOL byte_pool_0;
28 TX_BLOCK_POOL block_pool_0;
29 UCHAR memory_area[DEMO_BYTE_POOL_SIZE];
30
31
32 /* Define the counters used in the demo application... */
33
34 ULONG thread_0_counter;
35 ULONG thread_1_counter;
36 ULONG thread_1_messages_sent;
37 ULONG thread_2_counter;
38 ULONG thread_2_messages_received;
39 ULONG thread_3_counter;
40 ULONG thread_4_counter;
41 ULONG thread_5_counter;
42 ULONG thread_6_counter;
43 ULONG thread_7_counter;
44
45
46 /* Define thread prototypes. */
47
48 void thread_0_entry(ULONG thread_input);
49 void thread_1_entry(ULONG thread_input);
50 void thread_2_entry(ULONG thread_input);
51 void thread_3_and_4_entry(ULONG thread_input);
52 void thread_5_entry(ULONG thread_input);
53 void thread_6_and_7_entry(ULONG thread_input);
54
55
56 /* Define main entry point. */
57
main()58 int main()
59 {
60
61 /* Enter the ThreadX kernel. */
62 tx_kernel_enter();
63 }
64
65
66 /* Define what the initial system looks like. */
67
tx_application_define(void * first_unused_memory)68 void tx_application_define(void *first_unused_memory)
69 {
70
71 CHAR *pointer = TX_NULL;
72
73
74 /* Create a byte memory pool from which to allocate the thread stacks. */
75 tx_byte_pool_create(&byte_pool_0, "byte pool 0", memory_area, DEMO_BYTE_POOL_SIZE);
76
77 /* Put system definition stuff in here, e.g. thread creates and other assorted
78 create information. */
79
80 /* Allocate the stack for thread 0. */
81 tx_byte_allocate(&byte_pool_0, (VOID **) &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);
82
83 /* Create the main thread. */
84 tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
85 pointer, DEMO_STACK_SIZE,
86 1, 1, TX_NO_TIME_SLICE, TX_AUTO_START);
87
88
89 /* Allocate the stack for thread 1. */
90 tx_byte_allocate(&byte_pool_0, (VOID **) &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);
91
92 /* Create threads 1 and 2. These threads pass information through a ThreadX
93 message queue. It is also interesting to note that these threads have a time
94 slice. */
95 tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
96 pointer, DEMO_STACK_SIZE,
97 16, 16, 4, TX_AUTO_START);
98
99 /* Allocate the stack for thread 2. */
100 tx_byte_allocate(&byte_pool_0, (VOID **) &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);
101
102 tx_thread_create(&thread_2, "thread 2", thread_2_entry, 2,
103 pointer, DEMO_STACK_SIZE,
104 16, 16, 4, TX_AUTO_START);
105
106 /* Allocate the stack for thread 3. */
107 tx_byte_allocate(&byte_pool_0, (VOID **) &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);
108
109 /* Create threads 3 and 4. These threads compete for a ThreadX counting semaphore.
110 An interesting thing here is that both threads share the same instruction area. */
111 tx_thread_create(&thread_3, "thread 3", thread_3_and_4_entry, 3,
112 pointer, DEMO_STACK_SIZE,
113 8, 8, TX_NO_TIME_SLICE, TX_AUTO_START);
114
115 /* Allocate the stack for thread 4. */
116 tx_byte_allocate(&byte_pool_0, (VOID **) &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);
117
118 tx_thread_create(&thread_4, "thread 4", thread_3_and_4_entry, 4,
119 pointer, DEMO_STACK_SIZE,
120 8, 8, TX_NO_TIME_SLICE, TX_AUTO_START);
121
122 /* Allocate the stack for thread 5. */
123 tx_byte_allocate(&byte_pool_0, (VOID **) &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);
124
125 /* Create thread 5. This thread simply pends on an event flag which will be set
126 by thread_0. */
127 tx_thread_create(&thread_5, "thread 5", thread_5_entry, 5,
128 pointer, DEMO_STACK_SIZE,
129 4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
130
131 /* Allocate the stack for thread 6. */
132 tx_byte_allocate(&byte_pool_0, (VOID **) &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);
133
134 /* Create threads 6 and 7. These threads compete for a ThreadX mutex. */
135 tx_thread_create(&thread_6, "thread 6", thread_6_and_7_entry, 6,
136 pointer, DEMO_STACK_SIZE,
137 8, 8, TX_NO_TIME_SLICE, TX_AUTO_START);
138
139 /* Allocate the stack for thread 7. */
140 tx_byte_allocate(&byte_pool_0, (VOID **) &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);
141
142 tx_thread_create(&thread_7, "thread 7", thread_6_and_7_entry, 7,
143 pointer, DEMO_STACK_SIZE,
144 8, 8, TX_NO_TIME_SLICE, TX_AUTO_START);
145
146 /* Allocate the message queue. */
147 tx_byte_allocate(&byte_pool_0, (VOID **) &pointer, DEMO_QUEUE_SIZE*sizeof(ULONG), TX_NO_WAIT);
148
149 /* Create the message queue shared by threads 1 and 2. */
150 tx_queue_create(&queue_0, "queue 0", TX_1_ULONG, pointer, DEMO_QUEUE_SIZE*sizeof(ULONG));
151
152 /* Create the semaphore used by threads 3 and 4. */
153 tx_semaphore_create(&semaphore_0, "semaphore 0", 1);
154
155 /* Create the event flags group used by threads 1 and 5. */
156 tx_event_flags_create(&event_flags_0, "event flags 0");
157
158 /* Create the mutex used by thread 6 and 7 without priority inheritance. */
159 tx_mutex_create(&mutex_0, "mutex 0", TX_NO_INHERIT);
160
161 /* Allocate the memory for a small block pool. */
162 tx_byte_allocate(&byte_pool_0, (VOID **) &pointer, DEMO_BLOCK_POOL_SIZE, TX_NO_WAIT);
163
164 /* Create a block memory pool to allocate a message buffer from. */
165 tx_block_pool_create(&block_pool_0, "block pool 0", sizeof(ULONG), pointer, DEMO_BLOCK_POOL_SIZE);
166
167 /* Allocate a block and release the block memory. */
168 tx_block_allocate(&block_pool_0, (VOID **) &pointer, TX_NO_WAIT);
169
170 /* Release the block back to the pool. */
171 tx_block_release(pointer);
172 }
173
174
175
176 /* Define the test threads. */
177
thread_0_entry(ULONG thread_input)178 void thread_0_entry(ULONG thread_input)
179 {
180
181 UINT status;
182
183
184 /* This thread simply sits in while-forever-sleep loop. */
185 while(1)
186 {
187
188 /* Increment the thread counter. */
189 thread_0_counter++;
190
191 /* Sleep for 10 ticks. */
192 tx_thread_sleep(10);
193
194 /* Set event flag 0 to wakeup thread 5. */
195 status = tx_event_flags_set(&event_flags_0, 0x1, TX_OR);
196
197 /* Check status. */
198 if (status != TX_SUCCESS)
199 break;
200 }
201 }
202
203
thread_1_entry(ULONG thread_input)204 void thread_1_entry(ULONG thread_input)
205 {
206
207 UINT status;
208
209
210 /* This thread simply sends messages to a queue shared by thread 2. */
211 while(1)
212 {
213
214 /* Increment the thread counter. */
215 thread_1_counter++;
216
217 /* Send message to queue 0. */
218 status = tx_queue_send(&queue_0, &thread_1_messages_sent, TX_WAIT_FOREVER);
219
220 /* Check completion status. */
221 if (status != TX_SUCCESS)
222 break;
223
224 /* Increment the message sent. */
225 thread_1_messages_sent++;
226 }
227 }
228
229
thread_2_entry(ULONG thread_input)230 void thread_2_entry(ULONG thread_input)
231 {
232
233 ULONG received_message;
234 UINT status;
235
236 /* This thread retrieves messages placed on the queue by thread 1. */
237 while(1)
238 {
239
240 /* Increment the thread counter. */
241 thread_2_counter++;
242
243 /* Retrieve a message from the queue. */
244 status = tx_queue_receive(&queue_0, &received_message, TX_WAIT_FOREVER);
245
246 /* Check completion status and make sure the message is what we
247 expected. */
248 if ((status != TX_SUCCESS) || (received_message != thread_2_messages_received))
249 break;
250
251 /* Otherwise, all is okay. Increment the received message count. */
252 thread_2_messages_received++;
253 }
254 }
255
256
thread_3_and_4_entry(ULONG thread_input)257 void thread_3_and_4_entry(ULONG thread_input)
258 {
259
260 UINT status;
261
262
263 /* This function is executed from thread 3 and thread 4. As the loop
264 below shows, these function compete for ownership of semaphore_0. */
265 while(1)
266 {
267
268 /* Increment the thread counter. */
269 if (thread_input == 3)
270 thread_3_counter++;
271 else
272 thread_4_counter++;
273
274 /* Get the semaphore with suspension. */
275 status = tx_semaphore_get(&semaphore_0, TX_WAIT_FOREVER);
276
277 /* Check status. */
278 if (status != TX_SUCCESS)
279 break;
280
281 /* Sleep for 2 ticks to hold the semaphore. */
282 tx_thread_sleep(2);
283
284 /* Release the semaphore. */
285 status = tx_semaphore_put(&semaphore_0);
286
287 /* Check status. */
288 if (status != TX_SUCCESS)
289 break;
290 }
291 }
292
293
thread_5_entry(ULONG thread_input)294 void thread_5_entry(ULONG thread_input)
295 {
296
297 UINT status;
298 ULONG actual_flags;
299
300
301 /* This thread simply waits for an event in a forever loop. */
302 while(1)
303 {
304
305 /* Increment the thread counter. */
306 thread_5_counter++;
307
308 /* Wait for event flag 0. */
309 status = tx_event_flags_get(&event_flags_0, 0x1, TX_OR_CLEAR,
310 &actual_flags, TX_WAIT_FOREVER);
311
312 /* Check status. */
313 if ((status != TX_SUCCESS) || (actual_flags != 0x1))
314 break;
315 }
316 }
317
318
thread_6_and_7_entry(ULONG thread_input)319 void thread_6_and_7_entry(ULONG thread_input)
320 {
321
322 UINT status;
323
324
325 /* This function is executed from thread 6 and thread 7. As the loop
326 below shows, these function compete for ownership of mutex_0. */
327 while(1)
328 {
329
330 /* Increment the thread counter. */
331 if (thread_input == 6)
332 thread_6_counter++;
333 else
334 thread_7_counter++;
335
336 /* Get the mutex with suspension. */
337 status = tx_mutex_get(&mutex_0, TX_WAIT_FOREVER);
338
339 /* Check status. */
340 if (status != TX_SUCCESS)
341 break;
342
343 /* Get the mutex again with suspension. This shows
344 that an owning thread may retrieve the mutex it
345 owns multiple times. */
346 status = tx_mutex_get(&mutex_0, TX_WAIT_FOREVER);
347
348 /* Check status. */
349 if (status != TX_SUCCESS)
350 break;
351
352 /* Sleep for 2 ticks to hold the mutex. */
353 tx_thread_sleep(2);
354
355 /* Release the mutex. */
356 status = tx_mutex_put(&mutex_0);
357
358 /* Check status. */
359 if (status != TX_SUCCESS)
360 break;
361
362 /* Release the mutex again. This will actually
363 release ownership since it was obtained twice. */
364 status = tx_mutex_put(&mutex_0);
365
366 /* Check status. */
367 if (status != TX_SUCCESS)
368 break;
369 }
370 }
371