Rev 1377 | 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 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 | * |
||
37 | * CVS : $Id: jointest.c,v 1.1 2002-11-11 08:22:45 pj Exp $ |
||
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 | #include "drivers/keyb.h" |
||
69 | |||
70 | |||
71 | PID j0, j1, j2, j3, j4; |
||
72 | mutex_t m1; |
||
73 | |||
74 | void fine(KEY_EVT *e) |
||
75 | { |
||
76 | sys_end(); |
||
77 | } |
||
78 | |||
79 | TASK J1() |
||
80 | { |
||
81 | int err; |
||
82 | void *ret; |
||
83 | |||
84 | cprintf("J1: started, waiting 0.2 sec\n"); |
||
85 | |||
86 | while (sys_gettime(NULL) < 200000); |
||
87 | |||
88 | cprintf("J1: 0.2 sec reached, joining J2\n"); |
||
89 | |||
90 | err = task_join(j2, &ret); |
||
91 | |||
92 | cprintf("J1: join J2 returns %d error %d, exiting\n", |
||
93 | (int)ret,err); |
||
94 | return (void *)11; |
||
95 | } |
||
96 | |||
97 | TASK J2() |
||
98 | { |
||
99 | cprintf("J2: started, waiting 0.4 sec\n"); |
||
100 | |||
101 | while (sys_gettime(NULL) < 400000); |
||
102 | |||
103 | cprintf("J2: 0.4 sec reached, exiting\n"); |
||
104 | |||
105 | return (void *)22; |
||
106 | } |
||
107 | |||
108 | TASK J3() |
||
109 | { |
||
110 | int err; |
||
111 | void *ret; |
||
112 | |||
113 | cprintf("J3: started, joining J1\n"); |
||
114 | |||
115 | err = task_join(j1, &ret); |
||
116 | |||
117 | cprintf("J3: join J1 returns %d error %d, waiting 0.6sec\n", (int)ret, err); |
||
118 | |||
119 | while (sys_gettime(NULL) < 600000); |
||
120 | |||
121 | cprintf("J1: 0.6 sec reached, locking m1\n"); |
||
122 | |||
123 | mutex_lock(&m1); |
||
124 | |||
125 | cprintf("J3: locked m1, unlocking m1\n"); |
||
126 | |||
127 | mutex_unlock(&m1); |
||
128 | |||
129 | cprintf("J3: unlocked m1, exiting\n"); |
||
130 | |||
131 | return (void *)33; |
||
132 | } |
||
133 | |||
134 | TASK J4() |
||
135 | { |
||
136 | cprintf("J4: started, waiting 1 sec\n"); |
||
137 | |||
138 | while (sys_gettime(NULL) < 1000000); |
||
139 | |||
140 | cprintf("J4: 1 sec reached, exiting\n"); |
||
141 | |||
142 | return (void *)44; |
||
143 | } |
||
144 | |||
145 | int main(int argc, char **argv) |
||
146 | { |
||
147 | NRT_TASK_MODEL m; |
||
148 | |||
149 | PI_mutexattr_t a; |
||
150 | |||
151 | KEY_EVT emerg; |
||
152 | |||
153 | int err; |
||
154 | void *ret; |
||
155 | |||
156 | //keyb_set_map(itaMap); |
||
157 | emerg.ascii = 'x'; |
||
158 | emerg.scan = KEY_X; |
||
159 | emerg.flag = ALTL_BIT; |
||
160 | keyb_hook(emerg,fine); |
||
161 | |||
162 | j0 = exec_shadow; |
||
163 | nrt_task_default_model(m); |
||
164 | nrt_task_def_joinable(m); |
||
165 | |||
166 | /* --------------------------------------------------------------------- |
||
167 | Mutex creation |
||
168 | --------------------------------------------------------------------- */ |
||
169 | |||
170 | PI_mutexattr_default(a); |
||
171 | mutex_init(&m1,&a); |
||
172 | |||
173 | |||
174 | /* --------------------------------------------------------------------- |
||
175 | Let's go !!!! |
||
176 | --------------------------------------------------------------------- */ |
||
177 | |||
178 | cprintf("main: creating J1,J2,J3, locking m1\n"); |
||
179 | |||
180 | j1 = task_create("J1", J1, &m, NULL); |
||
181 | if (j1 == NIL) { cprintf("Can't create J1 task...\n"); return 1; } |
||
182 | task_activate(j1); |
||
183 | |||
184 | j2 = task_create("J2", J2, &m, NULL); |
||
185 | if (j2 == NIL) { cprintf("Can't create J2 task...\n"); return 1; } |
||
186 | task_activate(j2); |
||
187 | |||
188 | mutex_lock(&m1); |
||
189 | |||
190 | j3 = task_create("J3", J3, &m, NULL); |
||
191 | if (j3 == NIL) { cprintf("Can't create J3 task...\n"); return 1; } |
||
192 | task_activate(j3); |
||
193 | |||
194 | cprintf("main: waiting t=0.8 sec\n"); |
||
195 | |||
196 | while (sys_gettime(NULL) < 800000); |
||
197 | |||
198 | err = task_join(j3, NULL); |
||
199 | |||
200 | cprintf("main: join J3 error %d, unlocking m1\n",err); |
||
201 | |||
202 | mutex_unlock(&m1); |
||
203 | |||
204 | err = task_join(j3, &ret); |
||
205 | |||
206 | cprintf("main: join J3 returns %d error %d, unlocked m1, creating J4\n", |
||
207 | (int)ret,err); |
||
208 | |||
209 | nrt_task_def_unjoinable(m); |
||
210 | j4 = task_create("J4", J4, &m, NULL); |
||
211 | if (j4 == NIL) { cprintf("Can't create J4 task...\n"); return 1; } |
||
212 | |||
213 | task_activate(j4); |
||
214 | |||
215 | err = task_join(j4,&ret); |
||
216 | |||
217 | cprintf("main: join J4 returns %d error %d, exiting\n", (int)ret, err); |
||
218 | |||
219 | return 0; |
||
220 | } |