summaryrefslogtreecommitdiff
path: root/pfinet/glue-include/linux/sched.h
blob: e39263e5686d0899eea6c83129c4d90e629c070b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#ifndef _HACK_SCHED_H
#define _HACK_SCHED_H

#include <sys/signal.h>
#include <sys/time.h>
#include <mach.h>
#include <hurd/hurd_types.h>
#include <limits.h>
#include <assert.h>
#include <pthread.h>
#include <errno.h>

#include "mapped-time.h"

#include <linux/binfmts.h>
#include <linux/personality.h>
#include <linux/tasks.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/times.h>
#include <linux/timex.h>

#include <asm/system.h>
#if 0
#include <asm/semaphore.h>
#include <asm/page.h>

#include <linux/smp.h>
#include <linux/tty.h>
#include <linux/sem.h>
#include <linux/signal.h>
#include <linux/securebits.h>
#endif

#include <linux/kernel.h>
#include <linux/net.h>
#include <linux/wait.h>
#include <linux/timer.h>


#define jiffies (fetch_jiffies ())

#define current	(&current_contents)
extern struct task_struct current_contents;
struct task_struct
{
  uid_t pgrp, pid;
  int flags;
  int timeout;
  int signal;
  int blocked;
  int state, policy;
  int isroot;
  char *comm;
  struct wait_queue **next_wait;
};

static inline void
prepare_current (int isroot)
{
  current->signal = 0;
  current->isroot = isroot;
  /* All other members are constant zero and ignored.  */
}
#define become_task(user)		prepare_current ((user)->isroot)
#define become_task_protid(protid)	prepare_current ((protid)->isroot)

#define signal_pending(current)	((current)->signal)

/* FLAGS in task_struct's. */
#define PF_EXITING 1
/* STATE in task_struct's. */
#define TASK_INTERRUPTIBLE 0
#define TASK_RUNNING 0
/* policy in task_struct's. */
#define SCHED_YIELD 0

struct semaphore { };


static inline int
suser ()
{
  return current->isroot;
};

static inline int
capable(int cap)
{
  return current->isroot;
}


extern pthread_mutex_t global_lock;

static inline int
interruptible_sleep_on_timeout (struct wait_queue **p, struct timespec *tsp)
{
  pthread_cond_t **condp = (void *) p, *c;
  int isroot;
  struct wait_queue **next_wait;
  error_t err;

  c = *condp;
  if (c == 0)
    {
      c = malloc (sizeof **condp);
      assert (c);
      pthread_cond_init (c, NULL);
      *condp = c;
    }

  isroot = current->isroot;	/* This is our context that needs switched.  */
  next_wait = current->next_wait; /* This too, for multiple schedule calls.  */
  current->next_wait = 0;
  err = pthread_hurd_cond_timedwait_np(c, &global_lock, tsp);
  if (err == EINTR)
    current->signal = 1;	/* We got cancelled, mark it for later.  */
  current->isroot = isroot;	/* Switch back to our context.  */
  current->next_wait = next_wait;
  return (err == ETIMEDOUT);
}

static inline void
wake_up_interruptible (struct wait_queue **p)
{
  pthread_cond_t **condp = (void *) p, *c = *condp;
  if (c)
    pthread_cond_broadcast (c);
}
#define wake_up		wake_up_interruptible


static inline void
add_wait_queue(struct wait_queue ** p, struct wait_queue * wait)
{
  assert (current->next_wait == 0);
  current->next_wait = p;
}

static inline void
remove_wait_queue(struct wait_queue ** p, struct wait_queue * wait)
{
  assert (current->next_wait == p);
  current->next_wait = 0;
}

static inline void
schedule (void)
{
  assert (current->next_wait);
  interruptible_sleep_on_timeout (current->next_wait, NULL);
}

static inline void
process_schedule_timeout (unsigned long data)
{
  struct wait_queue **sleep = (struct wait_queue **) data;

  wake_up_interruptible (sleep);
}

static inline long
schedule_timeout (long timeout)
{
  long expire = timeout + jiffies;
  struct timer_list timer;
  static struct wait_queue *sleep = 0;  /* See comment in wait.h why this suffices.  */
  /* TODO: but free it !! */

  init_timer (&timer);
  timer.expires = expire;
  timer.data = (unsigned long) &sleep;
  timer.function = process_schedule_timeout;
  add_timer (&timer);

  interruptible_sleep_on_timeout (&sleep, NULL);
  if (signal_pending (current))
    {
      /* We were canceled.  */
      del_timer (&timer);
      expire -= jiffies;
      if (expire >= 0)
	return expire;
      else
	return 0;
    }
  /* It may happen that we get woken without a signal. Noticed notably during
     rsyslog testsuite.  Make sure we don't leave our timer in.  */
  del_timer(&timer);
  return 0;
}

#define	MAX_SCHEDULE_TIMEOUT	LONG_MAX

/* This function is used only to send SIGPIPE to the current
   task.  In all such cases, EPIPE is returned anyhow.  In the
   Hurd, servers are not responsible for SIGPIPE; the library
   does that itself upon receiving EPIPE.  So we can just
   NOP such calls.  */
static inline int
send_sig (u_long signo, struct task_struct *task, int priv)
{
  assert (signo == SIGPIPE);
  assert (task == current);
  return 0;
}


#endif