NEWS update.
[paraslash.git] / sched.h
1 /*
2 * Copyright (C) 2006-2008 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 sets \a t->error to a negative value, the
39 * task gets unregistered automatically.
40 *
41 * \sa struct sched.
42 */
43 struct task {
44 /**
45 * The pre select hook of \a t.
46 *
47 * Its purpose is to add file descriptors to the fd sets of the
48 * scheduler and to decrease the select timeout if necessary.
49 */
50 void (*pre_select)(struct sched *s, struct task *t);
51 /**
52 * The postselect hook of \a t.
53 *
54 * Evaluate and act upon the results of the previous select call.
55 */
56 void (*post_select)(struct sched *s, struct task *t);
57 /** Whether this task is in error state. */
58 int error;
59 /** Position of the task in the pre_select list of the scheduler. */
60 struct list_head pre_select_node;
61 /** Position of the task in the post_select list of the scheduler. */
62 struct list_head post_select_node;
63 /** Descriptive text and current status of the task. */
64 char status[MAXLINE];
65 };
66
67 /**
68 * This is set by the scheduler at the beginning of its main loop. It may be
69 * used (read-only) from everywhere. As none of the functions called by the
70 * scheduler are allowed to block, this value should be accurate enough so that
71 * there is no need to call gettimeofday() directly.
72 */
73 extern struct timeval *now;
74
75 void register_task(struct task *t);
76 int schedule(struct sched *s);
77 char *get_task_list(void);
78 int kill_task(char *id);
79 void sched_shutdown(void);