ioloop.c revision 3e41b3d0cf90bbc1c229abb98cad944373316362
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2002-2016 Dovecot authors, see the included COPYING file */
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
08d6658a4e2ec8104cd1307f6baa75fdb07a24f8Mark Washenberger#include "lib.h"
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen#include "array.h"
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen#include "time-util.h"
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen#include "istream-private.h"
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen#include "ioloop-private.h"
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen#include <unistd.h>
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen#define timer_is_larger(tvp, uvp) \
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen ((tvp)->tv_sec > (uvp)->tv_sec || \
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen ((tvp)->tv_sec == (uvp)->tv_sec && \
08d6658a4e2ec8104cd1307f6baa75fdb07a24f8Mark Washenberger (tvp)->tv_usec > (uvp)->tv_usec))
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainentime_t ioloop_time = 0;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenstruct timeval ioloop_timeval;
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenstruct ioloop *current_ioloop = NULL;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenstatic ARRAY(io_switch_callback_t *) io_switch_callbacks = ARRAY_INIT;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenstatic void io_loop_initialize_handler(struct ioloop *ioloop)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen unsigned int initial_fd_count;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen initial_fd_count = ioloop->max_fd_count > 0 &&
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen ioloop->max_fd_count < IOLOOP_INITIAL_FD_COUNT ?
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen ioloop->max_fd_count : IOLOOP_INITIAL_FD_COUNT;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_loop_handler_init(ioloop, initial_fd_count);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenstatic struct io_file *
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenio_add_file(int fd, enum io_condition condition,
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen const char *source_filename,
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen unsigned int source_linenum,
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_callback_t *callback, void *context)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct io_file *io;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_assert(callback != NULL);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_assert((condition & IO_NOTIFY) == 0);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io = i_new(struct io_file, 1);
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen io->io.condition = condition;
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen io->io.callback = callback;
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen io->io.context = context;
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen io->io.ioloop = current_ioloop;
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen io->io.source_filename = source_filename;
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen io->io.source_linenum = source_linenum;
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen io->refcount = 1;
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen io->fd = fd;
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen if (io->io.ioloop->cur_ctx != NULL) {
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen io->io.ctx = io->io.ioloop->cur_ctx;
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen io_loop_context_ref(io->io.ctx);
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen }
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen if (io->io.ioloop->handler_context == NULL)
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen io_loop_initialize_handler(io->io.ioloop);
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen if (fd != -1)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_loop_handle_add(io);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen else {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen /* we're adding an istream whose only way to get notified
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen is to call i_stream_set_input_pending() */
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen }
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (io->io.ioloop->io_files != NULL) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io->io.ioloop->io_files->prev = io;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io->next = io->io.ioloop->io_files;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen }
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen io->io.ioloop->io_files = io;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen return io;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen#undef io_add
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainenstruct io *io_add(int fd, enum io_condition condition,
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen const char *source_filename,
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen unsigned int source_linenum,
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_callback_t *callback, void *context)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct io_file *io;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_assert(fd >= 0);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io = io_add_file(fd, condition, source_filename, source_linenum, callback, context);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen return &io->io;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
b104354c4a829e828c361e4a167bf7106d124174Timo Sirainen#undef io_add_istream
b104354c4a829e828c361e4a167bf7106d124174Timo Sirainenstruct io *io_add_istream(struct istream *input, const char *source_filename,
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen unsigned int source_linenum,
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen io_callback_t *callback, void *context)
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen{
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen struct io_file *io;
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen io = io_add_file(i_stream_get_fd(input), IO_READ, source_filename,
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen source_linenum, callback, context);
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen io->istream = input;
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen i_stream_ref(io->istream);
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen i_stream_set_io(io->istream, &io->io);
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen return &io->io;
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen}
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainenstatic void io_file_unlink(struct io_file *io)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (io->prev != NULL)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io->prev->next = io->next;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen else
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io->io.ioloop->io_files = io->next;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (io->next != NULL)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen io->next->prev = io->prev;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen /* if we got here from an I/O handler callback, make sure we
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen don't try to handle this one next. */
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (io->io.ioloop->next_io_file == io)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io->io.ioloop->next_io_file = io->next;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenstatic void io_remove_full(struct io **_io, bool closed)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct io *io = *_io;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_assert(io->callback != NULL);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen *_io = NULL;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen /* make sure the callback doesn't get called anymore.
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen kqueue code relies on this. */
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io->callback = NULL;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (io->pending) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_assert(io->ioloop->io_pending_count > 0);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io->ioloop->io_pending_count--;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen }
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (io->ctx != NULL)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_loop_context_unref(&io->ctx);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if ((io->condition & IO_NOTIFY) != 0)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_loop_notify_remove(io);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen else {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct io_file *io_file = (struct io_file *)io;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct istream *istream = io_file->istream;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (istream != NULL) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen /* remove io before it's freed */
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_stream_unset_io(istream, io);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen }
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_file_unlink(io_file);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (io_file->fd != -1)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_loop_handle_remove(io_file, closed);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen else
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_free(io);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen /* remove io from the ioloop before unreferencing the istream,
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen because a destroyed istream may automatically close the
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen fd. */
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (istream != NULL)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_stream_unref(&istream);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen }
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenvoid io_remove(struct io **io)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_remove_full(io, FALSE);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenvoid io_remove_closed(struct io **io)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_assert(((*io)->condition & IO_NOTIFY) == 0);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_remove_full(io, TRUE);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenvoid io_set_pending(struct io *io)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_assert((io->condition & IO_NOTIFY) == 0);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
686ad6d723004b807fd558f3ef9d1f88afa7e127Timo Sirainen if (!io->pending) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io->pending = TRUE;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io->ioloop->io_pending_count++;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen }
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenstatic void timeout_update_next(struct timeout *timeout, struct timeval *tv_now)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (tv_now == NULL) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (gettimeofday(&timeout->next_run, NULL) < 0)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_fatal("gettimeofday(): %m");
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen } else {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout->next_run.tv_sec = tv_now->tv_sec;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout->next_run.tv_usec = tv_now->tv_usec;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen }
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen /* we don't want microsecond accuracy or this function will be
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen called all the time - millisecond is more than enough */
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen timeout->next_run.tv_usec -= timeout->next_run.tv_usec % 1000;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout->next_run.tv_sec += timeout->msecs/1000;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout->next_run.tv_usec += (timeout->msecs%1000)*1000;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (timeout->next_run.tv_usec > 1000000) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout->next_run.tv_sec++;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout->next_run.tv_usec -= 1000000;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen }
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainenstatic struct timeout *
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainentimeout_add_common(const char *source_filename, unsigned int source_linenum,
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout_callback_t *callback, void *context)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen struct timeout *timeout;
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout = i_new(struct timeout, 1);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout->item.idx = UINT_MAX;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout->source_filename = source_filename;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout->source_linenum = source_linenum;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout->ioloop = current_ioloop;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout->callback = callback;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout->context = context;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen if (timeout->ioloop->cur_ctx != NULL) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout->ctx = timeout->ioloop->cur_ctx;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_loop_context_ref(timeout->ctx);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen }
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen return timeout;
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen}
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen#undef timeout_add
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainenstruct timeout *timeout_add(unsigned int msecs, const char *source_filename,
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen unsigned int source_linenum,
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen timeout_callback_t *callback, void *context)
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct timeout *timeout;
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen timeout = timeout_add_common(source_filename, source_linenum, callback, context);
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen timeout->msecs = msecs;
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen if (msecs > 0) {
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen /* start this timeout in the next run cycle */
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen array_append(&timeout->ioloop->timeouts_new, &timeout, 1);
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen } else {
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen /* trigger zero timeouts as soon as possible */
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen timeout_update_next(timeout, timeout->ioloop->running ?
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen NULL : &ioloop_timeval);
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen priorityq_add(timeout->ioloop->timeouts, &timeout->item);
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen }
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen return timeout;
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen}
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen#undef timeout_add_short
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainenstruct timeout *
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainentimeout_add_short(unsigned int msecs, const char *source_filename,
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen unsigned int source_linenum,
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen timeout_callback_t *callback, void *context)
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen{
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen return timeout_add(msecs, source_filename, source_linenum, callback, context);
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen}
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen#undef timeout_add_absolute
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainenstruct timeout *
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainentimeout_add_absolute(const struct timeval *time,
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen const char *source_filename,
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen unsigned int source_linenum,
7da99e97d68f854b8726755d36dfb24b6cf08701Timo Sirainen timeout_callback_t *callback, void *context)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct timeout *timeout;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout = timeout_add_common(source_filename, source_linenum,
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen callback, context);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout->one_shot = TRUE;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout->next_run = *time;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen priorityq_add(timeout->ioloop->timeouts, &timeout->item);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen return timeout;
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen}
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainenstatic struct timeout *
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainentimeout_copy(const struct timeout *old_to)
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen{
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen struct timeout *new_to;
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen new_to = timeout_add_common
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen (old_to->source_filename, old_to->source_linenum,
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen old_to->callback, old_to->context);
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen new_to->one_shot = old_to->one_shot;
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen new_to->msecs = old_to->msecs;
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen new_to->next_run = old_to->next_run;
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen if (old_to->item.idx != UINT_MAX)
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen priorityq_add(new_to->ioloop->timeouts, &new_to->item);
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen else if (!new_to->one_shot) {
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen i_assert(new_to->msecs > 0);
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen array_append(&new_to->ioloop->timeouts_new, &new_to, 1);
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen }
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen return new_to;
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen}
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainenstatic void timeout_free(struct timeout *timeout)
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen{
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen if (timeout->ctx != NULL)
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen io_loop_context_unref(&timeout->ctx);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_free(timeout);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenvoid timeout_remove(struct timeout **_timeout)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct timeout *timeout = *_timeout;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct ioloop *ioloop = timeout->ioloop;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen *_timeout = NULL;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (timeout->item.idx != UINT_MAX)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen priorityq_remove(timeout->ioloop->timeouts, &timeout->item);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen else if (!timeout->one_shot && timeout->msecs > 0) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct timeout *const *to_idx;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen array_foreach(&ioloop->timeouts_new, to_idx) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (*to_idx == timeout) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen array_delete(&ioloop->timeouts_new,
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen array_foreach_idx(&ioloop->timeouts_new, to_idx), 1);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen break;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen }
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen }
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen }
686ad6d723004b807fd558f3ef9d1f88afa7e127Timo Sirainen timeout_free(timeout);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenstatic void ATTR_NULL(2)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainentimeout_reset_timeval(struct timeout *timeout, struct timeval *tv_now)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (timeout->item.idx == UINT_MAX)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen return;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout_update_next(timeout, tv_now);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (timeout->msecs <= 1) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen /* if we came here from io_loop_handle_timeouts(),
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen next_run must be larger than tv_now or we could go to
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen infinite loop. +1000 to get 1 ms further, another +1000 to
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen account for timeout_update_next()'s truncation. */
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout->next_run.tv_usec += 2000;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (timeout->next_run.tv_usec >= 1000000) {
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen timeout->next_run.tv_sec++;
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen timeout->next_run.tv_usec -= 1000000;
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen }
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen }
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen i_assert(tv_now == NULL ||
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout->next_run.tv_sec > tv_now->tv_sec ||
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen (timeout->next_run.tv_sec == tv_now->tv_sec &&
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout->next_run.tv_usec > tv_now->tv_usec));
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen priorityq_remove(timeout->ioloop->timeouts, &timeout->item);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen priorityq_add(timeout->ioloop->timeouts, &timeout->item);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenvoid timeout_reset(struct timeout *timeout)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_assert(!timeout->one_shot);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout_reset_timeval(timeout, NULL);
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen}
31ed209b28f30bcc2ce20de07aaa43d432126e24Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenstatic int timeout_get_wait_time(struct timeout *timeout, struct timeval *tv_r,
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct timeval *tv_now)
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen{
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen int ret;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (tv_now->tv_sec == 0) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (gettimeofday(tv_now, NULL) < 0)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_fatal("gettimeofday(): %m");
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen }
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen tv_r->tv_sec = tv_now->tv_sec;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen tv_r->tv_usec = tv_now->tv_usec;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_assert(tv_r->tv_sec > 0);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_assert(timeout->next_run.tv_sec > 0);
86bde2c1838d1ce967fa2b394bb952004a4adcb7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen tv_r->tv_sec = timeout->next_run.tv_sec - tv_r->tv_sec;
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen tv_r->tv_usec = timeout->next_run.tv_usec - tv_r->tv_usec;
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen if (tv_r->tv_usec < 0) {
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen tv_r->tv_sec--;
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen tv_r->tv_usec += 1000000;
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen }
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen if (tv_r->tv_sec < 0 || (tv_r->tv_sec == 0 && tv_r->tv_usec < 1000)) {
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen tv_r->tv_sec = 0;
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen tv_r->tv_usec = 0;
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen return 0;
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen }
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen if (tv_r->tv_sec > INT_MAX/1000-1)
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen tv_r->tv_sec = INT_MAX/1000-1;
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen /* round wait times up to next millisecond */
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen ret = tv_r->tv_sec * 1000 + (tv_r->tv_usec + 999) / 1000;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_assert(ret > 0 && tv_r->tv_sec >= 0 && tv_r->tv_usec >= 0);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen return ret;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenint io_loop_get_wait_time(struct ioloop *ioloop, struct timeval *tv_r)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct timeval tv_now;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct priorityq_item *item;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct timeout *timeout;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen int msecs;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen item = priorityq_peek(ioloop->timeouts);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout = (struct timeout *)item;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen /* we need to see if there are pending IO waiting,
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if there is, we set msecs = 0 to ensure they are
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen processed without delay */
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (timeout == NULL && ioloop->io_pending_count == 0) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen /* no timeouts. use INT_MAX msecs for timeval and
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen return -1 for poll/epoll infinity. */
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen tv_r->tv_sec = INT_MAX / 1000;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen tv_r->tv_usec = 0;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen ioloop->next_max_time = (1ULL << (TIME_T_MAX_BITS-1)) - 1;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen return -1;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen }
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (ioloop->io_pending_count > 0) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (gettimeofday(&tv_now, NULL) < 0)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_fatal("gettimeofday(): %m");
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen msecs = 0;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen tv_r->tv_sec = 0;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen tv_r->tv_usec = 0;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen } else {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen tv_now.tv_sec = 0;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen msecs = timeout_get_wait_time(timeout, tv_r, &tv_now);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen }
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen ioloop->next_max_time = (tv_now.tv_sec + msecs/1000) + 1;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen /* update ioloop_timeval - this is meant for io_loop_handle_timeouts()'s
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen ioloop_wait_usecs calculation. normally after this we go to the
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen ioloop and after that we update ioloop_timeval immediately again. */
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen ioloop_timeval = tv_now;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen ioloop_time = tv_now.tv_sec;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen return msecs;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainenstatic int timeout_cmp(const void *p1, const void *p2)
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen const struct timeout *to1 = p1, *to2 = p2;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen return timeval_cmp(&to1->next_run, &to2->next_run);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenstatic void io_loop_default_time_moved(time_t old_time, time_t new_time)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (old_time > new_time) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_warning("Time moved backwards by %ld seconds.",
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen (long)(old_time - new_time));
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen }
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenstatic void io_loop_timeouts_start_new(struct ioloop *ioloop)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct timeout *const *to_idx;
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (array_count(&ioloop->timeouts_new) == 0)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen return;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_loop_time_refresh();
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen array_foreach(&ioloop->timeouts_new, to_idx) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct timeout *timeout= *to_idx;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_assert(timeout->next_run.tv_sec == 0 &&
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout->next_run.tv_usec == 0);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_assert(!timeout->one_shot);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_assert(timeout->msecs > 0);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout_update_next(timeout, &ioloop_timeval);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen priorityq_add(ioloop->timeouts, &timeout->item);
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen }
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen array_clear(&ioloop->timeouts_new);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainenstatic void io_loop_timeouts_update(struct ioloop *ioloop, long diff_secs)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct priorityq_item *const *items;
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen unsigned int i, count;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen count = priorityq_count(ioloop->timeouts);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen items = priorityq_items(ioloop->timeouts);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen for (i = 0; i < count; i++) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct timeout *to = (struct timeout *)items[i];
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen to->next_run.tv_sec += diff_secs;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen }
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenstatic void io_loops_timeouts_update(long diff_secs)
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct ioloop *ioloop;
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen for (ioloop = current_ioloop; ioloop != NULL; ioloop = ioloop->prev)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_loop_timeouts_update(ioloop, diff_secs);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
08837f59c1466ec0f533f120b167f2a3e87da738Timo Sirainenstatic void io_loop_handle_timeouts_real(struct ioloop *ioloop)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct priorityq_item *item;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct timeval tv, tv_call, prev_ioloop_timeval = ioloop_timeval;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen data_stack_frame_t t_id;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (gettimeofday(&ioloop_timeval, NULL) < 0)
115cf0320f679e4e63db25e7f44f47977b8338deTimo Sirainen i_fatal("gettimeofday(): %m");
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
115cf0320f679e4e63db25e7f44f47977b8338deTimo Sirainen /* Don't bother comparing usecs. */
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (unlikely(ioloop_time > ioloop_timeval.tv_sec)) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen /* time moved backwards */
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_loops_timeouts_update(-(long)(ioloop_time -
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen ioloop_timeval.tv_sec));
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen ioloop->time_moved_callback(ioloop_time,
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen ioloop_timeval.tv_sec);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen /* the callback may have slept, so check the time again. */
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (gettimeofday(&ioloop_timeval, NULL) < 0)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_fatal("gettimeofday(): %m");
b7c841fd6dc508d87db8b1b64ea1a8f4cb0b8971Timo Sirainen } else {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (unlikely(ioloop_timeval.tv_sec >
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen ioloop->next_max_time)) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_loops_timeouts_update(ioloop_timeval.tv_sec -
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen ioloop->next_max_time);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen /* time moved forwards */
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen ioloop->time_moved_callback(ioloop->next_max_time,
b66d803de86bfb411165b3465b0d9ef64ecfe2a1Timo Sirainen ioloop_timeval.tv_sec);
459f60325f94f486ef057241b42d8a9e9c376fb4Timo Sirainen }
459f60325f94f486ef057241b42d8a9e9c376fb4Timo Sirainen ioloop->ioloop_wait_usecs +=
459f60325f94f486ef057241b42d8a9e9c376fb4Timo Sirainen timeval_diff_usecs(&ioloop_timeval, &prev_ioloop_timeval);
459f60325f94f486ef057241b42d8a9e9c376fb4Timo Sirainen }
459f60325f94f486ef057241b42d8a9e9c376fb4Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen ioloop_time = ioloop_timeval.tv_sec;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen tv_call = ioloop_timeval;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen while ((item = priorityq_peek(ioloop->timeouts)) != NULL) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct timeout *timeout = (struct timeout *)item;
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen /* use tv_call to make sure we don't get to infinite loop in
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen case callbacks update ioloop_timeval. */
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen if (timeout_get_wait_time(timeout, &tv, &tv_call) > 0)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen break;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (timeout->one_shot) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen /* remove timeout from queue */
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen priorityq_remove(timeout->ioloop->timeouts, &timeout->item);
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen } else {
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen /* update timeout's next_run and reposition it in the queue */
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout_reset_timeval(timeout, &tv_call);
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen }
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (timeout->ctx != NULL)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_loop_context_activate(timeout->ctx);
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen t_id = t_push_named("ioloop timeout handler %p",
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen (void *)timeout->callback);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen timeout->callback(timeout->context);
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen if (!t_pop(&t_id)) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_panic("Leaked a t_pop() call in timeout handler %p",
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen (void *)timeout->callback);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen }
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (ioloop->cur_ctx != NULL)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_loop_context_deactivate(ioloop->cur_ctx);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen }
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenvoid io_loop_handle_timeouts(struct ioloop *ioloop)
c2fbbf7515aa419dc8b2d62a3c2bb0471d51a391Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen T_BEGIN {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_loop_handle_timeouts_real(ioloop);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen } T_END;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenvoid io_loop_call_io(struct io *io)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct ioloop *ioloop = io->ioloop;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen data_stack_frame_t t_id;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (io->pending) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_assert(ioloop->io_pending_count > 0);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen ioloop->io_pending_count--;
51327f2489a4e0e615eb9f7d921473cf8512bb79Timo Sirainen io->pending = FALSE;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen }
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
3a508ab3b10ff08889f3046a6bbf8553b55e3d44Timo Sirainen if (io->ctx != NULL)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_loop_context_activate(io->ctx);
cddfd1355db6b60c71d7ee3c0b4f23b3efcc9ad1Timo Sirainen t_id = t_push_named("ioloop handler %p",
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen (void *)io->callback);
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen io->callback(io->context);
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen if (!t_pop(&t_id)) {
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen i_panic("Leaked a t_pop() call in I/O handler %p",
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen (void *)io->callback);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen }
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (ioloop->cur_ctx != NULL)
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen io_loop_context_deactivate(ioloop->cur_ctx);
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen}
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainenvoid io_loop_run(struct ioloop *ioloop)
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (ioloop->handler_context == NULL)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_loop_initialize_handler(ioloop);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (ioloop->cur_ctx != NULL)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_loop_context_unref(&ioloop->cur_ctx);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen /* recursive io_loop_run() isn't allowed for the same ioloop.
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen it can break backends. */
5caf685b62a30b1f935b80a3f9f1bdcefb63a38cTimo Sirainen i_assert(!ioloop->iolooping);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen ioloop->iolooping = TRUE;
5caf685b62a30b1f935b80a3f9f1bdcefb63a38cTimo Sirainen
5caf685b62a30b1f935b80a3f9f1bdcefb63a38cTimo Sirainen ioloop->running = TRUE;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen while (ioloop->running)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_loop_handler_run(ioloop);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen ioloop->iolooping = FALSE;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenstatic void io_loop_call_pending(struct ioloop *ioloop)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen struct io_file *io;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen while (ioloop->io_pending_count > 0) {
be6bde0e1f40db97871b657603188fc9041f1d04Timo Sirainen io = ioloop->io_files;
be6bde0e1f40db97871b657603188fc9041f1d04Timo Sirainen do {
be6bde0e1f40db97871b657603188fc9041f1d04Timo Sirainen ioloop->next_io_file = io->next;
be6bde0e1f40db97871b657603188fc9041f1d04Timo Sirainen if (io->io.pending)
be6bde0e1f40db97871b657603188fc9041f1d04Timo Sirainen io_loop_call_io(&io->io);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen if (ioloop->io_pending_count == 0)
be6bde0e1f40db97871b657603188fc9041f1d04Timo Sirainen break;
be6bde0e1f40db97871b657603188fc9041f1d04Timo Sirainen io = ioloop->next_io_file;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen } while (io != NULL);
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen }
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenvoid io_loop_handler_run(struct ioloop *ioloop)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen io_loop_timeouts_start_new(ioloop);
0d1b8b6bec79746c5d89d57dd8c1688946bd9237Josef 'Jeff' Sipek io_loop_handler_run_internal(ioloop);
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen io_loop_call_pending(ioloop);
da2f9bc5d477bdfee1773d7dcbcdd5a293c7d48cTimo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenvoid io_loop_stop(struct ioloop *ioloop)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen ioloop->running = FALSE;
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen}
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainenvoid io_loop_set_running(struct ioloop *ioloop)
d43bed2d458520fd01c28229ce2b178a4593a4a7Timo Sirainen{
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen ioloop->running = TRUE;
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen}
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainenvoid io_loop_set_max_fd_count(struct ioloop *ioloop, unsigned int max_fds)
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen{
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen ioloop->max_fd_count = max_fds;
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen}
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainenbool io_loop_is_running(struct ioloop *ioloop)
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen{
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen return ioloop->running;
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen}
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainenvoid io_loop_time_refresh(void)
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen{
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen if (gettimeofday(&ioloop_timeval, NULL) < 0)
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen i_fatal("gettimeofday(): %m");
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen ioloop_time = ioloop_timeval.tv_sec;
}
struct ioloop *io_loop_create(void)
{
struct ioloop *ioloop;
/* initialize time */
if (gettimeofday(&ioloop_timeval, NULL) < 0)
i_fatal("gettimeofday(): %m");
ioloop_time = ioloop_timeval.tv_sec;
ioloop = i_new(struct ioloop, 1);
ioloop->timeouts = priorityq_init(timeout_cmp, 32);
i_array_init(&ioloop->timeouts_new, 8);
ioloop->time_moved_callback = current_ioloop != NULL ?
current_ioloop->time_moved_callback :
io_loop_default_time_moved;
ioloop->prev = current_ioloop;
io_loop_set_current(ioloop);
return ioloop;
}
void io_loop_destroy(struct ioloop **_ioloop)
{
struct ioloop *ioloop = *_ioloop;
struct timeout *const *to_idx;
struct priorityq_item *item;
*_ioloop = NULL;
/* ->prev won't work unless loops are destroyed in create order */
i_assert(ioloop == current_ioloop);
io_loop_set_current(current_ioloop->prev);
if (ioloop->notify_handler_context != NULL)
io_loop_notify_handler_deinit(ioloop);
while (ioloop->io_files != NULL) {
struct io_file *io = ioloop->io_files;
struct io *_io = &io->io;
i_warning("I/O leak: %p (%s:%u, fd %d)",
(void *)io->io.callback,
io->io.source_filename,
io->io.source_linenum, io->fd);
io_remove(&_io);
}
i_assert(ioloop->io_pending_count == 0);
array_foreach(&ioloop->timeouts_new, to_idx) {
struct timeout *to = *to_idx;
i_warning("Timeout leak: %p (%s:%u)", (void *)to->callback,
to->source_filename,
to->source_linenum);
timeout_free(to);
}
array_free(&ioloop->timeouts_new);
while ((item = priorityq_pop(ioloop->timeouts)) != NULL) {
struct timeout *to = (struct timeout *)item;
i_warning("Timeout leak: %p (%s:%u)", (void *)to->callback,
to->source_filename,
to->source_linenum);
timeout_free(to);
}
priorityq_deinit(&ioloop->timeouts);
if (ioloop->handler_context != NULL)
io_loop_handler_deinit(ioloop);
if (ioloop->cur_ctx != NULL)
io_loop_context_deactivate(ioloop->cur_ctx);
i_free(ioloop);
}
void io_loop_set_time_moved_callback(struct ioloop *ioloop,
io_loop_time_moved_callback_t *callback)
{
ioloop->time_moved_callback = callback;
}
static void io_switch_callbacks_free(void)
{
array_free(&io_switch_callbacks);
}
void io_loop_set_current(struct ioloop *ioloop)
{
io_switch_callback_t *const *callbackp;
struct ioloop *prev_ioloop = current_ioloop;
current_ioloop = ioloop;
if (array_is_created(&io_switch_callbacks)) {
array_foreach(&io_switch_callbacks, callbackp)
(*callbackp)(prev_ioloop);
}
}
void io_loop_add_switch_callback(io_switch_callback_t *callback)
{
if (!array_is_created(&io_switch_callbacks)) {
i_array_init(&io_switch_callbacks, 4);
lib_atexit(io_switch_callbacks_free);
}
array_append(&io_switch_callbacks, &callback, 1);
}
void io_loop_remove_switch_callback(io_switch_callback_t *callback)
{
io_switch_callback_t *const *callbackp;
unsigned int idx;
array_foreach(&io_switch_callbacks, callbackp) {
if (*callbackp == callback) {
idx = array_foreach_idx(&io_switch_callbacks, callbackp);
array_delete(&io_switch_callbacks, idx, 1);
return;
}
}
i_unreached();
}
struct ioloop_context *io_loop_context_new(struct ioloop *ioloop)
{
struct ioloop_context *ctx;
ctx = i_new(struct ioloop_context, 1);
ctx->refcount = 2;
ctx->ioloop = ioloop;
i_array_init(&ctx->callbacks, 4);
if (ioloop->cur_ctx != NULL)
io_loop_context_unref(&ioloop->cur_ctx);
ioloop->cur_ctx = ctx;
return ctx;
}
void io_loop_context_ref(struct ioloop_context *ctx)
{
i_assert(ctx->refcount > 0);
ctx->refcount++;
}
void io_loop_context_unref(struct ioloop_context **_ctx)
{
struct ioloop_context *ctx = *_ctx;
*_ctx = NULL;
i_assert(ctx->refcount > 0);
if (--ctx->refcount > 0)
return;
/* cur_ctx itself keeps a reference */
i_assert(ctx->ioloop->cur_ctx != ctx);
array_free(&ctx->callbacks);
i_free(ctx);
}
#undef io_loop_context_add_callbacks
void io_loop_context_add_callbacks(struct ioloop_context *ctx,
io_callback_t *activate,
io_callback_t *deactivate, void *context)
{
struct ioloop_context_callback cb;
memset(&cb, 0, sizeof(cb));
cb.activate = activate;
cb.deactivate = deactivate;
cb.context = context;
array_append(&ctx->callbacks, &cb, 1);
}
#undef io_loop_context_remove_callbacks
void io_loop_context_remove_callbacks(struct ioloop_context *ctx,
io_callback_t *activate,
io_callback_t *deactivate, void *context)
{
struct ioloop_context_callback *cb;
array_foreach_modifiable(&ctx->callbacks, cb) {
if (cb->context == context &&
cb->activate == activate && cb->deactivate == deactivate) {
/* simply mark it as deleted, since we could get
here from activate/deactivate loop */
cb->activate = NULL;
cb->deactivate = NULL;
cb->context = NULL;
return;
}
}
i_panic("io_loop_context_remove_callbacks() context not found");
}
static void
io_loop_context_remove_deleted_callbacks(struct ioloop_context *ctx)
{
const struct ioloop_context_callback *cbs;
unsigned int i, count;
cbs = array_get(&ctx->callbacks, &count);
for (i = 0; i < count; ) {
if (cbs[i].activate != NULL)
i++;
else {
array_delete(&ctx->callbacks, i, 1);
cbs = array_get(&ctx->callbacks, &count);
}
}
}
void io_loop_context_activate(struct ioloop_context *ctx)
{
struct ioloop_context_callback *cb;
i_assert(ctx->ioloop->cur_ctx == NULL);
ctx->ioloop->cur_ctx = ctx;
io_loop_context_ref(ctx);
array_foreach_modifiable(&ctx->callbacks, cb) {
i_assert(!cb->activated);
if (cb->activate != NULL)
cb->activate(cb->context);
cb->activated = TRUE;
}
}
void io_loop_context_deactivate(struct ioloop_context *ctx)
{
struct ioloop_context_callback *cb;
i_assert(ctx->ioloop->cur_ctx != NULL);
array_foreach_modifiable(&ctx->callbacks, cb) {
if (!cb->activated) {
/* we just added this callback. don't deactivate it
before it gets first activated. */
} else {
if (cb->deactivate != NULL)
cb->deactivate(cb->context);
cb->activated = FALSE;
}
}
ctx->ioloop->cur_ctx = NULL;
io_loop_context_remove_deleted_callbacks(ctx);
io_loop_context_unref(&ctx);
}
struct ioloop_context *io_loop_get_current_context(struct ioloop *ioloop)
{
return ioloop->cur_ctx;
}
struct io *io_loop_move_io(struct io **_io)
{
struct io *old_io = *_io;
struct io_file *old_io_file, *new_io_file;
i_assert((old_io->condition & IO_NOTIFY) == 0);
if (old_io->ioloop == current_ioloop)
return old_io;
old_io_file = (struct io_file *)old_io;
new_io_file = io_add_file(old_io_file->fd, old_io->condition,
old_io->source_filename,
old_io->source_linenum,
old_io->callback, old_io->context);
if (old_io_file->istream != NULL) {
/* reference before io_remove() */
new_io_file->istream = old_io_file->istream;
i_stream_ref(new_io_file->istream);
}
if (old_io->pending)
io_set_pending(&new_io_file->io);
io_remove(_io);
if (new_io_file->istream != NULL) {
/* update istream io after it was removed with io_remove() */
i_stream_set_io(new_io_file->istream, &new_io_file->io);
}
return &new_io_file->io;
}
struct timeout *io_loop_move_timeout(struct timeout **_timeout)
{
struct timeout *new_to, *old_to = *_timeout;
if (old_to->ioloop == current_ioloop)
return old_to;
new_to = timeout_copy(old_to);
timeout_remove(_timeout);
return new_to;
}
bool io_loop_have_ios(struct ioloop *ioloop)
{
return ioloop->io_files != NULL;
}
bool io_loop_have_immediate_timeouts(struct ioloop *ioloop)
{
struct timeval tv;
return io_loop_get_wait_time(ioloop, &tv) == 0;
}
uint64_t io_loop_get_wait_usecs(struct ioloop *ioloop)
{
return ioloop->ioloop_wait_usecs;
}
enum io_condition io_loop_find_fd_conditions(struct ioloop *ioloop, int fd)
{
enum io_condition conditions = 0;
struct io_file *io;
i_assert(fd >= 0);
for (io = ioloop->io_files; io != NULL; io = io->next) {
if (io->fd == fd)
conditions |= io->io.condition;
}
return conditions;
}