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: pcdemo.c,v 1.2 2003-01-07 17:10:15 pj Exp $
1120 pj 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
 
1123 pj 145
  clear();
146
 
1120 pj 147
  cprintf("Priority Ceiling demo. Press Alt-X to exit the demo\n");
148
 
149
  //keyb_set_map(itaMap);
150
  emerg.ascii = 'x';
151
  emerg.scan = KEY_X;
152
  emerg.flag = ALTL_BIT;
153
  keyb_hook(emerg,fine);
154
 
155
  /* ---------------------------------------------------------------------
156
     Task creation
157
     --------------------------------------------------------------------- */
158
 
159
  hard_task_default_model(m);
160
  hard_task_def_mit(m, 1000000);
161
  hard_task_def_wcet(m, 20000);
162
  PC_res_default_model(r,0);
163
  p0 = task_create("J0", j0, &m, &r);
164
  if (p0 == NIL)
165
  { cprintf("Can't create J0 task...\n"); return 1; }
166
 
167
  hard_task_default_model(m);
168
  hard_task_def_mit(m, 2100000);
169
  hard_task_def_wcet(m, 20000);
170
  PC_res_default_model(r,1);
171
  p1 = task_create("J1", j1, &m, &r);
172
  if (p1 == NIL)
173
  { cprintf("Can't create J1 task...\n"); return 1; }
174
 
175
  hard_task_default_model(m);
176
  hard_task_def_mit(m, 10000000);
177
  hard_task_def_wcet(m, 3000000);
178
  PC_res_default_model(r,2);
179
  p2 = task_create("J2", j2, &m, &r);
180
  if (p2 == NIL)
181
  { cprintf("Can't create J2 task...\n"); return 1; }
182
 
183
  /* ---------------------------------------------------------------------
184
     Mutex creation
185
     --------------------------------------------------------------------- */
186
 
187
  PI_mutexattr_default(a2);
188
  PC_mutexattr_default(a,0);
189
  mutex_init(&m0,(mutexattr_t *)&a);
190
  mutex_init(&m1,(mutexattr_t *)&a);
191
 
192
  PC_mutexattr_default(a,1);
193
  mutex_init(&m2,(mutexattr_t *)&a);
194
 
195
  /* ---------------------------------------------------------------------
196
     Event post
197
     --------------------------------------------------------------------- */
198
 
199
  t.tv_sec = 0;
200
  t.tv_nsec = 500000000;
201
 
202
  kern_cli();
203
  kern_event_post(&t,startJ,(void *)p1);
204
 
205
  t.tv_sec = 1;
206
  kern_event_post(&t,startJ,(void *)p0);
207
  kern_sti();
208
 
209
  task_activate(p2);
210
 
211
  cprintf("END main\n");
212
 
213
  return 0;
214
}