1 /* This test is designed to test the semaphore create/delete and immediate return gets and puts. */
2
3 #include <stdio.h>
4 #include "tx_api.h"
5
6
7 typedef struct SEMAPHORE_MEMORY_TEST_STRUCT
8 {
9 ULONG first;
10 ULONG second;
11 TX_SEMAPHORE semaphore;
12 ULONG next_to_last;
13 ULONG last;
14
15 } SEMAPHORE_MEMORY_TEST;
16
17 static SEMAPHORE_MEMORY_TEST semaphore_memory;
18
19
20 /* Define the external symbol to obtain the status from a create call in initialization. */
21
22 extern UINT test_semaphore_from_init;
23
24
25 /* Define the ISR dispatch. */
26
27 extern VOID (*test_isr_dispatch)(void);
28
29
30 static unsigned long thread_0_counter = 0;
31 static TX_THREAD thread_0;
32 static TX_THREAD thread_1;
33
34
35 static TX_TIMER timer_0;
36
37
38 static unsigned long error = 0;
39 static unsigned long timer_executed = 0;
40 static unsigned long isr_executed = 0;
41
42
43 static TX_SEMAPHORE semaphore_0;
44 static TX_SEMAPHORE semaphore_1;
45 static TX_SEMAPHORE semaphore_2;
46 static TX_SEMAPHORE semaphore_3;
47
48
49 /* Define thread prototypes. */
50
51 static void thread_0_entry(ULONG thread_input);
52 static void thread_1_entry(ULONG thread_input);
53
54 UINT _txe_semaphore_create(TX_SEMAPHORE *semaphore_ptr, CHAR *name_ptr, ULONG initial_count, UINT semaphore_control_block_size);
55
56
57 /* Prototype for test control return. */
58
59 void test_control_return(UINT status);
60
61
62 /* Define the timer for this test. */
63
timer_entry(ULONG i)64 static void timer_entry(ULONG i)
65 {
66
67 #ifndef TX_DISABLE_ERROR_CHECKING
68
69 UINT status;
70
71
72 /* Determine if calling semaphore create from initialization was successful. */
73 if (test_semaphore_from_init != TX_SUCCESS)
74 {
75
76 /* Error! */
77 error++;
78 }
79
80 /* Attempt to create a semaphore from a timer. */
81 status = tx_semaphore_create(&semaphore_2, "semaphore 2", 1);
82
83 /* Check for status. */
84 if (status != TX_CALLER_ERROR)
85 {
86
87 /* Error! */
88 error++;
89 }
90
91 /* Attempt to delete a semaphore from a timer. */
92 status = tx_semaphore_delete(&semaphore_0);
93
94 /* Check for status. */
95 if (status != TX_CALLER_ERROR)
96 {
97
98 /* Error! */
99 error++;
100 }
101
102 /* Attempt to get a semaphore with suspension from a timer. */
103 status = tx_semaphore_get(&semaphore_0, 100);
104
105 /* Check for status. */
106 if (status != TX_WAIT_ERROR)
107 {
108
109 /* Error! */
110 error++;
111 }
112
113
114 timer_executed = 1;
115 #endif
116 }
117
118 /* Define the ISR dispatch routine. */
119
test_isr(void)120 static void test_isr(void)
121 {
122
123 #ifndef TX_DISABLE_ERROR_CHECKING
124
125 UINT status;
126
127
128 /* Attempt to create a semaphore from an ISR. */
129 status = tx_semaphore_create(&semaphore_2, "semaphore 2", 1);
130
131 /* Check for status. */
132 if (status != TX_CALLER_ERROR)
133 {
134
135 /* Error! */
136 error++;
137 }
138
139 /* Attempt to delete a semaphore from an ISR. */
140 status = tx_semaphore_delete(&semaphore_0);
141
142 /* Check for status. */
143 if (status != TX_CALLER_ERROR)
144 {
145
146 /* Error! */
147 error++;
148 }
149
150 /* Attempt to get a semaphore with suspension from an ISR. */
151 status = tx_semaphore_get(&semaphore_0, 100);
152
153 /* Check for status. */
154 if (status != TX_WAIT_ERROR)
155 {
156
157 /* Error! */
158 error++;
159 }
160
161 isr_executed = 1;
162 #endif
163 }
164
165
166
167 /* Define what the initial system looks like. */
168
169 #ifdef CTEST
test_application_define(void * first_unused_memory)170 void test_application_define(void *first_unused_memory)
171 #else
172 void threadx_semaphore_basic_application_define(void *first_unused_memory)
173 #endif
174 {
175
176 UINT status;
177 CHAR *pointer;
178
179 /* Put first available memory address into a character pointer. */
180 pointer = (CHAR *) first_unused_memory;
181
182 /* Put system definition stuff in here, e.g. thread creates and other assorted
183 create information. */
184
185 status = tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
186 pointer, TEST_STACK_SIZE_PRINTF,
187 16, 16, 100, TX_AUTO_START);
188 pointer = pointer + TEST_STACK_SIZE_PRINTF;
189
190 status += tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
191 pointer, TEST_STACK_SIZE_PRINTF,
192 18, 18, 100, TX_DONT_START);
193 pointer = pointer + TEST_STACK_SIZE_PRINTF;
194
195 /* Check for status. */
196 if (status != TX_SUCCESS)
197 {
198
199 printf("Running Semaphore Basic Test........................................ ERROR #1\n");
200 test_control_return(1);
201 }
202
203 /* Create a semaphore with an initial count of 1. */
204 status = tx_semaphore_create(&semaphore_0, "semaphore 0", 1);
205
206 /* Check for status. */
207 if (status != TX_SUCCESS)
208 {
209
210 printf("Running Semaphore Basic Test........................................ ERROR #2\n");
211 test_control_return(1);
212 }
213
214 /* Create a semaphore with an initial count of 0. */
215 status = tx_semaphore_create(&semaphore_1, "semaphore 1", 1);
216
217 /* Check for status. */
218 if (status != TX_SUCCESS)
219 {
220
221 printf("Running Semaphore Basic Test........................................ ERROR #3\n");
222 test_control_return(1);
223 }
224 }
225
226
227
228 /* Define the test threads. */
229
thread_0_entry(ULONG thread_input)230 static void thread_0_entry(ULONG thread_input)
231 {
232
233 UINT status;
234
235 /* Inform user. */
236 printf("Running Semaphore Basic Test........................................ ");
237
238 /* Perform semaphore memory test. */
239 semaphore_memory.first = 0x11223344;
240 semaphore_memory.second = 0x55667788;
241 semaphore_memory.next_to_last = 0x99aabbcc;
242 semaphore_memory.last = 0xddeeff00;
243
244 /* Create the semaphore. */
245 status = tx_semaphore_create(&semaphore_memory.semaphore, "semaphore memory", 0);
246 tx_semaphore_delete(&semaphore_memory.semaphore);
247
248 /* Check for status. */
249 if ((status != TX_SUCCESS) ||
250 (semaphore_memory.first != 0x11223344) ||
251 (semaphore_memory.second != 0x55667788) ||
252 (semaphore_memory.next_to_last != 0x99aabbcc) ||
253 (semaphore_memory.last != 0xddeeff00))
254 {
255
256 /* Semaphore error. */
257 printf("ERROR #4\n");
258 test_control_return(1);
259 }
260
261 /* Increment thread 0 counter. */
262 thread_0_counter++;
263
264 #ifndef TX_DISABLE_ERROR_CHECKING
265
266 /* Attempt to create a non-semaphore. */
267 status = tx_semaphore_create(TX_NULL, "semaphore 0", 1);
268
269 /* Check for status. */
270 if (status != TX_SEMAPHORE_ERROR)
271 {
272
273 /* Semaphore error. */
274 printf("ERROR #5\n");
275 test_control_return(1);
276 }
277
278 /* Attempt to create a semaphore with a bad sized block. */
279 status = _txe_semaphore_create(&semaphore_3, "semaphore 3", 1, (sizeof(TX_SEMAPHORE)+1));
280
281 /* Check for status. */
282 if (status != TX_SEMAPHORE_ERROR)
283 {
284
285 /* Semaphore error. */
286 printf("ERROR #6\n");
287 test_control_return(1);
288 }
289
290 /* Attempt to create an already created semaphore. */
291 status = tx_semaphore_create(&semaphore_0, "semaphore 0", 1);
292
293 /* Check for status. */
294 if (status != TX_SEMAPHORE_ERROR)
295 {
296
297 /* Semaphore error. */
298 printf("ERROR #7\n");
299 test_control_return(1);
300 }
301
302 /* Attempt to delete a non-semaphore. */
303 status = tx_semaphore_delete(TX_NULL);
304
305 /* Check for status. */
306 if (status != TX_SEMAPHORE_ERROR)
307 {
308
309 /* Semaphore error. */
310 printf("ERROR #8\n");
311 test_control_return(1);
312 }
313
314 /* Attempt to delete a non-created semaphore. */
315 semaphore_2.tx_semaphore_id = 0;
316 status = tx_semaphore_delete(&semaphore_2);
317
318 /* Check for status. */
319 if (status != TX_SEMAPHORE_ERROR)
320 {
321
322 /* Semaphore error. */
323 printf("ERROR #9\n");
324 test_control_return(1);
325 }
326
327 /* Attempt to get a non-semaphore. */
328 status = tx_semaphore_get(TX_NULL, TX_NO_WAIT);
329
330 /* Check for status. */
331 if (status != TX_SEMAPHORE_ERROR)
332 {
333
334 /* Semaphore error. */
335 printf("ERROR #10\n");
336 test_control_return(1);
337 }
338
339 /* Attempt to get a non-created semaphore. */
340 semaphore_2.tx_semaphore_id = 0;
341 status = tx_semaphore_get(&semaphore_2, TX_NO_WAIT);
342
343 /* Check for status. */
344 if (status != TX_SEMAPHORE_ERROR)
345 {
346
347 /* Semaphore error. */
348 printf("ERROR #11\n");
349 test_control_return(1);
350 }
351
352 /* Attempt to put a non-semaphore. */
353 status = tx_semaphore_put(TX_NULL);
354
355 /* Check for status. */
356 if (status != TX_SEMAPHORE_ERROR)
357 {
358
359 /* Semaphore error. */
360 printf("ERROR #12\n");
361 test_control_return(1);
362 }
363
364 /* Attempt to put a non-created semaphore. */
365 semaphore_2.tx_semaphore_id = 0;
366 status = tx_semaphore_put(&semaphore_2);
367
368 /* Check for status. */
369 if (status != TX_SEMAPHORE_ERROR)
370 {
371
372 /* Semaphore error. */
373 printf("ERROR #13\n");
374 test_control_return(1);
375 }
376
377 /* Attempt to ceiling put a non-semaphore. */
378 status = tx_semaphore_ceiling_put(TX_NULL, 0);
379
380 /* Check for status. */
381 if (status != TX_SEMAPHORE_ERROR)
382 {
383
384 /* Semaphore error. */
385 printf("ERROR #14\n");
386 test_control_return(1);
387 }
388
389 /* Attempt to ceiling put a non-created semaphore. */
390 semaphore_2.tx_semaphore_id = 0;
391 status = tx_semaphore_ceiling_put(&semaphore_2, 0);
392
393 /* Check for status. */
394 if (status != TX_SEMAPHORE_ERROR)
395 {
396
397 /* Semaphore error. */
398 printf("ERROR #15\n");
399 test_control_return(1);
400 }
401
402 /* Attempt to ceiling put with an invalid ceiling of 0. */
403 status = tx_semaphore_ceiling_put(&semaphore_0, 0);
404
405 /* Check for status. */
406 if (status != TX_INVALID_CEILING)
407 {
408
409 /* Semaphore error. */
410 printf("ERROR #16\n");
411 test_control_return(1);
412 }
413 #endif
414
415 /* Attempt to get from semaphore with an instance. Should be successful! */
416 status = tx_semaphore_get(&semaphore_0, TX_NO_WAIT);
417
418 /* Check status. */
419 if (status != TX_SUCCESS)
420 {
421
422 /* Semaphore error. */
423 printf("ERROR #17\n");
424 test_control_return(1);
425 }
426
427 /* Attempt to get from semaphore without an instance. Should be unsuccessful. */
428 status = tx_semaphore_get(&semaphore_0, TX_NO_WAIT);
429
430 /* Check status. */
431 if (status != TX_NO_INSTANCE)
432 {
433
434 /* Semaphore error. */
435 printf("ERROR #18\n");
436 test_control_return(1);
437 }
438
439 /* Put to semaphore that has an instance already. Should now be 2! */
440 status = tx_semaphore_put(&semaphore_1);
441
442 /* Check status. */
443 if ((status != TX_SUCCESS) || (semaphore_1.tx_semaphore_count != 2))
444 {
445
446 /* Semaphore error. */
447 printf("ERROR #19\n");
448 test_control_return(1);
449 }
450
451
452 /* Attempt to get from semaphore with an instance. Should be successful! */
453 status = tx_semaphore_get(&semaphore_1, TX_NO_WAIT);
454
455 /* Check status. */
456 if (status != TX_SUCCESS)
457 {
458
459 /* Semaphore error. */
460 printf("ERROR #20\n");
461 test_control_return(1);
462 }
463
464 /* Attempt to get from semaphore with an instance. Should be successful. */
465 status = tx_semaphore_get(&semaphore_1, TX_NO_WAIT);
466
467 /* Check status. */
468 if (status != TX_SUCCESS)
469 {
470
471 /* Semaphore error. */
472 printf("ERROR #21\n");
473 test_control_return(1);
474 }
475
476 #ifndef TX_DISABLE_ERROR_CHECKING
477
478 /* Create a timer for the test. */
479 tx_timer_create(&timer_0, "timer 0", timer_entry, 0, 1, 1, TX_AUTO_ACTIVATE);
480
481 /* Setup the ISR. */
482 test_isr_dispatch = test_isr;
483
484 /* Sleep for a bit... */
485 tx_thread_sleep(3);
486
487 /* Resume the thread 1 so we can take an interrupt on top of it. */
488 tx_thread_resume(&thread_1);
489
490 /* Sleep for a bit... */
491 tx_thread_sleep(3);
492
493 /* Clear the ISR. */
494 test_isr_dispatch = TX_NULL;
495
496 /* Test for error. */
497 if ((error) || (timer_executed != 1) || (isr_executed != 1))
498 {
499
500 /* Semaphore error. */
501 printf("ERROR #22\n");
502 test_control_return(1);
503 }
504
505 #endif
506
507 /* Delete semaphores. */
508 status = tx_semaphore_delete(&semaphore_0);
509
510 /* Check status. */
511 if (status != TX_SUCCESS)
512 {
513
514 /* Semaphore error. */
515 printf("ERROR #23\n");
516 test_control_return(1);
517 }
518
519 status = tx_semaphore_delete(&semaphore_1);
520 /* Check status. */
521 if (status != TX_SUCCESS)
522 {
523
524 /* Semaphore error. */
525 printf("ERROR #24\n");
526 test_control_return(1);
527 }
528 else
529 {
530
531 /* Successful test. */
532 printf("SUCCESS!\n");
533 test_control_return(0);
534 }
535 }
536
thread_1_entry(ULONG thread_input)537 static void thread_1_entry(ULONG thread_input)
538 {
539
540 while(1)
541 {
542
543 tx_thread_relinquish();
544 }
545 }