2 * UCW Library -- Main Loop
4 * (c) 2004--2005 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
10 #include "lib/clists.h"
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;
18 /* User-defined fields are marked with [*], all other fields must be initialized to zero. */
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 */
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);
32 void main_get_time(void); /* Refresh main_now */
38 int fd; /* [*] File descriptor */
39 int (*read_handler)(struct main_file *fi); /* [*] To be called when ready for reading/writing; must call file_chg() afterwards */
40 int (*write_handler)(struct main_file *fi);
41 void (*error_handler)(struct main_file *fi, int cause); /* [*] Handler to call on errors */
42 void *data; /* [*] Data for use by the handlers */
43 byte *rbuf; /* Read/write pointers for use by file_read/write */
47 void (*read_done)(struct main_file *fi); /* [*] Called when file_read is finished; rpos < rlen if EOF */
48 void (*write_done)(struct main_file *fi); /* [*] Called when file_write is finished */
49 struct main_timer timer;
50 struct pollfd *pollfd;
53 enum main_file_err_cause {
59 void file_add(struct main_file *fi);
60 void file_chg(struct main_file *fi);
61 void file_del(struct main_file *fi);
62 void file_read(struct main_file *fi, void *buf, uns len);
63 void file_write(struct main_file *fi, void *buf, uns len);
64 void file_set_timeout(struct main_file *fi, timestamp_t expires);
65 void file_close_all(void); /* Close all known main_file's; frequently used before fork() */
67 /* Hooks to be called in each iteration of the main loop */
71 int (*handler)(struct main_hook *ho); /* [*] Hook function; returns 1 if should be called again */
72 void *data; /* [*] For use by the handler */
75 void hook_add(struct main_hook *ho);
76 void hook_del(struct main_hook *ho);
78 /* Processes to watch */
82 int pid; /* Process id (0=not running) */
83 int status; /* Exit status (-1=fork failed) */
84 byte status_msg[EXIT_STATUS_MSG_SIZE];
85 void (*handler)(struct main_process *mp); /* [*] Called when the process exits; process_del done automatically */
86 void *data; /* [*] For use by the handler */
89 void process_add(struct main_process *mp);
90 void process_del(struct main_process *mp);
91 int process_fork(struct main_process *mp);
97 void main_debug(void);