annotate src/lib/ioloop-kqueue.c @ 23007:36e01285b5b8

lib: buffer - Improve header comment for buffer_insert() and buffer_delete().
author Stephan Bosch <stephan.bosch@dovecot.fi>
date Mon, 18 Mar 2019 00:52:37 +0100
parents 25d3a72cdf20
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1 /*
3781
d996a407aa4b Fix IO_ERROR behaviour. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents: 3749
diff changeset
2 * BSD kqueue() based ioloop handler.
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 *
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
4 * Copyright (c) 2005 Vaclav Haisman <v.haisman@sh.cvut.cz>
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 */
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
6
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
7 #include "lib.h"
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9 #ifdef IOLOOP_KQUEUE
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
10
4573
8d977716f449 Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents: 4309
diff changeset
11 #include "array.h"
3958
3e9b43d0cd80 kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents: 3781
diff changeset
12 #include "fd-close-on-exec.h"
13529
cf77e448295c Renamed lib/*-internal.h files to lib/*-private.h for consistency.
Timo Sirainen <tss@iki.fi>
parents: 12497
diff changeset
13 #include "ioloop-private.h"
4573
8d977716f449 Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents: 4309
diff changeset
14
3781
d996a407aa4b Fix IO_ERROR behaviour. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents: 3749
diff changeset
15 #include <unistd.h>
3958
3e9b43d0cd80 kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents: 3781
diff changeset
16 #include <fcntl.h>
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17 #include <sys/types.h>
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18 #include <sys/event.h>
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
19 #include <sys/time.h>
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
20
4737
fd0d7e9e0e72 Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents: 4596
diff changeset
21 /* kevent.udata's type just has to be different in NetBSD than in
fd0d7e9e0e72 Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents: 4596
diff changeset
22 FreeBSD and OpenBSD.. */
fd0d7e9e0e72 Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents: 4596
diff changeset
23 #ifdef __NetBSD__
fd0d7e9e0e72 Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents: 4596
diff changeset
24 # define MY_EV_SET(a, b, c, d, e, f, g) \
fd0d7e9e0e72 Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents: 4596
diff changeset
25 EV_SET(a, b, c, d, e, f, (intptr_t)g)
fd0d7e9e0e72 Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents: 4596
diff changeset
26 #else
fd0d7e9e0e72 Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents: 4596
diff changeset
27 # define MY_EV_SET(a, b, c, d, e, f, g) \
fd0d7e9e0e72 Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents: 4596
diff changeset
28 EV_SET(a, b, c, d, e, f, g)
fd0d7e9e0e72 Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents: 4596
diff changeset
29 #endif
fd0d7e9e0e72 Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents: 4596
diff changeset
30
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
31 struct ioloop_handler_context {
3958
3e9b43d0cd80 kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents: 3781
diff changeset
32 int kq;
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
33
4573
8d977716f449 Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents: 4309
diff changeset
34 unsigned int deleted_count;
14920
a097ef0a9d6d Array API changed: ARRAY_DEFINE(name, type) -> ARRAY(type) name
Timo Sirainen <tss@iki.fi>
parents: 14686
diff changeset
35 ARRAY(struct kevent) events;
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
36 };
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
37
8634
86c28d14ddeb Added io_loop_set_max_fd_count() to specify how many fds we expect to use.
Timo Sirainen <tss@iki.fi>
parents: 8366
diff changeset
38 void io_loop_handler_init(struct ioloop *ioloop, unsigned int initial_fd_count)
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
39 {
3958
3e9b43d0cd80 kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents: 3781
diff changeset
40 struct ioloop_handler_context *ctx;
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
41
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 4863
diff changeset
42 ioloop->handler_context = ctx = i_new(struct ioloop_handler_context, 1);
3958
3e9b43d0cd80 kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents: 3781
diff changeset
43 ctx->kq = kqueue();
3e9b43d0cd80 kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents: 3781
diff changeset
44 if (ctx->kq < 0)
3e9b43d0cd80 kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents: 3781
diff changeset
45 i_fatal("kqueue() in io_loop_handler_init() failed: %m");
3e9b43d0cd80 kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents: 3781
diff changeset
46 fd_close_on_exec(ctx->kq, TRUE);
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
47
8634
86c28d14ddeb Added io_loop_set_max_fd_count() to specify how many fds we expect to use.
Timo Sirainen <tss@iki.fi>
parents: 8366
diff changeset
48 i_array_init(&ctx->events, initial_fd_count);
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
49 }
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
50
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
51 void io_loop_handler_deinit(struct ioloop *ioloop)
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
52 {
3781
d996a407aa4b Fix IO_ERROR behaviour. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents: 3749
diff changeset
53 if (close(ioloop->handler_context->kq) < 0)
3958
3e9b43d0cd80 kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents: 3781
diff changeset
54 i_error("close(kqueue) in io_loop_handler_deinit() failed: %m");
4573
8d977716f449 Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents: 4309
diff changeset
55 array_free(&ioloop->handler_context->events);
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 4863
diff changeset
56 i_free(ioloop->handler_context);
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
57 }
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
58
8366
2c111b572eee Don't break if io_remove() or timeout_remove() are called for non-active ioloops.
Timo Sirainen <tss@iki.fi>
parents: 8100
diff changeset
59 void io_loop_handle_add(struct io_file *io)
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
60 {
8366
2c111b572eee Don't break if io_remove() or timeout_remove() are called for non-active ioloops.
Timo Sirainen <tss@iki.fi>
parents: 8100
diff changeset
61 struct ioloop_handler_context *ctx = io->io.ioloop->handler_context;
4573
8d977716f449 Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents: 4309
diff changeset
62 struct kevent ev;
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
63
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 4863
diff changeset
64 if ((io->io.condition & (IO_READ | IO_ERROR)) != 0) {
4737
fd0d7e9e0e72 Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents: 4596
diff changeset
65 MY_EV_SET(&ev, io->fd, EVFILT_READ, EV_ADD, 0, 0, io);
4579
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
66 if (kevent(ctx->kq, &ev, 1, NULL, 0, NULL) < 0)
17307
304b95de6a5f kqueue: Changed all i_fatal() calls to i_panic()s to make debugging them possible.
Timo Sirainen <tss@iki.fi>
parents: 17187
diff changeset
67 i_panic("kevent(EV_ADD, READ, %d) failed: %m", io->fd);
4579
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
68 }
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 4863
diff changeset
69 if ((io->io.condition & IO_WRITE) != 0) {
4737
fd0d7e9e0e72 Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents: 4596
diff changeset
70 MY_EV_SET(&ev, io->fd, EVFILT_WRITE, EV_ADD, 0, 0, io);
4579
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
71 if (kevent(ctx->kq, &ev, 1, NULL, 0, NULL) < 0)
17307
304b95de6a5f kqueue: Changed all i_fatal() calls to i_panic()s to make debugging them possible.
Timo Sirainen <tss@iki.fi>
parents: 17187
diff changeset
72 i_panic("kevent(EV_ADD, WRITE, %d) failed: %m", io->fd);
4579
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
73 }
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
74
4579
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
75 /* allow kevent() to return the maximum number of events
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
76 by keeping space allocated for each handle */
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
77 if (ctx->deleted_count > 0)
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
78 ctx->deleted_count--;
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
79 else
14686
9ff19c1d5f69 Added array_append_zero() to write a zero-filled record to an array.
Timo Sirainen <tss@iki.fi>
parents: 13529
diff changeset
80 array_append_zero(&ctx->events);
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
81 }
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
82
8366
2c111b572eee Don't break if io_remove() or timeout_remove() are called for non-active ioloops.
Timo Sirainen <tss@iki.fi>
parents: 8100
diff changeset
83 void io_loop_handle_remove(struct io_file *io, bool closed)
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
84 {
8366
2c111b572eee Don't break if io_remove() or timeout_remove() are called for non-active ioloops.
Timo Sirainen <tss@iki.fi>
parents: 8100
diff changeset
85 struct ioloop_handler_context *ctx = io->io.ioloop->handler_context;
4573
8d977716f449 Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents: 4309
diff changeset
86 struct kevent ev;
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
87
16864
10f3030047b6 ioloop-kqueue: Added extra assert.
Timo Sirainen <tss@iki.fi>
parents: 16777
diff changeset
88 i_assert(io->io.condition != 0);
8100
83aef3a6c0a3 Added io_remove_closed().
Timo Sirainen <tss@iki.fi>
parents: 7153
diff changeset
89 if ((io->io.condition & (IO_READ | IO_ERROR)) != 0 && !closed) {
4737
fd0d7e9e0e72 Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents: 4596
diff changeset
90 MY_EV_SET(&ev, io->fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
4579
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
91 if (kevent(ctx->kq, &ev, 1, NULL, 0, NULL) < 0)
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
92 i_error("kevent(EV_DELETE, %d) failed: %m", io->fd);
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
93 }
8100
83aef3a6c0a3 Added io_remove_closed().
Timo Sirainen <tss@iki.fi>
parents: 7153
diff changeset
94 if ((io->io.condition & IO_WRITE) != 0 && !closed) {
4737
fd0d7e9e0e72 Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents: 4596
diff changeset
95 MY_EV_SET(&ev, io->fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
4579
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
96 if (kevent(ctx->kq, &ev, 1, NULL, 0, NULL) < 0)
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
97 i_error("kevent(EV_DELETE, %d) failed: %m", io->fd);
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
98 }
16864
10f3030047b6 ioloop-kqueue: Added extra assert.
Timo Sirainen <tss@iki.fi>
parents: 16777
diff changeset
99 io->io.condition = 0;
4573
8d977716f449 Rewrote much of the kqueue code. It didn't work correctly if there were both
Timo Sirainen <tss@iki.fi>
parents: 4309
diff changeset
100
4579
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
101 /* since we're not freeing memory in any case, just increase
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
102 deleted counter so next handle_add() can just decrease it
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
103 insteading of appending to the events array */
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
104 ctx->deleted_count++;
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 4863
diff changeset
105
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 4863
diff changeset
106 i_assert(io->refcount > 0);
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 4863
diff changeset
107 if (--io->refcount == 0)
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 4863
diff changeset
108 i_free(io);
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
109 }
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
110
17187
d2a6f57e174f ioloop: Added io_set_pending()
Timo Sirainen <tss@iki.fi>
parents: 16864
diff changeset
111 void io_loop_handler_run_internal(struct ioloop *ioloop)
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
112 {
3958
3e9b43d0cd80 kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents: 3781
diff changeset
113 struct ioloop_handler_context *ctx = ioloop->handler_context;
4576
Timo Sirainen <tss@iki.fi>
parents: 4574
diff changeset
114 struct kevent *events;
Timo Sirainen <tss@iki.fi>
parents: 4574
diff changeset
115 const struct kevent *event;
3958
3e9b43d0cd80 kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents: 3781
diff changeset
116 struct timeval tv;
3e9b43d0cd80 kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents: 3781
diff changeset
117 struct timespec ts;
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 4863
diff changeset
118 struct io_file *io;
12497
1bac1c09201a ioloop: Added support for per-io/timeout callback log prefix automation.
Timo Sirainen <tss@iki.fi>
parents: 10067
diff changeset
119 unsigned int events_count;
20943
5d1efb601d6c lib: Fix kqueue io_loop_get_wait_time usage
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 17307
diff changeset
120 int ret, i, msecs;
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
121
3958
3e9b43d0cd80 kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents: 3781
diff changeset
122 /* get the time left for next timeout task */
20943
5d1efb601d6c lib: Fix kqueue io_loop_get_wait_time usage
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 17307
diff changeset
123 msecs = io_loop_get_wait_time(ioloop, &tv);
3958
3e9b43d0cd80 kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents: 3781
diff changeset
124 ts.tv_sec = tv.tv_sec;
3e9b43d0cd80 kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents: 3781
diff changeset
125 ts.tv_nsec = tv.tv_usec * 1000;
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
126
3958
3e9b43d0cd80 kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents: 3781
diff changeset
127 /* wait for events */
4863
d13324a8e242 s/array_get_modifyable/array_get_modifiable/
Timo Sirainen <tss@iki.fi>
parents: 4737
diff changeset
128 events = array_get_modifiable(&ctx->events, &events_count);
20943
5d1efb601d6c lib: Fix kqueue io_loop_get_wait_time usage
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 17307
diff changeset
129
5d1efb601d6c lib: Fix kqueue io_loop_get_wait_time usage
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 17307
diff changeset
130 if (events_count > 0) {
5d1efb601d6c lib: Fix kqueue io_loop_get_wait_time usage
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 17307
diff changeset
131 ret = kevent (ctx->kq, NULL, 0, events, events_count, &ts);
22152
25d3a72cdf20 lib: ioloop-kqueue - Improve kevent() panic log message
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 20943
diff changeset
132 if (ret < 0 && errno != EINTR) {
25d3a72cdf20 lib: ioloop-kqueue - Improve kevent() panic log message
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 20943
diff changeset
133 i_panic("kevent(events=%u, ts=%ld.%u) failed: %m",
25d3a72cdf20 lib: ioloop-kqueue - Improve kevent() panic log message
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 20943
diff changeset
134 events_count, (long)ts.tv_sec,
25d3a72cdf20 lib: ioloop-kqueue - Improve kevent() panic log message
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 20943
diff changeset
135 (unsigned int)ts.tv_nsec);
25d3a72cdf20 lib: ioloop-kqueue - Improve kevent() panic log message
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 20943
diff changeset
136 }
20943
5d1efb601d6c lib: Fix kqueue io_loop_get_wait_time usage
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 17307
diff changeset
137 } else {
5d1efb601d6c lib: Fix kqueue io_loop_get_wait_time usage
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 17307
diff changeset
138 if (msecs < 0)
5d1efb601d6c lib: Fix kqueue io_loop_get_wait_time usage
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 17307
diff changeset
139 i_panic("BUG: No IOs or timeouts set. Not waiting for infinity.");
5d1efb601d6c lib: Fix kqueue io_loop_get_wait_time usage
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 17307
diff changeset
140 usleep(msecs * 1000);
5d1efb601d6c lib: Fix kqueue io_loop_get_wait_time usage
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 17307
diff changeset
141 ret = 0;
5d1efb601d6c lib: Fix kqueue io_loop_get_wait_time usage
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 17307
diff changeset
142 }
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
143
4579
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
144 /* reference all IOs */
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
145 for (i = 0; i < ret; i++) {
4737
fd0d7e9e0e72 Removed compiler warnings with NetBSD.
Timo Sirainen <tss@iki.fi>
parents: 4596
diff changeset
146 io = (void *)events[i].udata;
16777
c8b1e5833a28 ioloop-kqueue: Added assert
Timo Sirainen <tss@iki.fi>
parents: 14920
diff changeset
147 i_assert(io->refcount > 0);
4579
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
148 io->refcount++;
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
149 }
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
150
4580
4644b3038b9a Reference IOs before calling timeout handlers, in case they call
Timo Sirainen <tss@iki.fi>
parents: 4579
diff changeset
151 /* execute timeout handlers */
7098
becdf2eacdce Use priority queue to implement timeout handling. Added timeout_reset().
Timo Sirainen <tss@iki.fi>
parents: 5682
diff changeset
152 io_loop_handle_timeouts(ioloop);
4580
4644b3038b9a Reference IOs before calling timeout handlers, in case they call
Timo Sirainen <tss@iki.fi>
parents: 4579
diff changeset
153
4576
Timo Sirainen <tss@iki.fi>
parents: 4574
diff changeset
154 for (i = 0; i < ret; i++) {
Timo Sirainen <tss@iki.fi>
parents: 4574
diff changeset
155 /* io_loop_handle_add() may cause events array reallocation,
Timo Sirainen <tss@iki.fi>
parents: 4574
diff changeset
156 so we have use array_idx() */
Timo Sirainen <tss@iki.fi>
parents: 4574
diff changeset
157 event = array_idx(&ctx->events, i);
4579
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
158 io = (void *)event->udata;
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
159
4579
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
160 /* callback is NULL if io_remove() was already called */
12497
1bac1c09201a ioloop: Added support for per-io/timeout callback log prefix automation.
Timo Sirainen <tss@iki.fi>
parents: 10067
diff changeset
161 if (io->io.callback != NULL)
1bac1c09201a ioloop: Added support for per-io/timeout callback log prefix automation.
Timo Sirainen <tss@iki.fi>
parents: 10067
diff changeset
162 io_loop_call_io(&io->io);
4579
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
163
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
164 i_assert(io->refcount > 0);
40b353def38c OK, so the original kqueue code wasn't actually broken, but it could have
Timo Sirainen <tss@iki.fi>
parents: 4577
diff changeset
165 if (--io->refcount == 0)
5248
12ac5f685814 Various cleanups to ioloop code.
Timo Sirainen <tss@iki.fi>
parents: 4863
diff changeset
166 i_free(io);
3958
3e9b43d0cd80 kqueue updates. Patch by Vaclav Haisman
Timo Sirainen <tss@iki.fi>
parents: 3781
diff changeset
167 }
3749
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
168 }
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
169
194295062e5e Added kqueue support. Patch by Vaclav Haisman.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
170 #endif