File: rtl_sched.h

package info (click to toggle)
rtlinux 3.1pre3-3
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 4,896 kB
  • ctags: 4,228
  • sloc: ansic: 26,204; sh: 2,069; makefile: 1,414; perl: 855; tcl: 489; asm: 380; cpp: 42
file content (406 lines) | stat: -rw-r--r-- 10,697 bytes parent folder | download | duplicates (2)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * RTLinux scheduler
 *
 * Written by Michael Barabanov, Victor Yodaiken
 * Copyright (C) Finite State Machine Labs Inc., 1998-1999
 * Released under the terms of the GPL Version 2
 *
 */

#ifndef __RTL__SCHED__
#define __RTL__SCHED__

#include <asm/ptrace.h>
#include <linux/version.h>
#if LINUX_VERSION_CODE >= 0x020300
#include <linux/spinlock.h>
#else
#include <asm/spinlock.h>
#endif

#include <asm/bitops.h>
#include <rtl_conf.h>
#include <rtl_limits.h>
#include <rtl_core.h>
#include <rtl_time.h>
#include <arch/rtl_fpu.h>
#include <rtl_spinlock.h>
#include <signal.h>
#include <reserve_cpu.h>

#ifdef __cplusplus
struct sched_param {
	int sched_priority;
};
#define SCHED_OTHER		0
#define SCHED_FIFO		1
#define SCHED_RR		2
#else
#include <linux/sched.h>
#endif

#define rtl_sched_param sched_param

#define TIMER_ABSTIME 1
#define RTL_MAX_TIMERS 32


typedef struct rtl_timer_struct *timer_t;

struct rtl_cleanup_struct {
	void (*routine)(void*);
	void *arg;
	struct rtl_cleanup_struct *next;
};

/* threads have no clocks. Clocks belong to schedulers.  We can add scheduling
   policies in which a scheduler juggles multiple clocks, but there is no advantage
   that I can see in allowing a thread to specify its  hardware clock
   */
#define RTL_THREAD_MAGIC 0x79433743
   
struct rtl_thread_struct;
typedef struct rtl_thread_struct *pthread_t;

#include <rtl_posix.h>

struct rtl_thread_struct {
	int *stack;	/* hardcoded */
	int fpu_initialized;
	RTL_FPU_CONTEXT fpu_regs;
	int uses_fp;
	int *kmalloc_stack_bottom;
	struct rtl_sched_param sched_param;
	struct rtl_thread_struct *next;
	int cpu;
	hrtime_t resume_time;
	hrtime_t period;
	hrtime_t timeval;
	struct module *creator;
	void (*abort)(void *);
	void *abortdata;
	int threadflags;
	rtl_sigset_t pending;
	rtl_sigset_t blocked;
	void *user[4];
	int errno_val;
	struct rtl_cleanup_struct *cleanup;
	int magic;
	struct rtl_posix_thread_struct posix_data;
	void *tsd [RTL_PTHREAD_KEYS_MAX];
};

#define RTL_POSIX_DATA(th) (&(th)->posix_data)

#define hrt2ts(hrt) ((const struct timespec *) ({ pthread_self()->timeval = hrt; (&pthread_self()->timeval); }))

#define RTL_PRIO(th) ((th)->sched_param.sched_priority)

enum {RTL_CANCELPENDING, RTL_CANCELTYPE, RTL_THREAD_JOINABLE, RTL_THREAD_FINISHED, RTL_THREAD_TIMERARMED, RTL_THREAD_WAIT_FOR_JOIN, RTL_THREAD_OK_TO_FINISH_JOIN};

#define RTL_MAX_SIGNAL 31 /* this is max for internal RTLinux signals */
/* these are bit positions */
#define RTL_SIGNAL_NULL 0 /* posix wants signal=0 to simply check */
#define RTL_SIGNAL_WAKEUP 1
#define RTL_SIGNAL_CANCEL 2
#define RTL_SIGNAL_SUSPEND 3
#define RTL_SIGNAL_TIMER 5
#define RTL_SIGNAL_READY 6

/*TODO How will this work on PPC */
#define RTL_LINUX_MIN_SIGNAL 256  /* signals to Linux start here. global then local */
#define RTL_LINUX_MAX_SIGNAL 1024
#define RTL_LINUX_MIN_LOCAL_SIGNAL  512

extern int rtl_schedule (void);
#define RTL_TIMED_OUT(x) rtl_sigismember((x), RTL_SIGNAL_TIMER)
#define RTL_SIGINTR(x) (0)

typedef struct rtl_thread_struct RTL_THREAD_STRUCT;


