Subversion Repositories shark

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1085 pj 1
/*
2
 * Project: HARTIK (HA-rd R-eal TI-me K-ernel)
3
 *
4
 * Coordinators: Giorgio Buttazzo <giorgio@sssup.it>
5
 *               Gerardo Lamastra <gerardo@sssup.it>
6
 *
7
 * Authors     : Paolo Gai <pj@hartik.sssup.it>
8
 * (see authors.txt for full list of hartik's authors)
9
 *
10
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
11
 *
12
 * http://www.sssup.it
13
 * http://retis.sssup.it
14
 * http://hartik.sssup.it
15
 */
16
 
17
/**
18
 ------------
19
 CVS :        $Id: testj.c,v 1.1.1.1 2002-09-02 09:37:48 pj Exp $
20
 
21
 File:        $File$
22
 Revision:    $Revision: 1.1.1.1 $
23
 Last update: $Date: 2002-09-02 09:37:48 $
24
 ------------
25
 
26
 Test Number 19 (J):
27
 
28
 This test verify the correctness of the SRP module.
29
 
30
 There are 3 taks, Jh, Jm, Jl that uses 3 mutexes m1, m2, m3
31
 
32
 the main task (NRT) creates the three tasks.
33
 
34
 Jh with preemption level 3
35
   starts at t=1.5 sec and lock m3, lock m1, unlock m1, unlock m3
36
 
37
 Jm with preemption level 2
38
   starts at t=0.5 sec and lock m3, lock m2, unlock m2, unlock m3
39
   then lock and unlock m1
40
 
41
 Jl with preemption level 1
42
   it starts and locks m2
43
   at t=1 sec it locks m1
44
   at t=1.5 sec it unlocks m1
45
   then it unlocks m2, and finally it locks and unlocks m3
46
 
47
 
48
 The example is similar to the scheduling diagram shown at p. 210 of the
49
 book "Sistemi in tempo Reale", by Giorgio Buttazzo, Pitagora Editrice
50
 
51
**/
52
 
53
/*
54
 * Copyright (C) 2000 Paolo Gai
55
 *
56
 * This program is free software; you can redistribute it and/or modify
57
 * it under the terms of the GNU General Public License as published by
58
 * the Free Software Foundation; either version 2 of the License, or
59
 * (at your option) any later version.
60
 *
61
 * This program is distributed in the hope that it will be useful,
62
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
63
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
64
 * GNU General Public License for more details.
65
 *
66
 * You should have received a copy of the GNU General Public License
67
 * along with this program; if not, write to the Free Software
68
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
69
 *
70
 */
71
 
72
#include "kernel/kern.h"
73
#include "drivers/keyb.h"
74
 
75
#include <modules//srp.h>
76
 
77
mutex_t  m1,m2,m3;
78
 
79
 
80
TIME gt(void)
81
{
82
  TIME t;
83
  kern_cli();
84
  t = ll_gettime(TIME_EXACT,NULL);
85
  kern_sti();
86
  return t;
87
}
88
 
89
void startJ(void *a)
90
{
91
//  task_activate((PID)a);
92
  PID p = (PID) a;
93
  LEVEL l;
94
 
95
  kern_printf("startJ: %d\n",a);
96
 
97
  l = proc_table[p].task_level;
98
  level_table[l]->task_activate(l,p);
99
  event_need_reschedule();
100
}
101
 
102
TASK Jlobby()
103
{
104
  kern_printf("(*)");
105
  return 0;
106
}
107
 
108
TASK Jh()
109
{
110
  PID l;
111
  HARD_TASK_MODEL m;
112
  SRP_RES_MODEL r;
113
 
114
  kern_printf("JH: creating Jy before locking   m3\n");
115
 
116
  hard_task_default_model(m);
117
  hard_task_def_mit(m,30000);
118
  hard_task_def_wcet(m,1000);
119
  SRP_res_default_model(r,4);
120
  l = task_create("Jlobby",Jlobby,&m,&r);
121
  task_activate(l);
122
 
123
  mutex_lock(&m3);
124
  kern_printf("JH: locked           m3, locking m1\n");
125
  mutex_lock(&m1);
126
  kern_printf("JH: locked           m1, unlocking m1\n");
127
  mutex_unlock(&m1);
128
  kern_printf("JH: unlocked         m1, unlocking m3\n");
129
  mutex_unlock(&m3);
130
  kern_printf("JH: unlocked         m3, end task\n");
131
  return 0;
132
}
133
 
134
 
