Subversion Repositories shark

Rev

Rev 1120 | 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 Giorgio Buttazzo, 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
 *
1377 giacomo 37
 * CVS :        $Id: jointest.c,v 1.2 2004-04-17 11:36:14 giacomo Exp $
1120 pj 38
 
39
 This test verify the correctness of the task_join primitive. (that
40
 function is the same as pthread_join... someday I will change the
41
 names...)
42
 
43
 There are 4 taks, J1, J2, J3, are created as joinable, J4 as detached
44
 
45
 The main task:
46
   Creates J1 and J2, locks m1 (a PI mitex), creates C3.
47
   at t=0.8 sec it calls a task_join on J3 (that returns EDEADLK),
48
   it unlocks m1, then it makes task_join on J3 another time.
49
   Next it creates J4 as detached and finally it does a task_join on J4
50
   (that returns EINVAL).
51
 
52
 J1:
53
   at t=0.2 sec it calls task_join on J2, the it ends.
54
 
55
 J2:
56
   it simply waits t=0.4 sec and it ends.
57
 
58
 J3:
59
   First, it calls task_join on J1.
60
   Then, at t=0.6 sec it locks m1, then unlocks it
61
 
62
 J4:
63
   it simply waits t=1 sec and it ends.
64
 
65
*/
66
 
67
#include "kernel/kern.h"
68
 
69
PID j0, j1, j2, j3, j4;
70
mutex_t  m1;
71
 
72
TASK J1()
73
{
74
  int err;
75
  void *ret;
76
 
77
  cprintf("J1: started, waiting 0.2 sec\n");
78
 
79
  while (sys_gettime(NULL) < 200000);
80
 
81
  cprintf("J1: 0.2 sec reached, joining J2\n");
82
 
83
  err = task_join(j2, &ret);
84
 
85
  cprintf("J1: join J2 returns %d error %d, exiting\n",
86
              (int)ret,err);
87
  return (void *)11;
88
}
89
 
90
TASK J2()
91
{
92
  cprintf("J2: started, waiting 0.4 sec\n");
93
 
94
  while (sys_gettime(NULL) < 400000);
95
 
96
  cprintf("J2: 0.4 sec reached, exiting\n");
97
 
98
  return (void *)22;
99
}
100
 
101
TASK J3()
102
{
103
  int err;
104
  void *ret;
105
 
106
  cprintf("J3: started, joining J1\n");
107
 
108
  err = task_join(j1, &ret);
109
 
110
  cprintf("J3: join J1 returns %d error %d, waiting 0.6sec\n", (int)ret, err);
111
 
112
  while (sys_gettime(NULL) < 600000);
113
 
114
  cprintf("J1: 0.6 sec reached, locking m1\n");
115
 
116
  mutex_lock(&m1);
117
 
118
  cprintf("J3: locked m1, unlocking m1\n");
119
 
120
  mutex_unlock(&m1);
121
 
122
  cprintf("J3: unlocked m1, exiting\n");
123
 
124
  return (void *)33;
125
}
126
 
127
TASK J4()
128
{
129
  cprintf("J4: started, waiting 1 sec\n");
130
 
131
  while (sys_gettime(NULL) < 1000000);
132
 
133
  cprintf("J4: 1 sec reached, exiting\n");
134
 
135
  return (void *)44;
136
}
137
 
138
int main(int argc, char **argv)
139
{
140
  NRT_TASK_MODEL m;
141
 
142
  PI_mutexattr_t a;
143
 
144
  int err;
145
  void *ret;
146
 
1377 giacomo 147
  struct timespec t;
1120 pj 148
 
149
  j0 = exec_shadow;
150
  nrt_task_default_model(m);
151
  nrt_task_def_joinable(m);
152
 
153
  /* ---------------------------------------------------------------------
154
     Mutex creation
155
     --------------------------------------------------------------------- */
156
 
157
  PI_mutexattr_default(a);
158
  mutex_init(&m1,&a);
159
 
160
 
161
  /* ---------------------------------------------------------------------
162
     Let's go !!!!
163
     --------------------------------------------------------------------- */
164
 
165
  cprintf("main: creating J1,J2,J3, locking m1\n");
166
 
167
  j1 = task_create("J1", J1, &m, NULL);
168
  if (j1 == NIL) { cprintf("Can't create J1 task...\n"); return 1; }
169
  task_activate(j1);
170
 
171
  j2 = task_create("J2", J2, &m, NULL);
172
  if (j2 == NIL) { cprintf("Can't create J2 task...\n"); return 1; }
173
  task_activate(j2);
174
 
175
  mutex_lock(&m1);
176
 
177
  j3 = task_create("J3", J3, &m, NULL);
178
  if (j3 == NIL) { cprintf("Can't create J3 task...\n"); return 1; }
179
  task_activate(j3);
180
 
181
  cprintf("main: waiting t=0.8 sec\n");
182
 
183
  while (sys_gettime(NULL) < 800000);
184
 
185
  err = task_join(j3, NULL);
186
 
187
  cprintf("main: join J3 error %d, unlocking m1\n",err);
188
 
189
  mutex_unlock(&m1);
190
 
191
  err = task_join(j3, &ret);
192
 
193
  cprintf("main: join J3 returns %d error %d, unlocked m1, creating J4\n",
194
            (int)ret,err);
195
 
196
  nrt_task_def_unjoinable(m);
197
  j4 = task_create("J4", J4, &m, NULL);
198
  if (j4 == NIL) { cprintf("Can't create J4 task...\n"); return 1; }
199
 
200
  task_activate(j4);
201
 
202
  err = task_join(j4,&ret);
203
 
204
  cprintf("main: join J4 returns %d error %d, exiting\n", (int)ret, err);
205
 
1377 giacomo 206
  do {
207
    sys_gettime(&t);
208
  } while (t.tv_sec < 10);
209
 
210
  sys_end();
211
 
1120 pj 212
  return 0;
1377 giacomo 213
 
1120 pj 214
}