Subversion Repositories shark

Rev

Rev 2 | Rev 38 | 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
 ------------
21
 CVS :        $Id: nanoslp.c,v 1.1.1.1 2002-03-29 14:12:52 pj Exp $
22
 
23
 File:        $File$
24
 Revision:    $Revision: 1.1.1.1 $
25
 Last update: $Date: 2002-03-29 14:12:52 $
26
 ------------
27
 
28
 This file contains the nanosleep function (posix 14.2.5) and related
29
 functions
30
 
31
**/
32
 
33
/*
34
 * Copyright (C) 2000 Paolo Gai
35
 *
36
 * This program is free software; you can redistribute it and/or modify
37
 * it under the terms of the GNU General Public License as published by
38
 * the Free Software Foundation; either version 2 of the License, or
39
 * (at your option) any later version.
40
 *
41
 * This program is distributed in the hope that it will be useful,
42
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44
 * GNU General Public License for more details.
45
 *
46
 * You should have received a copy of the GNU General Public License
47
 * along with this program; if not, write to the Free Software
48
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
49
 *
50
 */
51
 
52
#include <stdarg.h>
53
#include <ll/ll.h>
54
#include <ll/stdlib.h>
55
#include <ll/stdio.h>
56
#include <ll/string.h>
57
#include <kernel/config.h>
58
#include <kernel/model.h>
59
#include <kernel/const.h>
60
#include <sys/types.h>
61
#include <kernel/types.h>
62
#include <kernel/descr.h>
63
#include <errno.h>
64
#include <kernel/var.h>
65
#include <kernel/func.h>
66
 
67
static int nanosleep_once = 1;
68
 
69
/* this table is used to store the wakeup time of the task,
70
   because if the rmtp argument is specified, the remaining sleep time
71
   have to be returned. */
72
struct timespec nanosleep_table[MAX_PROC];
73
 
74
 
75
/* this is the test that is done when a task is being killed
76
   and it is waiting on a sigwait */
77
static int nanosleep_cancellation_point(PID i, void *arg)
78
{
79
    LEVEL l;
80
 
81
    if (proc_table[i].status == WAIT_NANOSLEEP) {
82
      /* the task that have to be killed is waiting on a nanosleep */
83
 
84
      /* the nanosleep event have to be removed */
85
      event_delete(proc_table[i].delay_timer);
86
      proc_table[i].delay_timer = -1;
87
 
88
      /* and the task have to be reinserted into the ready queues, so it
89
         will fall into task_testcancel */
90
      l = proc_table[i].task_level;
91
      level_table[l]->task_insert(l,i);
92
 
93
      return 1;
94
    }
95
 
96
    return 0;
97
}
98
 
99
int nanosleep_interrupted_by_signal(PID i, void *arg)
100
{
101
    LEVEL l;
102
    struct timespec t1, t2;
103
 
104
    if (proc_table[i].status == WAIT_NANOSLEEP) {
105
      /* the task is waiting on a nanosleep and it is still receiving a
106
         signal... */
107
      ll_gettime(TIME_EXACT,&t1);
108
      SUBTIMESPEC(&nanosleep_table[i], &t1, &t2);
109
      TIMESPEC_ASSIGN(&nanosleep_table[i], &t2);
110
 
111
      /* the nanosleep event have to be removed */
112
      event_delete(proc_table[i].delay_timer);
113
      proc_table[i].delay_timer = -1;
114
 
115
      /* and the task have to be reinserted into the ready queues, so it
116
         will fall into task_testcancel */
117
      l = proc_table[i].task_level;
118
      level_table[l]->task_insert(l,i);
119
 
120
      return 1;
121
    }
122
 
123
    return 0;
124
}
125
 
126
static void nanosleep_timer(void *arg)
127
{
128
  PID p = (PID)arg;
129
  LEVEL l;
130
 
131
  NULL_TIMESPEC(&nanosleep_table[p]);
132
 
133
  proc_table[p].delay_timer = -1;
134
 
135
  l = proc_table[p].task_level;
136
  level_table[l]->task_insert(l,p);
137
 
138
  event_need_reschedule();
139
}
140
 
141
 
142
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
143
{
144
  struct timespec ty;
145
  TIME tx;
146
  LEVEL l;
147
 
148
  if (rqtp->tv_sec < 0 || rqtp->tv_nsec > 1000000000)
149
    return EINVAL;
150
 
151
  proc_table[exec_shadow].context = kern_context_save();
152
 
153
  /* first, if it is the first time that the nanosleep is called,
154
     register the cancellation point */
155
  if (nanosleep_once) {
156
    nanosleep_once = 0;
157
    register_cancellation_point(nanosleep_cancellation_point, NULL);
158
    register_interruptable_point(nanosleep_interrupted_by_signal, NULL);
159
  }
160
 
161
  /* SAME AS SCHEDULER... manage the capacity event and the load_info */
162
  ll_gettime(TIME_EXACT, &schedule_time);
163
  SUBTIMESPEC(&schedule_time, &cap_lasttime, &ty);
164
  tx = TIMESPEC2USEC(&ty);
165
  proc_table[exec_shadow].avail_time -= tx;
166
  jet_update_slice(tx);
167
  if (cap_timer != NIL) {
168
    event_delete(cap_timer);
169
    cap_timer = NIL;
170
  }
171
 
172
  /* now, we block the current task, waiting the end of the target task */
173
  l = proc_table[exec_shadow].task_level;
174
  level_table[l]->task_extract(l,exec_shadow);
175
  proc_table[exec_shadow].status = WAIT_NANOSLEEP;
176
 
177
  ADDTIMESPEC(&schedule_time, rqtp, &nanosleep_table[exec_shadow]);
178
 
179
  /* we can use the delaytimer because if we are here we are not in a
180
     task_delay */
181
  proc_table[exec_shadow].delay_timer =
182
    kern_event_post(&nanosleep_table[exec_shadow],
183
                    nanosleep_timer,
184
                    (void *)exec_shadow);
185
 
186
  exec = exec_shadow = -1;
187
  scheduler();
188
  kern_context_load(proc_table[exec_shadow].context);
189
 
190
  task_testcancel();
191
 
192
  /* if the nanosleep_table[exec_shadow] != {0,0}, the nanosleep was
193
     interrupted by a signal... */
194
  if (nanosleep_table[exec_shadow].tv_sec  != 0 ||
195
      nanosleep_table[exec_shadow].tv_nsec != 0    ) {
196
    if (rmtp)
197
      TIMESPEC_ASSIGN(rmtp, &nanosleep_table[exec_shadow]);
198
 
199
    return (EINTR);
200
  }
201
  else
202
    return 0;
203
}
204
 
205
unsigned int sleep(unsigned int seconds)
206
{
207
  struct timespec t, t2;
208
 
209
  t.tv_sec = seconds;
210
  t.tv_nsec = 0;
211
  nanosleep(&t, &t2);
212
 
213
  return t2.tv_sec;
214
}
215
 
216