ARRAY_DEFINE(events, struct epoll_event);
};
-void io_loop_handler_init(struct ioloop *ioloop)
+void io_loop_handler_init(struct ioloop *ioloop, unsigned int initial_fd_count)
{
struct ioloop_handler_context *ctx;
ioloop->handler_context = ctx = i_new(struct ioloop_handler_context, 1);
- i_array_init(&ctx->events, IOLOOP_INITIAL_FD_COUNT);
- i_array_init(&ctx->fd_index, IOLOOP_INITIAL_FD_COUNT);
+ i_array_init(&ctx->events, initial_fd_count);
+ i_array_init(&ctx->fd_index, initial_fd_count);
- ctx->epfd = epoll_create(IOLOOP_INITIAL_FD_COUNT);
+ ctx->epfd = epoll_create(initial_fd_count);
if (ctx->epfd < 0)
i_fatal("epoll_create(): %m");
fd_close_on_exec(ctx->epfd, TRUE);
struct ioloop_handler_context *handler_context;
struct ioloop_notify_handler_context *notify_handler_context;
+ unsigned int max_fd_count;
unsigned int running:1;
};
void io_loop_handle_add(struct io_file *io);
void io_loop_handle_remove(struct io_file *io, bool closed);
-void io_loop_handler_init(struct ioloop *ioloop);
+void io_loop_handler_init(struct ioloop *ioloop, unsigned int initial_fd_count);
void io_loop_handler_deinit(struct ioloop *ioloop);
void io_loop_notify_remove(struct io *io);
ARRAY_DEFINE(events, struct kevent);
};
-void io_loop_handler_init(struct ioloop *ioloop)
+void io_loop_handler_init(struct ioloop *ioloop, unsigned int initial_fd_count)
{
struct ioloop_handler_context *ctx;
i_fatal("kqueue() in io_loop_handler_init() failed: %m");
fd_close_on_exec(ctx->kq, TRUE);
- i_array_init(&ctx->events, IOLOOP_INITIAL_FD_COUNT);
+ i_array_init(&ctx->events, initial_fd_count);
}
void io_loop_handler_deinit(struct ioloop *ioloop)
int *fd_index;
};
-void io_loop_handler_init(struct ioloop *ioloop)
+void io_loop_handler_init(struct ioloop *ioloop, unsigned int initial_fd_count)
{
struct ioloop_handler_context *ctx;
ioloop->handler_context = ctx = i_new(struct ioloop_handler_context, 1);
- ctx->fds_count = IOLOOP_INITIAL_FD_COUNT;
+ ctx->fds_count = initial_fd_count;
ctx->fds = i_new(struct pollfd, ctx->fds_count);
- ctx->idx_count = IOLOOP_INITIAL_FD_COUNT;
+ ctx->idx_count = initial_fd_count;
ctx->fd_index = i_new(int, ctx->idx_count);
memset(ctx->fd_index, 0xff, sizeof(int) * ctx->idx_count);
}
struct ioloop *current_ioloop = NULL;
+static void io_loop_initialize_handler(struct ioloop *ioloop)
+{
+ unsigned int initial_fd_count;
+
+ initial_fd_count = ioloop->max_fd_count > 0 &&
+ ioloop->max_fd_count < IOLOOP_INITIAL_FD_COUNT ?
+ ioloop->max_fd_count : IOLOOP_INITIAL_FD_COUNT;
+ io_loop_handler_init(ioloop, initial_fd_count);
+}
+
#undef io_add
struct io *io_add(int fd, enum io_condition condition,
io_callback_t *callback, void *context)
io->fd = fd;
if (io->io.ioloop->handler_context == NULL)
- io_loop_handler_init(io->io.ioloop);
+ io_loop_initialize_handler(io->io.ioloop);
io_loop_handle_add(io);
if (io->io.ioloop->io_files != NULL) {
void io_loop_run(struct ioloop *ioloop)
{
if (ioloop->handler_context == NULL)
- io_loop_handler_init(ioloop);
+ io_loop_initialize_handler(ioloop);
ioloop->running = TRUE;
while (ioloop->running)
ioloop->running = TRUE;
}
+void io_loop_set_max_fd_count(struct ioloop *ioloop, unsigned int max_fds)
+{
+ ioloop->max_fd_count = max_fds;
+}
+
bool io_loop_is_running(struct ioloop *ioloop)
{
return ioloop->running;
void io_loop_handler_run(struct ioloop *ioloop);
struct ioloop *io_loop_create(void);
+/* Specify the maximum number of fds we're expecting to use. */
+void io_loop_set_max_fd_count(struct ioloop *ioloop, unsigned int max_fds);
/* Destroy I/O loop and set ioloop pointer to NULL. */
void io_loop_destroy(struct ioloop **ioloop);