Subversion Repositories shark

Rev

Rev 1120 | Rev 1377 | Go to most recent revision | Details | Compare with Previous | 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
 *
1123 pj 37
 * CVS :        $Id: srpdemo.c,v 1.2 2003-01-07 17:10:16 pj Exp $
1120 pj 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
 
1123 pj 176
  clear();
177
 
1120 pj 178
  cprintf("Stack resource Policy demo. Press Alt-X to exit the demo\n");
179
 
180
  //keyb_set_map(itaMap);
181
  emerg.ascii = 'x';
182
  emerg.scan = KEY_X;
183
  emerg.flag = ALTL_BIT;
184
  keyb_hook(emerg,fine);
185
 
186
  /* ---------------------------------------------------------------------
187
     Mutex creation
188
     --------------------------------------------------------------------- */
189
 
190
  PI_mutexattr_default(a2);
191
  SRP_mutexattr_default(a);
192
  mutex_init(&m1,&a);
193
  mutex_init(&m2,&a);
194
  mutex_init(&m3,&a);
195
 
196
 
197
  /* ---------------------------------------------------------------------
198
     Task creation
199
     --------------------------------------------------------------------- */
200
 
201
  hard_task_default_model(m);
202
  hard_task_def_mit(m, 1000000);
203
  hard_task_def_wcet(m, 80000);
204
  SRP_res_default_model(r, 3);
205
  p0 = task_createn("JH", Jh, (TASK_MODEL *)&m, &r, SRP_usemutex(&m3), SRP_usemutex(&m1), NULL);
206
  if (p0 == NIL)
207
  { cprintf("Can't create JH task...\n"); return 1; }
208
 
209
  hard_task_default_model(m);
210
  hard_task_def_mit(m, 2100000);
211
  hard_task_def_wcet(m, 80000);
212
  SRP_res_default_model(r, 2);
213
  p1 = task_createn("JM", Jm, (TASK_MODEL *)&m, &r, SRP_usemutex(&m3), SRP_usemutex(&m1),
214
                                      SRP_usemutex(&m2), NULL);
215
  if (p1 == NIL)
216
  { cprintf("Can't create JM task...\n"); return 1; }
217
 
218
  hard_task_default_model(m);
219
  hard_task_def_mit(m, 10000000);
220
  hard_task_def_wcet(m, 3000000);
221
  SRP_res_default_model(r, 1);
222
  p2 = task_createn("JL", Jl, (TASK_MODEL *)&m, &r, SRP_usemutex(&m3), SRP_usemutex(&m1),
223
                                      SRP_usemutex(&m2), NULL);
224
  if (p2 == NIL)
225
  { cprintf("Can't create JL task...\n"); return 1; }
226
 
227
  /* ---------------------------------------------------------------------
228
     Event post
229
     --------------------------------------------------------------------- */
230
 
231
  t.tv_sec = 0;
232
  t.tv_nsec = 500000000;
233
 
234
  kern_cli();
235
  kern_event_post(&t,startJ,(void *)p1);
236
 
237
  t.tv_sec = 1;
238
  kern_event_post(&t,startJ,(void *)p0);
239
  kern_sti();
240
 
241
  task_activate(p2);
242
 
243
  cprintf("END main\n");
244
 
245
  return 0;
246
}