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 1024
51 #define TEST_TIME_OUT_MS 4000
54 // Settings for the test process.
57 #define DELAY_PROC_T1 INC_PROC_T1*DELAY
60 #define DELAY_PROC_T2 INC_PROC_T2*DELAY
63 #define DELAY_PROC_T3 INC_PROC_T3*DELAY
66 #define DELAY_PROC_T4 INC_PROC_T3*DELAY
68 #define INC_PROC_T5 11
69 #define DELAY_PROC_T5 INC_PROC_T3*DELAY
71 #define INC_PROC_T6 13
72 #define DELAY_PROC_T6 INC_PROC_T3*DELAY
74 #define INC_PROC_T7 17
75 #define DELAY_PROC_T7 INC_PROC_T3*DELAY
77 #define INC_PROC_T8 19
78 #define DELAY_PROC_T8 INC_PROC_T3*DELAY
81 unsigned int global_count = 0;
84 * These macro generate the code that needed to create the
85 * test process function and all it needed.
87 #define PROC_TEST(num) static void proc_test##num(void) \
89 unsigned int local_count = 0; \
91 for (int i = 0; i < INC_PROC_T##num; ++i) \
94 kprintf("> test%d: Obtain semaphore.\n", num); \
95 local_count = global_count; \
96 kprintf("> test%d: Read global count [%d]\n", num, local_count); \
97 timer_delay(DELAY_PROC_T##num); \
98 local_count += INC_PROC_T##num; \
99 global_count = local_count; \
100 kprintf("> test%d: Update count g[%d] l[%d]\n", num, global_count, local_count); \
102 kprintf("> test%d: Relase semaphore.\n", num); \
106 #define PROC_TEST_STACK(num) static cpu_stack_t proc_test##num##_stack[CONFIG_KERN_MINSTACKSIZE / sizeof(cpu_stack_t)];
107 #define PROC_TEST_INIT(num) proc_new(proc_test##num, NULL, sizeof(proc_test##num##_stack), proc_test##num##_stack);
119 // Define process stacks for test.
132 int sem_testRun(void)
134 ticks_t start_time = timer_clock();
136 kprintf("Run semaphore test..\n");
138 //Init the process tests
147 kputs("> Main: Processes created\n");
150 * Wait until all process finishing, if some going wrong we return
151 * error after time_out_ms ms.
153 while((timer_clock() - start_time) < ms_to_ticks(TEST_TIME_OUT_MS))
155 if (sem_attempt(&sem))
157 kputs("> Main: Check if test is finish..\n");
158 if(global_count == MAX_GLOBAL_COUNT)
160 kputs("> Main: Test Finished..Ok!\n");
164 kputs("> Main: Test is still running..\n");
169 kputs("Semaphore Test fail..\n");
173 int sem_testSetup(void)
177 kprintf("Init Semaphore..");
181 #if CONFIG_KERN_PREEMPT
182 kprintf("Init Interrupt (preempt mode)..");
187 kprintf("Init Timer..");
191 kprintf("Init Process..");
198 int sem_testTearDown(void)
200 kputs("TearDown Semaphore test.\n");