4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2008 Develer S.r.l. (http://www.develer.com/)
32 * \brief Semaphore test.
36 * \author Daniele Basile <asterix@develer.com>
37 * \author Stefano Fedrigo <aleph@develer.com>
44 #include <kern/proc.h>
47 #include <drv/timer.h>
49 // Global settings for the test.
50 #define MAX_GLOBAL_COUNT 10
51 #define TEST_TIME_OUT_MS 1000
54 #define DELAY_PROC_T1 INC_PROC_T1*DELAY
56 #define DELAY_PROC_T2 INC_PROC_T2*DELAY
59 unsigned int global_count = 0;
62 * Proc scheduling test subthread 1
64 static void proc_test1(void)
66 unsigned int local_count = 0;
68 for (int i = 0; i < INC_PROC_T1; ++i)
72 kputs("> test1: Obtain semaphore.\n");
73 local_count = global_count;
74 kprintf("> test1: Read global count [%d]\n", local_count);
75 timer_delay(DELAY_PROC_T1);
76 local_count += INC_PROC_T1;
77 global_count = local_count;
78 kprintf("> test1: Update count g[%d] l[%d]\n", global_count, local_count);
80 kputs("> test1: Relase semaphore.\n");
85 * Proc scheduling test subthread 2
87 static void proc_test2(void)
89 unsigned int local_count = 0;
91 for (int i = 0; i < INC_PROC_T2; ++i)
95 kputs("> test2: Obtain semaphore.\n");
96 local_count = global_count;
97 kprintf("> test2: Read global count [%d]\n", local_count);
98 timer_delay(DELAY_PROC_T2);
99 local_count += INC_PROC_T2;
100 global_count = local_count;
101 kprintf("> test2: Update count g[%d] l[%d]\n", global_count, local_count);
103 kputs("> test2: Relase semaphore.\n");
107 // Define process stacks for test.
108 static cpu_stack_t proc_test1_stack[CONFIG_KERN_MINSTACKSIZE / sizeof(cpu_stack_t)];
109 static cpu_stack_t proc_test2_stack[CONFIG_KERN_MINSTACKSIZE / sizeof(cpu_stack_t)];
115 int sem_testRun(void)
117 ticks_t start_time = timer_clock();
119 kprintf("Run semaphore test..\n");
121 proc_new(proc_test1, NULL, sizeof(proc_test1_stack), proc_test1_stack);
122 proc_new(proc_test2, NULL, sizeof(proc_test2_stack), proc_test2_stack);
123 kputs("> Main: Processes created\n");
126 * Wait until all process finishing, if some going wrong we return
127 * error after time_out_ms ms.
129 while((timer_clock() - start_time) < ms_to_ticks(TEST_TIME_OUT_MS))
131 if (sem_attempt(&sem))
133 kputs("> Main: Check if test is finish..\n");
134 if(global_count == MAX_GLOBAL_COUNT)
136 kputs("> Main: Test Finished..Ok!\n");
140 kputs("> Main: Test is still running..\n");
145 kputs("Semaphore Test fail..\n");
149 int sem_testSetup(void)
153 kprintf("Init Semaphore..");
157 #if CONFIG_KERN_PREEMPT
158 kprintf("Init Interrupt (preempt mode)..");
163 kprintf("Init Timer..");
167 kprintf("Init Process..");
174 int sem_testTearDown(void)
176 kputs("TearDown Semaphore test.\n");