Subversion Repositories shark

Rev

Rev 1547 | 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
 *
1552 pj 37
 * CVS :        $Id: srpdemo.c,v 1.5 2005-02-25 11:10:46 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
 
1552 pj 66
#include "srp/srp/srp.h"
1120 pj 67
 
68
mutex_t  m1,m2,m3;
69
 
70
void startJ(void *a)
71
{
72
  task_activate((PID)a);
73
}
74
 
75
TASK Jlobby()
76
{
77
  cprintf("(*) JLobby!!!\n");
78
  return 0;
79
}
80
 
81
TASK Jh()
82
{
83
  PID l;
84
  HARD_TASK_MODEL m;
85
  SRP_RES_MODEL r;
86
 
87
  cprintf("JH: creating Jy before locking   m3\n");
88
 
89
  hard_task_default_model(m);
90
  hard_task_def_mit(m,30000);
91
  hard_task_def_wcet(m,1000);
92
  SRP_res_default_model(r,4);
93
  l = task_create("Jlobby",Jlobby,&m,&r);
94
  task_activate(l);
95
 
96
  mutex_lock(&m3);
97
  cprintf("JH: locked           m3, locking m1\n");
98
  mutex_lock(&m1);
99
  cprintf("JH: locked           m1, unlocking m1\n");
100
  mutex_unlock(&m1);
101
  cprintf("JH: unlocked         m1, unlocking m3\n");
102
  mutex_unlock(&m3);
103
  cprintf("JH: unlocked         m3, end task\n");
104
  return 0;
105
}
106
 
107
 
108
TASK Jm()
109
{
110
  cprintf("JM: before locking   m3\n");
111
  mutex_lock(&m3);
112
  cprintf("JM: locked           m3, locking m2\n");
113
  mutex_lock(&m2);
114
  cprintf("JM: locked           m2, unlocking m2\n");
115
  mutex_unlock(&m2);
116
  cprintf("JM: unlocked         m2, unlocking m3\n");
117
  mutex_unlock(&m3);
118
  cprintf("JM: unlocked         m3, locking m1\n");
119
  mutex_lock(&m1);
120
  cprintf("JM: locked           m1, unlocking m1\n");
121
  mutex_unlock(&m1);
122
  cprintf("JM: unlocked         m1, end task\n");
123
  return 0;
124
}
125
 
126
 
127
TASK Jl()
128
{
129
  cprintf("JL: before locking   m2\n");
130
  mutex_lock(&m2);
131
  cprintf("JL: locked           m2, waiting to t=1 sec\n");
132
 
133
  while (sys_gettime(NULL) < 1000000);
134
 
135
  cprintf("JL: t = 1 sec reached, locking m1\n");
136
  mutex_lock(&m1);
137
  cprintf("JL: locked           m1, waiting to t=2 sec\n");
138
 
139
  while (sys_gettime(NULL) < 2000000);
140
 
141
  cprintf("JL: t = 2 sec reached, unlocking m1\n");
142
  mutex_unlock(&m1);
143
  cprintf("JL: unlocked         m1, unlocking m2\n");
144
 
145
  mutex_unlock(&m2);
146
 
147
  cprintf("JL: unlocked         m2, locking m3\n");
148
  mutex_lock(&m3);
149
  cprintf("JL: locked           m3, unlocking m3\n");
150
  mutex_unlock(&m3);
151
  cprintf("JL: unlocked         m3, end task\n");
152
  return 0;
153
}
154
 
155
int main(int argc, char **argv)
156
{
157
  struct timespec t;
158
 
159
  HARD_TASK_MODEL m;
160
  PID      p0,p1,p2;
161
 
162
  SRP_mutexattr_t a;
163
  SRP_RES_MODEL r;
164
 
165
  PI_mutexattr_t a2;
166
 
1123 pj 167
  clear();
168
 
1377 giacomo 169
  cprintf("Stack resource Policy demo. It ends after 60 sec\n");
1120 pj 170
 
171
  /* ---------------------------------------------------------------------
172
     Mutex creation
173
     --------------------------------------------------------------------- */
174
 
175
  PI_mutexattr_default(a2);
176
  SRP_mutexattr_default(a);
177
  mutex_init(&m1,&a);
178
  mutex_init(&m2,&a);
179
  mutex_init(&m3,&a);
180
 
181
  /* ---------------------------------------------------------------------
182
     Task creation
183
     --------------------------------------------------------------------- */
184
 
185
  hard_task_default_model(m);
186
  hard_task_def_mit(m, 1000000);
187
  hard_task_def_wcet(m, 80000);
188
  SRP_res_default_model(r, 3);
189
  p0 = task_createn("JH", Jh, (TASK_MODEL *)&m, &r, SRP_usemutex(&m3), SRP_usemutex(&m1), NULL);
190
  if (p0 == NIL)
191
  { cprintf("Can't create JH task...\n"); return 1; }
192
 
193
  hard_task_default_model(m);
194
  hard_task_def_mit(m, 2100000);
195
  hard_task_def_wcet(m, 80000);
196
  SRP_res_default_model(r, 2);
197
  p1 = task_createn("JM", Jm, (TASK_MODEL *)&m, &r, SRP_usemutex(&m3), SRP_usemutex(&m1),
198
                                      SRP_usemutex(&m2), NULL);
199
  if (p1 == NIL)
200
  { cprintf("Can't create JM task...\n"); return 1; }
201
 
202
  hard_task_default_model(m);
203
  hard_task_def_mit(m, 10000000);
204
  hard_task_def_wcet(m, 3000000);
205
  SRP_res_default_model(r, 1);
206
  p2 = task_createn("JL", Jl, (TASK_MODEL *)&m, &r, SRP_usemutex(&m3), SRP_usemutex(&m1),
207
                                      SRP_usemutex(&m2), NULL);
208
  if (p2 == NIL)
209
  { cprintf("Can't create JL task...\n"); return 1; }
210
 
211
  /* ---------------------------------------------------------------------
212
     Event post
213
     --------------------------------------------------------------------- */
214
 
215
  t.tv_sec = 0;
216
  t.tv_nsec = 500000000;
217
 
218
  kern_cli();
219
  kern_event_post(&t,startJ,(void *)p1);
220
 
221
  t.tv_sec = 1;
222
  kern_event_post(&t,startJ,(void *)p0);
223
  kern_sti();
224
 
225
  task_activate(p2);
226
 
1377 giacomo 227
  do {
228
    sys_gettime(&t);
229
  } while (t.tv_sec < 60);
230
 
1547 pj 231
  exit(0);
1120 pj 232
 
233
  return 0;
234
}