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
 *
1547 pj 37
 * CVS :        $Id: pcdemo.c,v 1.4 2005-01-08 14:31:38 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
 
67
mutex_t  m0,m1,m2;
68
 
69
void startJ(void *a)
70
{
71
  task_activate((PID)a);
72
}
73
 
1690 fabio 74
TASK J0()
1120 pj 75
{
76
  cprintf("J0: before locking   m0\n");
77
  mutex_lock(&m0);
78
  cprintf("J0: locked           m0\n");
79
  mutex_unlock(&m0);
80
  cprintf("J0: unlocked         m0, locking m1\n");
81
 
82
  mutex_lock(&m1);
83
  cprintf("J0: locked           m1\n");
84
  mutex_unlock(&m1);
85
  cprintf("J0: unlocked         m1, end task\n");
86
  return 0;
87
}
88
 
89
 
1690 fabio 90
TASK J1()
1120 pj 91
{
92
  cprintf("J1: before locking   m2\n");
93
  mutex_lock(&m2);
94
  cprintf("J1: locked           m2\n");
95
  mutex_unlock(&m2);
96
  cprintf("J1: unlocked         m2, end task\n");
97
  return 0;
98
}
99
 
100
 
101
TASK j2()
102
{
103
  cprintf("J2: before locking   m2\n");
104
  mutex_lock(&m2);
105
  cprintf("J2: locked           m2, waiting to t=1 sec\n");
106
 
107
  while (sys_gettime(NULL) < 1000000);
108
 
109
  cprintf("J2: t = 1 sec reached\n");
110
  mutex_lock(&m1);
111
  cprintf("J2: locked           m1, waiting to t=2 sec\n");
112
 
113
  while (sys_gettime(NULL) < 2000000);
114
 
115
  cprintf("J2: t = 2 sec reached\n");
116
  mutex_unlock(&m1);
117
  cprintf("J2: unlocked         m1\n");
118
 
119
  mutex_unlock(&m2);
120
  cprintf("J2: unlocked         m2, end task\n");
121
  return 0;
122
}
123
 
124
int main(int argc, char **argv)
125
{
126
  struct timespec t;
127
 
128
  HARD_TASK_MODEL m;
129
  PID      p0,p1,p2;
130
 
131
  PC_mutexattr_t a;
132
  PI_mutexattr_t a2;
133
  PC_RES_MODEL r;
134
 
1123 pj 135
  clear();
136
 
1377 giacomo 137
  cprintf("Priority Ceiling demo. It ends after 60 sec\n");
1120 pj 138
 
139
  /* ---------------------------------------------------------------------
140
     Task creation
141
     --------------------------------------------------------------------- */
142
 
143
  hard_task_default_model(m);
144
  hard_task_def_mit(m, 1000000);
145
  hard_task_def_wcet(m, 20000);
146
  PC_res_default_model(r,0);
1690 fabio 147
  p0 = task_create("J0", J0, &m, &r);
1120 pj 148
  if (p0 == NIL)
149
  { cprintf("Can't create J0 task...\n"); return 1; }
150
 
151
  hard_task_default_model(m);
152
  hard_task_def_mit(m, 2100000);
153
  hard_task_def_wcet(m, 20000);
154
  PC_res_default_model(r,1);
1690 fabio 155
  p1 = task_create("J1", J1, &m, &r);
1120 pj 156
  if (p1 == NIL)
157
  { cprintf("Can't create J1 task...\n"); return 1; }
158
 
159
  hard_task_default_model(m);
160
  hard_task_def_mit(m, 10000000);
161
  hard_task_def_wcet(m, 3000000);
162
  PC_res_default_model(r,2);
163
  p2 = task_create("J2", j2, &m, &r);
164
  if (p2 == NIL)
165
  { cprintf("Can't create J2 task...\n"); return 1; }
166
 
167
  /* ---------------------------------------------------------------------
168
     Mutex creation
169
     --------------------------------------------------------------------- */
170
 
171
  PI_mutexattr_default(a2);
172
  PC_mutexattr_default(a,0);
173
  mutex_init(&m0,(mutexattr_t *)&a);
174
  mutex_init(&m1,(mutexattr_t *)&a);
175
 
176
  PC_mutexattr_default(a,1);
177
  mutex_init(&m2,(mutexattr_t *)&a);
178
 
179
  /* ---------------------------------------------------------------------
180
     Event post
181
     --------------------------------------------------------------------- */
182
 
183
  t.tv_sec = 0;
184
  t.tv_nsec = 500000000;
185
 
186
  kern_cli();
187
  kern_event_post(&t,startJ,(void *)p1);
188
 
189
  t.tv_sec = 1;
190
  kern_event_post(&t,startJ,(void *)p0);
191
  kern_sti();
192
 
193
  task_activate(p2);
194
 
1377 giacomo 195
  do {
196
    sys_gettime(&t);
197
  } while (t.tv_sec < 10);
198
 
1547 pj 199
  exit(0);
1120 pj 200
 
201
  return 0;
1377 giacomo 202
 
1120 pj 203
}