Add process to test semaphore. Add debung string.
[bertos.git] / bertos / kern / sem_test.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2008 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  * \brief Semaphore test.
33  *
34  * \version $Id$
35  * 
36  * \author Daniele Basile <asterix@develer.com>
37  * \author Stefano Fedrigo <aleph@develer.com> 
38  * 
39  */
40
41 #include <cfg/test.h>
42
43 #include <kern/sem.h>
44 #include <kern/proc.h>
45 #include <kern/irq.h>
46
47 #include <drv/timer.h>
48
49 // Global settings for the test.
50 #define MAX_GLOBAL_COUNT               10
51 #define TEST_TIME_OUT_MS             1000
52 #define DELAY                          10
53 #define INC_PROC_T1                     1
54 #define DELAY_PROC_T1   INC_PROC_T1*DELAY
55 #define INC_PROC_T2                     3
56 #define DELAY_PROC_T2   INC_PROC_T2*DELAY
57
58 Semaphore sem;
59 unsigned int global_count = 0;
60
61 /*
62  * Proc scheduling test subthread 1
63  */
64 static void proc_test1(void)
65 {
66         unsigned int local_count = 0;
67         
68         for (int i = 0; i < INC_PROC_T1; ++i)
69         {
70                 kputs("> test1\n");
71                 sem_obtain(&sem);
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);
79                 sem_release(&sem);
80                 kputs("> test1: Relase semaphore.\n");
81         }
82 }
83
84 /*
85  * Proc scheduling test subthread 2
86  */
87 static void proc_test2(void)
88 {
89         unsigned int local_count = 0;
90         
91         for (int i = 0; i < INC_PROC_T2; ++i)
92         {
93                 kputs("> test2\n");
94                 sem_obtain(&sem);
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);
102                 sem_release(&sem);
103                 kputs("> test2: Relase semaphore.\n");
104         }
105 }
106
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)];
110
111
112 /**
113  * Run semaphore test
114  */
115 int sem_testRun(void)
116 {
117         ticks_t start_time = timer_clock();
118
119         kprintf("Run semaphore test..\n");
120         
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");
124         
125         /*
126          * Wait until all process finishing, if some going wrong we return 
127          * error after time_out_ms ms.
128          */ 
129         while((timer_clock() - start_time) < ms_to_ticks(TEST_TIME_OUT_MS))
130         {
131                 if (sem_attempt(&sem))
132                 {
133                         kputs("> Main: Check if test is finish..\n");
134                         if(global_count == MAX_GLOBAL_COUNT)
135                         {
136                                 kputs("> Main: Test Finished..Ok!\n");
137                                 return 0;
138                         }
139                         sem_release(&sem);
140                         kputs("> Main: Test is still running..\n");
141                 }
142                 proc_yield();
143         }
144         
145         kputs("Semaphore Test fail..\n");
146         return -1;
147 }
148
149 int sem_testSetup(void)
150 {
151         kdbg_init();
152
153         kprintf("Init Semaphore..");
154         sem_init(&sem);
155         kprintf("Done.\n");
156         
157         #if CONFIG_KERN_PREEMPT
158                 kprintf("Init Interrupt (preempt mode)..");
159                 irq_init();
160                 kprintf("Done.\n");
161         #endif
162
163         kprintf("Init Timer..");
164         timer_init();
165         kprintf("Done.\n");
166         
167         kprintf("Init Process..");
168         proc_init();
169         kprintf("Done.\n");
170         
171         return 0;
172 }
173
174 int sem_testTearDown(void)
175 {
176         kputs("TearDown Semaphore test.\n");
177         return 0;
178 }
179
180 TEST_MAIN(sem);