Rev 1120 | Rev 1547 | 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: condtest.c,v 1.2 2004-04-17 11:36:13 giacomo Exp $ |
1120 | pj | 38 | |
39 | This test verify the correctness of the condition variables. |
||
40 | (... it doesn't test all...) |
||
41 | |||
42 | The test uses 1 mutex |
||
43 | |||
44 | the main task (NRT) creates three tasks. |
||
45 | |||
46 | J0, J1, J3 |
||
47 | starts, lock the mutex, and wait on a condition variable |
||
48 | |||
49 | J2 |
||
50 | at t = 0.5 lock the mutex and call cond_signal |
||
51 | at t = 1 lock the mutex and call cond_signal |
||
52 | |||
53 | */ |
||
54 | |||
55 | #include "kernel/kern.h" |
||
56 | |||
57 | mutex_t m0; |
||
58 | cond_t c0; |
||
59 | |||
60 | int number = 0; |
||
61 | |||
62 | PID p0,p1,p2,p3; |
||
63 | |||
64 | TASK j0() |
||
65 | { |
||
66 | cprintf("J0: before locking m0\n"); |
||
67 | mutex_lock(&m0); |
||
68 | cprintf("J0: locked m0, waiting on c0, number =%d\n", number); |
||
69 | while (!number) { |
||
70 | cond_wait(&c0,&m0); |
||
71 | cprintf("J0: number = %d, if >0 unlocking m0\n",number); |
||
72 | } |
||
73 | number--; |
||
74 | mutex_unlock(&m0); |
||
75 | cprintf("J0: unlocked m0, end task\n"); |
||
76 | return 0; |
||
77 | } |
||
78 | |||
79 | |||
80 | TASK j1() |
||
81 | { |
||
82 | cprintf("J1: before locking m0\n"); |
||
83 | mutex_lock(&m0); |
||
84 | cprintf("J1: locked m0, waiting on c0, number =%d\n", number); |
||
85 | while (!number) { |
||
86 | cond_wait(&c0,&m0); |
||
87 | cprintf("J1: number = %d, if >0 unlocking m0\n",number); |
||
88 | } |
||
89 | number--; |
||
90 | mutex_unlock(&m0); |
||
91 | cprintf("J1: unlocked m0, end task\n"); |
||
92 | return 0; |
||
93 | } |
||
94 | |||
95 | |||
96 | TASK j2() |
||
97 | { |
||
98 | |||
99 | cprintf("J2: started, waiting t=0.5 sec\n"); |
||
100 | while (sys_gettime(NULL) < 500000); |
||
101 | |||
102 | cprintf("J2: before locking m0\n"); |
||
103 | mutex_lock(&m0); |
||
104 | cprintf("J2: locked m0, number++ (was %d), cond_signal\n", number); |
||
105 | |||
106 | number++; |
||
107 | cond_signal(&c0); |
||
108 | |||
109 | cprintf("J2: unlocking m0\n"); |
||
110 | mutex_unlock(&m0); |
||
111 | |||
112 | cprintf("J2: waiting t=1 sec\n"); |
||
113 | while (sys_gettime(NULL) < 1000000); |
||
114 | |||
115 | cprintf("J2: Killing J3\n"); |
||
116 | task_kill(p3); |
||
117 | cprintf("J2: before locking m0\n"); |
||
118 | mutex_lock(&m0); |
||
119 | cprintf("J2: locked m0, number++ (was %d), cond_signal\n", number); |
||
120 | |||
121 | number++; |
||
122 | cond_signal(&c0); |
||
123 | |||
124 | cprintf("J2: unlocking m0\n"); |
||
125 | mutex_unlock(&m0); |
||
126 | cprintf("J2: unlocked m0, end task\n"); |
||
127 | return 0; |
||
128 | } |
||
129 | |||
130 | void cleanup_lock(void *arg) |
||
131 | { |
||
132 | cprintf("J3: KILL!!!\n"); |
||
133 | mutex_unlock(&m0); |
||
134 | cprintf("J3: unlocked m0 by the cleanup function\n"); |
||
135 | } |
||
136 | |||
137 | TASK j3() |
||
138 | { |
||
139 | cprintf("J3: before locking m0\n"); |
||
140 | mutex_lock(&m0); |
||
141 | cprintf("J3: locked m0, waiting on c0, number =%d\n", number); |
||
142 | task_cleanup_push(cleanup_lock, (void *)&m0); |
||
143 | while (!number) { |
||
144 | cond_wait(&c0,&m0); |
||
145 | cprintf("J3: number = %d, if >0 unlocking m0\n",number); |
||
146 | } |
||
147 | task_cleanup_pop(0); |
||
148 | // I hope this task never reach this point... it is killed by J2!!! |
||
149 | number--; |
||
150 | mutex_unlock(&m0); |
||
151 | cprintf("J3: unlocked m0, end task\n"); |
||
152 | return 0; |
||
153 | } |
||
154 | |||
155 | |||
156 | int main(int argc, char **argv) |
||
157 | { |
||
1377 | giacomo | 158 | struct timespec t; |
1120 | pj | 159 | |
160 | NRT_TASK_MODEL m; |
||
161 | |||
162 | PI_mutexattr_t a; |
||
163 | |||
164 | /* --------------------------------------------------------------------- |
||
165 | Task creation |
||
166 | --------------------------------------------------------------------- */ |
||
167 | |||
168 | nrt_task_default_model(m); |
||
169 | nrt_task_def_group(m,1); |
||
170 | p0 = task_create("J0", j0, &m, NULL); |
||
171 | if (p0 == NIL) |
||
172 | { cprintf("Can't create J0 task...\n"); return 1; } |
||
173 | |||
174 | p1 = task_create("J1", j1, &m, NULL); |
||
175 | if (p1 == NIL) |
||
176 | { cprintf("Can't create J1 task...\n"); return 1; } |
||
177 | |||
178 | p2 = task_create("J2", j2, &m, NULL); |
||
179 | if (p2 == NIL) |
||
180 | { cprintf("Can't create J2 task...\n"); return 1; } |
||
181 | |||
182 | p3 = task_create("J3", j3, &m, NULL); |
||
183 | if (p3 == NIL) |
||
184 | { cprintf("Can't create J3 task...\n"); return 1; } |
||
185 | |||
186 | /* --------------------------------------------------------------------- |
||
187 | Mutex creation |
||
188 | --------------------------------------------------------------------- */ |
||
189 | |||
190 | PI_mutexattr_default(a); |
||
191 | mutex_init(&m0,&a); |
||
192 | |||
193 | cond_init(&c0); |
||
194 | |||
195 | /* --------------------------------------------------------------------- |
||
196 | Event post |
||
197 | --------------------------------------------------------------------- */ |
||
198 | |||
199 | group_activate(1); |
||
200 | |||
1377 | giacomo | 201 | do { |
202 | sys_gettime(&t); |
||
203 | } while (t.tv_sec < 10); |
||
204 | |||
205 | sys_end(); |
||
1120 | pj | 206 | |
207 | return 0; |
||
1377 | giacomo | 208 | |
1120 | pj | 209 | } |