aboutsummaryrefslogtreecommitdiffstats
path: root/include/litmus/rt_domain.h
blob: 8dc8efb24d986d6ed80c98cc9529d35cc1a00326 (plain) (blame)
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
/* CLEANUP: Add comments and make it less messy.
 *
 */

#ifndef __UNC_RT_DOMAIN_H__
#define __UNC_RT_DOMAIN_H__

#include <litmus/bheap.h>
#include <litmus/binheap.h>
#include <litmus/reservations/ext_reservation.h>

#define RELEASE_QUEUE_SLOTS 127 /* prime */

struct _rt_domain;

typedef int (*check_resched_needed_t)(struct _rt_domain *rt);
typedef void (*release_jobs_t)(struct _rt_domain *rt, struct bheap* tasks);

struct release_queue {
	struct list_head slot[RELEASE_QUEUE_SLOTS];
	struct binheap queue;
	volatile lt_t earliest_release;
};

typedef struct _rt_domain {
	/* runnable rt tasks are in here */
	raw_spinlock_t 			ready_lock;
	struct bheap	 		ready_queue;

	/* real-time tasks waiting for release are in here */
	raw_spinlock_t 			release_lock;
	struct release_queue 		release_queue;

#ifdef CONFIG_RELEASE_MASTER
	int				release_master;
	/* used to delegate releases */
	struct hrtimer_start_on_info	info;
#endif

	/* release timer for earliest heap in release_queue */
	struct hrtimer			timer;

	/* for moving tasks to the release queue */
	raw_spinlock_t			tobe_lock;
	struct list_head		tobe_released;

	/* how do we check if we need to kick another CPU? */
	check_resched_needed_t		check_resched;

	/* how do we release jobs? */
	release_jobs_t			release_jobs;

	/* how are tasks ordered in the ready queue? */
	bheap_prio_t			order;
} rt_domain_t;

struct release_heap {
	/* for enqueueing into release_queue */
	struct binheap_node node;
	/* list_head for per-time-slot list */
	struct list_head		list;
	lt_t				release_time;
	/* all tasks to be released at release_time */
	struct bheap			heap;
	/* list of all tasks, used for release callback in reservation */
	struct list_head		list_head;
};

void domain_suspend_releases(rt_domain_t* rt);
void domain_resume_releases(rt_domain_t* rt);

/* caller must hold release lock */
struct release_heap* get_release_heap_res(rt_domain_t *rt,
						struct reservation* res,
						int use_task_heap);

static inline struct task_struct* __next_ready(rt_domain_t* rt)
{
	struct bheap_node *hn = bheap_peek(rt->order, &rt->ready_queue);
	if (hn)
		return bheap2task(hn);
	else
		return NULL;
}

static inline struct reservation* __next_ready_res(rt_domain_t* rt)
{
	struct bheap_node *hn = bheap_peek(rt->order, &rt->ready_queue);
	if (hn)
		return bheap2res(hn);
	else
		return NULL;
}

void rt_domain_init(rt_domain_t *rt, bheap_prio_t order,
		    check_resched_needed_t check,
		    release_jobs_t relase);

void __add_ready(rt_domain_t* rt, struct task_struct *new);
void __merge_ready(rt_domain_t* rt, struct bheap *tasks);
void __add_release(rt_domain_t* rt, struct task_struct *task);

void __add_ready_res(rt_domain_t* rt, struct reservation* new);
void __add_release_res(rt_domain_t* rt, struct reservation* res);

static inline struct task_struct* __take_ready(rt_domain_t* rt)
{
	struct bheap_node* hn = bheap_take(rt->order, &rt->ready_queue);
	if (hn)
		return bheap2task(hn);
	else
		return NULL;
}

static inline struct reservation* __take_ready_res(rt_domain_t* rt)
{
	struct bheap_node* hn = bheap_take(rt->order, &rt->ready_queue);
	if (hn)
		return bheap2res(hn);
	else
		return NULL;
}

static inline struct task_struct* __peek_ready(rt_domain_t* rt)
{
	struct bheap_node* hn = bheap_peek(rt->order, &rt->ready_queue);
	if (hn)
		return bheap2task(hn);
	else
		return NULL;
}

static inline struct reservation* __peek_ready_res(rt_domain_t* rt)
{
	struct bheap_node* hn = bheap_peek(rt->order, &rt->ready_queue);
	if (hn)
		return bheap2res(hn);
	else
		return NULL;
}

static inline int  is_queued(struct task_struct *t)
{
	BUG_ON(!tsk_rt(t)->heap_node);
	return bheap_node_in_heap(tsk_rt(t)->heap_node);
}