#define RTL_MARK_READY(th) rtl_sigaddset(&(th)->pending, RTL_SIGNAL_READY)
#define RTL_MARK_SUSPENDED(th) rtl_sigdelset(&(th)->pending, RTL_SIGNAL_READY)

struct rtl_sched_cpu_struct {
	struct rtl_thread_struct *rtl_current;
	struct rtl_thread_struct rtl_linux_task;
	struct rtl_thread_struct *rtl_task_fpu_owner;	/* the task whose FP context is currently in the FPU unit */
	struct rtl_thread_struct *rtl_tasks; /* the queue of  RT tasks */
	struct rtl_thread_struct *rtl_new_tasks;
	clockid_t clock;
	spinlock_t rtl_tasks_lock;
	int sched_flags;
	int sched_user[4];	/* on x86 sched_user[0] is the Linux TS flag */
}/* __attribute__ ((aligned (64)))*/;

typedef struct rtl_sched_cpu_struct  schedule_t;

#ifdef CONFIG_SMP
extern struct rtl_sched_cpu_struct rtl_sched [NR_CPUS];
#define sched_data(cpu) (&rtl_sched [cpu])
#define LOCAL_SCHED (&rtl_sched[rtl_getcpuid()])
#else
extern struct rtl_sched_cpu_struct rtl_sched [1];
#define sched_data(cpu) (&rtl_sched[0])
#define LOCAL_SCHED (&rtl_sched[0])
#endif

static inline pthread_t pthread_linux(void)
{
	return &(rtl_sched[rtl_getcpuid()].rtl_linux_task);
}

#define RTL_CURRENT (LOCAL_SCHED->rtl_current)


/* RTL-specific function TODO: write POSIX equivalents for these */


extern int pthread_delete_np (pthread_t thread);

extern int pthread_setfp_np (pthread_t thread, int flag);

extern int pthread_wakeup_np (pthread_t thread);

extern int pthread_suspend_np (pthread_t thread);

/* end RTL-specific */


/* POSIX interface */

#define pthread_cleanup_push(p_routine, p_arg) \
{ \
	rtl_irqstate_t __flags; \
	struct rtl_cleanup_struct __cleanup; \
	__cleanup.routine = (p_routine); \
	__cleanup.arg = (p_arg); \
	rtl_no_interrupts (__flags); \
	__cleanup.next = pthread_self()->cleanup; \
	pthread_self()->cleanup = &__cleanup; \
	rtl_restore_interrupts (__flags);


#define pthread_cleanup_pop(execute) \
	rtl_no_interrupts (__flags); \
	pthread_self()->cleanup = pthread_self()->cleanup->next; \
	if (execute) \
		__cleanup.routine(__cleanup.arg); \
	rtl_restore_interrupts (__flags); \
}


extern inline int sched_get_priority_max(int policy) { return 1000000; }
extern inline int sched_get_priority_min(int policy) { return 0; }

extern inline pthread_t pthread_self(void) {
	return (LOCAL_SCHED)-> rtl_current;
}

extern inline int pthread_equal(pthread_t thread1, pthread_t thread2)
{
	return thread1 == thread2;
}

typedef int pthread_key_t;

extern inline int pthread_setspecific(pthread_key_t key, const void *value)
{
	pthread_self()->tsd[key] = (void *) value;
	return 0;
}

extern inline void *pthread_getspecific(pthread_key_t key)
{
	return pthread_self()->tsd[key];
}

#define RTL_PTHREAD_STACK_MIN 8192

#define PTHREAD_CANCEL_ENABLE 0
#define PTHREAD_CANCEL_DISABLE 1
#define PTHREAD_CANCEL_DEFERRED 0
#define PTHREAD_CANCEL_ASYNCHRONOUS 1
#define PTHREAD_CREATE_JOINABLE 0
#define PTHREAD_CREATE_DETACHED 1

extern int pthread_setcancelstate(int state, int *oldstate);
extern int pthread_setcanceltype(int type, int *oldtype);
extern int pthread_cancel (pthread_t thread);
extern void pthread_testcancel(void);

typedef struct STRUCT_PTHREAD_ATTR {
	size_t stack_size;
	void *stack_addr;
	struct rtl_sched_param sched_param;
	int cpu;
	int use_fp;
	rtl_sigset_t initial_state;
	int detachstate;
} pthread_attr_t;

