Subversion Repositories shark

Rev

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: testr.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 27 (R):
27
 
28
 This test verify the correctness of the condition variables.
29
 (... it doesn't test all...)
30
 
31
 The test uses 1 mutex
32
 
33
 the main task (NRT) creates three tasks.
34
 
35
 J0, J1, J3
36
   starts, lock the mutex, and wait on a condition variable
37
 
38
 J2
39
   at t = 0.5 lock the mutex and call cond_signal
40
   at t = 1   lock the mutex and call cond_signal
41
 
42
 
43
**/
44
 
45
/*
46
 * Copyright (C) 2000 Paolo Gai
47
 *
48
 * This program is free software; you can redistribute it and/or modify
49
 * it under the terms of the GNU General Public License as published by
50
 * the Free Software Foundation; either version 2 of the License, or
51
 * (at your option) any later version.
52
 *
53
 * This program is distributed in the hope that it will be useful,
54
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
55
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
56
 * GNU General Public License for more details.
57
 *
58
 * You should have received a copy of the GNU General Public License
59
 * along with this program; if not, write to the Free Software
60
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
61
 *
62
 */
63
 
64
#include "kernel/kern.h"
65
#include "drivers/keyb.h"
66
 
67
 
68
mutex_t  m0;
69
cond_t   c0;
70
 
71
int number = 0;
72
 
73
PID p0,p1,p2,p3;
74
 
75
TASK j0()
76
{
77
  kern_printf("J0: before locking   m0\n");
78
  mutex_lock(&m0);
79
  kern_printf("J0: locked           m0, waiting on c0, number =%d\n", number);
80
  while (!number) {
81
    cond_wait(&c0,&m0);
82
    kern_printf("J0: number = %d, if >0 unlocking m0\n",number);
83
  }
84
  number--;
85
  mutex_unlock(&m0);
86
  kern_printf("J0: unlocked         m0, end task\n");
87
  return 0;
88
}
89
 
90
 
91
TASK j1()
92
{
93
  kern_printf("J1: before locking   m0\n");
94
  mutex_lock(&m0);
95
  kern_printf("J1: locked           m0, waiting on c0, number =%d\n", number);
96
  while (!number) {
97
    cond_wait(&c0,&m0);
98
    kern_printf("J1: number = %d, if >0 unlocking m0\n",number);
99
  }
100
  number--;
101
  mutex_unlock(&m0);
102
  kern_printf("J1: unlocked         m0, end task\n");
103
  return 0;
104
}
105
 
106
 
107
TASK j2()
108
{
109
//  struct timespec t;
110
 
111
  kern_printf("J2: started, waiting t=0.5 sec\n");
112
  while (sys_gettime(NULL) < 500000);
113
 
114
  kern_printf("J2: before locking   m0\n");
115
  mutex_lock(&m0);
116
  kern_printf("J2: locked           m0, number++ (was %d), cond_signal\n", number);
117
 
118
  number++;
119
  cond_signal(&c0);
120
//  cond_broadcast(&c0);
121
 
122
  kern_printf("J2: unlocking m0\n");
123
  mutex_unlock(&m0);
124
 
125
  kern_printf("J2: waiting t=1 sec\n");
126
  while (sys_gettime(NULL) < 1000000);
127
 
128
  kern_printf("J2: Killing J3\n");
129
  task_kill(p3);
130
  kern_printf("J2: before locking   m0\n");
131
  mutex_lock(&m0);
132
  kern_printf("J2: locked           m0, number++ (was %d), cond_signal\n", number);
133
 
134
  number++;
135
  cond_signal(&c0);
136
//  cond_broadcast(&c0);
137
 
138
  kern_printf("J2: unlocking m0\n");
139
  mutex_unlock(&m0);
140
  kern_printf("J2: unlocked         m0, end task\n");
141
  return 0;
142
}
143
 
144
void cleanup_lock(void *arg)
145
{
146
  kern_printf("J3: KILL!!!\n");
147
  mutex_unlock(&m0);
148
  kern_printf("J3: unlocked m0 by the cleanup function\n");
149
}
150
 
151
TASK j3()
152
{
153
  kern_printf("J3: before locking   m0\n");
154
  mutex_lock(&m0);
155
  kern_printf("J3: locked           m0, waiting on c0, number =%d\n", number);
156
  task_cleanup_push(cleanup_lock, (void *)&m0);
157
  while (!number) {
158
    cond_wait(&c0,&m0);
159
    kern_printf("J3: number = %d, if >0 unlocking m0\n",number);
160
  }
161
  task_cleanup_pop(0);
162
  // I hope this task never reach this point... it is killed by J2!!!
163
  number--;
164
  mutex_unlock(&m0);
165
  kern_printf("J3: unlocked         m0, end task\n");
166
  return 0;
167
}
168
 
169
void fine(KEY_EVT *e)
170
{
171
  sys_end();
172
}
173
 
174
 
175
int main(int argc, char **argv)
176
{
177
//  struct timespec t;
178
 
179
  NRT_TASK_MODEL m;
180
 
181
  PI_mutexattr_t a;
182
 
183
  KEY_EVT emerg;
184
 
185
  //keyb_set_map(itaMap);
186
  emerg.ascii = 'x';
187
  emerg.scan = KEY_X;
188
  emerg.flag = ALTL_BIT;
189
  keyb_hook(emerg,fine);
190
 
191
  /* ---------------------------------------------------------------------
192
     Task creation
193
     --------------------------------------------------------------------- */
194
 
195
  nrt_task_default_model(m);
196
  nrt_task_def_group(m,1);
197
  p0 = task_create("J0", j0, &m, NULL);
198
  if (p0 == NIL)
199
  { kern_printf("Can't create J0 task...\n"); return 1; }
200
 
201
  p1 = task_create("J1", j1, &m, NULL);
202
  if (p1 == NIL)
203
  { kern_printf("Can't create J1 task...\n"); return 1; }
204
 
205
  p2 = task_create("J2", j2, &m, NULL);
206
  if (p2 == NIL)
207
  { kern_printf("Can't create J2 task...\n"); return 1; }
208
 
209
  p3 = task_create("J3", j3, &m, NULL);
210
  if (p3 == NIL)
211
  { kern_printf("Can't create J3 task...\n"); return 1; }
212
 
213
  /* ---------------------------------------------------------------------
214
     Mutex creation
215
     --------------------------------------------------------------------- */
216
 
217
  PI_mutexattr_default(a);
218
  mutex_init(&m0,&a);
219
 
220
  cond_init(&c0);
221
 
222
  /* ---------------------------------------------------------------------
223
     Event post
224
     --------------------------------------------------------------------- */
225
 
226
  group_activate(1);
227
 
228
  kern_printf("END main\n");
229
 
230
  return 0;
231
}