Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file select.h |
| 2 | * select loop abstraction. |
| 3 | */ |
| 4 | |
Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 5 | #pragma once |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 6 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 7 | #include <osmocom/core/linuxlist.h> |
Philipp Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 8 | #include <stdbool.h> |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 9 | #include <time.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 10 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 11 | /*! \defgroup select Select loop abstraction |
| 12 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 13 | * \file select.h */ |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 14 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 15 | /*! Indicate interest in reading from the file descriptor */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 16 | #define BSC_FD_READ 0x0001 |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 17 | /*! Indicate interest in writing to the file descriptor */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 18 | #define BSC_FD_WRITE 0x0002 |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 19 | /*! Indicate interest in exceptions from the file descriptor */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 20 | #define BSC_FD_EXCEPT 0x0004 |
| 21 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 22 | /*! Structure representing a file dsecriptor */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 23 | struct osmo_fd { |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 24 | /*! linked list for internal management */ |
| 25 | struct llist_head list; |
| 26 | /*! actual operating-system level file decriptor */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 27 | int fd; |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 28 | /*! bit-mask or of \ref BSC_FD_READ, \ref BSC_FD_WRITE and/or |
| 29 | * \ref BSC_FD_EXCEPT */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 30 | unsigned int when; |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 31 | /*! call-back function to be called once file descriptor becomes |
| 32 | * available */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 33 | int (*cb)(struct osmo_fd *fd, unsigned int what); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 34 | /*! data pointer passed through to call-back function */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 35 | void *data; |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 36 | /*! private number, extending \a data */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 37 | unsigned int priv_nr; |
| 38 | }; |
| 39 | |
Harald Welte | 6c0a0e6 | 2017-08-12 11:43:14 +0200 | [diff] [blame] | 40 | void 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 Maier | b288853 | 2016-12-09 14:07:18 +0100 | [diff] [blame] | 44 | bool osmo_fd_is_registered(struct osmo_fd *fd); |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 45 | int osmo_fd_register(struct osmo_fd *fd); |
| 46 | void osmo_fd_unregister(struct osmo_fd *fd); |
Harald Welte | ea91a51 | 2017-07-13 14:28:30 +0200 | [diff] [blame] | 47 | void osmo_fd_close(struct osmo_fd *fd); |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 48 | int osmo_select_main(int polling); |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 49 | |
Harald Welte | 6c33ae2 | 2016-03-19 21:17:58 +0100 | [diff] [blame] | 50 | struct osmo_fd *osmo_fd_get_by_fd(int fd); |
| 51 | |
Holger Hans Peter Freyther | 61f2888 | 2016-03-21 09:55:05 +0100 | [diff] [blame] | 52 | /* |
| 53 | * foreign event loop integration |
| 54 | */ |
| 55 | int osmo_fd_fill_fds(void *readset, void *writeset, void *exceptset); |
| 56 | int osmo_fd_disp_fds(void *readset, void *writeset, void *exceptset); |
| 57 | |
Harald Welte | ea4d893 | 2017-12-03 16:13:39 +0100 | [diff] [blame] | 58 | /* timerfd integration */ |
| 59 | int osmo_timerfd_disable(struct osmo_fd *ofd); |
| 60 | int osmo_timerfd_schedule(struct osmo_fd *ofd, const struct timespec *first, |
| 61 | const struct timespec *interval); |
| 62 | int osmo_timerfd_setup(struct osmo_fd *ofd, int (*cb)(struct osmo_fd *, unsigned int), void *data); |
| 63 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 64 | /*! @} */ |