bdca15f828cb60c177b7d8fb0cb497c1eb99b375
[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.h sched and task structures and exported symbols from sched.c */
20
21
22 /**
23  * paraslash's scheduler
24  *
25  * desinged with KISS in mind. It manages two lists of tasks.  The pre_select
26  * list contains pointers to functions that are called before calling select()
27  * from the main loop. Similarly, \a post_select_list is a list of function
28  * pointers each of which is called after the select call. Tasks add hooks to
29  * these lists by registering themselves to the scheduler.
30  */
31 struct sched {
32         /** initial value before any pre_select call */
33         struct timeval default_timeout;
34         /** the current timeout for the upcoming select call */
35         struct timeval timeout;
36         /** fds that should be watched for readability */
37         fd_set rfds;
38         /** fds that should be watched for writability */
39         fd_set wfds;
40         /** highest numbered file descriptor in any of the above fd sets */
41         int max_fileno;
42         /** the return value of the previous select call */
43         int select_ret;
44 };
45
46 /**
47  * paraslash's task structure
48  *
49  * before registering a task to the scheduler, the task structure must be
50  * filled in properly by the caller.
51  *
52  * If one of these functions return a negative value via \a t->ret the
53  * (optional) event_handler gets called (it may also be called in case another
54  * event happend). In many cases the only possible event is an error or an eof
55  * condition and the event handler simply unregisters the task from the
56  * scheduler.
57  *
58  * \sa struct sched
59  */
60 struct task {
61         /** pointer to the struct this task is embedded in */
62         void *private_data;
63         /**
64          * the pre select hook of \a t
65          *
66          * Its purpose is to add file descriptors to the fd sets of the
67          * scheduler and to decrease the select timeout if neccessary.
68          */
69         void (*pre_select)(struct sched *s, struct task *t);
70         /**
71          * the postselect hook of \a t
72          *
73          * evaluate and act upon the results of the previous select call.
74          */
75         void (*post_select)(struct sched *s, struct task *t);
76         /** gets called if pre_select or post_select returned an error */
77         void (*event_handler)(struct task *t);
78         /** pre_select() and post_select store their return value here */
79         int ret;
80         /** position of the task in the pre_select list of the scheduler */
81         struct list_head pre_select_node;
82         /** position of the task in the post_select list of the scheduler */
83         struct list_head post_select_node;
84         /** descriptive text and current status of the task */
85         char status[MAXLINE];
86 };
87
88 /**
89  * This is set by the scheduler at the beginning of its main loop.  It may be
90  * used (read-only) from everywhere. As none of the functions called by the
91  * scheduler are allowed to block, this value should be accurate enough so that
92  * there is no need to call gettimeofday() directly.
93  */
94 extern struct timeval *now;
95
96 void register_task(struct task *t);
97 void unregister_task(struct task *t);
98 int sched(struct sched *s);
99 char *get_task_list(void);
100 int kill_task(char *id);