# rtai_sched.h

    00001 /*
    00002  * Copyright (C) 1999-2003 Paolo Mantegazza <mantegazza@aero.polimi.it>
    00003  *
    00004  * This program is free software; you can redistribute it and/or
    00005  * modify it under the terms of the GNU General Public License as
    00006  * published by the Free Software Foundation; either version 2 of the
    00007  * License, or (at your option) any later version.
    00008  *
    00009  * This program is distributed in the hope that it will be useful,
    00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00012  * GNU General Public License for more details.
    00013  *
    00014  * You should have received a copy of the GNU General Public License
    00015  * along with this program; if not, write to the Free Software
    00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    00017  */
    00018
    00019 #ifndef _RTAI_SCHED_H
    00020 #define _RTAI_SCHED_H
    00021
    00022 #include <rtai.h>
    00023 #ifndef __KERNEL__
    00024 #include <sys/time.h>
    00025 #include <time.h>
    00026 #include <errno.h>
    00027 #include <rtai_types.h>
    00028 #endif /* __KERNEL__ */
    00029
    00030 #define RT_SCHED_UP   1
    00031 #define RT_SCHED_SMP  2
    00032 #define RT_SCHED_MUP  3
    00033
    00034 #define RT_SCHED_HIGHEST_PRIORITY  0
    00035 #define RT_SCHED_LOWEST_PRIORITY   0x3fffFfff
    00036 #define RT_SCHED_LINUX_PRIORITY    0x7fffFfff
    00037
    00038 #define RT_SCHED_READY        1
    00039 #define RT_SCHED_SUSPENDED    2
    00040 #define RT_SCHED_DELAYED      4
    00041 #define RT_SCHED_SEMAPHORE    8
    00042 #define RT_SCHED_SEND        16
    00043 #define RT_SCHED_RECEIVE     32
    00044 #define RT_SCHED_RPC         64
    00045 #define RT_SCHED_RETURN     128
    00046 #define RT_SCHED_MBXSUSP    256
    00047 #define RT_SCHED_SFTRDY     512
    00048
    00049 #define RT_IRQ_TASK         0
    00050 #define RT_IRQ_TASKLET      1
    00051 #define RT_IRQ_TASK_ERR     0x7FFFFFFF
    00052
    00053 struct rt_task_struct;
    00054
    00055 #ifdef __KERNEL__
    00056
    00057 #include <linux/time.h>
    00058 #include <linux/errno.h>
    00059
    00060 #define RT_TASK_MAGIC 0x754d2774
    00061
    00062 #ifndef __cplusplus
    00063
    00064 #include <linux/sched.h>
    00065
    00066 typedef struct rt_queue {
    00067         struct rt_queue *prev;
    00068         struct rt_queue *next;
    00069         struct rt_task_struct *task;
    00070 } QUEUE;
    00071
    00072 struct mcb_t {
    00073     void *sbuf;
    00074     int sbytes;
    00075     void *rbuf;
    00076     int rbytes;
    00077 };
    00078
    00079 typedef struct rt_ExitHandler {
    00080     /* Exit handler functions are called like C++ destructors in
    00081        rt_task_delete(). */
    00082     struct rt_ExitHandler *nxt;
    00083     void (*fun) (void *arg1, int arg2);
    00084     void *arg1;
    00085     int   arg2;
    00086 } XHDL;
    00087
    00088 struct rt_heap_t { void *heap, *kadr, *uadr; };
    00089
    00090 typedef struct rt_task_struct {
    00091
    00092     int *stack;
    00093     int uses_fpu;
    00094     int magic;
    00095     volatile int state, running;
    00096     unsigned long runnable_on_cpus;
    00097     int *stack_bottom;
    00098     volatile int priority;
    00099     int base_priority;
    00100     int policy;
    00101     int sched_lock_priority;
    00102     struct rt_task_struct *prio_passed_to;
    00103     RTIME period;
    00104     RTIME resume_time;
    00105     RTIME yield_time;
    00106     int rr_quantum;
    00107     int rr_remaining;
    00108     int suspdepth;
    00109     struct rt_queue queue;
    00110     int owndres;
    00111     struct rt_queue *blocked_on;
    00112     struct rt_queue msg_queue;
    00113     int tid;    /* trace ID */
    00114     unsigned msg;
    00115     struct rt_queue ret_queue;
    00116     void (*signal)(void);
    00117     FPU_ENV fpu_reg;
    00118     struct rt_task_struct *prev;
    00119     struct rt_task_struct *next;
    00120     struct rt_task_struct *tprev;
    00121     struct rt_task_struct *tnext;
    00122     struct rt_task_struct *rprev;
    00123     struct rt_task_struct *rnext;
    00124
    00125     /* Appended for calls from LINUX. */
    00126     int *fun_args, *bstack;
    00127     struct task_struct *lnxtsk;
    00128     long long retval;
    00129     char *msg_buf[2];
    00130     int max_msg_size[2];
    00131     char task_name[16];
    00132     void *system_data_ptr;
    00133     struct rt_task_struct *nextp;
    00134     struct rt_task_struct *prevp;
    00135
    00136     /* Added to support user specific trap handlers. */
    00137     RT_TRAP_HANDLER task_trap_handler[RTAI_NR_TRAPS];
    00138
    00139     /* Added from rtai-22. */
    00140     void (*usp_signal)(void);
    00141     volatile unsigned long pstate;
    00142     unsigned long usp_flags;
    00143     unsigned long usp_flags_mask;
    00144     unsigned long force_soft;
    00145     volatile int is_hard;
    00146
    00147     /* Added to terminate qBlks. */
    00148     void *tick_queue;
    00149
    00150     /* Added to terminate re-entry of user space functions. */
    00151
    00152     void *trap_handler_data;
    00153     int trap_signo;
    00154
    00155     /* For use by watchdog. */
    00156     int resync_frame;
    00157
    00158     /* For use by exit handler functions. */
    00159     XHDL *ExitHook;
    00160     int linux_signal;
    00161     int errno;
    00162     void (*linux_signal_handler)(int sig);
    00163     RTIME exectime[2];
    00164     struct mcb_t mcb;
    00165
    00166         /* Real time heaps. */
    00167         struct rt_heap_t heap[2];
    00168
    00169 } RT_TASK __attribute__ ((__aligned__ (16)));
    00170
    00171 #else /* __cplusplus */
    00172 extern "C" {
    00173 #endif /* !__cplusplus */
    00174
    00175 int rt_task_init(struct rt_task_struct *task,
    00176                  void (*rt_thread)(int),
    00177                  int data,
    00178                  int stack_size,
    00179                  int priority,
    00180                  int uses_fpu,
    00181                  void(*signal)(void));
    00182
    00183 int rt_task_init_cpuid(struct rt_task_struct *task,
    00184                        void (*rt_thread)(int),
    00185                        int data,
    00186                        int stack_size,
    00187                        int priority,
    00188                        int uses_fpu,
    00189                        void(*signal)(void),
    00190                        unsigned run_on_cpu);
    00191
    00192 void rt_set_runnable_on_cpus(struct rt_task_struct *task,
    00193                              unsigned long cpu_mask);
    00194
    00195 void rt_set_runnable_on_cpuid(struct rt_task_struct *task,
    00196                               unsigned cpuid);
    00197
    00198 void rt_set_sched_policy(struct rt_task_struct *task,
    00199                          int policy,
    00200                          int rr_quantum_ns);
    00201
    00202 int rt_task_delete(struct rt_task_struct *task);
    00203
    00204 int rt_get_task_state(struct rt_task_struct *task);
    00205
    00206 void rt_gettimeorig(RTIME time_orig[]);
    00207
    00208 int rt_get_timer_cpu(void);
    00209
    00210 int rt_is_hard_timer_running(void);
    00211
    00212 void rt_set_periodic_mode(void);
    00213
    00214 void rt_set_oneshot_mode(void);
    00215
    00216 RTIME start_rt_timer(int period);
    00217
    00218 #define start_rt_timer_ns(period) start_rt_timer(nano2count((period)))
    00219
    00220 void start_rt_apic_timers(struct apic_timer_setup_data *setup_mode,
    00221                           unsigned rcvr_jiffies_cpuid);
    00222
    00223 void stop_rt_timer(void);
    00224
    00225 struct rt_task_struct *rt_whoami(void);
    00226
    00227 int rt_sched_type(void);
    00228
    00229 int rt_task_signal_handler(struct rt_task_struct *task,
    00230                            void (*handler)(void));
    00231
    00232 int rt_task_use_fpu(struct rt_task_struct *task,
    00233                     int use_fpu_flag);
    00234
    00235 void rt_linux_use_fpu(int use_fpu_flag);
    00236
    00237 void rt_preempt_always(int yes_no);
    00238
    00239 void rt_preempt_always_cpuid(int yes_no,
    00240                              unsigned cpuid);
    00241
    00242 RTIME count2nano(RTIME timercounts);
    00243
    00244 RTIME nano2count(RTIME nanosecs);
    00245
    00246 RTIME count2nano_cpuid(RTIME timercounts,
    00247                        unsigned cpuid);
    00248
    00249 RTIME nano2count_cpuid(RTIME nanosecs,
    00250                        unsigned cpuid);
    00251
    00252 RTIME rt_get_time(void);
    00253
    00254 RTIME rt_get_time_cpuid(unsigned cpuid);
    00255
    00256 RTIME rt_get_time_ns(void);
    00257
    00258 RTIME rt_get_time_ns_cpuid(unsigned cpuid);
    00259
    00260 RTIME rt_get_cpu_time_ns(void);
    00261
    00262 int rt_get_prio(struct rt_task_struct *task);
    00263
    00264 int rt_get_inher_prio(struct rt_task_struct *task);
    00265
    00266 void rt_spv_RMS(int cpuid);
    00267
    00268 int rt_change_prio(struct rt_task_struct *task,
    00269                    int priority);
    00270
    00271 void rt_sched_lock(void);
    00272
    00273 void rt_sched_unlock(void);
    00274
    00275 void rt_task_yield(void);
    00276
    00277 int rt_task_suspend(struct rt_task_struct *task);
    00278
    00279 int rt_task_suspend_if(struct rt_task_struct *task);
    00280
    00281 int rt_task_suspend_until(struct rt_task_struct *task, RTIME until);
    00282
    00283 int rt_task_suspend_timed(struct rt_task_struct *task, RTIME delay);
    00284
    00285 int rt_task_resume(struct rt_task_struct *task);
    00286
    00287 int rt_irq_wait(unsigned irq);
    00288
    00289 int rt_irq_wait_if(unsigned irq);
    00290
    00291 int rt_irq_wait_until(unsigned irq, RTIME until);
    00292
    00293 int rt_irq_wait_timed(unsigned irq, RTIME delay);
    00294
    00295 void rt_irq_signal(unsigned irq);
    00296
    00297 int rt_request_irq_task (unsigned irq, void *handler, int type, int affine2task);
    00298
    00299 int rt_release_irq_task (unsigned irq);
    00300
    00301 int rt_task_make_periodic_relative_ns(struct rt_task_struct *task,
    00302                                       RTIME start_delay,
    00303                                       RTIME period);
    00304
    00305 int rt_task_make_periodic(struct rt_task_struct *task,
    00306                           RTIME start_time,
    00307                           RTIME period);
    00308
    00309 void rt_task_set_resume_end_times(RTIME resume,
    00310                                   RTIME end);
    00311
    00312 int rt_set_resume_time(struct rt_task_struct *task,
    00313                        RTIME new_resume_time);
    00314
    00315 int rt_set_period(struct rt_task_struct *task,
    00316                   RTIME new_period);
    00317
    00318 void rt_task_wait_period(void);
    00319
    00320 void rt_schedule(void);
    00321
    00322 RTIME next_period(void);
    00323
    00324 void rt_busy_sleep(int nanosecs);
    00325
    00326 void rt_sleep(RTIME delay);
    00327
    00328 void rt_sleep_until(RTIME time);
    00329
    00330 int rt_task_wakeup_sleeping(struct rt_task_struct *task);
    00331
    00332 struct rt_task_struct *rt_named_task_init(const char *task_name,
    00333                                           void (*thread)(int),
    00334                                           int data,
    00335                                           int stack_size,
    00336                                           int prio,
    00337                                           int uses_fpu,
    00338                                           void(*signal)(void));
    00339
    00340 struct rt_task_struct *rt_named_task_init_cpuid(const char *task_name,
    00341                                                 void (*thread)(int),
    00342                                                 int data,
    00343                                                 int stack_size,
    00344                                                 int prio,
    00345                                                 int uses_fpu,
    00346                                                 void(*signal)(void),
    00347                                                 unsigned run_on_cpu);
    00348
    00349 int rt_named_task_delete(struct rt_task_struct *task);
    00350
    00351 RT_TRAP_HANDLER rt_set_task_trap_handler(struct rt_task_struct *task,
    00352                                          unsigned vec,
    00353                                          RT_TRAP_HANDLER handler);
    00354
    00355 static inline RTIME timeval2count(struct timeval *t)
    00356 {
    00357         return nano2count(t->tv_sec*1000000000LL + t->tv_usec*1000);
    00358 }
    00359
    00360 static inline void count2timeval(RTIME rt, struct timeval *t)
    00361 {
    00362         t->tv_sec = ulldiv(count2nano(rt), 1000000000, (unsigned long *)&t->tv_usec);
    00363         t->tv_usec /= 1000;
    00364 }
    00365
    00366 static inline RTIME timespec2count(const struct timespec *t)
    00367 {
    00368         return nano2count(t->tv_sec*1000000000LL + t->tv_nsec);
    00369 }
    00370
    00371 static inline void count2timespec(RTIME rt, struct timespec *t)
    00372 {
    00373         t->tv_sec = ulldiv(count2nano(rt), 1000000000, (unsigned long *)&t->tv_nsec);
    00374 }
    00375
    00376 static inline RTIME timespec2nanos(const struct timespec *t)
    00377 {
    00378         return t->tv_sec*1000000000LL + t->tv_nsec;
    00379 }
    00380
    00381 static inline void nanos2timespec(RTIME rt, struct timespec *t)
    00382 {
    00383         t->tv_sec = ulldiv(rt, 1000000000, (unsigned long *)&t->tv_nsec);
    00384 }
    00385
    00386 #ifdef __cplusplus
    00387 }
    00388 #else /* !__cplusplus */
    00389
    00390 /* FIXME: These calls should move to rtai_schedcore.h */
    00391
    00392 RT_TASK *rt_get_base_linux_task(RT_TASK **base_linux_task);
    00393
    00394 RT_TASK *rt_alloc_dynamic_task(void);
    00395
    00396 void rt_enq_ready_edf_task(RT_TASK *ready_task);
    00397
    00398 void rt_enq_ready_task(RT_TASK *ready_task);
    00399
    00400 int rt_renq_ready_task(RT_TASK *ready_task,
    00401                        int priority);
    00402
    00403 void rt_rem_ready_task(RT_TASK *task);
    00404
    00405 void rt_rem_ready_current(RT_TASK *rt_current);
    00406
    00407 void rt_enq_timed_task(RT_TASK *timed_task);
    00408
    00409 void rt_rem_timed_task(RT_TASK *task);
    00410
    00411 void rt_dequeue_blocked(RT_TASK *task);
    00412
    00413 RT_TASK **rt_register_watchdog(RT_TASK *wdog,
    00414                                int cpuid);
    00415
    00416 void rt_deregister_watchdog(RT_TASK *wdog,
    00417                             int cpuid);
    00418
    00419 #endif /* __cplusplus */
    00420
    00421 #endif /* __KERNEL__ */
    00422
    00423 #if !defined(__KERNEL__) || defined(__cplusplus)
    00424
    00425 typedef struct rt_task_struct {
    00426     int opaque;
    00427 } RT_TASK;
    00428
    00429 typedef struct QueueBlock {
    00430     int opaque;
    00431 } QBLK;
    00432
    00433 typedef struct QueueHook {
    00434     int opaque;
    00435 } QHOOK;
    00436
    00437 #endif /* !__KERNEL__ || __cplusplus */
    00438
    00439 #endif /* !_RTAI_SCHED_H */