Subversion Repositories shark

Rev

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