]> mj.ucw.cz Git - libucw.git/blob - lib/mainloop.h
hashtables: fixed combination of HASH_GIVE_ALLOC and HASH_TABLE_ALLOC
[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 #ifndef _UCW_MAINLOOP_H
11 #define _UCW_MAINLOOP_H
12
13 #include "lib/clists.h"
14
15 typedef s64 timestamp_t;                        /* We measure time in milliseconds */
16 extern timestamp_t main_now;                    /* Current time in milliseconds since UNIX epoch */
17 extern sh_time_t main_now_seconds;              /* Current time in seconds since the epoch */
18 extern uns main_shutdown;
19 extern clist main_timer_list, main_file_list, main_hook_list, main_process_list;
20
21 /* User-defined fields are marked with [*], all other fields must be initialized to zero. */
22
23 /* Timers */
24
25 struct main_timer {
26   cnode n;
27   timestamp_t expires;
28   void (*handler)(struct main_timer *tm);       /* [*] Function to be called when the timer expires. Must re-add/del the timer.*/
29   void *data;                                   /* [*] Data for use by the handler */
30 };
31
32 void timer_add(struct main_timer *tm, timestamp_t expires);     /* Can modify a running timer, too */
33 void timer_del(struct main_timer *tm);
34
35 void main_get_time(void);                       /* Refresh main_now */
36
37 /* Files to poll */
38
39 struct main_file {
40   cnode n;
41   int fd;                                       /* [*] File descriptor */
42   int (*read_handler)(struct main_file *fi);    /* [*] To be called when ready for reading/writing; must call file_chg() afterwards */
43   int (*write_handler)(struct main_file *fi);
44   void (*error_handler)(struct main_file *fi, int cause);       /* [*] Handler to call on errors */
45   void *data;                                   /* [*] Data for use by the handlers */
46   byte *rbuf;                                   /* Read/write pointers for use by file_read/write */
47   uns rpos, rlen;
48   byte *wbuf;
49   uns wpos, wlen;
50   void (*read_done)(struct main_file *fi);      /* [*] Called when file_read is finished; rpos < rlen if EOF */
51   void (*write_done)(struct main_file *fi);     /* [*] Called when file_write is finished */
52   struct main_timer timer;
53   struct pollfd *pollfd;
54 };
55
56 enum main_file_err_cause {
57   MFERR_READ,
58   MFERR_WRITE,
59   MFERR_TIMEOUT
60 };
61
62 void file_add(struct main_file *fi);
63 void file_chg(struct main_file *fi);
64 void file_del(struct main_file *fi);
65 void file_read(struct main_file *fi, void *buf, uns len);
66 void file_write(struct main_file *fi, void *buf, uns len);
67 void file_set_timeout(struct main_file *fi, timestamp_t expires);
68 void file_close_all(void);                      /* Close all known main_file's; frequently used before fork() */
69
70 /* Hooks to be called in each iteration of the main loop */
71
72 struct main_hook {
73   cnode n;
74   int (*handler)(struct main_hook *ho);         /* [*] Hook function; returns HOOK_xxx */
75   void *data;                                   /* [*] For use by the handler */
76 };
77
78 enum main_hook_return {
79   HOOK_IDLE,                                    /* Call again when the main loop becomes idle again */
80   HOOK_RETRY,                                   /* Call again as soon as possible */
81   HOOK_DONE = -1,                               /* Shut down the main loop if all hooks return this value */
82   HOOK_SHUTDOWN = -2                            /* Shut down the main loop immediately */
83 };
84
85 void hook_add(struct main_hook *ho);
86 void hook_del(struct main_hook *ho);
87
88 /* Processes to watch */
89
90 struct main_process {
91   cnode n;
92   int pid;                                      /* Process id (0=not running) */
93   int status;                                   /* Exit status (-1=fork failed) */
94   byte status_msg[EXIT_STATUS_MSG_SIZE];
95   void (*handler)(struct main_process *mp);     /* [*] Called when the process exits; process_del done automatically */
96   void *data;                                   /* [*] For use by the handler */
97 };
98
99 void process_add(struct main_process *mp);
100 void process_del(struct main_process *mp);
101 int process_fork(struct main_process *mp);
102
103 /* The main loop */
104
105 void main_init(void);
106 void main_loop(void);
107 void main_debug(void);
108
109 #endif