blob: a200b6f3cf789d7f7268ded7cf9d025eef4d8beb [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 Welte16886992019-03-20 10:26:39 +010016#define OSMO_FD_READ 0x0001
Neels Hofmeyr87e45502017-06-20 00:17:59 +020017/*! Indicate interest in writing to the file descriptor */
Harald Welte16886992019-03-20 10:26:39 +010018#define OSMO_FD_WRITE 0x0002
Neels Hofmeyr87e45502017-06-20 00:17:59 +020019/*! Indicate interest in exceptions from the file descriptor */
Harald Welte16886992019-03-20 10:26:39 +010020#define OSMO_FD_EXCEPT 0x0004
21
22/* legacy naming dating back to early OpenBSC / bsc_hack of 2008 */
23#define BSC_FD_READ OSMO_FD_READ
24#define BSC_FD_WRITE OSMO_FD_WRITE
25#define BSC_FD_EXCEPT OSMO_FD_EXCEPT
Harald Welteec8b4502010-02-20 20:34:29 +010026
Neels Hofmeyr87e45502017-06-20 00:17:59 +020027/*! Structure representing a file dsecriptor */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020028struct osmo_fd {
Harald Weltebd598e32011-08-16 23:26:52 +020029 /*! linked list for internal management */
30 struct llist_head list;
31 /*! actual operating-system level file decriptor */
Harald Welteec8b4502010-02-20 20:34:29 +010032 int fd;
Harald Welte16886992019-03-20 10:26:39 +010033 /*! bit-mask or of \ref OSMO_FD_READ, \ref OSMO_FD_WRITE and/or
34 * \ref OSMO_FD_EXCEPT */
Harald Welteec8b4502010-02-20 20:34:29 +010035 unsigned int when;
Harald Weltebd598e32011-08-16 23:26:52 +020036 /*! call-back function to be called once file descriptor becomes
37 * available */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020038 int (*cb)(struct osmo_fd *fd, unsigned int what);
Harald Weltebd598e32011-08-16 23:26:52 +020039 /*! data pointer passed through to call-back function */
Harald Welteec8b4502010-02-20 20:34:29 +010040 void *data;
Harald Weltebd598e32011-08-16 23:26:52 +020041 /*! private number, extending \a data */
Harald Welteec8b4502010-02-20 20:34:29 +010042 unsigned int priv_nr;
43};
44
Harald Welte6c0a0e62017-08-12 11:43:14 +020045void osmo_fd_setup(struct osmo_fd *ofd, int fd, unsigned int when,
46 int (*cb)(struct osmo_fd *fd, unsigned int what),
47 void *data, unsigned int priv_nr);
48
Philipp Maierb2888532016-12-09 14:07:18 +010049bool osmo_fd_is_registered(struct osmo_fd *fd);
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020050int osmo_fd_register(struct osmo_fd *fd);
51void osmo_fd_unregister(struct osmo_fd *fd);
Harald Welteea91a512017-07-13 14:28:30 +020052void osmo_fd_close(struct osmo_fd *fd);
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020053int osmo_select_main(int polling);
Harald Welte2d906112019-03-18 17:17:43 +010054int osmo_select_main_ctx(int polling);
Harald Weltebd598e32011-08-16 23:26:52 +020055
Harald Welte6c33ae22016-03-19 21:17:58 +010056struct osmo_fd *osmo_fd_get_by_fd(int fd);
57
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +010058/*
59 * foreign event loop integration
60 */
61int osmo_fd_fill_fds(void *readset, void *writeset, void *exceptset);
62int osmo_fd_disp_fds(void *readset, void *writeset, void *exceptset);
63
Harald Welteea4d8932017-12-03 16:13:39 +010064/* timerfd integration */
65int osmo_timerfd_disable(struct osmo_fd *ofd);
66int osmo_timerfd_schedule(struct osmo_fd *ofd, const struct timespec *first,
67 const struct timespec *interval);
68int osmo_timerfd_setup(struct osmo_fd *ofd, int (*cb)(struct osmo_fd *, unsigned int), void *data);
69
Sylvain Munautdca7d2c2012-04-18 21:53:23 +020070/*! @} */