Merge branch 't/test_man'
[paraslash.git] / sched.h
1 /*
2 * Copyright (C) 2006-2014 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file sched.h Sched and task structures and exported symbols from sched.c. */
8
9
10 /**
11 * Paraslash's scheduler.
12 *
13 * Designed with KISS in mind. It manages two lists of tasks. The pre_select
14 * list contains pointers to functions that are called before calling select()
15 * from the main loop. Similarly, \a post_select_list is a list of function
16 * pointers each of which is called after the select call. Tasks add hooks to
17 * these lists by registering themselves to the scheduler.
18 */
19 struct sched {
20 /** Initial value before any pre_select call. */
21 struct timeval default_timeout;
22 /** The current timeout for the upcoming select call. */
23 struct timeval select_timeout;
24 /** fds that should be watched for readability. */
25 fd_set rfds;
26 /** fds that should be watched for writability. */
27 fd_set wfds;
28 /** Highest numbered file descriptor in any of the above fd sets. */
29 int max_fileno;
30 /** If non-NULL, use this function instead of para_select. */
31 int (*select_function)(int, fd_set *, fd_set *, struct timeval *);
32 /** Currently active pre_select functions. */
33 struct list_head pre_select_list;
34 /** Currently active post_select functions. */
35 struct list_head post_select_list;
36 };
37
38 /**
39 * Paraslash's task structure.
40 *
41 * Before registering a task to the scheduler, the task structure must be
42 * filled in properly by the caller.
43 *
44 * \sa \ref sched.
45 */
46 struct task {
47 /**
48 * The pre select hook of \a t.
49 *
50 * Its purpose is to add file descriptors to the fd sets of the
51 * scheduler and to decrease the select timeout if necessary.
52 */
53 void (*pre_select)(struct sched *s, struct task *t);
54 /**
55 * The post select hook of \a t.
56 *
57 * Its purpose is to evaluate and act upon the results of the previous
58 * select call. If this function returns a negative value, the
59 * scheduler unregisters the task.
60 */
61 int (*post_select)(struct sched *s, struct task *t);
62 /** Whether this task is in error state. */
63 int error;
64 /** Position of the task in the pre_select list of the scheduler. */
65 struct list_head pre_select_node;
66 /** Position of the task in the post_select list of the scheduler. */
67 struct list_head post_select_node;
68 /** Descriptive text and current status of the task. */
69 char status[255];
70 /** If less than zero, the task was notified by another task. */
71 int notification;
72 };
73
74 /**
75 * This is set by the scheduler at the beginning of its main loop. It may be
76 * used (read-only) from everywhere. As none of the functions called by the
77 * scheduler are allowed to block, this value should be accurate enough so that
78 * there is no need to call clock_gettime() directly.
79 */
80 extern struct timeval *now;
81
82 void register_task(struct sched *s, struct task *t);
83 int schedule(struct sched *s);
84 char *get_task_list(struct sched *s);
85 void task_notify(struct task *t, int err);
86 void task_notify_all(struct sched *s, int err);
87 int task_get_notification(struct task *t);
88 void sched_min_delay(struct sched *s);
89 void sched_request_timeout(struct timeval *to, struct sched *s);
90 void sched_request_timeout_ms(long unsigned ms, struct sched *s);
91 int sched_request_barrier(struct timeval *barrier, struct sched *s);
92 int sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s);