task_register() conversion: afs command task
[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  * This is considered an internal structure and will eventually be made private.
40  *
41  * \sa \ref sched.
42  */
43 struct task {
44         /** Copied from the task_info struct during task_register(). */
45         void (*pre_select)(struct sched *s, struct task *t);
46         /** Copied from the task_info struct during task_register(). */
47         int (*post_select)(struct sched *s, struct task *t);
48         /** Whether this task is active (>=0) or in error state (<0). */
49         int error;
50         /** Position of the task in the task list of the scheduler. */
51         struct list_head node;
52         /** The task name supplied when the task was registered(). */
53         char status[255];
54         /** If less than zero, the task was notified by another task. */
55         int notification;
56         /** Whether the task structure should be freed in sched_shutdown(). */
57         bool owned_by_sched;
58         /** True if task is in error state and exit status has been queried. */
59         bool dead;
60         /** Usually a pointer to the struct containing this task. */
61         void *context;
62 };
63
64 /** Information that must be supplied by callers of \ref task_register(). */
65 struct task_info {
66         /** Used for log messages and by \ref get_task_list(). */
67         const char *name;
68         /**
69          * The optional pre select method.
70          *
71          * Its purpose is to add file descriptors to the fd sets of the
72          * scheduler and to decrease the select timeout if necessary.
73          */
74         void (*pre_select)(struct sched *s, struct task *t);
75         /**
76          * The mandatory post select method.
77          *
78          * Its purpose is to evaluate and act upon the results of the previous
79          * select call. If this function returns a negative value, the
80          * scheduler unregisters the task.
81          */
82         int (*post_select)(struct sched *s, struct task *t);
83         /**
84          * This pointer is saved when the task is register(ed). It may be
85          * queried from ->pre_select() and ->post_select() via \ref
86          * task_context(). Usually this is a pointer to the struct owned by the
87          * caller which contains the task pointer as one member.
88          */
89         void *context;
90 };
91
92 /**
93  * This is set by the scheduler at the beginning of its main loop.  It may be
94  * used (read-only) from everywhere. As none of the functions called by the
95  * scheduler are allowed to block, this value should be accurate enough so that
96  * there is no need to call clock_gettime() directly.
97  */
98 extern struct timeval *now;
99
100 struct task *task_register(struct task_info *info, struct sched *s);
101 void *task_context(struct task *t);
102 void register_task(struct sched *s, struct task *t);
103 int schedule(struct sched *s);
104 void sched_shutdown(struct sched *s);
105 char *get_task_list(struct sched *s);
106 void task_notify(struct task *t, int err);
107 void task_notify_all(struct sched *s, int err);
108 int task_get_notification(const struct task *t);
109 int task_reap(struct task **tptr);
110 void sched_min_delay(struct sched *s);
111 void sched_request_timeout(struct timeval *to, struct sched *s);
112 void sched_request_timeout_ms(long unsigned ms, struct sched *s);
113 int sched_request_barrier(struct timeval *barrier, struct sched *s);
114 int sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s);