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