Add scheduler documentation
[paraslash.git] / sched.h
1 /*
2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file sched.c Paraslash's task and scheduling functions */
20
21
22 /**
23 * paraslash's scheduler
24 *
25 * desinged with KISS in mind. It maintains two lists: The pre_select list
26 * and the post_select list. Tasks add hokks to these lists by registering
27 * themselves to the scheduler.
28 */
29 struct sched {
30 /** initial value before any pre_select call */
31 struct timeval default_timeout;
32 /** the current timeout for the upcoming select call */
33 struct timeval timeout;
34 /** fds that should be watched for readability */
35 fd_set rfds;
36 /** fds that should be watched for writability */
37 fd_set wfds;
38 /** highest numbered file descriptor in any of the above fd sets */
39 int max_fileno;
40 /** the return value of the previous select call */
41 int select_ret;
42 };
43
44 /**
45 * paraslash's task structure
46 *
47 * before registering a task to the scheduler, the task structure must be
48 * filled in properly by the caller.
49 *
50 * The pre_select or the post_select pointer, but not both may be NULL. Once
51 * a task is registered, its pre_select and post_select function gets called
52 * from the scheduler's mainloop. The purpose of the pre_select loop is to add
53 * file descriptors to the fd sets of the scheduler and to decrease the select
54 * timeout if neccessary. The post_select function may then evaluate these fd
55 * sets and act upon the results.
56
57 * If one of these functions return a negative value via \a t->ret the
58 * (optional) event_handler gets called (it may also be called in case another
59 * event happend). In many cases the only possible event is an error or an eof
60 * condition and the event handler simply unregisters the task from the
61 * scheduler.
62 *
63 * \sa struct sched
64 */
65 struct task {
66 /** pointer to the struct this task is embedded in */
67 void *private_data;
68 /** pre_select hook */
69 void (*pre_select)(struct sched *s, struct task *t);
70 /** post_select hook */
71 void (*post_select)(struct sched *s, struct task *t);
72 /** gets called */
73 void (*event_handler)(struct task *t);
74 /** pre_select() and post_select store their return value here */
75 int ret;
76 /** position of the task in the pre_select list of the scheduler */
77 struct list_head pre_select_node;
78 /** position of the task in the post_select list of the scheduler */
79 struct list_head post_select_node;
80 /** descriptive text and current status of the task */
81 char status[MAXLINE];
82 };
83
84 /**
85 * This is set by the scheduler at the beginning of its main loop. It may be
86 * used (read-only) from everywhere. As none of the functions called by the
87 * scheduler are allowed to block, this value should be accurate enough so that
88 * there is no need to call gettimeofday() directly.
89 */
90 extern struct timeval *now;
91
92 void *register_task(struct task *t);
93 void unregister_task(struct task *t);
94 int sched(struct sched *s);
95 void init_sched(void);
96 char *get_task_list(void);
97 int kill_task(char *id);