2 * Copyright (C) 2006-2013 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file sched.c Paraslash's scheduling functions. */
21 static struct timeval now_struct
;
22 struct timeval
*now
= &now_struct
;
25 * Remove a task from the scheduler.
27 * \param t The task to remove.
29 * If the pre_select pointer of \a t is not \p NULL, it is removed from
30 * the pre_select list of the scheduler. Same goes for \a post_select.
32 static void unregister_task(struct task
*t
)
35 PARA_INFO_LOG("unregistering %s (%s)\n", t
->status
,
36 para_strerror(-t
->error
));
38 list_del(&t
->pre_select_node
);
40 list_del(&t
->post_select_node
);
43 static inline bool timeout_is_zero(struct sched
*s
)
45 struct timeval
*tv
= &s
->select_timeout
;
46 return tv
->tv_sec
== 0 && tv
->tv_usec
== 0;
49 static void sched_preselect(struct sched
*s
)
53 list_for_each_entry_safe(t
, tmp
, &s
->pre_select_list
, pre_select_node
) {
54 if (t
->notification
!= 0)
61 //#define SCHED_DEBUG 1
62 static inline void call_post_select(struct sched
*s
, struct task
*t
)
65 t
->error
= t
->post_select(s
, t
);
67 struct timeval t1
, t2
, diff
;
70 clock_get_realtime(&t1
);
71 t
->error
= t
->post_select(s
, t
);
72 clock_get_realtime(&t2
);
73 tv_diff(&t1
, &t2
, &diff
);
76 PARA_WARNING_LOG("%s: post_select time: %lums\n",
81 static void sched_post_select(struct sched
*s
)
85 list_for_each_entry_safe(t
, tmp
, &s
->post_select_list
, post_select_node
) {
87 call_post_select(s
, t
);
88 // PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
97 * The core function for all paraslash programs.
99 * \param s Pointer to the scheduler struct.
101 * This function updates the global \a now pointer, calls all registered
102 * pre_select hooks which may set the timeout and add any file descriptors to
103 * the fd sets of \a s. Next, it calls para_select() and makes the result available
104 * to the registered tasks by calling their post_select hook.
106 * \return Zero if no more tasks are left in either of the two lists, negative
107 * if para_select returned an error.
111 int schedule(struct sched
*s
)
115 if (!s
->select_function
)
116 s
->select_function
= para_select
;
120 s
->select_timeout
= s
->default_timeout
;
122 clock_get_realtime(now
);
124 ret
= s
->select_function(s
->max_fileno
+ 1, &s
->rfds
, &s
->wfds
,
130 * APUE: Be careful not to check the descriptor sets on return
131 * unless the return value is greater than zero. The return
132 * state of the descriptor sets is implementation dependent if
133 * either a signal is caught or the timer expires.
138 clock_get_realtime(now
);
139 sched_post_select(s
);
140 if (list_empty(&s
->pre_select_list
) && list_empty(&s
->post_select_list
))
146 * Add a task to the scheduler.
148 * \param t The task to add.
149 * \param s The scheduler instance to add the task to.
151 * If the pre_select pointer of \a t is not \p NULL, it is added to
152 * the pre_select list of the scheduler. Same goes for post_select.
154 * \sa task::pre_select, task::post_select
156 void register_task(struct sched
*s
, struct task
*t
)
158 PARA_INFO_LOG("registering %s (%p)\n", t
->status
, t
);
160 if (!s
->pre_select_list
.next
)
161 INIT_LIST_HEAD(&s
->pre_select_list
);
162 if (!s
->post_select_list
.next
)
163 INIT_LIST_HEAD(&s
->post_select_list
);
165 PARA_DEBUG_LOG("pre_select: %p\n", &t
->pre_select
);
166 list_add_tail(&t
->pre_select_node
, &s
->pre_select_list
);
168 if (t
->post_select
) {
169 PARA_DEBUG_LOG("post_select: %p\n", &t
->post_select
);
170 list_add_tail(&t
->post_select_node
, &s
->post_select_list
);
175 * Get the list of all registered tasks.
177 * \param s The scheduler instance to get the task list from.
179 * \return The task list.
181 * Each entry of the list contains an identifier which is simply a hex number.
182 * The result is dynamically allocated and must be freed by the caller.
184 char *get_task_list(struct sched
*s
)
186 struct task
*t
, *tmp
;
189 list_for_each_entry_safe(t
, tmp
, &s
->pre_select_list
, pre_select_node
) {
191 tmp_msg
= make_message("%s%p\tpre\t%s\n", msg
? msg
: "", t
, t
->status
);
195 list_for_each_entry_safe(t
, tmp
, &s
->post_select_list
, post_select_node
) {
197 // if (t->pre_select)
199 tmp_msg
= make_message("%s%p\tpost\t%s\n", msg
? msg
: "", t
, t
->status
);
203 //PARA_DEBUG_LOG("task list:\n%s", msg);
208 * Set the notification value of a task.
210 * \param t The task to notify.
211 * \param err A positive error code.
213 * Tasks which honor notifications are supposed to call \ref
214 * task_get_notification() in their post_select function and act on the
215 * returned notification value.
217 * If the scheduler detects during its pre_select loop that at least one task
218 * has been notified, the loop terminates, and the post_select methods of all
219 * taks are immediately called again.
221 * The notification for a task is reset after the call to its post_select
224 * \sa \ref task_get_notification().
226 void task_notify(struct task
*t
, int err
)
229 if (t
->notification
== -err
) /* ignore subsequent notifications */
231 PARA_INFO_LOG("notifying task %s: %s\n", t
->status
, para_strerror(err
));
232 t
->notification
= -err
;
236 * Return the notification value of a task.
238 * \param t The task to get the notification value from.
240 * \return The notification value. If this is negative, the task has been
241 * notified by another task. Tasks are supposed to check for notifications by
242 * calling this function from their post_select method.
244 * \sa \ref task_notify().
246 int task_get_notification(struct task
*t
)
248 return t
->notification
;
252 * Set the notification value of all tasks of a scheduler instance.
254 * \param s The scheduler instance whose tasks should be notified.
255 * \param err A positive error code.
257 * This simply iterates over all existing tasks of \a s and sets each
258 * task's notification value to \p -err.
260 void task_notify_all(struct sched
*s
, int err
)
264 list_for_each_entry(t
, &s
->pre_select_list
, pre_select_node
)
266 list_for_each_entry(t
, &s
->post_select_list
, post_select_node
)
271 * Set the select timeout to the minimal possible value.
273 * \param s Pointer to the scheduler struct.
275 * This causes the next select() call to return immediately.
277 void sched_min_delay(struct sched
*s
)
279 s
->select_timeout
.tv_sec
= s
->select_timeout
.tv_usec
= 0;
283 * Impose an upper bound for the timeout of the next select() call.
285 * \param to Maximal allowed timeout.
286 * \param s Pointer to the scheduler struct.
288 * If the current scheduler timeout is already smaller than \a to, this
289 * function does nothing. Otherwise the timeout for the next select() call is
290 * set to the given value.
292 * \sa sched_request_timeout_ms().
294 void sched_request_timeout(struct timeval
*to
, struct sched
*s
)
296 if (tv_diff(&s
->select_timeout
, to
, NULL
) > 0)
297 s
->select_timeout
= *to
;
301 * Force the next select() call to return before the given amount of milliseconds.
303 * \param ms The maximal allowed timeout in milliseconds.
304 * \param s Pointer to the scheduler struct.
306 * Like sched_request_timeout() this imposes an upper bound on the timeout
307 * value for the next select() call.
309 void sched_request_timeout_ms(long unsigned ms
, struct sched
*s
)
313 sched_request_timeout(&tv
, s
);
317 * Force the next select() call to return before the given future time.
319 * \param barrier Absolute time before select() should return.
320 * \param s Pointer to the scheduler struct.
322 * \return If \a barrier is in the past, this function does nothing and returns
323 * zero. Otherwise it returns one.
325 * \sa sched_request_barrier_or_min_delay().
327 int sched_request_barrier(struct timeval
*barrier
, struct sched
*s
)
331 if (tv_diff(now
, barrier
, &diff
) > 0)
333 sched_request_timeout(&diff
, s
);
338 * Force the next select() call to return before the given time.
340 * \param barrier Absolute time before select() should return.
341 * \param s Pointer to the scheduler struct.
343 * \return If \a barrier is in the past, this function requests a minimal
344 * timeout and returns zero. Otherwise it returns one.
346 * \sa sched_min_delay(), sched_request_barrier().
348 int sched_request_barrier_or_min_delay(struct timeval
*barrier
, struct sched
*s
)
352 if (tv_diff(now
, barrier
, &diff
) > 0) {
356 sched_request_timeout(&diff
, s
);