Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/ioloop.h @ 7098:becdf2eacdce HEAD
Use priority queue to implement timeout handling. Added timeout_reset().
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Thu, 03 Jan 2008 23:19:33 +0200 |
parents | e4eb71ae8e96 |
children | 940641318f12 |
rev | line source |
---|---|
6410
e4eb71ae8e96
Changed .h ifdef/defines to use <NAME>_H format.
Timo Sirainen <tss@iki.fi>
parents:
5937
diff
changeset
|
1 #ifndef IOLOOP_H |
e4eb71ae8e96
Changed .h ifdef/defines to use <NAME>_H format.
Timo Sirainen <tss@iki.fi>
parents:
5937
diff
changeset
|
2 #define IOLOOP_H |
0 | 3 |
1431
8a2e1c539faf
struct timeval requires sys/time.h
Timo Sirainen <tss@iki.fi>
parents:
1036
diff
changeset
|
4 #include <sys/time.h> |
0 | 5 #include <time.h> |
6 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
492
diff
changeset
|
7 struct io; |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
492
diff
changeset
|
8 struct timeout; |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
492
diff
changeset
|
9 struct ioloop; |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
492
diff
changeset
|
10 |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
1499
diff
changeset
|
11 enum io_condition { |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
1499
diff
changeset
|
12 IO_READ = 0x01, |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
1499
diff
changeset
|
13 IO_WRITE = 0x02, |
4579
40b353def38c
OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents:
4150
diff
changeset
|
14 /* IO_ERROR can be used to check when writable pipe's reader side |
40b353def38c
OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents:
4150
diff
changeset
|
15 closes the pipe. For other uses IO_READ should work just as well. */ |
3613
c4c1b538d8d0
Added IO_ERROR condition that we can watch now.
Timo Sirainen <tss@iki.fi>
parents:
3534
diff
changeset
|
16 IO_ERROR = 0x04, |
3534
a9be1824403b
New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents:
1729
diff
changeset
|
17 |
a9be1824403b
New inotify code and notify API change. Patch by Johannes Berg
Timo Sirainen <tss@iki.fi>
parents:
1729
diff
changeset
|
18 /* internal */ |
4150
49e024519e22
IO_ERROR and IO_NOTIFY had conflicting values.
Timo Sirainen <tss@iki.fi>
parents:
4039
diff
changeset
|
19 IO_NOTIFY = 0x08 |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
1499
diff
changeset
|
20 }; |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
1499
diff
changeset
|
21 |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
22 enum io_notify_result { |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
23 IO_NOTIFY_ADDED, |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
24 IO_NOTIFY_NOTFOUND, |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
25 IO_NOTIFY_DISABLED |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
26 }; |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
27 |
1036
f782b3319553
Removed useless parameters from io_callback_t and timeout_callback_t.
Timo Sirainen <tss@iki.fi>
parents:
1033
diff
changeset
|
28 typedef void io_callback_t(void *context); |
f782b3319553
Removed useless parameters from io_callback_t and timeout_callback_t.
Timo Sirainen <tss@iki.fi>
parents:
1033
diff
changeset
|
29 typedef void timeout_callback_t(void *context); |
0 | 30 |
31 /* Time when the I/O loop started calling handlers. | |
32 Can be used instead of time(NULL). */ | |
33 extern time_t ioloop_time; | |
34 extern struct timeval ioloop_timeval; | |
492
efa46e28a0d7
Fixes to timezone handling which were handling quite badly. added
Timo Sirainen <tss@iki.fi>
parents:
389
diff
changeset
|
35 extern struct timezone ioloop_timezone; |
0 | 36 |
3798
be1bac1dd005
Export current_ioloop globally.
Timo Sirainen <tss@iki.fi>
parents:
3613
diff
changeset
|
37 extern struct ioloop *current_ioloop; |
be1bac1dd005
Export current_ioloop globally.
Timo Sirainen <tss@iki.fi>
parents:
3613
diff
changeset
|
38 |
4579
40b353def38c
OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents:
4150
diff
changeset
|
39 /* You can create different handlers for IO_READ and IO_WRITE. IO_READ and |
40b353def38c
OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents:
4150
diff
changeset
|
40 IO_ERROR can't use different handlers (and there's no point anyway). |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
1499
diff
changeset
|
41 |
4579
40b353def38c
OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents:
4150
diff
changeset
|
42 Don't try to add multiple handlers for the same type. It's not checked and |
40b353def38c
OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents:
4150
diff
changeset
|
43 the behavior will be undefined. */ |
1729
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
1499
diff
changeset
|
44 struct io *io_add(int fd, enum io_condition condition, |
5bf22d6bb65e
Added IO_DIR_NOTIFY and IO_FILE_NOTIFY conditions to io_add(). IO_DIR_NOTIFY
Timo Sirainen <tss@iki.fi>
parents:
1499
diff
changeset
|
45 io_callback_t *callback, void *context); |
4903
204d7edc7cdc
Added context parameter type safety checks for most callback APIs.
Timo Sirainen <tss@iki.fi>
parents:
4579
diff
changeset
|
46 #define io_add(fd, condition, callback, context) \ |
204d7edc7cdc
Added context parameter type safety checks for most callback APIs.
Timo Sirainen <tss@iki.fi>
parents:
4579
diff
changeset
|
47 CONTEXT_CALLBACK(io_add, io_callback_t, \ |
204d7edc7cdc
Added context parameter type safety checks for most callback APIs.
Timo Sirainen <tss@iki.fi>
parents:
4579
diff
changeset
|
48 callback, context, fd, condition) |
5937
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
49 enum io_notify_result io_add_notify(const char *path, io_callback_t *callback, |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
50 void *context, struct io **io_r); |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
51 #ifdef CONTEXT_TYPE_SAFETY |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
52 # define io_add_notify(path, callback, context, io_r) \ |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
53 ({(void)(1 ? 0 : callback(context)); \ |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
54 io_add_notify(path, (io_callback_t *)callback, context, io_r); }) |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
55 #else |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
56 # define io_add_notify(path, callback, context, io_r) \ |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
57 io_add_notify(path, (io_callback_t *)callback, context, io_r) |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
58 #endif |
772f4e8bd2a9
Changed io_add_notify() API so that it can return "file doesn't exist"
Timo Sirainen <tss@iki.fi>
parents:
5248
diff
changeset
|
59 |
3879
928229f8b3e6
deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents:
3863
diff
changeset
|
60 /* Remove I/O handler, and set io pointer to NULL. */ |
928229f8b3e6
deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents:
3863
diff
changeset
|
61 void io_remove(struct io **io); |
0 | 62 |
63 /* Timeout handlers */ | |
1499
e850252cdc7e
Removed I/O priorities. They were pretty much useless and were just getting
Timo Sirainen <tss@iki.fi>
parents:
1431
diff
changeset
|
64 struct timeout *timeout_add(unsigned int msecs, timeout_callback_t *callback, |
953
411006be3c66
Naming change for function typedefs.
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
65 void *context); |
4903
204d7edc7cdc
Added context parameter type safety checks for most callback APIs.
Timo Sirainen <tss@iki.fi>
parents:
4579
diff
changeset
|
66 #define timeout_add(msecs, callback, context) \ |
204d7edc7cdc
Added context parameter type safety checks for most callback APIs.
Timo Sirainen <tss@iki.fi>
parents:
4579
diff
changeset
|
67 CONTEXT_CALLBACK(timeout_add, timeout_callback_t, \ |
204d7edc7cdc
Added context parameter type safety checks for most callback APIs.
Timo Sirainen <tss@iki.fi>
parents:
4579
diff
changeset
|
68 callback, context, msecs) |
3879
928229f8b3e6
deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents:
3863
diff
changeset
|
69 /* Remove timeout handler, and set timeout pointer to NULL. */ |
928229f8b3e6
deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents:
3863
diff
changeset
|
70 void timeout_remove(struct timeout **timeout); |
7098
becdf2eacdce
Use priority queue to implement timeout handling. Added timeout_reset().
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
71 /* Reset timeout so it's next run after now+msecs. */ |
becdf2eacdce
Use priority queue to implement timeout handling. Added timeout_reset().
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
72 void timeout_reset(struct timeout *timeout); |
0 | 73 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
492
diff
changeset
|
74 void io_loop_run(struct ioloop *ioloop); |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
492
diff
changeset
|
75 void io_loop_stop(struct ioloop *ioloop); /* safe to run in signal handler */ |
0 | 76 |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3798
diff
changeset
|
77 bool io_loop_is_running(struct ioloop *ioloop); |
1033 | 78 |
0 | 79 /* call these if you wish to run the iteration only once */ |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
492
diff
changeset
|
80 void io_loop_set_running(struct ioloop *ioloop); |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
492
diff
changeset
|
81 void io_loop_handler_run(struct ioloop *ioloop); |
0 | 82 |
5248
12ac5f685814
Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents:
4903
diff
changeset
|
83 struct ioloop *io_loop_create(void); |
3879
928229f8b3e6
deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents:
3863
diff
changeset
|
84 /* Destroy I/O loop and set ioloop pointer to NULL. */ |
928229f8b3e6
deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents:
3863
diff
changeset
|
85 void io_loop_destroy(struct ioloop **ioloop); |
0 | 86 |
87 #endif |