Simplify sched: Use only a single task list.
[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 maintains a list of task structures which is
14  * extended when a new task is registered. Each task may define a pre_select
15  * function which is called from the scheduler main loop before it calls
16  * select(). Similarly, each task must define a post_select function which is
17  * called after the select call.
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         /** Tasks which have been registered to the scheduler. */
33         struct list_head task_list;
34 };
35
36 /**
37  * Paraslash's task structure.
38  *
39  * Before registering a task to the scheduler, the task structure must be
40  * filled in properly by the caller.
41  *
42  * \sa \ref sched.
43  */
44 struct task {
45         /**
46          * The optional pre select hook of \a t.
47          *
48          * Its purpose is to add file descriptors to the fd sets of the
49          * scheduler and to decrease the select timeout if necessary.
50          */
51         void (*pre_select)(struct sched *s, struct task *t);
52         /**
53          * The mandatory post select hook of \a t.
54          *
55          * Its purpose is to evaluate and act upon the results of the previous
56          * select call. If this function returns a negative value, the
57          * scheduler unregisters the task.
58          */
59         int (*post_select)(struct sched *s, struct task *t);
60         /** Whether this task is active (>=0) or in error state (<0). */
61         int error;
62         /** Position of the task in the task list of the scheduler. */
63         struct list_head node;
64         /** Descriptive text and current status of the task. */
65         char status[255];
66         /** If less than zero, the task was notified by another task. */
67         int notification;
68 };
69
70 /**
71  * This is set by the scheduler at the beginning of its main loop.  It may be
72  * used (read-only) from everywhere. As none of the functions called by the
73  * scheduler are allowed to block, this value should be accurate enough so that
74  * there is no need to call clock_gettime() directly.
75  */
76 extern struct timeval *now;
77
78 void register_task(struct sched *s, struct task *t);
79 int schedule(struct sched *s);
80 char *get_task_list(struct sched *s);
81 void task_notify(struct task *t, int err);
82 void task_notify_all(struct sched *s, int err);
83 int task_get_notification(struct task *t);
84 void sched_min_delay(struct sched *s);
85 void sched_request_timeout(struct timeval *to, struct sched *s);
86 void sched_request_timeout_ms(long unsigned ms, struct sched *s);
87 int sched_request_barrier(struct timeval *barrier, struct sched *s);
88 int sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s);