Rev 1123 | 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 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: srpdemo.c,v 1.1 2002-11-11 08:22:46 pj Exp $ |
||
38 | |||
39 | This test verify the correctness of the SRP module. |
||
40 | |||
41 | There are 3 taks, Jh, Jm, Jl that uses 3 mutexes m1, m2, m3 |
||
42 | |||
43 | the main task (NRT) creates the three tasks. |
||
44 | |||
45 | Jh with preemption level 3 |
||
46 | starts at t=1.5 sec and lock m3, lock m1, unlock m1, unlock m3 |
||
47 | |||
48 | Jm with preemption level 2 |
||
49 | starts at t=0.5 sec and lock m3, lock m2, unlock m2, unlock m3 |
||
50 | then lock and unlock m1 |
||
51 | |||
52 | Jl with preemption level 1 |
||
53 | it starts and locks m2 |
||
54 | at t=1 sec it locks m1 |
||
55 | at t=1.5 sec it unlocks m1 |
||
56 | then it unlocks m2, and finally it locks and unlocks m3 |
||
57 | |||
58 | |||
59 | The example is similar to the scheduling diagram shown at p. 210 of the |
||
60 | book "Sistemi in tempo Reale", by Giorgio Buttazzo, Pitagora Editrice |
||
61 | |||
62 | */ |
||
63 | |||
64 | #include "kernel/kern.h" |
||
65 | #include "drivers/keyb.h" |
||
66 | |||
67 | #include "modules/srp.h" |
||
68 | |||
69 | mutex_t m1,m2,m3; |
||
70 | |||
71 | void startJ(void *a) |
||
72 | { |
||
73 | task_activate((PID)a); |
||
74 | } |
||
75 | |||
76 | TASK Jlobby() |
||
77 | { |
||
78 | cprintf("(*) JLobby!!!\n"); |
||
79 | return 0; |
||
80 | } |
||
81 | |||
82 | TASK Jh() |
||
83 | { |
||
84 | PID l; |
||
85 | HARD_TASK_MODEL m; |
||
86 | SRP_RES_MODEL r; |
||
87 | |||
88 | cprintf("JH: creating Jy before locking m3\n"); |
||
89 | |||
90 | hard_task_default_model(m); |
||
91 | hard_task_def_mit(m,30000); |
||
92 | hard_task_def_wcet(m,1000); |
||
93 | SRP_res_default_model(r,4); |
||
94 | l = task_create("Jlobby",Jlobby,&m,&r); |
||
95 | task_activate(l); |
||
96 | |||
97 | mutex_lock(&m3); |
||
98 | cprintf("JH: locked m3, locking m1\n"); |
||
99 | mutex_lock(&m1); |
||
100 | cprintf("JH: locked m1, unlocking m1\n"); |
||
101 | mutex_unlock(&m1); |
||
102 | cprintf("JH: unlocked m1, unlocking m3\n"); |
||
103 | mutex_unlock(&m3); |
||
104 | cprintf("JH: unlocked m3, end task\n"); |
||
105 | return 0; |
||
106 | } |
||
107 | |||
108 | |||
109 | TASK Jm() |
||
110 | { |
||
111 | cprintf("JM: before locking m3\n"); |
||
112 | mutex_lock(&m3); |
||
113 | cprintf("JM: locked m3, locking m2\n"); |
||
114 | mutex_lock(&m2); |
||
115 | cprintf("JM: locked m2, unlocking m2\n"); |
||
116 | mutex_unlock(&m2); |
||
117 | cprintf("JM: unlocked m2, unlocking m3\n"); |
||
118 | mutex_unlock(&m3); |
||
119 | cprintf("JM: unlocked m3, locking m1\n"); |
||
120 | mutex_lock(&m1); |
||
121 | cprintf("JM: locked m1, unlocking m1\n"); |
||
122 | mutex_unlock(&m1); |
||
123 | cprintf("JM: unlocked m1, end task\n"); |
||
124 | return 0; |
||
125 | } |
||
126 | |||
127 | |||
128 | TASK Jl() |
||
129 | { |
||
130 | cprintf("JL: before locking m2\n"); |
||
131 | mutex_lock(&m2); |
||
132 | cprintf("JL: locked m2, waiting to t=1 sec\n"); |
||
133 | |||
134 | while (sys_gettime(NULL) < 1000000); |
||
135 | |||
136 | cprintf("JL: t = 1 sec reached, locking m1\n"); |
||
137 | mutex_lock(&m1); |
||
138 | cprintf("JL: locked m1, waiting to t=2 sec\n"); |
||
139 | |||
140 | while (sys_gettime(NULL) < 2000000); |
||
141 | |||
142 | cprintf("JL: t = 2 sec reached, unlocking m1\n"); |
||
143 | mutex_unlock(&m1); |
||
144 | cprintf("JL: unlocked m1, unlocking m2\n"); |
||
145 | |||
146 | mutex_unlock(&m2); |
||
147 | |||
148 | cprintf("JL: unlocked m2, locking m3\n"); |
||
149 | mutex_lock(&m3); |
||
150 | cprintf("JL: locked m3, unlocking m3\n"); |
||
151 | mutex_unlock(&m3); |
||
152 | cprintf("JL: unlocked m3, end task\n"); |
||
153 | return 0; |
||
154 | } |
||
155 | |||
156 | void fine(KEY_EVT *e) |
||
157 | { |
||
158 | sys_end(); |
||
159 | } |
||
160 | |||
161 | |||
162 | int main(int argc, char **argv) |
||
163 | { |
||
164 | struct timespec t; |
||
165 | |||
166 | HARD_TASK_MODEL m; |
||
167 | PID p0,p1,p2; |
||
168 | |||
169 | SRP_mutexattr_t a; |
||
170 | SRP_RES_MODEL r; |
||
171 | |||
172 | PI_mutexattr_t a2; |
||
173 | |||
174 | KEY_EVT emerg; |
||
175 | |||
176 | cprintf("Stack resource Policy demo. Press Alt-X to exit the demo\n"); |
||
177 | |||
178 | //keyb_set_map(itaMap); |
||
179 | emerg.ascii = 'x'; |
||
180 | emerg.scan = KEY_X; |
||
181 | emerg.flag = ALTL_BIT; |
||
182 | keyb_hook(emerg,fine); |
||
183 | |||
184 | /* --------------------------------------------------------------------- |
||
185 | Mutex creation |
||
186 | --------------------------------------------------------------------- */ |
||
187 | |||
188 | PI_mutexattr_default(a2); |
||
189 | SRP_mutexattr_default(a); |
||
190 | mutex_init(&m1,&a); |
||
191 | mutex_init(&m2,&a); |
||
192 | mutex_init(&m3,&a); |
||
193 | |||
194 | |||
195 | /* --------------------------------------------------------------------- |
||
196 | Task creation |
||
197 | --------------------------------------------------------------------- */ |
||
198 | |||
199 | hard_task_default_model(m); |
||
200 | hard_task_def_mit(m, 1000000); |
||
201 | hard_task_def_wcet(m, 80000); |
||
202 | SRP_res_default_model(r, 3); |
||
203 | p0 = task_createn("JH", Jh, (TASK_MODEL *)&m, &r, SRP_usemutex(&m3), SRP_usemutex(&m1), NULL); |
||
204 | if (p0 == NIL) |
||
205 | { cprintf("Can't create JH task...\n"); return 1; } |
||
206 | |||
207 | hard_task_default_model(m); |
||
208 | hard_task_def_mit(m, 2100000); |
||
209 | hard_task_def_wcet(m, 80000); |
||
210 | SRP_res_default_model(r, 2); |
||
211 | p1 = task_createn("JM", Jm, (TASK_MODEL *)&m, &r, SRP_usemutex(&m3), SRP_usemutex(&m1), |
||
212 | SRP_usemutex(&m2), NULL); |
||
213 | if (p1 == NIL) |
||
214 | { cprintf("Can't create JM task...\n"); return 1; } |
||
215 | |||
216 | hard_task_default_model(m); |
||
217 | hard_task_def_mit(m, 10000000); |
||
218 | hard_task_def_wcet(m, 3000000); |
||
219 | SRP_res_default_model(r, 1); |
||
220 | p2 = task_createn("JL", Jl, (TASK_MODEL *)&m, &r, SRP_usemutex(&m3), SRP_usemutex(&m1), |
||
221 | SRP_usemutex(&m2), NULL); |
||
222 | if (p2 == NIL) |
||
223 | { cprintf("Can't create JL task...\n"); return 1; } |
||
224 | |||
225 | /* --------------------------------------------------------------------- |
||
226 | Event post |
||
227 | --------------------------------------------------------------------- */ |
||
228 | |||
229 | t.tv_sec = 0; |
||
230 | t.tv_nsec = 500000000; |
||
231 | |||
232 | kern_cli(); |
||
233 | kern_event_post(&t,startJ,(void *)p1); |
||
234 | |||
235 | t.tv_sec = 1; |
||
236 | kern_event_post(&t,startJ,(void *)p0); |
||
237 | kern_sti(); |
||
238 | |||
239 | task_activate(p2); |
||
240 | |||
241 | cprintf("END main\n"); |
||
242 | |||
243 | return 0; |
||
244 | } |