Rev 1377 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1120 | pj | 1 | /* |
2 | * Project: S.Ha.R.K. |
||
3 | * |
||
4 | * Coordinators: |
||
5 | * Giorgio Buttazzo <giorgio@sssup.it> |
||
6 | * Paolo Gai <pj@gandalf.sssup.it> |
||
7 | * |
||
8 | * Authors : |
||
9 | * Paolo Gai <pj@gandalf.sssup.it> |
||
10 | * (see the web pages for full authors list) |
||
11 | * |
||
12 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
13 | * |
||
14 | * http://www.sssup.it |
||
15 | * http://retis.sssup.it |
||
16 | * http://shark.sssup.it |
||
17 | */ |
||
18 | |||
19 | /* |
||
20 | * Copyright (C) 2000 Giorgio Buttazzo, Paolo Gai |
||
21 | * |
||
22 | * This program is free software; you can redistribute it and/or modify |
||
23 | * it under the terms of the GNU General Public License as published by |
||
24 | * the Free Software Foundation; either version 2 of the License, or |
||
25 | * (at your option) any later version. |
||
26 | * |
||
27 | * This program is distributed in the hope that it will be useful, |
||
28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
30 | * GNU General Public License for more details. |
||
31 | * |
||
32 | * You should have received a copy of the GNU General Public License |
||
33 | * along with this program; if not, write to the Free Software |
||
34 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
35 | * |
||
36 | * |
||
37 | * CVS : $Id: condtest.c,v 1.1 2002-11-11 08:22:45 pj Exp $ |
||
38 | |||
39 | This test verify the correctness of the condition variables. |
||
40 | (... it doesn't test all...) |
||
41 | |||
42 | The test uses 1 mutex |
||
43 | |||
44 | the main task (NRT) creates three tasks. |
||
45 | |||
46 | J0, J1, J3 |
||
47 | starts, lock the mutex, and wait on a condition variable |
||
48 | |||
49 | J2 |
||
50 | at t = 0.5 lock the mutex and call cond_signal |
||
51 | at t = 1 lock the mutex and call cond_signal |
||
52 | |||
53 | */ |
||
54 | |||
55 | #include "kernel/kern.h" |
||
56 | #include "drivers/keyb.h" |
||
57 | |||
58 | |||
59 | mutex_t m0; |
||
60 | cond_t c0; |
||
61 | |||
62 | int number = 0; |
||
63 | |||
64 | PID p0,p1,p2,p3; |
||
65 | |||
66 | TASK j0() |
||
67 | { |
||
68 | cprintf("J0: before locking m0\n"); |
||
69 | mutex_lock(&m0); |
||
70 | cprintf("J0: locked m0, waiting on c0, number =%d\n", number); |
||
71 | while (!number) { |
||
72 | cond_wait(&c0,&m0); |
||
73 | cprintf("J0: number = %d, if >0 unlocking m0\n",number); |
||
74 | } |
||
75 | number--; |
||
76 | mutex_unlock(&m0); |
||
77 | cprintf("J0: unlocked m0, end task\n"); |
||
78 | return 0; |
||
79 | } |
||
80 | |||
81 | |||
82 | TASK j1() |
||
83 | { |
||
84 | cprintf("J1: before locking m0\n"); |
||
85 | mutex_lock(&m0); |
||
86 | cprintf("J1: locked m0, waiting on c0, number =%d\n", number); |
||
87 | while (!number) { |
||
88 | cond_wait(&c0,&m0); |
||
89 | cprintf("J1: number = %d, if >0 unlocking m0\n",number); |
||
90 | } |
||
91 | number--; |
||
92 | mutex_unlock(&m0); |
||
93 | cprintf("J1: unlocked m0, end task\n"); |
||
94 | return 0; |
||
95 | } |
||
96 | |||
97 | |||
98 | TASK j2() |
||
99 | { |
||
100 | // struct timespec t; |
||
101 | |||
102 | cprintf("J2: started, waiting t=0.5 sec\n"); |
||
103 | while (sys_gettime(NULL) < 500000); |
||
104 | |||
105 | cprintf("J2: before locking m0\n"); |
||
106 | mutex_lock(&m0); |
||
107 | cprintf("J2: locked m0, number++ (was %d), cond_signal\n", number); |
||
108 | |||
109 | number++; |
||
110 | cond_signal(&c0); |
||
111 | // cond_broadcast(&c0); |
||
112 | |||
113 | cprintf("J2: unlocking m0\n"); |
||
114 | mutex_unlock(&m0); |
||
115 | |||
116 | cprintf("J2: waiting t=1 sec\n"); |
||
117 | while (sys_gettime(NULL) < 1000000); |
||
118 | |||
119 | cprintf("J2: Killing J3\n"); |
||
120 | task_kill(p3); |
||
121 | cprintf("J2: before locking m0\n"); |
||
122 | mutex_lock(&m0); |
||
123 | cprintf("J2: locked m0, number++ (was %d), cond_signal\n", number); |
||
124 | |||
125 | number++; |
||
126 | cond_signal(&c0); |
||
127 | // cond_broadcast(&c0); |
||
128 | |||
129 | cprintf("J2: unlocking m0\n"); |
||
130 | mutex_unlock(&m0); |
||
131 | cprintf("J2: unlocked m0, end task\n"); |
||
132 | return 0; |
||
133 | } |
||
134 | |||
135 | void cleanup_lock(void *arg) |
||
136 | { |
||
137 | cprintf("J3: KILL!!!\n"); |
||
138 | mutex_unlock(&m0); |
||
139 | cprintf("J3: unlocked m0 by the cleanup function\n"); |
||
140 | } |
||
141 | |||
142 | TASK j3() |
||
143 | { |
||
144 | cprintf("J3: before locking m0\n"); |
||
145 | mutex_lock(&m0); |
||
146 | cprintf("J3: locked m0, waiting on c0, number =%d\n", number); |
||
147 | task_cleanup_push(cleanup_lock, (void *)&m0); |
||
148 | while (!number) { |
||
149 | cond_wait(&c0,&m0); |
||
150 | cprintf("J3: number = %d, if >0 unlocking m0\n",number); |
||
151 | } |
||
152 | task_cleanup_pop(0); |
||
153 | // I hope this task never reach this point... it is killed by J2!!! |
||
154 | number--; |
||
155 | mutex_unlock(&m0); |
||
156 | cprintf("J3: unlocked m0, end task\n"); |
||
157 | return 0; |
||
158 | } |
||
159 | |||
160 | void fine(KEY_EVT *e) |
||
161 | { |
||
162 | sys_end(); |
||
163 | } |
||
164 | |||
165 | |||
166 | int main(int argc, char **argv) |
||
167 | { |
||
168 | // struct timespec t; |
||
169 | |||
170 | NRT_TASK_MODEL m; |
||
171 | |||
172 | PI_mutexattr_t a; |
||
173 | |||
174 | KEY_EVT emerg; |
||
175 | |||
176 | //keyb_set_map(itaMap); |
||
177 | emerg.ascii = 'x'; |
||
178 | emerg.scan = KEY_X; |
||
179 | emerg.flag = ALTL_BIT; |
||
180 | keyb_hook(emerg,fine); |
||
181 | |||
182 | /* --------------------------------------------------------------------- |
||
183 | Task creation |
||
184 | --------------------------------------------------------------------- */ |
||
185 | |||
186 | nrt_task_default_model(m); |
||
187 | nrt_task_def_group(m,1); |
||
188 | p0 = task_create("J0", j0, &m, NULL); |
||
189 | if (p0 == NIL) |
||
190 | { cprintf("Can't create J0 task...\n"); return 1; } |
||
191 | |||
192 | p1 = task_create("J1", j1, &m, NULL); |
||
193 | if (p1 == NIL) |
||
194 | { cprintf("Can't create J1 task...\n"); return 1; } |
||
195 | |||
196 | p2 = task_create("J2", j2, &m, NULL); |
||
197 | if (p2 == NIL) |
||
198 | { cprintf("Can't create J2 task...\n"); return 1; } |
||
199 | |||
200 | p3 = task_create("J3", j3, &m, NULL); |
||
201 | if (p3 == NIL) |
||
202 | { cprintf("Can't create J3 task...\n"); return 1; } |
||
203 | |||
204 | /* --------------------------------------------------------------------- |
||
205 | Mutex creation |
||
206 | --------------------------------------------------------------------- */ |
||
207 | |||
208 | PI_mutexattr_default(a); |
||
209 | mutex_init(&m0,&a); |
||
210 | |||
211 | cond_init(&c0); |
||
212 | |||
213 | /* --------------------------------------------------------------------- |
||
214 | Event post |
||
215 | --------------------------------------------------------------------- */ |
||
216 | |||
217 | group_activate(1); |
||
218 | |||
219 | cprintf("END main\n"); |
||
220 | |||
221 | return 0; |
||
222 | } |