blob: b6fed3c7369d212a752d1a0733e068fb7825c24e [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 Welteec8b4502010-02-20 20:34:29 +01009
Harald Welteba6988b2011-08-17 12:46:48 +020010/*! \defgroup select Select loop abstraction
11 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020012 * \file select.h */
Harald Weltebd598e32011-08-16 23:26:52 +020013
Neels Hofmeyr87e45502017-06-20 00:17:59 +020014/*! Indicate interest in reading from the file descriptor */
Harald Welteec8b4502010-02-20 20:34:29 +010015#define BSC_FD_READ 0x0001
Neels Hofmeyr87e45502017-06-20 00:17:59 +020016/*! Indicate interest in writing to the file descriptor */
Harald Welteec8b4502010-02-20 20:34:29 +010017#define BSC_FD_WRITE 0x0002
Neels Hofmeyr87e45502017-06-20 00:17:59 +020018/*! Indicate interest in exceptions from the file descriptor */
Harald Welteec8b4502010-02-20 20:34:29 +010019#define BSC_FD_EXCEPT 0x0004
20
Neels Hofmeyr87e45502017-06-20 00:17:59 +020021/*! Structure representing a file dsecriptor */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020022struct osmo_fd {
Harald Weltebd598e32011-08-16 23:26:52 +020023 /*! linked list for internal management */
24 struct llist_head list;
25 /*! actual operating-system level file decriptor */
Harald Welteec8b4502010-02-20 20:34:29 +010026 int fd;
Harald Weltebd598e32011-08-16 23:26:52 +020027 /*! bit-mask or of \ref BSC_FD_READ, \ref BSC_FD_WRITE and/or
28 * \ref BSC_FD_EXCEPT */
Harald Welteec8b4502010-02-20 20:34:29 +010029 unsigned int when;
Harald Weltebd598e32011-08-16 23:26:52 +020030 /*! call-back function to be called once file descriptor becomes
31 * available */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020032 int (*cb)(struct osmo_fd *fd, unsigned int what);
Harald Weltebd598e32011-08-16 23:26:52 +020033 /*! data pointer passed through to call-back function */
Harald Welteec8b4502010-02-20 20:34:29 +010034 void *data;
Harald Weltebd598e32011-08-16 23:26:52 +020035 /*! private number, extending \a data */
Harald Welteec8b4502010-02-20 20:34:29 +010036 unsigned int priv_nr;
37};
38
Harald Welte6c0a0e62017-08-12 11:43:14 +020039void osmo_fd_setup(struct osmo_fd *ofd, int fd, unsigned int when,
40 int (*cb)(struct osmo_fd *fd, unsigned int what),
41 void *data, unsigned int priv_nr);
42
Philipp Maierb2888532016-12-09 14:07:18 +010043bool osmo_fd_is_registered(struct osmo_fd *fd);
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020044int osmo_fd_register(struct osmo_fd *fd);
45void osmo_fd_unregister(struct osmo_fd *fd);
Harald Welteea91a512017-07-13 14:28:30 +020046void osmo_fd_close(struct osmo_fd *fd);
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020047int osmo_select_main(int polling);
Harald Weltebd598e32011-08-16 23:26:52 +020048
Harald Welte6c33ae22016-03-19 21:17:58 +010049struct osmo_fd *osmo_fd_get_by_fd(int fd);
50
Holger Hans Peter Freyther61f28882016-03-21 09:55:05 +010051/*
52 * foreign event loop integration
53 */
54int osmo_fd_fill_fds(void *readset, void *writeset, void *exceptset);
55int osmo_fd_disp_fds(void *readset, void *writeset, void *exceptset);
56
Sylvain Munautdca7d2c2012-04-18 21:53:23 +020057/*! @} */