diff options
Diffstat (limited to 'litmus/sync.c')
-rw-r--r-- | litmus/sync.c | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/litmus/sync.c b/litmus/sync.c new file mode 100644 index 000000000000..5d180603f46b --- /dev/null +++ b/litmus/sync.c | |||
@@ -0,0 +1,152 @@ | |||
1 | /* litmus/sync.c - Support for synchronous and asynchronous task system releases. | ||
2 | * | ||
3 | * | ||
4 | */ | ||
5 | |||
6 | #include <asm/atomic.h> | ||
7 | #include <asm/uaccess.h> | ||
8 | #include <linux/spinlock.h> | ||
9 | #include <linux/list.h> | ||
10 | #include <linux/sched.h> | ||
11 | #include <linux/completion.h> | ||
12 | |||
13 | #include <litmus/litmus.h> | ||
14 | #include <litmus/sched_plugin.h> | ||
15 | #include <litmus/jobs.h> | ||
16 | |||
17 | #include <litmus/sched_trace.h> | ||
18 | |||
19 | struct ts_release_wait { | ||
20 | struct list_head list; | ||
21 | struct completion completion; | ||
22 | lt_t ts_release_time; | ||
23 | }; | ||
24 | |||
25 | #define DECLARE_TS_RELEASE_WAIT(symb) \ | ||
26 | struct ts_release_wait symb = \ | ||
27 | { \ | ||
28 | LIST_HEAD_INIT(symb.list), \ | ||
29 | COMPLETION_INITIALIZER_ONSTACK(symb.completion), \ | ||
30 | 0 \ | ||
31 | } | ||
32 | |||
33 | static LIST_HEAD(task_release_list); | ||
34 | static DEFINE_MUTEX(task_release_lock); | ||
35 | |||
36 | static long do_wait_for_ts_release(void) | ||
37 | { | ||
38 | DECLARE_TS_RELEASE_WAIT(wait); | ||
39 | |||
40 | long ret = -ERESTARTSYS; | ||
41 | |||
42 | if (mutex_lock_interruptible(&task_release_lock)) | ||
43 | goto out; | ||
44 | |||
45 | list_add(&wait.list, &task_release_list); | ||
46 | |||
47 | mutex_unlock(&task_release_lock); | ||
48 | |||
49 | /* We are enqueued, now we wait for someone to wake us up. */ | ||
50 | ret = wait_for_completion_interruptible(&wait.completion); | ||
51 | |||
52 | if (!ret) { | ||
53 | /* Completion succeeded, setup release time. */ | ||
54 | ret = litmus->wait_for_release_at( | ||
55 | wait.ts_release_time + get_rt_phase(current)); | ||
56 | } else { | ||
57 | /* We were interrupted, must cleanup list. */ | ||
58 | mutex_lock(&task_release_lock); | ||
59 | if (!wait.completion.done) | ||
60 | list_del(&wait.list); | ||
61 | mutex_unlock(&task_release_lock); | ||
62 | } | ||
63 | |||
64 | out: | ||
65 | return ret; | ||
66 | } | ||
67 | |||
68 | int count_tasks_waiting_for_release(void) | ||
69 | { | ||
70 | int task_count = 0; | ||
71 | struct list_head *pos; | ||
72 | |||
73 | mutex_lock(&task_release_lock); | ||
74 | |||
75 | list_for_each(pos, &task_release_list) { | ||
76 | task_count++; | ||
77 | } | ||
78 | |||
79 | mutex_unlock(&task_release_lock); | ||
80 | |||
81 | |||
82 | return task_count; | ||
83 | } | ||
84 | |||
85 | static long do_release_ts(lt_t start) | ||
86 | { | ||
87 | long task_count = 0; | ||
88 | |||
89 | struct list_head *pos, *safe; | ||
90 | struct ts_release_wait *wait; | ||
91 | |||
92 | if (mutex_lock_interruptible(&task_release_lock)) { | ||
93 | task_count = -ERESTARTSYS; | ||
94 | goto out; | ||
95 | } | ||
96 | |||
97 | TRACE("<<<<<< synchronous task system release >>>>>>\n"); | ||
98 | sched_trace_sys_release(&start); | ||
99 | litmus->synchronous_release_at(start); | ||
100 | |||
101 | task_count = 0; | ||
102 | list_for_each_safe(pos, safe, &task_release_list) { | ||
103 | wait = (struct ts_release_wait*) | ||
104 | list_entry(pos, struct ts_release_wait, list); | ||
105 | |||
106 | task_count++; | ||
107 | wait->ts_release_time = start; | ||
108 | complete(&wait->completion); | ||
109 | } | ||
110 | |||
111 | /* clear stale list */ | ||
112 | INIT_LIST_HEAD(&task_release_list); | ||
113 | |||
114 | mutex_unlock(&task_release_lock); | ||
115 | |||
116 | out: | ||
117 | return task_count; | ||
118 | } | ||
119 | |||
120 | |||
121 | asmlinkage long sys_wait_for_ts_release(void) | ||
122 | { | ||
123 | long ret = -EPERM; | ||
124 | struct task_struct *t = current; | ||
125 | |||
126 | if (is_realtime(t)) | ||
127 | ret = do_wait_for_ts_release(); | ||
128 | |||
129 | return ret; | ||
130 | } | ||
131 | |||
132 | #define ONE_MS 1000000 | ||
133 | |||
134 | asmlinkage long sys_release_ts(lt_t __user *__delay) | ||
135 | { | ||
136 | long ret; | ||
137 | lt_t delay; | ||
138 | lt_t start_time; | ||
139 | |||
140 | /* FIXME: check capabilities... */ | ||
141 | |||
142 | ret = copy_from_user(&delay, __delay, sizeof(delay)); | ||
143 | if (ret == 0) { | ||
144 | /* round up to next larger integral millisecond */ | ||
145 | start_time = litmus_clock(); | ||
146 | do_div(start_time, ONE_MS); | ||
147 | start_time *= ONE_MS; | ||
148 | ret = do_release_ts(start_time + delay); | ||
149 | } | ||
150 | |||
151 | return ret; | ||
152 | } | ||