135
TASK Jm()
136
{
137
  kern_printf("JM: before locking   m3\n");
138
  mutex_lock(&m3);
139
  kern_printf("JM: locked           m3, locking m2\n");
140
  mutex_lock(&m2);
141
  kern_printf("JM: locked           m2, unlocking m2\n");
142
  mutex_unlock(&m2);
143
  kern_printf("JM: unlocked         m2, unlocking m3\n");
144
  mutex_unlock(&m3);
145
  kern_printf("JM: unlocked         m3, locking m1\n");
146
  mutex_lock(&m1);
147
  kern_printf("JM: locked           m1, unlocking m1\n");
148
  mutex_unlock(&m1);
149
  kern_printf("JM: unlocked         m1, end task\n");
150
  return 0;
151
}
152
 
153
 
154
TASK Jl()
155
{
156
//  struct timespec t;
157
  kern_printf("JL: before locking   m2\n");
158
  mutex_lock(&m2);
159
  kern_printf("JL: locked           m2, waiting to t=1 sec\n");
160
 
161
  while (gt() < 1000000);
162
 
163
  kern_printf("JL: t = 1 sec reached, locking m1\n");
164
  mutex_lock(&m1);
165
  kern_printf("JL: locked           m1, waiting to t=2 sec\n");
166
 
167
  while (gt() < 2000000);
168
 
169
  kern_printf("JL: t = 2 sec reached, unlocking m1\n");
170
  mutex_unlock(&m1);
171
  kern_printf("JL: unlocked         m1, unlocking m2\n");
172
 
173
  mutex_unlock(&m2);
174
 
175
  kern_printf("JL: unlocked         m2, locking m3\n");
176
  mutex_lock(&m3);
177
  kern_printf("JL: locked           m3, unlocking m3\n");
178
  mutex_unlock(&m3);
179
  kern_printf("JL: unlocked         m3, end task\n");
180
  return 0;
181
}
182
 
183
void fine(KEY_EVT *e)
184
{
185
  sys_end();
186
}
187
 
188
 
189
int main(int argc, char **argv)
190
{
191
  struct timespec t;
192
 
193
  HARD_TASK_MODEL m;
194
  PID      p0,p1,p2;
195
 
196
  SRP_mutexattr_t a;
197
  SRP_RES_MODEL r;
198
 
199
  PI_mutexattr_t a2;
200
 
201
  KEY_EVT emerg;
202
 
203
  //keyb_set_map(itaMap);
204
  emerg.ascii = 'x';
205
  emerg.scan = KEY_X;
206
  emerg.flag = ALTL_BIT;
207
  keyb_hook(emerg,fine);
208
 
209
  /* ---------------------------------------------------------------------
210
     Mutex creation
211
     --------------------------------------------------------------------- */
212
 
213
  PI_mutexattr_default(a2);
214
  SRP_mutexattr_default(a);
215
  mutex_init(&m1,&a);
216
  mutex_init(&m2,&a);
217
  mutex_init(&m3,&a);
218
 
219
 
220
  /* ---------------------------------------------------------------------
221
     Task creation
222
     --------------------------------------------------------------------- */
223
 
224
  hard_task_default_model(m);
225
  hard_task_def_mit(m, 1000000);
226
  hard_task_def_wcet(m, 80000);
227
  SRP_res_default_model(r, 3);
228
  p0 = task_createn("JH", Jh, (TASK_MODEL *)&m, &r, SRP_usemutex(&m3), SRP_usemutex(&m1), NULL);
229
  if (p0 == NIL)
230
  { kern_printf("Can't create JH task...\n"); return 1; }
231
 
232
  hard_task_default_model(m);
233
  hard_task_def_mit(m, 2100000);
234
  hard_task_def_wcet(m, 80000);
235
  SRP_res_default_model(r, 2);
236
  p1 = task_createn("JM", Jm, (TASK_MODEL *)&m, &r, SRP_usemutex(&m3), SRP_usemutex(&m1),
237
                                      SRP_usemutex(&m2), NULL);
238
  if (p1 == NIL)
239
  { kern_printf("Can't create JM task...\n"); return 1; }
240
 
241
  hard_task_default_model(m);
242
  hard_task_def_mit(m, 10000000);
243
  hard_task_def_wcet(m, 3000000);
244
  SRP_res_default_model(r, 1);
245
  p2 = task_createn("JL", Jl, (TASK_MODEL *)&m, &r, SRP_usemutex(&m3), SRP_usemutex(&m1),
246
                                      SRP_usemutex(&m2), NULL);
247
  if (p2 == NIL)
248
  { kern_printf("Can't create JL task...\n"); return 1; }
249
 
250
//  sys_abort(1);
251
  /* ---------------------------------------------------------------------
252
     Event post
253
     --------------------------------------------------------------------- */
254
 
255
  t.tv_sec = 0;
256
  t.tv_nsec = 500000000;
257
//  t.tv_nsec = 30000000;
258
  kern_event_post(&t,startJ,(void *)p1);
259
 
260
  t.tv_sec = 1;
261
  kern_event_post(&t,startJ,(void *)p0);
262
 
263
  task_activate(p2);
264
 
265
  kern_printf("END main\n");
266
 
267
  return 0;
268
}