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

#ifndef __UNC_RT_DOMAIN_H__
#define __UNC_RT_DOMAIN_H__

#include <litmus/bheap.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 {
	/* each slot maintains a list of release heaps sorted
	 * by release time */
	struct list_head		slot[RELEASE_QUEUE_SLOTS];
};

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;
#endif

	/* 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 {
	/* 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;
	/* used to trigger the release */
	struct hrtimer			timer;

#ifdef CONFIG_RELEASE_MASTER
	/* used to delegate releases */
	struct hrtimer_start_on_info	info;
#endif
	/* required for the timer callback */
	rt_domain_t*			dom;
};


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;
}

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);

static inline struct task_struct* __take_ready(rt_domain_t* rt)
{
	struct bheap_node* hn = bheap_take(rt->order, &rt->ready_queue);
	if (hn) {
		struct task_struct* taken = bheap2task(hn);
		TRACE("rt: taking %s/%d [inh_task: %s/%d]\n",
			(taken) ? taken->comm : "nil",
			(taken) ? taken->pid : -1,
			(taken && tsk_rt(taken)->inh_task) ?
				tsk_rt(taken)->inh_task->comm : "nil",
			(taken && tsk_rt(taken)->inh_task) ?
				tsk_rt(taken)->inh_task->pid : -1);

		return bheap2task(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 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 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 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 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 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 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);
}

#ifdef CONFIG_RELEASE_MASTER
void __add_release_on(rt_domain_t* rt, struct task_struct *task,
		      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);
}
#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