diff options
Diffstat (limited to 'src/task.c')
-rw-r--r-- | src/task.c | 43 |
1 files changed, 33 insertions, 10 deletions
@@ -1,30 +1,31 @@ | |||
1 | #include <stdlib.h> | 1 | #include <stdlib.h> |
2 | #include <stdio.h> | 2 | #include <stdio.h> |
3 | #include <unistd.h> | 3 | #include <unistd.h> |
4 | #include <errno.h> | ||
4 | 5 | ||
5 | #include "litmus.h" | 6 | #include "litmus.h" |
6 | #include "internal.h" | 7 | #include "internal.h" |
7 | 8 | ||
8 | static void tperrorx(char* msg) | 9 | static void tperrorx(char* msg) |
9 | { | 10 | { |
10 | fprintf(stderr, | 11 | fprintf(stderr, |
11 | "Task %d: %s: %m", | 12 | "Task %d: %s: %m", |
12 | getpid(), msg); | 13 | gettid(), msg); |
13 | exit(-1); | 14 | exit(-1); |
14 | } | 15 | } |
15 | 16 | ||
16 | /* common launch routine */ | 17 | /* common launch routine */ |
17 | int __launch_rt_task(rt_fn_t rt_prog, void *rt_arg, rt_setup_fn_t setup, | 18 | int __launch_rt_task(rt_fn_t rt_prog, void *rt_arg, rt_setup_fn_t setup, |
18 | void* setup_arg) | 19 | void* setup_arg) |
19 | { | 20 | { |
20 | int ret; | 21 | int ret; |
21 | int rt_task = fork(); | 22 | int rt_task = fork(); |
22 | 23 | ||
23 | if (rt_task == 0) { | 24 | if (rt_task == 0) { |
24 | /* we are the real-time task | 25 | /* we are the real-time task |
25 | * launch task and die when it is done | 26 | * launch task and die when it is done |
26 | */ | 27 | */ |
27 | rt_task = getpid(); | 28 | rt_task = gettid(); |
28 | ret = setup(rt_task, setup_arg); | 29 | ret = setup(rt_task, setup_arg); |
29 | if (ret < 0) | 30 | if (ret < 0) |
30 | tperrorx("could not setup task parameters"); | 31 | tperrorx("could not setup task parameters"); |
@@ -38,14 +39,14 @@ int __launch_rt_task(rt_fn_t rt_prog, void *rt_arg, rt_setup_fn_t setup, | |||
38 | } | 39 | } |
39 | 40 | ||
40 | int __create_rt_task(rt_fn_t rt_prog, void *arg, int cpu, int wcet, int period, | 41 | int __create_rt_task(rt_fn_t rt_prog, void *arg, int cpu, int wcet, int period, |
41 | task_class_t class) | 42 | task_class_t class) |
42 | { | 43 | { |
43 | struct rt_task params; | 44 | struct rt_task params; |
44 | params.cpu = cpu; | 45 | params.cpu = cpu; |
45 | params.period = period; | 46 | params.period = period; |
46 | params.exec_cost = wcet; | 47 | params.exec_cost = wcet; |
47 | params.cls = class; | 48 | params.cls = class; |
48 | return __launch_rt_task(rt_prog, arg, | 49 | return __launch_rt_task(rt_prog, arg, |
49 | (rt_setup_fn_t) set_rt_task_param, ¶ms); | 50 | (rt_setup_fn_t) set_rt_task_param, ¶ms); |
50 | } | 51 | } |
51 | 52 | ||
@@ -53,3 +54,25 @@ int create_rt_task(rt_fn_t rt_prog, void *arg, int cpu, int wcet, int period) { | |||
53 | return __create_rt_task(rt_prog, arg, cpu, wcet, period, RT_CLASS_HARD); | 54 | return __create_rt_task(rt_prog, arg, cpu, wcet, period, RT_CLASS_HARD); |
54 | } | 55 | } |
55 | 56 | ||
57 | |||
58 | #define SCHED_NORMAL 0 | ||
59 | #define SCHED_LITMUS 6 | ||
60 | |||
61 | int task_mode(int mode) | ||
62 | { | ||
63 | int prio = 0; | ||
64 | int me = gettid(); | ||
65 | int policy = sched_getscheduler(gettid()); | ||
66 | int old_mode = policy == SCHED_LITMUS ? LITMUS_RT_TASK : BACKGROUND_TASK; | ||
67 | |||
68 | if (old_mode == LITMUS_RT_TASK && mode == BACKGROUND_TASK) { | ||
69 | /* transition to normal task */ | ||
70 | return sched_setscheduler(me, SCHED_NORMAL, &prio); | ||
71 | } else if (old_mode == BACKGROUND_TASK && mode == LITMUS_RT_TASK) { | ||
72 | /* transition to RT task */ | ||
73 | return sched_setscheduler(me, SCHED_LITMUS, &prio); | ||
74 | } else { | ||
75 | errno = -EINVAL; | ||
76 | return -1; | ||
77 | } | ||
78 | } | ||