static inline int is_queued_res(struct reservation* res)
{
	BUG_ON(!res->heap_node);
	return bheap_node_in_heap(res->heap_node);
}

static inline void remove(rt_domain_t* rt, struct task_struct *t)
{
	bheap_delete(rt->order, &rt->ready_queue, tsk_rt(t)->heap_node);
}

static inline void remove_res(rt_domain_t* rt, struct reservation* res)
{
	bheap_delete(rt->order, &rt->ready_queue, res->heap_node);
}

static inline void add_ready(rt_domain_t* rt, struct task_struct *new)
{
	unsigned long flags;
	/* first we need the write lock for rt_ready_queue */
	raw_spin_lock_irqsave(&rt->ready_lock, flags);
	__add_ready(rt, new);
	raw_spin_unlock_irqrestore(&rt->ready_lock, flags);
}

static inline void add_ready_res(rt_domain_t* rt, struct reservation *new)
{
	unsigned long flags;
	/* first we need the write lock for rt_ready_queue */
	raw_spin_lock_irqsave(&rt->ready_lock, flags);
	__add_ready_res(rt, new);
	raw_spin_unlock_irqrestore(&rt->ready_lock, flags);
}

static inline void merge_ready(rt_domain_t* rt, struct bheap* tasks)
{
	unsigned long flags;
	raw_spin_lock_irqsave(&rt->ready_lock, flags);
	__merge_ready(rt, tasks);
	raw_spin_unlock_irqrestore(&rt->ready_lock, flags);
}

static inline void merge_ready_res(rt_domain_t* rt, struct bheap* tasks)
{
	unsigned long flags;
	raw_spin_lock_irqsave(&rt->ready_lock, flags);
	__merge_ready(rt, tasks);
	raw_spin_unlock_irqrestore(&rt->ready_lock, flags);
}

static inline struct task_struct* take_ready(rt_domain_t* rt)
{
	unsigned long flags;
	struct task_struct* ret;
	/* first we need the write lock for rt_ready_queue */
	raw_spin_lock_irqsave(&rt->ready_lock, flags);
	ret = __take_ready(rt);
	raw_spin_unlock_irqrestore(&rt->ready_lock, flags);
	return ret;
}

static inline struct reservation* take_ready_res(rt_domain_t* rt)
{
	unsigned long flags;
	struct reservation* ret;
	/* first we need the write lock for rt_ready_queue */
	raw_spin_lock_irqsave(&rt->ready_lock, flags);
	ret = __take_ready_res(rt);
	raw_spin_unlock_irqrestore(&rt->ready_lock, flags);
	return ret;
}

static inline void add_release(rt_domain_t* rt, struct task_struct *task)
{
	unsigned long flags;
	raw_spin_lock_irqsave(&rt->tobe_lock, flags);
	__add_release(rt, task);
	raw_spin_unlock_irqrestore(&rt->tobe_lock, flags);
}

static inline void add_release_res(rt_domain_t* rt, struct reservation* res)
{
	unsigned long flags;
	raw_spin_lock_irqsave(&rt->tobe_lock, flags);
	__add_release_res(rt, res);
	raw_spin_unlock_irqrestore(&rt->tobe_lock, flags);
}

#ifdef CONFIG_RELEASE_MASTER
void __add_release_on(rt_domain_t* rt, struct task_struct *task,
		      int target_cpu);

void __add_release_res_on(rt_domain_t* rt, struct reservation* res,
			  int target_cpu);

static inline void add_release_on(rt_domain_t* rt,
				  struct task_struct *task,
				  int target_cpu)
{
	unsigned long flags;
	raw_spin_lock_irqsave(&rt->tobe_lock, flags);
	__add_release_on(rt, task, target_cpu);
	raw_spin_unlock_irqrestore(&rt->tobe_lock, flags);
}

static inline void add_release_res_on(rt_domain_t* rt,
				  struct reservation* res,
				  int target_cpu)
{
	unsigned long flags;
	raw_spin_lock_irqsave(&rt->tobe_lock, flags);
	__add_release_res_on(rt, res, target_cpu);
	raw_spin_unlock_irqrestore(&rt->tobe_lock, flags);
}
#endif

static inline int __jobs_pending(rt_domain_t* rt)
{
	return !bheap_empty(&rt->ready_queue);
}

static inline int jobs_pending(rt_domain_t* rt)
{
	unsigned long flags;
	int ret;
	/* first we need the write lock for rt_ready_queue */
	raw_spin_lock_irqsave(&rt->ready_lock, flags);
	ret = !bheap_empty(&rt->ready_queue);
	raw_spin_unlock_irqrestore(&rt->ready_lock, flags);
	return ret;
}

#endif