ioloop.h revision 007d354a674fb3ddf49db160cf050cf61270a1a0
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#ifndef __IOLOOP_H
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#define __IOLOOP_H
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
49e358eebea107aad9919dcc4bd88cee8519ba2eTimo Sirainen#include <sys/time.h>
49e358eebea107aad9919dcc4bd88cee8519ba2eTimo Sirainen#include <time.h>
49e358eebea107aad9919dcc4bd88cee8519ba2eTimo Sirainen
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen#define IO_READ (1 << 0)
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen#define IO_WRITE (1 << 1)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainenstruct io;
03f5c621d06d6b6d77a145196c9633a7aa64dc78Timo Sirainenstruct timeout;
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainenstruct ioloop;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
da2aa032ccfa8e7e4a4380ef738014549f4d2c2dTimo Sirainentypedef void io_callback_t(void *context);
da2aa032ccfa8e7e4a4380ef738014549f4d2c2dTimo Sirainentypedef void timeout_callback_t(void *context);
411d6baa37f31d90730e90c4a28c43e1974bbe58Timo Sirainen
7e1f68ad71d3485f1882142837b01f7a98ca8467Timo Sirainen/* Time when the I/O loop started calling handlers.
7e1f68ad71d3485f1882142837b01f7a98ca8467Timo Sirainen Can be used instead of time(NULL). */
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainenextern time_t ioloop_time;
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainenextern struct timeval ioloop_timeval;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainenextern struct timezone ioloop_timezone;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
5ac0b0bf32898c63da086ae169674ecac151a31eTimo Sirainen/* I/O listeners - you can create different handlers for IO_READ and IO_WRITE,
5ac0b0bf32898c63da086ae169674ecac151a31eTimo Sirainen but make sure you don't create multiple handlers of same type, it's not
2526d52441ef368215ab6bf04fd0356d3b09d235Timo Sirainen checked and removing one will stop the other from working as well. */
2526d52441ef368215ab6bf04fd0356d3b09d235Timo Sirainenstruct io *io_add(int fd, int condition, io_callback_t *callback, void *context);
09801f106cd531a28b4e03ec665e44c421264560Timo Sirainenvoid io_remove(struct io *io);
09801f106cd531a28b4e03ec665e44c421264560Timo Sirainen
09801f106cd531a28b4e03ec665e44c421264560Timo Sirainen/* Timeout handlers */
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainenstruct timeout *timeout_add(unsigned int msecs, timeout_callback_t *callback,
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen void *context);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainenvoid timeout_remove(struct timeout *timeout);
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainen
10c96a244935de4add8213ba0b894178dfb889a5Timo Sirainenvoid io_loop_run(struct ioloop *ioloop);
bdcb00145ad87765e3fd22d4ebc4d2c029a326b9Timo Sirainenvoid io_loop_stop(struct ioloop *ioloop); /* safe to run in signal handler */
bdcb00145ad87765e3fd22d4ebc4d2c029a326b9Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainenint io_loop_is_running(struct ioloop *ioloop);
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen/* call these if you wish to run the iteration only once */
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainenvoid io_loop_set_running(struct ioloop *ioloop);
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainenvoid io_loop_handler_run(struct ioloop *ioloop);
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainenstruct ioloop *io_loop_create(pool_t pool);
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainenvoid io_loop_destroy(struct ioloop *ioloop);
cd56a23e21f1df3f79648cf07e2f4385e2fadebbTimo Sirainen
cd56a23e21f1df3f79648cf07e2f4385e2fadebbTimo Sirainen#endif
cd56a23e21f1df3f79648cf07e2f4385e2fadebbTimo Sirainen