# rtai_tasklets.h

Go to the documentation of this file.

    00001
    00026 #ifndef _RTAI_TASKLETS_H
    00027 #define _RTAI_TASKLETS_H
    00028
    00033 #include <rtai_types.h>
    00034
    00035 #define TSKIDX  1
    00036
    00037 #define INIT             0
    00038 #define DELETE           1
    00039 #define TASK_INSERT      2
    00040 #define TASK_REMOVE      3
    00041 #define USE_FPU          4
    00042 #define TIMER_INSERT     5
    00043 #define TIMER_REMOVE     6
    00044 #define SET_TASKLETS_PRI 7
    00045 #define SET_FIR_TIM      8
    00046 #define SET_PER          9
    00047 #define SET_HDL         10
    00048 #define SET_DAT         11
    00049 #define EXEC_TASKLET    12
    00050 #define WAIT_IS_HARD    13
    00051 #define SET_TSK_PRI     14
    00052 #define REG_TASK        15
    00053
    00054 struct rt_task_struct;
    00055
    00056 struct rt_tasklet_struct {
    00057
    00058     struct rt_tasklet_struct *next, *prev;
    00059     int priority, uses_fpu;
    00060     RTIME firing_time, period;
    00061     void (*handler)(unsigned long);
    00062     unsigned long data, id;
    00063     int thread;
    00064     struct rt_task_struct *task;
    00065     struct rt_tasklet_struct *usptasklet;
    00066 };
    00067
    00068 #define TASKLET_STACK_SIZE  8196
    00069
    00070 #ifdef __KERNEL__
    00071
    00072 #ifdef __cplusplus
    00073 extern "C" {
    00074 #endif /* !__cplusplus */
    00075
    00076 int __rtai_tasklets_init(void);
    00077
    00078 void __rtai_tasklets_exit(void);
    00079
    00080 struct rt_tasklet_struct *rt_init_tasklet(void);
    00081
    00082 int rt_delete_tasklet(struct rt_tasklet_struct *tasklet);
    00083
    00084 int rt_insert_tasklet(struct rt_tasklet_struct *tasklet,
    00085                       int priority,
    00086                       void (*handler)(unsigned long),
    00087                       unsigned long data,
    00088                       unsigned long id,
    00089                       int pid);
    00090
    00091 void rt_remove_tasklet(struct rt_tasklet_struct *tasklet);
    00092
    00093 struct rt_tasklet_struct *rt_find_tasklet_by_id(unsigned long id);
    00094
    00095 int rt_exec_tasklet(struct rt_tasklet_struct *tasklet);
    00096
    00097 void rt_set_tasklet_priority(struct rt_tasklet_struct *tasklet,
    00098                              int priority);
    00099
    00100 int rt_set_tasklet_handler(struct rt_tasklet_struct *tasklet,
    00101                            void (*handler)(unsigned long));
    00102
    00103 #define rt_fast_set_tasklet_handler(t, h) do { (t)->handler = (h); } while (0)
    00104
    00105 void rt_set_tasklet_data(struct rt_tasklet_struct *tasklet,
    00106                          unsigned long data);
    00107
    00108 #define rt_fast_set_tasklet_data(t, d) \
    00109 do { \
    00110    (t)->data = (d); \
    00111 } while (0)
    00112
    00135 struct rt_task_struct *rt_tasklet_use_fpu(struct rt_tasklet_struct *tasklet,
    00136                                           int use_fpu);
    00137
    00154 #define rt_init_timer rt_init_tasklet
    00155
    00172 #define rt_delete_timer rt_delete_tasklet
    00173
    00174 int rt_insert_timer(struct rt_tasklet_struct *timer,
    00175                     int priority,
    00176                     RTIME firing_time,
    00177                     RTIME period,
    00178                     void (*handler)(unsigned long),
    00179                     unsigned long data,
    00180                     int pid);
    00181
    00182 void rt_remove_timer(struct rt_tasklet_struct *timer);
    00183
    00184 void rt_set_timer_priority(struct rt_tasklet_struct *timer,
    00185                            int priority);
    00186
    00187 void rt_set_timer_firing_time(struct rt_tasklet_struct *timer,
    00188                               RTIME firing_time);
    00189
    00190 void rt_set_timer_period(struct rt_tasklet_struct *timer,
    00191                          RTIME period);
    00192
    00193 #define rt_fast_set_timer_period(t, p) \
    00194 do { \
    00195    (t)->period = (p); \
    00196 } while (0)
    00197
    00221 #define rt_set_timer_handler rt_set_tasklet_handler
    00222
    00223 #define rt_fast_set_timer_handler(t, h) do { (t)->handler = (h); } while (0)
    00224
    00247 #define rt_set_timer_data rt_set_tasklet_data
    00248
    00249 #define rt_fast_set_timer_data(t, d) do { (t)->data = (d); } while (0)
    00250
    00251 #define rt_timer_use_fpu rt_tasklet_use_fpu
    00252
    00253 void rt_wait_tasklet_is_hard(struct rt_tasklet_struct *tasklet,
    00254                              int thread);
    00255
    00256 void rt_register_task(struct rt_tasklet_struct *tasklet,
    00257                       struct rt_tasklet_struct *usptasklet,
    00258                       struct rt_task_struct *task);
    00259
    00260 #ifdef __cplusplus
    00261 }
    00262 #endif /* __cplusplus */
    00263
    00264 #else /* !__KERNEL__ */
    00265
    00266 #include <sys/types.h>
    00267 #include <sys/wait.h>
    00268 #include <sys/mman.h>
    00269 #include <stdarg.h>
    00270 #include <rtai_lxrt.h>
    00271
    00272 #ifndef __SUPPORT_TASKLET__
    00273 #define __SUPPORT_TASKLET__
    00274
    00275 static int support_tasklet(void *tasklet)
    00276 {
    00277         RT_TASK *task;
    00278         struct rt_tasklet_struct usptasklet;
    00279         struct { struct rt_tasklet_struct *tasklet; void *handler; } arg = { tasklet, };
    00280
    00281         if (!(task = rt_thread_init((unsigned long)arg.tasklet, 98, 0, SCHED_FIFO, 0xF))) {
    00282                 printf("CANNOT INIT SUPPORT TASKLET\n");
    00283                 return -1;
    00284         } else {
    00285                 struct { struct rt_tasklet_struct *tasklet, *usptasklet; RT_TASK *task; } reg = { arg.tasklet, &usptasklet, task };
    00286                 rtai_lxrt(TSKIDX, sizeof(reg), REG_TASK, &reg);
    00287         }
    00288
    00289         mlockall(MCL_CURRENT | MCL_FUTURE);
    00290         rt_make_hard_real_time();
    00291         while (1) {
    00292                 rt_task_suspend(task);
    00293                 if ((arg.handler = (void*)usptasklet.handler)) {
    00294                         rtai_lxrt(TSKIDX, SIZARG, SET_HDL, &arg);
    00295                         usptasklet.handler(usptasklet.data);
    00296                 } else {
    00297                         break;
    00298                 }
    00299         }
    00300         rt_make_soft_real_time();
    00301         rt_task_delete(task);
    00302
    00303         return 0;
    00304 }
    00305 #endif /* __SUPPORT_TASKLET__ */
    00306
    00307 #ifdef __cplusplus
    00308 extern "C" {
    00309 #endif /* __cplusplus */
    00310
    00311 RTAI_PROTO(struct rt_tasklet_struct *, rt_init_tasklet,(void))
    00312 {
    00313         struct { void *tasklet; int thread; } arg;
    00314
    00315         arg.tasklet = (struct rt_tasklet_struct*)rtai_lxrt(TSKIDX, SIZARG, INIT, &arg).v[LOW];
    00316         arg.thread = rt_thread_create(support_tasklet, arg.tasklet, TASKLET_STACK_SIZE);
    00317 //      arg.thread = clone(support_tasklet, sp + TASKLET_STACK_SIZE - 1, CLONE_VM | CLONE_FS | CLONE_FILES, arg.tasklet);
    00318         rtai_lxrt(TSKIDX, SIZARG, WAIT_IS_HARD, &arg);
    00319
    00320         return arg.tasklet;
    00321 }
    00322
    00323 #define rt_init_timer rt_init_tasklet
    00324
    00325 RTAI_PROTO(void, rt_delete_tasklet,(struct rt_tasklet_struct *tasklet))
    00326 {
    00327         int thread;
    00328         struct { struct rt_tasklet_struct *tasklet; } arg = { tasklet };
    00329         if ((thread = rtai_lxrt(TSKIDX, SIZARG, DELETE, &arg).i[LOW])) {
    00330                 rt_thread_join(thread);
    00331         }
    00332 }
    00333
    00334 #define rt_delete_timer rt_delete_tasklet
    00335
    00336 RTAI_PROTO(int, rt_insert_timer,(struct rt_tasklet_struct *timer,
    00337                                  int priority,
    00338                                  RTIME firing_time,
    00339                                  RTIME period,
    00340                                  void (*handler)(unsigned long),
    00341                                  unsigned long data,
    00342                                  int pid))
    00343 {
    00344         struct { struct rt_tasklet_struct *timer; int priority; RTIME firing_time;
    00345             RTIME period; void (*handler)(unsigned long); unsigned long data; int pid; } arg =
    00346                 { timer, priority, firing_time, period, handler, data, pid };
    00347         return rtai_lxrt(TSKIDX, SIZARG, TIMER_INSERT, &arg).i[LOW];
    00348 }
    00349
    00350 RTAI_PROTO(void, rt_remove_timer,(struct rt_tasklet_struct *timer))
    00351 {
    00352         struct { struct rt_tasklet_struct *timer; } arg = { timer };
    00353         rtai_lxrt(TSKIDX, SIZARG, TIMER_REMOVE, &arg);
    00354 }
    00355
    00356 RTAI_PROTO(void, rt_set_timer_priority,(struct rt_tasklet_struct *timer, int priority))
    00357 {
    00358         struct { struct rt_tasklet_struct *timer; int priority; } arg = { timer, priority };
    00359         rtai_lxrt(TSKIDX, SIZARG, SET_TASKLETS_PRI, &arg);
    00360 }
    00361
    00362 RTAI_PROTO(void, rt_set_timer_firing_time,(struct rt_tasklet_struct *timer, RTIME firing_time))
    00363 {
    00364         struct { struct rt_tasklet_struct *timer; RTIME firing_time; } arg = { timer, firing_time };
    00365         rtai_lxrt(TSKIDX, SIZARG, SET_FIR_TIM, &arg);
    00366 }
    00367
    00368 RTAI_PROTO(void, rt_set_timer_period,(struct rt_tasklet_struct *timer, RTIME period))
    00369 {
    00370         struct { struct rt_tasklet_struct *timer; RTIME period; } arg = { timer, period };
    00371         rtai_lxrt(TSKIDX, SIZARG, SET_PER, &arg);
    00372 }
    00373
    00374 RTAI_PROTO(int, rt_set_tasklet_handler,(struct rt_tasklet_struct *tasklet, void (*handler)(unsigned long)))
    00375 {
    00376         struct { struct rt_tasklet_struct *tasklet; void (*handler)(unsigned long); } arg = { tasklet, handler };
    00377         return rtai_lxrt(TSKIDX, SIZARG, SET_HDL, &arg).i[LOW];
    00378 }
    00379
    00380 #define rt_set_timer_handler rt_set_tasklet_handler
    00381
    00382 RTAI_PROTO(void, rt_set_tasklet_data,(struct rt_tasklet_struct *tasklet, unsigned long data))
    00383 {
    00384         struct { struct rt_tasklet_struct *tasklet; unsigned long data; } arg = { tasklet, data };
    00385         rtai_lxrt(TSKIDX, SIZARG, SET_DAT, &arg);
    00386 }
    00387
    00388 #define rt_set_timer_data rt_set_tasklet_data
    00389
    00390 RTAI_PROTO(RT_TASK *, rt_tasklet_use_fpu,(struct rt_tasklet_struct *tasklet, int use_fpu))
    00391 {
    00392         RT_TASK *task;
    00393         struct { struct rt_tasklet_struct *tasklet; int use_fpu; } arg = { tasklet, use_fpu };
    00394         if ((task = (RT_TASK*)rtai_lxrt(TSKIDX, SIZARG, USE_FPU, &arg).v[LOW])) {
    00395                 rt_task_use_fpu(task, use_fpu);
    00396         }
    00397         return task;
    00398 }
    00399
    00400 #define rt_timer_use_fpu rt_tasklet_use_fpu
    00401
    00402 RTAI_PROTO(int, rt_insert_tasklet,(struct rt_tasklet_struct *tasklet,
    00403                                    int priority,
    00404                                    void (*handler)(unsigned long),
    00405                                    unsigned long data,
    00406                                    unsigned long id,
    00407                                    int pid))
    00408 {
    00409         struct { struct rt_tasklet_struct *tasklet; int priority; void (*handler)(unsigned long);
    00410             unsigned long data; unsigned long id; int pid; } arg = { tasklet, priority, handler, data, id, pid };
    00411         return rtai_lxrt(TSKIDX, SIZARG, TASK_INSERT, &arg).i[LOW];
    00412 }
    00413
    00414 RTAI_PROTO(void, rt_set_tasklet_priority,(struct rt_tasklet_struct *tasklet, int priority))
    00415 {
    00416         struct { struct rt_tasklet_struct *tasklet; int priority; } arg = { tasklet, priority };
    00417         rtai_lxrt(TSKIDX, SIZARG, SET_TSK_PRI, &arg);
    00418 }
    00419
    00420 RTAI_PROTO(void, rt_remove_tasklet,(struct rt_tasklet_struct *tasklet))
    00421 {
    00422         struct { struct rt_tasklet_struct *tasklet; } arg = { tasklet };
    00423         rtai_lxrt(TSKIDX, SIZARG, TASK_REMOVE, &arg);
    00424 }
    00425
    00426 RTAI_PROTO(int, rt_exec_tasklet,(struct rt_tasklet_struct *tasklet))
    00427 {
    00428         struct { struct rt_tasklet_struct *tasklet; } arg = { tasklet };
    00429         return rtai_lxrt(TSKIDX, SIZARG, EXEC_TASKLET, &arg).i[LOW];
    00430 }
    00431
    00432 #ifdef __cplusplus
    00433 }
    00434 #endif /* __cplusplus */
    00435
    00436 #endif /* __KERNEL__ */
    00437
    00440 #endif /* !_RTAI_TASKLETS_H */