]> git.tuebingen.mpg.de Git - paraslash.git/blob - sched.h
36d0769beac802d118a8e9db668cb1d2196a0fbc
[paraslash.git] / sched.h
1 /*
2  * Copyright (C) 2006 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 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 };
31
32 /**
33  * paraslash's task structure
34  *
35  * before registering a task to the scheduler, the task structure must be
36  * filled in properly by the caller.
37  *
38  * If one of these functions return a negative value via \a t->ret the
39  * (optional) event_handler gets called (it may also be called in case another
40  * event happened). In many cases the only possible event is an error or an eof
41  * condition and the event handler simply unregisters the task from the
42  * scheduler.
43  *
44  * \sa struct 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 postselect hook of \a t
56          *
57          * evaluate and act upon the results of the previous select call.
58          */
59         void (*post_select)(struct sched *s, struct task *t);
60         /** Whether this task is in error state. */
61         int error;
62         /** position of the task in the pre_select list of the scheduler */
63         struct list_head pre_select_node;
64         /** position of the task in the post_select list of the scheduler */
65         struct list_head post_select_node;
66         /** descriptive text and current status of the task */
67         char status[MAXLINE];
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 gettimeofday() directly.
75  */
76 extern struct timeval *now;
77
78 void register_task(struct task *t);
79 void unregister_task(struct task *t);
80 int schedule(struct sched *s);
81 char *get_task_list(void);
82 int kill_task(char *id);