blob: 1ba6b8322cb42a74e20e9a3e86f071d3829c0598 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file select.h
2 * select loop abstraction.
3 */
4
Sylvain Munaut12ba7782014-06-16 10:13:40 +02005#pragma once
Harald Welteec8b4502010-02-20 20:34:29 +01006
Pablo Neira Ayuso83419342011-03-22 16:36:13 +01007#include <osmocom/core/linuxlist.h>
Philipp Maierb2888532016-12-09 14:07:18 +01008#include <stdbool.h>
Harald Welteea4d8932017-12-03 16:13:39 +01009#include <time.h>
Harald Welteec8b4502010-02-20 20:34:29 +010010
Harald Welteba6988b2011-08-17 12:46:48 +020011/*! \defgroup select Select loop abstraction
12 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020013 * \file select.h */
Harald Weltebd598e32011-08-16 23:26:52 +020014
Neels Hofmeyr87e45502017-06-20 00:17:59 +020015/*! Indicate interest in reading from the file descriptor */
Harald Welteec8b4502010-02-20 20:34:29 +010016#define BSC_FD_READ 0x0001
Neels Hofmeyr87e45502017-06-20 00:17:59 +020017/*! Indicate interest in writing to the file descriptor */
Harald Welteec8b4502010-02-20 20:34:29 +010018#define BSC_FD_WRITE 0x0002
Neels Hofmeyr87e45502017-06-20 00:17:59 +020019/*! Indicate interest in exceptions from the file descriptor */
Harald Welteec8b4502010-02-20 20:34:29 +010020#define BSC_FD_EXCEPT 0x0004
21
Neels Hofmeyr87e45502017-06-20 00:17:59 +020022/*! Structure representing a file dsecriptor */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020023struct osmo_fd {
Harald Weltebd598e32011-08-16 23:26:52 +020024 /*! linked list for internal management */
25 struct llist_head list;
26 /*! actual operating-system level file decriptor */
Harald Welteec8b4502010-02-20 20:34:29 +010027 int fd;
Harald Weltebd598e32011-08-16 23:26:52 +020028 /*! bit-mask or of \ref BSC_FD_READ, \ref BSC_FD_WRITE and/or
29 * \ref BSC_FD_EXCEPT */
Harald Welteec8b4502010-02-20 20:34:29 +010030 unsigned int when;
Harald Weltebd598e32011-08-16 23:26:52 +020031 /*! call-back function to be called once file descriptor becomes
32 * available */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020033 int (*cb)(struct osmo_fd *fd, unsigned int what);
Harald Weltebd598e32011-08-16 23:26:52 +020034 /*! data pointer passed through to call-back function */
Harald Welteec8b4502010-02-20 20:34:29 +010035 void *data;
Harald Weltebd598e32011-08-16 23:26:52 +020036 /*! private number, extending \a data */
Harald Welteec8b4502010-02-20 20:34:29 +010037 unsigned int priv_nr;
38};
39
Harald Welte6c0a0e62017-08-12 11:43:14 +020040void osmo_fd_setup(struct osmo_fd *ofd, int fd, unsigned int when,
41 int (*cb)(struct osmo_fd *fd, unsigned int what),
42 void *data, unsigned int priv_nr);
43
Philipp Maierb2888532016-12-09 14:07:18 +010044bool osmo_fd_is_registered(struct osmo_fd *fd);
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020045int osmo_fd_register(struct osmo_fd *fd);
46void osmo_fd_unregister(struct osmo_fd *fd);
Harald Welteea91a512017-07-13 14:28:30 +020047void osmo_fd_close(struct osmo_fd *fd);
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020048int osmo_select_main(int polling);
Harald Weltebd598e32011-08-16 23:26:52 +020049
Harald Welte6c33ae22016-03-19 21:17:58 +010050struct osmo_fd *osmo_fd_get_by_fd(int fd);
51
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +010052/*
53 * foreign event loop integration
54 */
55int osmo_fd_fill_fds(void *readset, void *writeset, void *exceptset);
56int osmo_fd_disp_fds(void *readset, void *writeset, void *exceptset);
57
Harald Welteea4d8932017-12-03 16:13:39 +010058/* timerfd integration */
59int osmo_timerfd_disable(struct osmo_fd *ofd);
60int osmo_timerfd_schedule(struct osmo_fd *ofd, const struct timespec *first,
61 const struct timespec *interval);
62int osmo_timerfd_setup(struct osmo_fd *ofd, int (*cb)(struct osmo_fd *, unsigned int), void *data);
63
Sylvain Munautdca7d2c2012-04-18 21:53:23 +020064/*! @} */