Subversion Repositories shark

Rev

Rev 172 | 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
 ------------
172 giacomo 21
 CVS :        $Id: nanoslp.c,v 1.4 2003-05-22 14:10:05 giacomo Exp $
2 pj 22
 
23
 File:        $File$
172 giacomo 24
 Revision:    $Revision: 1.4 $
25
 Last update: $Date: 2003-05-22 14:10:05 $
2 pj 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 */
38 pj 85
      kern_event_delete(proc_table[i].delay_timer);
2 pj 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;
38 pj 91
      level_table[l]->public_unblock(l,i);
2 pj 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... */
38 pj 107
      kern_gettime(&t1);
2 pj 108
      SUBTIMESPEC(&nanosleep_table[i], &t1, &t2);
109
      TIMESPEC_ASSIGN(&nanosleep_table[i], &t2);
110
 
111
      /* the nanosleep event have to be removed */
38 pj 112
      kern_event_delete(proc_table[i].delay_timer);
2 pj 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;
38 pj 118
      level_table[l]->public_unblock(l,i);
2 pj 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;
38 pj 136
  level_table[l]->public_unblock(l,p);
2 pj 137
 
138
  event_need_reschedule();
139
}
140
 
141
 
142
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
143
{
144
  LEVEL l;
145
 
146
  if (rqtp->tv_sec < 0 || rqtp->tv_nsec > 1000000000)
147
    return EINVAL;
148
 
149
  proc_table[exec_shadow].context = kern_context_save();
150
 
151
  /* first, if it is the first time that the nanosleep is called,
152
     register the cancellation point */
153
  if (nanosleep_once) {
154
    nanosleep_once = 0;
155
    register_cancellation_point(nanosleep_cancellation_point, NULL);
156
    register_interruptable_point(nanosleep_interrupted_by_signal, NULL);
157
  }
158
 
38 pj 159
  kern_epilogue_macro();
2 pj 160
 
161
  /* now, we block the current task, waiting the end of the target task */
162
  l = proc_table[exec_shadow].task_level;
38 pj 163
  level_table[l]->public_block(l,exec_shadow);
2 pj 164
  proc_table[exec_shadow].status = WAIT_NANOSLEEP;
165
 
166
  ADDTIMESPEC(&schedule_time, rqtp, &nanosleep_table[exec_shadow]);
167
 
168
  /* we can use the delaytimer because if we are here we are not in a
169
     task_delay */
170
  proc_table[exec_shadow].delay_timer =
171
    kern_event_post(&nanosleep_table[exec_shadow],
172
                    nanosleep_timer,
173
                    (void *)exec_shadow);
174
 
175
  exec = exec_shadow = -1;
176
  scheduler();
177
  kern_context_load(proc_table[exec_shadow].context);
178
 
179
  task_testcancel();
180
 
181
  /* if the nanosleep_table[exec_shadow] != {0,0}, the nanosleep was
182
     interrupted by a signal... */
183
  if (nanosleep_table[exec_shadow].tv_sec  != 0 ||
184
      nanosleep_table[exec_shadow].tv_nsec != 0    ) {
185
    if (rmtp)
186
      TIMESPEC_ASSIGN(rmtp, &nanosleep_table[exec_shadow]);
187
 
188
    return (EINTR);
189
  }
190
  else
191
    return 0;
192
}
193
 
194
unsigned int sleep(unsigned int seconds)
195
{
196
  struct timespec t, t2;
197
 
198
  t.tv_sec = seconds;
199
  t.tv_nsec = 0;
200
  nanosleep(&t, &t2);
201
 
202
  return t2.tv_sec;
203
}
204
 
164 giacomo 205
unsigned int usleep(unsigned int usec)
206
{
207
  struct timespec t, t2;
2 pj 208
 
172 giacomo 209
  t.tv_sec = usec / 1000000;
210
  t.tv_nsec = usec % 1000000 * 1000;
164 giacomo 211
  nanosleep(&t, &t2);
212
 
213
  return t2.tv_sec;
214
}