Rev 29 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | 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 | ------------ |
||
38 | pj | 21 | CVS : $Id: join.c,v 1.3 2003-01-07 17:07:49 pj Exp $ |
2 | pj | 22 | |
23 | File: $File$ |
||
38 | pj | 24 | Revision: $Revision: 1.3 $ |
25 | Last update: $Date: 2003-01-07 17:07:49 $ |
||
2 | pj | 26 | ------------ |
27 | |||
28 | task join and related primitives |
||
29 | |||
30 | **/ |
||
31 | |||
32 | /* |
||
33 | * Copyright (C) 2000 Paolo Gai |
||
34 | * |
||
35 | * This program is free software; you can redistribute it and/or modify |
||
36 | * it under the terms of the GNU General Public License as published by |
||
37 | * the Free Software Foundation; either version 2 of the License, or |
||
38 | * (at your option) any later version. |
||
39 | * |
||
40 | * This program is distributed in the hope that it will be useful, |
||
41 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
42 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
43 | * GNU General Public License for more details. |
||
44 | * |
||
45 | * You should have received a copy of the GNU General Public License |
||
46 | * along with this program; if not, write to the Free Software |
||
47 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
48 | * |
||
49 | */ |
||
50 | |||
51 | #include <stdarg.h> |
||
52 | #include <ll/ll.h> |
||
53 | #include <ll/stdlib.h> |
||
54 | #include <ll/stdio.h> |
||
55 | #include <ll/string.h> |
||
56 | #include <kernel/config.h> |
||
57 | #include <kernel/model.h> |
||
58 | #include <kernel/const.h> |
||
59 | #include <sys/types.h> |
||
60 | #include <kernel/types.h> |
||
61 | #include <kernel/descr.h> |
||
62 | #include <errno.h> |
||
63 | #include <kernel/var.h> |
||
64 | #include <kernel/func.h> |
||
65 | |||
66 | static int taskjoin_once = 1; |
||
67 | |||
68 | |||
69 | /* this is the test that is done when a task is being killed |
||
70 | and it is waiting on a sigwait */ |
||
71 | static int taskjoin_cancellation_point(PID i, void *arg) |
||
72 | { |
||
73 | LEVEL l; |
||
74 | |||
75 | if (proc_table[i].status == WAIT_JOIN) { |
||
76 | /* the task that have to be killed is waiting on a task_join. |
||
77 | we reset the data structures set in task_join and then when the |
||
78 | task will exit from task_join it will fall into a task_testcancel */ |
||
79 | proc_table[ proc_table[i].shadow ].waiting_for_me = NIL; |
||
80 | proc_table[i].shadow = i; |
||
81 | |||
82 | l = proc_table[i].task_level; |
||
38 | pj | 83 | level_table[l]->public_unblock(l,i); |
2 | pj | 84 | |
85 | return 1; |
||
86 | } |
||
87 | |||
88 | return 0; |
||
89 | } |
||
90 | |||
91 | |||
92 | /*+ this function suspends execution of the calling task until the target |
||
93 | task terminates, unless the target task has already terminated. |
||
94 | It works like the pthread_join +*/ |
||
95 | int task_join(PID p, void **value) |
||
96 | { |
||
97 | PID x; /* used to follow the shadow chain */ |
||
98 | int blocked = 0; /* a flag */ |
||
99 | LEVEL l; |
||
100 | |||
101 | /* task_join is a cancellation point... if the task is suspended |
||
102 | the control on the status is done in task_kill */ |
||
103 | task_testcancel(); |
||
104 | |||
105 | /* some controls on the task p */ |
||
106 | if (p<0 || p>=MAX_PROC) return ESRCH; |
||
107 | if (proc_table[p].status == FREE && |
||
108 | !(proc_table[p].control & WAIT_FOR_JOIN)) return ESRCH; |
||
109 | if (!(proc_table[p].control & TASK_JOINABLE)) return EINVAL; |
||
110 | |||
111 | proc_table[exec_shadow].context = kern_context_save(); |
||
112 | |||
113 | /* first, if it is the first time that the task_join is called, |
||
114 | register the cancellation point */ |
||
115 | if (taskjoin_once) { |
||
116 | taskjoin_once = 0; |
||
117 | register_cancellation_point(taskjoin_cancellation_point, NULL); |
||
118 | } |
||
119 | |||
120 | if (proc_table[p].waiting_for_me != NIL) { |
||
121 | kern_context_load(proc_table[exec_shadow].context); |
||
122 | return EINVAL; |
||
123 | } |
||
124 | |||
125 | /* deadlock checks; we check the shadow chain to prevent cycles */ |
||
126 | x = p; |
||
127 | do { |
||
128 | x = proc_table[x].shadow; |
||
129 | if (x == exec_shadow) { |
||
130 | kern_context_load(proc_table[exec_shadow].context); |
||
131 | return EDEADLK; |
||
132 | } |
||
133 | } while (x != proc_table[x].shadow); |
||
134 | |||
135 | /* we prepare all the stuffs for joining the target task... */ |
||
136 | if (!(proc_table[p].control & WAIT_FOR_JOIN)) { |
||
137 | /* the target task is already running... so we block yhe current task |
||
138 | on it!!! |
||
139 | |||
140 | Note: It is not correct to set only the shadow and reschedule, as done |
||
141 | with the mutexes, because there is no inheritance with join... |
||
142 | Normally we have to call task_extract because blocking on join is |
||
143 | like blocking on a classic semaphore. |
||
144 | Althought, we set the shadow because: |
||
145 | - if the task call task_join when holding a mutex (AAARRRGGHHH!!!) |
||
146 | the system continue working... |
||
147 | - The deadlock detection strategy works on shadows... |
||
148 | Setting shadows means also that implementation of mutexes that |
||
149 | manage shadows in a strange way WILL NOT WORK with task_join |
||
150 | (for example, the srp.c module doesn't work with task_join) */ |
||
151 | proc_table[p].waiting_for_me = exec_shadow; |
||
152 | proc_table[exec_shadow].shadow = p; |
||
153 | |||
38 | pj | 154 | kern_epilogue_macro(); |
2 | pj | 155 | |
156 | /* now, we block the current task, waiting the end of the target task */ |
||
157 | l = proc_table[exec_shadow].task_level; |
||
38 | pj | 158 | level_table[l]->public_block(l,exec_shadow); |
2 | pj | 159 | proc_table[exec_shadow].status = WAIT_JOIN; |
160 | |||
161 | exec = exec_shadow = -1; |
||
162 | scheduler(); |
||
163 | /* note that we don't use kern_context_load because when rescheduled |
||
164 | we remain in kernel mode... */ |
||
165 | ll_context_to(proc_table[exec_shadow].context); |
||
166 | |||
167 | blocked = 1; |
||
168 | } |
||
169 | /* task-join is a cancellation point; if the task is killed while it is |
||
170 | waiting on a join, the task-kill set the kill-request bit and wake up |
||
171 | the task, so it can die :-) */ |
||
172 | task_testcancel(); |
||
173 | |||
174 | /* the target task is terminated... we reset the WAIT_FOR_JOIN flag |
||
175 | so the descriptor can be reused by task_create; if the descriptor was |
||
176 | already discarded by the task_create, we reinsert it into the free |
||
177 | queue */ |
||
178 | proc_table[p].control &= ~WAIT_FOR_JOIN; |
||
179 | if (proc_table[p].control & DESCRIPTOR_DISCARDED) |
||
29 | pj | 180 | iq_insertfirst(p, &freedesc); |
2 | pj | 181 | |
182 | if (value) |
||
183 | *value = proc_table[p].return_value; |
||
184 | |||
185 | if (blocked) { |
||
186 | /* the ll_context_to is already done... */ |
||
187 | kern_deliver_pending_signals(); |
||
188 | sti(); |
||
189 | } |
||
190 | else |
||
191 | /* we did a kern_context_save before... */ |
||
192 | kern_context_load(proc_table[exec_shadow].context); |
||
193 | |||
194 | return 0; |
||
195 | } |
||
196 | |||
197 | /*+ this function set the detach state of a task to joinable. This function |
||
198 | is not present in Posix standard... |
||
199 | returns ESRCH if p is non correct +*/ |
||
200 | int task_joinable(PID p) |
||
201 | { |
||
202 | if (p<0 || p>=MAX_PROC) return ESRCH; |
||
203 | if (proc_table[p].status == FREE) return ESRCH; |
||
204 | |||
205 | kern_cli(); |
||
206 | proc_table[p].control |= TASK_JOINABLE; |
||
207 | kern_sti(); |
||
208 | return 0; |
||
209 | } |
||
210 | |||
211 | /*+ this function set the detach state of a task to detached. This function |
||
212 | works as the posix's pthread_detach |
||
213 | returns EINVAL if p can't be joined (or currently a task has done a |
||
214 | join on it (condition not provided in posix) |
||
215 | ESRCH if p is not correct +*/ |
||
216 | int task_unjoinable(PID p) |
||
217 | { |
||
218 | if (p<0 || p>=MAX_PROC) return ESRCH; |
||
219 | if (proc_table[p].status == FREE) return ESRCH; |
||
220 | |||
221 | kern_cli(); |
||
222 | |||
223 | if (!(proc_table[p].control & TASK_JOINABLE) || |
||
224 | proc_table[p].waiting_for_me != NIL) { |
||
225 | kern_sti(); |
||
226 | return EINVAL; |
||
227 | } |
||
228 | |||
229 | proc_table[p].control |= TASK_JOINABLE; |
||
230 | |||
231 | kern_sti(); |
||
232 | return 0; |
||
233 | } |