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 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: mdemo.c,v 1.1 2002-11-11 08:22:46 pj Exp $ |
||
38 | |||
39 | This test verify the correctness of the NOP module. It works with the |
||
40 | PI, PC, SRP module, too. |
||
41 | |||
42 | The test uses one mutex |
||
43 | |||
44 | the main task (NRT) creates three tasks. |
||
45 | |||
46 | J1 with PC priority 0 |
||
47 | starts at t=0.5 sec and lock m0 |
||
48 | |||
49 | J2 with PC priority 1 |
||
50 | starts at t=1 sec and doesn't lock any mutex |
||
51 | |||
52 | J3 with PC priority 2 |
||
53 | it starts and locks m0 |
||
54 | at t=2 sec it unlocks m1 |
||
55 | |||
56 | |||
57 | The example is similar to the scheduling diagram shown at p. 188 of the |
||
58 | book "Sistemi in tempo Reale", by Giorgio Buttazzo, Pitagora Editrice |
||
59 | |||
60 | */ |
||
61 | |||
62 | #include "kernel/kern.h" |
||
63 | #include "drivers/keyb.h" |
||
64 | #include "modules/srp.h" |
||
65 | |||
66 | mutex_t m0; |
||
67 | |||
68 | |||
69 | void startJ(void *a) |
||
70 | { |
||
71 | task_activate((PID)a); |
||
72 | } |
||
73 | |||
74 | TASK j1() |
||
75 | { |
||
76 | cprintf("J1: before locking m0\n"); |
||
77 | mutex_lock(&m0); |
||
78 | cprintf("J1: locked m0\n"); |
||
79 | mutex_unlock(&m0); |
||
80 | cprintf("J1: unlocked m0, end task\n"); |
||
81 | return 0; |
||
82 | } |
||
83 | |||
84 | |||
85 | TASK j2() |
||
86 | { |
||
87 | cprintf("J2: waiting t=1.5 sec\n"); |
||
88 | |||
89 | while (sys_gettime(NULL) < 1500000); |
||
90 | |||
91 | cprintf("J2: end task\n"); |
||
92 | return 0; |
||
93 | } |
||
94 | |||
95 | |||
96 | TASK j3() |
||
97 | { |
||
98 | cprintf("J3: before locking m0\n"); |
||
99 | mutex_lock(&m0); |
||
100 | cprintf("J3: locked m0, waiting to t=2 sec\n"); |
||
101 | |||
102 | while (sys_gettime(NULL) < 2000000); |
||
103 | |||
104 | cprintf("J3: t = 1 sec reached, unlocking m0\n"); |
||
105 | mutex_unlock(&m0); |
||
106 | cprintf("J3: unlocked m0, end task\n"); |
||
107 | return 0; |
||
108 | } |
||
109 | |||
110 | void fine(KEY_EVT *e) |
||
111 | { |
||
112 | sys_end(); |
||
113 | } |
||
114 | |||
115 | |||
116 | int main(int argc, char **argv) |
||
117 | { |
||
118 | struct timespec t; |
||
119 | |||
120 | HARD_TASK_MODEL m; |
||
121 | PID p0,p1,p2; |
||
122 | |||
123 | PC_mutexattr_t a; |
||
124 | PI_mutexattr_t a2; |
||
125 | NOP_mutexattr_t a3; |
||
126 | SRP_mutexattr_t a4; |
||
127 | NPP_mutexattr_t a5; |
||
128 | |||
129 | PC_RES_MODEL r; |
||
130 | SRP_RES_MODEL srp; |
||
131 | |||
132 | KEY_EVT emerg; |
||
133 | |||
134 | //keyb_set_map(itaMap); |
||
135 | emerg.ascii = 'x'; |
||
136 | emerg.scan = KEY_X; |
||
137 | emerg.flag = ALTL_BIT; |
||
138 | keyb_hook(emerg,fine); |
||
139 | |||
140 | /* --------------------------------------------------------------------- |
||
141 | Mutex creation |
||
142 | --------------------------------------------------------------------- */ |
||
143 | |||
144 | PC_mutexattr_default(a,0); |
||
145 | PI_mutexattr_default(a2); |
||
146 | NOP_mutexattr_default(a3); |
||
147 | SRP_mutexattr_default(a4); |
||
148 | NPP_mutexattr_default(a5); |
||
149 | mutex_init(&m0,&a4); |
||
150 | |||
151 | /* --------------------------------------------------------------------- |
||
152 | Task creation |
||
153 | --------------------------------------------------------------------- */ |
||
154 | |||
155 | hard_task_default_model(m); |
||
156 | hard_task_def_wcet(m,20000); |
||
157 | hard_task_def_mit(m,10000000); |
||
158 | PC_res_default_model(r,0); |
||
159 | SRP_res_default_model(srp,3); |
||
160 | p0 = task_createn("J1", j1, (TASK_MODEL *)&m, &r, &srp, SRP_usemutex(&m0), NULL); |
||
161 | if (p0 == NIL) |
||
162 | { cprintf("Can't create J1 task...\n"); return 1; } |
||
163 | |||
164 | hard_task_def_wcet(m,1600000); |
||
165 | hard_task_def_mit(m,21000000); |
||
166 | PC_res_default_model(r,1); |
||
167 | SRP_res_default_model(srp,2); |
||
168 | p1 = task_createn("J2", j2, (TASK_MODEL *)&m, &r, &srp, NULL); |
||
169 | if (p1 == NIL) |
||
170 | { cprintf("Can't create J2 task...\n"); return 1; } |
||
171 | |||
172 | hard_task_def_wcet(m,3000000); |
||
173 | hard_task_def_mit(m,100000000); |
||
174 | PC_res_default_model(r,2); |
||
175 | SRP_res_default_model(srp,1); |
||
176 | p2 = task_createn("J3", j3, (TASK_MODEL *)&m, &r, &srp, SRP_usemutex(&m0), NULL); |
||
177 | if (p2 == NIL) |
||
178 | { cprintf("Can't create J3 task...\n"); return 1; } |
||
179 | |||
180 | |||
181 | /* --------------------------------------------------------------------- |
||
182 | Event post |
||
183 | --------------------------------------------------------------------- */ |
||
184 | |||
185 | t.tv_sec = 0; |
||
186 | t.tv_nsec = 500000000; |
||
187 | |||
188 | kern_cli(); |
||
189 | kern_event_post(&t,startJ,(void *)p0); |
||
190 | |||
191 | t.tv_sec = 1; |
||
192 | kern_event_post(&t,startJ,(void *)p1); |
||
193 | kern_sti(); |
||
194 | |||
195 | task_activate(p2); |
||
196 | |||
197 | cprintf("END main\n"); |
||
198 | |||
199 | return 0; |
||
200 | } |