Subversion Repositories shark

Rev

Details | 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
 ------------
21
 CVS :        $Id: join.c,v 1.1.1.1 2002-03-29 14:12:51 pj Exp $
22
 
23
 File:        $File$
24
 Revision:    $Revision: 1.1.1.1 $
25
 Last update: $Date: 2002-03-29 14:12:51 $
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;
83
      level_table[l]->task_insert(l,i);
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
  struct timespec ty;
100
  TIME tx;
101
  LEVEL l;
102
 
103
  /* task_join is a cancellation point... if the task is suspended
104
     the control on the status is done in task_kill */
105
  task_testcancel();
106
 
107
  /* some controls on the task p */
108
  if (p<0 || p>=MAX_PROC)                       return ESRCH;
109
  if (proc_table[p].status == FREE &&
110
      !(proc_table[p].control & WAIT_FOR_JOIN)) return ESRCH;
111
  if (!(proc_table[p].control & TASK_JOINABLE)) return EINVAL;
112
 
113
  proc_table[exec_shadow].context = kern_context_save();
114
 
115
  /* first, if it is the first time that the task_join is called,
116
     register the cancellation point */
117
  if (taskjoin_once) {
118
    taskjoin_once = 0;
119
    register_cancellation_point(taskjoin_cancellation_point, NULL);
120
  }
121
 
122
  if (proc_table[p].waiting_for_me != NIL) {
123
    kern_context_load(proc_table[exec_shadow].context);
124
    return EINVAL;
125
  }
126
 
127
  /* deadlock checks; we check the shadow chain to prevent cycles */
128
  x = p;
129
  do {
130
    x = proc_table[x].shadow;
131
    if (x == exec_shadow) {
132
      kern_context_load(proc_table[exec_shadow].context);
133
      return EDEADLK;
134
    }
135
  } while (x != proc_table[x].shadow);
136
 
137
  /* we prepare all the stuffs for joining the target task... */
138
  if (!(proc_table[p].control & WAIT_FOR_JOIN)) {
139
    /* the target task is already running... so we block yhe current task
140
       on it!!!
141
 
142
       Note: It is not correct to set only the shadow and reschedule, as done
143
       with the mutexes, because there is no inheritance with join...
144
       Normally we have to call task_extract because blocking on join is
145
       like blocking on a classic semaphore.
146
       Althought, we set the shadow because:
147
       - if the task call task_join when holding a mutex (AAARRRGGHHH!!!)
148
         the system continue working...
149
       - The deadlock detection strategy works on shadows...
150
       Setting shadows means also that implementation of mutexes that
151
       manage shadows in a strange way WILL NOT WORK with task_join
152
       (for example, the srp.c module doesn't work with task_join) */
153
    proc_table[p].waiting_for_me = exec_shadow;
154
    proc_table[exec_shadow].shadow = p;
155
 
156
    /* SAME AS SCHEDULER... manage the capacity event and the load_info */
157
    ll_gettime(TIME_EXACT, &schedule_time);
158
    SUBTIMESPEC(&schedule_time, &cap_lasttime, &ty);
159
    tx = TIMESPEC2USEC(&ty);
160
    proc_table[exec_shadow].avail_time -= tx;
161
    jet_update_slice(tx);
162
    if (cap_timer != NIL) {
163
      event_delete(cap_timer);
164
      cap_timer = NIL;
165
    }
166
 
167
    /* now, we block the current task, waiting the end of the target task */
168
    l = proc_table[exec_shadow].task_level;
169
    level_table[l]->task_extract(l,exec_shadow);
170
    proc_table[exec_shadow].status = WAIT_JOIN;
171
 
172
    exec = exec_shadow = -1;
173
    scheduler();
174
    /* note that we don't use kern_context_load because when rescheduled
175
       we remain in kernel mode... */
176
    ll_context_to(proc_table[exec_shadow].context);
177
 
178
    blocked = 1;
179
  }
180
  /* task-join is a cancellation point; if the task is killed while it is
181
     waiting on a join, the task-kill set the kill-request bit and wake up
182
     the task, so it can die :-) */
183
  task_testcancel();
184
 
185
  /* the target task is terminated... we reset the WAIT_FOR_JOIN flag
186
     so the descriptor can be reused by task_create; if the descriptor was
187
     already discarded by the task_create, we reinsert it into the free
188
     queue */
189
  proc_table[p].control &= ~WAIT_FOR_JOIN;
190
  if (proc_table[p].control & DESCRIPTOR_DISCARDED)
191
    q_insertfirst(p, &freedesc);
192
 
193
  if (value)
194
    *value = proc_table[p].return_value;
195
 
196
  if (blocked) {
197
    /* the ll_context_to is already done... */
198
    kern_deliver_pending_signals();
199
    sti();
200
  }
201
  else
202
    /* we did a kern_context_save before... */
203
    kern_context_load(proc_table[exec_shadow].context);
204
 
205
  return 0;
206
}
207
 
208
/*+ this function set the detach state of a task to joinable. This function
209
    is not present in Posix standard...
210
    returns ESRCH if p is non correct +*/
211
int task_joinable(PID p)
212
{
213
  if (p<0 || p>=MAX_PROC)                       return ESRCH;
214
  if (proc_table[p].status == FREE)             return ESRCH;
215
 
216
  kern_cli();
217
  proc_table[p].control |= TASK_JOINABLE;
218
  kern_sti();
219
  return 0;
220
}
221
 
222
/*+ this function set the detach state of a task to detached. This function
223
    works as the posix's pthread_detach
224
    returns EINVAL if p can't be joined (or currently a task has done a
225
    join on it (condition not provided in posix)
226
    ESRCH if p is not correct +*/
227
int task_unjoinable(PID p)
228
{
229
  if (p<0 || p>=MAX_PROC)                       return ESRCH;
230
  if (proc_table[p].status == FREE)             return ESRCH;
231
 
232
  kern_cli();
233
 
234
  if (!(proc_table[p].control & TASK_JOINABLE) ||
235
      proc_table[p].waiting_for_me != NIL) {
236
    kern_sti();
237
    return EINVAL;
238
  }
239
 
240
  proc_table[p].control |= TASK_JOINABLE;
241
 
242
  kern_sti();
243
  return 0;
244
}