]> mj.ucw.cz Git - libucw.git/blob - lib/mainloop.h
Mainloop timers are now measured in milliseconds. Will be used by shep-reap one day.
[libucw.git] / lib / mainloop.h
1 /*
2  *      UCW Library -- Main Loop
3  *
4  *      (c) 2004--2005 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/clists.h"
11
12 typedef s64 timestamp_t;                        /* We measure time in milliseconds */
13 extern timestamp_t main_now;                    /* Current time in milliseconds since UNIX epoch */
14 extern sh_time_t main_now_seconds;              /* Current time in seconds since the epoch */
15 extern uns main_shutdown;
16 extern clist main_timer_list, main_file_list, main_hook_list, main_process_list;
17
18 /* User-defined fields are marked with [*], all other fields must be initialized to zero. */
19
20 /* Timers */
21
22 struct main_timer {
23   cnode n;
24   timestamp_t expires;
25   void (*handler)(struct main_timer *tm);       /* [*] Function to be called when the timer expires. Must re-add/del the timer.*/
26   void *data;                                   /* [*] Data for use by the handler */
27 };
28
29 void timer_add(struct main_timer *tm, timestamp_t expires);     /* Can modify a running timer, too */
30 void timer_del(struct main_timer *tm);
31
32 /* Files to poll */
33
34 struct main_file {
35   cnode n;
36   int fd;                                       /* [*] File descriptor */
37   int (*read_handler)(struct main_file *fi);    /* [*] To be called when ready for reading/writing; must call file_chg() afterwards */
38   int (*write_handler)(struct main_file *fi);
39   void (*error_handler)(struct main_file *fi, int cause);       /* [*] Handler to call on errors */
40   void *data;                                   /* [*] Data for use by the handlers */
41   byte *rbuf;                                   /* Read/write pointers for use by file_read/write */
42   uns rpos, rlen;
43   byte *wbuf;
44   uns wpos, wlen;
45   void (*read_done)(struct main_file *fi);      /* [*] Called when file_read is finished; rpos < rlen if EOF */
46   void (*write_done)(struct main_file *fi);     /* [*] Called when file_write is finished */
47   struct main_timer timer;
48   struct pollfd *pollfd;
49 };
50
51 enum main_file_err_cause {
52   MFERR_READ,
53   MFERR_WRITE,
54   MFERR_TIMEOUT
55 };
56
57 void file_add(struct main_file *fi);
58 void file_chg(struct main_file *fi);
59 void file_del(struct main_file *fi);
60 void file_read(struct main_file *fi, void *buf, uns len);
61 void file_write(struct main_file *fi, void *buf, uns len);
62 void file_set_timeout(struct main_file *fi, timestamp_t expires);
63
64 /* Hooks to be called in each iteration of the main loop */
65
66 struct main_hook {
67   cnode n;
68   int (*handler)(struct main_hook *ho);         /* [*] Hook function; returns 1 if should be called again */
69   void *data;                                   /* [*] For use by the handler */
70 };
71
72 void hook_add(struct main_hook *ho);
73 void hook_del(struct main_hook *ho);
74
75 /* Processes to watch */
76
77 struct main_process {
78   cnode n;
79   int pid;                                      /* Process id (0=not running) */
80   int status;                                   /* Exit status (-1=fork failed) */
81   byte status_msg[EXIT_STATUS_MSG_SIZE];
82   void (*handler)(struct main_process *mp);     /* [*] Called when the process exits; process_del done automatically */
83   void *data;                                   /* [*] For use by the handler */
84 };
85
86 void process_add(struct main_process *mp);
87 void process_del(struct main_process *mp);
88 int process_fork(struct main_process *mp);
89
90 /* The main loop */
91
92 void main_init(void);
93 void main_loop(void);
94 void main_debug(void);