Update contrib.
1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\personality\example\main.cpp
15 // Test code for example RTOS personality.
19 #include <kernel/kern_priv.h>
20 #include <personality/example/personality.h>
29 #define NONEXISTENT_TASK 3
37 void oo_overall_control(void);
38 void l1_task_entry(void);
39 void l2_task_entry(void);
40 void rr_task_entry(void);
41 void tm_task_entry(void);
42 void task1_entry(void);
43 void task2_entry(void);
44 void task3_entry(void);
45 void task4_entry(void);
47 typedef void (*isr_entry)(unsigned);
49 extern int start_random_isr(isr_entry vector);
50 extern void stop_random_isr(void);
52 const taskinfo task_list[] =
55 /* entry_pt, priority, stack_size, task_id, auto_start */
57 { &oo_overall_control, 120, 1024, OC_TASK, 1 },
58 { &l2_task_entry, 236, 1024, L2_TASK, 0 },
59 { &rr_task_entry, 224, 1024, RR_TASK, 0 },
60 { &tm_task_entry, 240, 1024, TM_TASK, 0 },
61 { &task1_entry, 112, 1024, TASK1, 0 },
62 { &task2_entry, 112, 1024, TASK2, 0 },
63 { &task3_entry, 112, 1024, TASK3, 0 },
64 { &task4_entry, 112, 1024, TASK4, 0 },
65 { &l1_task_entry, 244, 1024, L1_TASK, 0 },
70 const poolinfo pool_list[] =
72 /* block size, block count */
82 const int timer_count = 8;
83 const int semaphore_count = 2;
87 #define TM_INIT_DELAY 1000
90 volatile unsigned next_random_id = 0;
91 volatile unsigned random_sem_signal_interval = 0;
92 volatile unsigned random_sem_signal_count = 0;
93 volatile unsigned random_send_interval = 0;
94 volatile unsigned random_send_count = 0;
95 volatile unsigned tmcount = 0;
96 volatile int t1func = 0;
97 volatile int t2func = 0;
98 volatile int t3func = 0;
99 volatile int t4func = 0;
104 #define MSG_ID_INIT 1
106 #define MSG_ID_RUN_P 3
107 #define MSG_ID_RND_ISR 4
108 #define MSG_ID_DONE 5
109 #define MSG_ID_DATA 6
110 #define MSG_ID_FLUSH 7
111 #define MSG_ID_SEM_RPT 8
112 #define MSG_ID_RCV_RPT 9
113 #define MSG_ID_TM_RPT 10
115 typedef struct _run_msg
123 typedef struct _random_isr_msg
126 unsigned random_isr_number;
130 typedef struct _data_msg
134 unsigned char checksum;
135 unsigned char data[1];
138 typedef struct _report_msg
147 void busy_wait(unsigned ticks)
149 unsigned t0 = tmcount;
150 while ((tmcount - t0) < ticks)
154 void send_run_signal()
156 run_msg* m = (run_msg*)alloc_mem_block(sizeof(run_msg));
158 m->header.msg_id = MSG_ID_RUN;
159 m->task_id = current_task_id();
160 m->tmcount = tmcount;
161 int r = send_msg(OC_TASK, &m->header);
165 void send_run_signal_p(int parameter)
167 run_msg* m = (run_msg*)alloc_mem_block(sizeof(run_msg));
169 m->header.msg_id = MSG_ID_RUN_P;
170 m->task_id = current_task_id();
171 m->tmcount = tmcount;
172 m->parameter = parameter;
173 int r = send_msg(OC_TASK, &m->header);
177 void tsend_run_signal_p(int task_id, int parameter)
179 run_msg* m = (run_msg*)alloc_mem_block(sizeof(run_msg));
181 m->header.msg_id = MSG_ID_RUN_P;
182 m->task_id = current_task_id();
183 m->tmcount = tmcount;
184 m->parameter = parameter;
185 int r = send_msg(task_id, &m->header);
189 void check_no_signal()
192 int r = recv_msg(&m, NO_WAIT);
193 assert(r == TIMED_OUT);
196 unsigned check_for_signal(int task_id)
199 int r = recv_msg(&m, NO_WAIT);
201 assert(m->msg_id == MSG_ID_RUN);
202 run_msg* rm = (run_msg*)m;
203 assert(rm->task_id == task_id);
204 unsigned tmc = rm->tmcount;
209 int check_for_signal_p(int task_id, int task_id2, unsigned* pt)
212 int r = recv_msg(&m, NO_WAIT);
214 assert(m->msg_id == MSG_ID_RUN_P);
215 run_msg* rm = (run_msg*)m;
216 assert(rm->task_id == task_id);
217 assert(m->sending_task_id == task_id2);
225 int wait_for_signal_p(int task_id, unsigned* pt)
228 int r = recv_msg(&m, WAIT_FOREVER);
230 assert(m->msg_id == MSG_ID_RUN_P);
231 run_msg* rm = (run_msg*)m;
232 assert(rm->task_id == task_id);
240 void resume_4(int t1, int t2, int t3, int t4)
243 assert(resume_task(t1)==OK);
245 assert(resume_task(t2)==OK);
247 assert(resume_task(t3)==OK);
249 assert(resume_task(t4)==OK);
252 void check_signal_4(int t1, int t2, int t3, int t4)
255 check_for_signal(t1);
259 check_for_signal(t2);
263 check_for_signal(t3);
267 check_for_signal(t4);
272 void check_for_multiple_signals(int task_id, int count)
274 unsigned t = check_for_signal(task_id);
277 unsigned t2 = check_for_signal(task_id);
283 int flush_signals(void)
289 int r = recv_msg(&m, NO_WAIT);
293 assert(m->msg_id == MSG_ID_RUN);
300 void test_mem_pool(size_t size, int count, void** chain)
305 for (i=0; i<count; ++i)
307 b = alloc_mem_block(size);
309 fill = (int)(size>>5);
313 memset(b, fill, size);
315 ((int*)b)[1] = (int)size;
318 bb = alloc_mem_block(size);
323 void check_blocks(void* chain)
328 unsigned char *q, *qq;
332 fill = (fill*fill)&0xff;
333 q = (unsigned char*)p + sizeof(void*) + sizeof(int);
334 qq = (unsigned char*)p + size;
343 int free_blocks(void* chain)
349 void* n = *(void**)p;
357 void test_mem_mgr(void)
360 const poolinfo* pi = pool_list;
363 for (; pi->block_size; ++pi)
365 nblocks += pi->block_count;
366 test_mem_pool(pi->block_size, pi->block_count, &chain);
369 nfreed = free_blocks(chain);
370 assert(nfreed == nblocks);
372 for (--pi; pi >= pool_list; --pi)
373 test_mem_pool(pi->block_size, pi->block_count, &chain);
375 nfreed = free_blocks(chain);
376 assert(nfreed == nblocks);
378 kprintf("Memory Manager Test OK");
381 void test_suspend_1(void)
388 assert( ((int)t2)-((int)t1) >= 5 );
389 r = suspend_task(TM_TASK);
395 r = resume_task(TM_TASK);
398 assert( ((int)t3)-((int)t2) >= 5 );
400 r = suspend_task(TM_TASK);
402 r = suspend_task(TM_TASK);
408 r = resume_task(TM_TASK);
412 r = resume_task(TM_TASK);
415 assert( ((int)t3)-((int)t2) >= 5 );
417 r = suspend_task(-1);
418 assert(r == BAD_TASK_ID);
419 r = suspend_task(300);
420 assert(r == BAD_TASK_ID);
421 r = suspend_task(NONEXISTENT_TASK);
422 assert(r == BAD_TASK_ID);
424 assert(r == BAD_TASK_ID);
425 r = resume_task(300);
426 assert(r == BAD_TASK_ID);
427 r = resume_task(NONEXISTENT_TASK);
428 assert(r == BAD_TASK_ID);
430 kprintf("test_suspend_1 OK");
433 void test_priority_scheduling(void)
435 int init_pri = get_task_priority(current_task_id());
436 resume_4(TASK1, TASK2, TASK3, TASK4);
438 check_for_multiple_signals(TASK1, 50); // check no timeslicing
439 assert(flush_signals()<=31);
442 check_for_multiple_signals(TASK2, 50); // check no timeslicing
443 assert(flush_signals()<=31);
446 check_for_multiple_signals(TASK3, 50); // check no timeslicing
447 assert(flush_signals()<=31);
450 check_for_signal(TASK4);
451 assert(flush_signals()<=1);
458 resume_4(TASK1, TASK2, TASK3, TASK4);
462 resume_4(TASK3, TASK2, TASK4, TASK1);
464 check_signal_4(TASK3, TASK2, TASK4, TASK1);
466 resume_4(TASK1, TASK2, TASK3, TASK4);
467 check_no_signal(); // all lower priority so don't run
468 set_task_priority(TASK2, 255); // higher than current task so run immediately
469 check_for_signal(TASK2);
470 set_task_priority(TASK4, 116);
471 check_no_signal(); // all lower priority so don't run
473 check_for_signal(TASK4);
474 check_for_signal(TASK1);
475 check_for_signal(TASK3);
476 set_task_priority(TASK1, 116);
477 set_task_priority(TASK2, 116);
478 set_task_priority(TASK3, 116);
479 set_task_priority(TASK4, 116);
480 resume_4(TASK1, TASK2, TASK3, TASK4);
481 set_task_priority(current_task_id(), 112); // drop current task priority
482 assert(get_task_priority(current_task_id())==112);
483 check_signal_4(TASK1, TASK2, TASK3, TASK4);
484 set_task_priority(current_task_id(), init_pri);
485 assert(get_task_priority(current_task_id())==init_pri);
487 kprintf("test_priority_scheduling OK");
490 unsigned sem_test(int task_id)
492 int r = semaphore_signal(TEST_SEM);
494 return check_for_signal(task_id);
497 unsigned sem_test_p(int task_id, int parameter)
500 int r = semaphore_signal(TEST_SEM);
502 r = check_for_signal_p(task_id, task_id, &t);
503 assert(r == parameter);
507 unsigned sem_test_pt(int task_id, int parameter)
510 int r = semaphore_signal(TEST_SEM);
512 r = check_for_signal_p(task_id, task_id, &t);
513 assert(r == parameter);
517 void test_semaphore(void)
521 int init_pri = get_task_priority(current_task_id());
522 set_task_priority(TASK1, 128);
523 set_task_priority(TASK2, 128);
524 set_task_priority(TASK3, 128);
525 set_task_priority(TASK4, 128);
530 resume_4(TASK1, TASK2, TASK3, TASK4);
531 delay(10); // let tasks wait on semaphore
533 sem_test(TASK1); // test they are released in same order
538 set_task_priority(TASK3, 132); // test highest priority is released first
541 suspend_task(TASK3); // test suspended task doesn't contend for semaphore
550 set_task_priority(TASK2, 136); // change priority while suspended
556 sem_test(TASK2); // test new highest priority task acquires semaphore first
557 delay(100*TM_PERIOD);
558 check_no_signal(); // check waits don't time out
560 t2func = 3; // switch over to timed waits for task 2
561 t1 = sem_test(TASK2); // get one last message of previous type
563 t2 = sem_test_p(TASK2, OK); // signal after half the timeout and check OK
564 delay(11*TM_PERIOD); // wait for > timeout
565 r = check_for_signal_p(TASK2, TASK2, &t3);
566 assert(r == TIMED_OUT);
567 kprintf("t2-t1=%d t3-t2=%d", t2-t1, t3-t2);
570 sem_test_p(TASK2, OK);
573 set_task_priority(current_task_id(), 176); // raise current task priority
574 semaphore_signal(TEST_SEM); // signal semaphore 4 times - should release all 4 waiting threads
575 semaphore_signal(TEST_SEM);
576 semaphore_signal(TEST_SEM);
577 semaphore_signal(TEST_SEM);
578 set_task_priority(current_task_id(), init_pri); // let tasks run
579 r = check_for_signal_p(TASK2, TASK2, NULL);
581 check_for_signal(TASK3);
582 check_for_signal(TASK4);
583 check_for_signal(TASK1);
584 set_task_priority(current_task_id(), 176); // raise current task priority
585 busy_wait(11); // let semaphore wait time out
586 t1func = 4; // switch all threads over
590 semaphore_signal(TEST_SEM); // signal semaphore 3 times - should release other 3 waiting threads
591 semaphore_signal(TEST_SEM);
592 semaphore_signal(TEST_SEM);
593 set_task_priority(current_task_id(), init_pri); // let tasks run
594 r = check_for_signal_p(TASK2, TASK2, NULL);
595 assert(r == TIMED_OUT);
596 check_for_signal(TASK3);
597 check_for_signal(TASK4);
598 check_for_signal(TASK1);
600 kprintf("test_semaphore OK");
603 void test_message_queue(void)
605 unsigned t1, t2, t3, t4;
607 int init_pri = get_task_priority(current_task_id());
610 for (tid = TASK1; tid <= TASK4; ++tid)
612 for (p = 1; p; p<<=1)
614 tsend_run_signal_p(tid, p);
615 r = check_for_signal_p(OC_TASK, tid, NULL);
620 set_task_priority(current_task_id(), 176); // raise current task priority
621 set_task_priority(TASK4, 144); // change task priorities while they are waiting
622 set_task_priority(TASK3, 140);
623 set_task_priority(TASK2, 136);
624 set_task_priority(TASK1, 132);
625 t1func = 5; // switch task 1 to timed waits
626 for (tid = TASK1; tid <= TASK4; ++tid)
628 for (p = 0; p<0x40000000; p+=(0x413b9cb+tid))
630 tsend_run_signal_p(tid, p); // let multiple messages accumulate on the queues
634 set_task_priority(current_task_id(), init_pri); // let tasks run
635 kprintf("init_pri=%d",init_pri);
636 for (tid = TASK4; tid >= TASK1; --tid)
638 for (p = 0; p<0x40000000; p+=(0x413b9cb+tid))
640 r = check_for_signal_p(OC_TASK, tid, &t1);
646 tsend_run_signal_p(TASK1, p); // send after half timeout
647 r = check_for_signal_p(OC_TASK, TASK1, &t2);
649 delay(11*TM_PERIOD); // wait for > timeout
650 tsend_run_signal_p(TASK1, ~p); // send after timeout
651 r = check_for_signal_p(TASK1, TASK1, &t3);
652 assert(r == TIMED_OUT);
653 kprintf("t2-t1=%d t3-t2=%d", t2-t1, t3-t2);
656 r = check_for_signal_p(OC_TASK, TASK1, &t4);
659 t1func = 6; // switch task 1 to timed semaphore wait
660 t2func = 7; // switch task 2 to timed queue wait
663 for (tid = TASK1; tid <= TASK4; ++tid)
665 tsend_run_signal_p(tid, 0);
666 r = check_for_signal_p(OC_TASK, tid, NULL);
671 kprintf("test_message_queue OK");
674 void random_isr(unsigned n)
686 m = (random_isr_msg*)alloc_mem_block(sizeof(random_isr_msg));
687 m->header.msg_id = MSG_ID_RND_ISR;
688 m->random_isr_number = n;
691 r = send_msg(L1_TASK, &m->header);
693 if (random_sem_signal_count && !--random_sem_signal_count)
695 random_sem_signal_count = random_sem_signal_interval;
696 semaphore_signal(ISR_SEM);
700 void flush_queue(msghdr** f, msghdr** l, msghdr* tm)
714 void l1_task_entry(void)
716 msghdr* first = NULL;
719 unsigned extra_count = 0;
720 unsigned extra_value = 0;
721 assert(current_task_id() == L1_TASK);
722 kprintf("L1_TASK running");
726 int r = recv_msg(&m, WAIT_FOREVER);
732 random_isr_msg* rm = (random_isr_msg*)m;
733 assert(m->sending_task_id == TASK_ID_ISR);
734 assert(rm->random_isr_number == next_random_id);
738 if (!(next_random_id % 11))
740 if (!(next_random_id % 13))
742 extra_value = next_random_id;
746 extra_value *= next_random_id;
748 assert(rm->extra == extra_value);
749 if (++state > extra_count)
753 if (rm->random_isr_number == 0)
754 send_msg(OC_TASK, m), m=NULL;
755 if (state == 1 && extra_count == 2 && m)
757 flush_queue(&first, &last, m);
760 if (random_send_count && !--random_send_count)
762 random_send_count = random_send_interval;
764 send_msg(TASK2, m), m=NULL;
778 flush_queue(&first, &last, m);
782 kprintf("L1<-%08x",m->msg_id);
790 void l2_task_entry(void)
792 assert(current_task_id() == L2_TASK);
793 kprintf("L2_TASK running");
797 int r = recv_msg(&m, WAIT_FOREVER);
803 data_msg* dm = (data_msg*)m;
805 unsigned char cs = 0;
806 for (i=0; i<dm->length; ++i)
807 cs = (unsigned char)(cs + dm->data[i]);
809 send_msg(L1_TASK, m);
814 kprintf("L2<-%08x",m->msg_id);
822 void rr_task_entry(void)
824 assert(current_task_id() == RR_TASK);
825 kprintf("RR_TASK running");
829 int r = recv_msg(&m, WAIT_FOREVER);
834 send_msg(L2_TASK, m);
838 kprintf("RR<-%08x",m->msg_id);
846 void tm_task_entry(void)
848 assert(current_task_id() == TM_TASK);
849 kprintf("TM_TASK running");
853 int r = recv_msg(&m, WAIT_FOREVER);
858 tmcount = ((timer_msg*)m)->count;
859 assert(m->sending_task_id == TASK_ID_ISR);
860 if (!(tmcount & 255))
862 report_msg* rpt = (report_msg*)alloc_mem_block(sizeof(report_msg));
863 rpt->header.msg_id = MSG_ID_TM_RPT;
864 rpt->count = tmcount;
867 send_to_epoc(&rpt->header);
871 kprintf("TM<-%08x",m->msg_id);
878 void generic_task(volatile int* f)
884 unsigned ok_count = 0;
885 unsigned bad_count = 0;
894 suspend_task(current_task_id());
898 r = semaphore_wait(TEST_SEM, WAIT_FOREVER);
904 r = semaphore_wait(TEST_SEM, 10*TM_PERIOD);
905 assert(r==OK || r==TIMED_OUT);
906 send_run_signal_p(r);
910 r = recv_msg(&m, WAIT_FOREVER);
912 assert(m->sending_task_id == OC_TASK);
913 r = send_msg(OC_TASK, m);
918 r = recv_msg(&m, 10*TM_PERIOD);
919 assert(r==OK || r==TIMED_OUT);
922 assert(m->sending_task_id == OC_TASK);
923 r = send_msg(OC_TASK, m);
927 send_run_signal_p(r);
932 r = semaphore_wait(ISR_SEM, 5);
933 t2 = tick_count() - t1;
934 if (r == TIMED_OUT && t2<5)
936 kprintf("SEM timed out too soon: %d", t2);
944 report_msg* rpt = (report_msg*)alloc_mem_block(sizeof(report_msg));
945 rpt->header.msg_id = MSG_ID_SEM_RPT;
947 rpt->ok_count = ok_count;
948 rpt->bad_count = bad_count;
949 send_to_epoc(&rpt->header);
956 t2 = tick_count() - t1;
957 if (r == TIMED_OUT && t2<5)
959 kprintf("RECV timed out too soon: %d", t2);
963 ++ok_count, free_mem_block(m);
967 report_msg* rpt = (report_msg*)alloc_mem_block(sizeof(report_msg));
968 rpt->header.msg_id = MSG_ID_RCV_RPT;
970 rpt->ok_count = ok_count;
971 rpt->bad_count = bad_count;
972 send_to_epoc(&rpt->header);
975 kprintf("Task %d finished", current_task_id());
977 suspend_task(current_task_id());
980 void task1_entry(void)
982 assert(current_task_id() == TASK1);
983 generic_task(&t1func);
986 void task2_entry(void)
988 assert(current_task_id() == TASK2);
989 generic_task(&t2func);
992 void task3_entry(void)
994 assert(current_task_id() == TASK3);
995 generic_task(&t3func);
998 void task4_entry(void)
1000 assert(current_task_id() == TASK4);
1001 generic_task(&t4func);
1006 void oo_overall_control(void)
1011 unsigned t1, t2, rss_interval;
1012 kprintf("OC_TASK running");
1013 assert(current_task_id() == OC_TASK);
1014 resume_task(L2_TASK);
1015 resume_task(RR_TASK);
1016 resume_task(TM_TASK);
1019 kprintf("Wait for init msg");
1020 r = recv_msg(&m, WAIT_FOREVER);
1022 assert(m->msg_id == MSG_ID_INIT);
1023 assert(m->sending_task_id == TASK_ID_UNKNOWN);
1025 kprintf("Received init msg");
1027 r = start_periodic_timer(TM_TIMER, TM_TASK, TM_INIT_DELAY, TM_PERIOD, NULL);
1029 delay(TM_INIT_DELAY-10);
1030 assert(tmcount == 0);
1031 delay(10*TM_PERIOD+20);
1032 assert(tmcount > 0);
1034 test_priority_scheduling();
1036 test_message_queue();
1038 resume_task(L1_TASK);
1039 r = start_random_isr(&random_isr);
1043 r = recv_msg(&m, WAIT_FOREVER);
1045 assert(m->msg_id == MSG_ID_RND_ISR);
1046 assert(m->sending_task_id == L1_TASK);
1047 rm = (random_isr_msg*)m;
1048 assert(rm->random_isr_number == 0);
1050 t1 = next_random_id;
1052 t2 = next_random_id;
1053 kprintf("%d random ISRs in 1024 ticks", t2-t1);
1054 rss_interval = (5*(t2-t1)+512)/1024;
1055 set_task_priority(TASK1, 196); // needs to be higher than DfcThread1
1056 set_task_priority(TASK2, 196);
1057 random_sem_signal_interval = rss_interval;
1058 random_sem_signal_count = rss_interval;
1059 random_send_interval = rss_interval;
1060 random_send_count = rss_interval;
1063 m = (msghdr*)alloc_mem_block(sizeof(msghdr));
1064 m->msg_id = MSG_ID_DONE;
1066 kprintf("All tests completed OK");
1069 int r = recv_msg(&m, WAIT_FOREVER);
1074 send_msg(RR_TASK, m);
1078 send_msg(L1_TASK, m);
1083 stop_timer(TM_TIMER);
1084 suspend_task(L1_TASK);
1085 suspend_task(L2_TASK);
1086 suspend_task(RR_TASK);
1087 suspend_task(TM_TASK);
1088 suspend_task(TASK1);
1089 suspend_task(TASK2);
1090 suspend_task(TASK3);
1091 suspend_task(TASK4);
1094 kprintf("OC<-%08x",m->msg_id);