extern inline int pthread_attr_init(pthread_attr_t *attr)
{
	attr->stack_addr = 0;
	attr->stack_size = 20480;
	attr->sched_param.sched_priority = sched_get_priority_min(0);
	attr->cpu = rtl_getcpuid();
        rtl_sigemptyset(&attr->initial_state);
        rtl_sigaddset(&attr->initial_state, RTL_SIGNAL_READY);
	attr->use_fp = 0;
	attr->detachstate = PTHREAD_CREATE_JOINABLE;
	return 0;
}

extern inline int pthread_attr_destroy(pthread_attr_t *attr)
{
	return 0;
}

extern inline int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
{
	if (stacksize < RTL_PTHREAD_STACK_MIN) {
		return EINVAL;
	}

	attr->stack_size = stacksize;
	return 0;
}

extern inline int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
{
	*stacksize = attr->stack_size;
	return 0;
}

extern int pthread_attr_setfp_np (pthread_attr_t *attr, int flag);
extern inline int pthread_attr_getfp_np (const pthread_attr_t *attr, int *use_fp)
{
	*use_fp = attr->use_fp;
	return 0;
}

#define pthread_attr_setfp(attr,flag) pthread_attr_setfp_np(attr,flag)
#define pthread_attr_getfp(attr,flag) pthread_attr_getfp_np(attr,flag)

extern inline int pthread_attr_getcpu_np(const pthread_attr_t *attr, int * cpu)
{
	*cpu = attr->cpu;
	return 0;
}

extern int pthread_attr_setcpu_np(pthread_attr_t *attr, int cpu);

#define pthread_attr_getcpu(attr,cpu) pthread_attr_getcpu_np(attr,cpu)
#define pthread_attr_setcpu(attr,cpu) pthread_attr_setcpu_np(attr,cpu)

extern int pthread_wait_np(void);

extern int pthread_make_periodic_np (pthread_t p, hrtime_t start_time, hrtime_t period);


/* this one is deprecated */
extern inline clockid_t rtl_getschedclock(void)
{
	return LOCAL_SCHED->clock;
}

#define CLOCK_RTL_SCHED (LOCAL_SCHED->clock)
#define CLOCK_REALTIME CLOCK_RTL_SCHED

extern int rtl_setclockmode (clockid_t clock, int mode, hrtime_t period);


extern int sched_yield(void);


extern inline int pthread_setschedparam(pthread_t thread, int policy,
		    const struct rtl_sched_param *param) {
	thread->sched_param = *param;
	return 0;
}
 
extern inline int pthread_getschedparam(pthread_t thread, int *policy,
		    struct rtl_sched_param *param) {
	*param = thread->sched_param;
	return 0;
}

extern inline int pthread_attr_setschedparam(pthread_attr_t *attr,
		    const struct rtl_sched_param *param) {
	attr->sched_param = *param;
	return 0;
}

extern inline int pthread_attr_getschedparam(const pthread_attr_t *attr,
		    struct rtl_sched_param *param) {
	*param = attr->sched_param;
	return 0;
}
 
extern int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr);
extern int pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr);

struct module;
extern struct module __this_module;
extern int __pthread_create (pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg, struct module *mod);

#define pthread_create(thread, attr, start, arg) __pthread_create(thread, attr, start, arg, &__this_module)


#define PTHREAD_CANCELED ((void *) -1)

extern void pthread_exit(void *retval);

int pthread_kill(pthread_t , int signo);

extern inline int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
{
	attr->detachstate = detachstate;
	return 0;
}

extern inline int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
{
	*detachstate = attr->detachstate;
	return 0;
}

typedef unsigned useconds_t;

extern int usleep(useconds_t useconds);

extern int clock_nanosleep(clockid_t clock_id, int flags,
		const struct timespec *rqtp, struct timespec *rmtp);

extern int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);

#include <rtl_compat.h>

extern int rtl_reserve_cpus(unsigned cpumask);
extern int rtl_unreserve_cpus(void);

struct task_struct *get_linux_current(void);

#define RTL_SCHED_TIMER_OK 0

#define __rtl_setup_timeout(th,timeout) do { \
	(th)->resume_time = (timeout); \
	set_bit (RTL_THREAD_TIMERARMED, &(th) -> threadflags); \
	clear_bit (RTL_SCHED_TIMER_OK, &sched_data((th)->cpu)->sched_flags); \
} while (0)

static inline hrtime_t __rtl_fix_timeout_for_clock(clockid_t clock, hrtime_t timeout)
{
	if (clock == CLOCK_RTL_SCHED) {
		return timeout;
	}
/*	if (clock->mode == RTL_CLOCK_MODE_ONESHOT) {
		return timeout - clock->delta;
	} */
	return timeout - clock_gethrtime(clock) + clock_gethrtime(CLOCK_RTL_SCHED);
}


#endif