Loading...
Searching...
No Matches
Select loop abstraction

select() loop abstraction More...

## Files

file | select.h
| libmnl integration
file | select.c
| select filedescriptor handling.

## Data Structures

struct | osmo_fd
| Structure representing a file dsecriptor. More...
struct | osmo_signalfd
struct | poll_state

## Macros

#define | OSMO_FD_READ 0x0001
| Indicate interest in reading from the file descriptor.
#define | OSMO_FD_WRITE 0x0002
| Indicate interest in writing to the file descriptor.
#define | OSMO_FD_EXCEPT 0x0004
| Indicate interest in exceptions from the file descriptor.
#define | OSMO_FD_MASK 0xFFFF
| Used as when_mask in osmo_fd_update_when()
#define | BSC_FD_READ OSMO_FD_READ
#define | BSC_FD_WRITE OSMO_FD_WRITE
#define | BSC_FD_EXCEPT OSMO_FD_EXCEPT

## Typedefs

typedef void | osmo_signalfd_cb(struct osmo_signalfd *osfd, const struct signalfd_siginfo *fdsi)

## Functions

void | osmo_fd_setup (struct osmo_fd *ofd, int fd, unsigned int when, int(*cb)(struct osmo_fd *fd, unsigned int what), void *data, unsigned int priv_nr)
| Set up an osmo-fd.
void | osmo_fd_update_when (struct osmo_fd *ofd, unsigned int when_mask, unsigned int when)
| Update the 'when' field of osmo_fd.
static void | osmo_fd_read_enable (struct osmo_fd *ofd)
static void | osmo_fd_read_disable (struct osmo_fd *ofd)
static void | osmo_fd_write_enable (struct osmo_fd *ofd)
static void | osmo_fd_write_disable (struct osmo_fd *ofd)
bool | osmo_fd_is_registered (struct osmo_fd *fd)
| Check if a file descriptor is already registered.
int | osmo_fd_register (struct osmo_fd *fd)
| Register a new file descriptor with select loop abstraction.
void | osmo_fd_unregister (struct osmo_fd *fd)
| Unregister a file descriptor from select loop abstraction.
void | osmo_fd_close (struct osmo_fd *fd)
| Close a file descriptor, mark it as closed + unregister from select loop abstraction.
int | osmo_select_main (int polling)
| select main loop integration
int | osmo_select_main_ctx (int polling)
| select main loop integration with temporary select-dispatch talloc context
void | osmo_select_init (void)
| initialize the osmocom select abstraction for the current thread
struct osmo_fd * | osmo_fd_get_by_fd (int fd)
| find an osmo_fd based on the integer fd
int | osmo_fd_fill_fds (void *_rset, void *_wset, void *_eset)
| Populate the fd_sets and return the highest fd number.
int | osmo_fd_disp_fds (void *readset, void *writeset, void *exceptset)
int | osmo_timerfd_disable (struct osmo_fd *ofd)
| disable the osmocom-wrapped timerfd
int | osmo_timerfd_schedule (struct osmo_fd *ofd, const struct timespec *first, const struct timespec *interval)
| schedule the osmocom-wrapped timerfd to occur first at first, then periodically at interval
int | osmo_timerfd_setup (struct osmo_fd *ofd, int(*cb)(struct osmo_fd *, unsigned int), void *data)
| setup osmocom-wrapped timerfd
struct osmo_signalfd * | osmo_signalfd_setup (void *ctx, sigset_t set, osmo_signalfd_cb *cb, void *data)
| create a signalfd and register it with osmocom select loop.
void | osmo_select_shutdown_request ()
| Request osmo_select_* to only service pending OSMO_FD_WRITE requests.
int | osmo_select_shutdown_requested ()
| Return the number of times osmo_select_shutdown_request() was called before.
bool | osmo_select_shutdown_done ()
| Return true after osmo_select_shutdown_requested() was called, and after an osmo_select poll loop found no more pending OSMO_FD_WRITE on any registered socket.
static unsigned int | poll_fill_fds (void)
static int | poll_disp_fds (unsigned int n_fd)
static int | _osmo_select_main (int polling)
static | __attribute__ ((constructor))
static int | signalfd_callback (struct osmo_fd *ofd, unsigned int what)

## Variables

static __thread int | maxfd = 0
static __thread struct llist_head | osmo_fds
static __thread int | unregistered_count
static __thread struct poll_state | g_poll
static int | _osmo_select_shutdown_requested = 0
| See osmo_select_shutdown_request()
static bool | _osmo_select_shutdown_done = false
| See osmo_select_shutdown_request()

## Detailed Description

select() loop abstraction

## Macro Definition Documentation

## BSC_FD_EXCEPT

#define BSC_FD_EXCEPT OSMO_FD_EXCEPT

## BSC_FD_READ

#define BSC_FD_READ OSMO_FD_READ

## BSC_FD_WRITE

#define BSC_FD_WRITE OSMO_FD_WRITE

## OSMO_FD_EXCEPT

#define OSMO_FD_EXCEPT 0x0004

Indicate interest in exceptions from the file descriptor.

## OSMO_FD_MASK

#define OSMO_FD_MASK 0xFFFF

Used as when_mask in osmo_fd_update_when()

## OSMO_FD_READ

#define OSMO_FD_READ 0x0001

Indicate interest in reading from the file descriptor.

## OSMO_FD_WRITE

#define OSMO_FD_WRITE 0x0002

Indicate interest in writing to the file descriptor.

## Typedef Documentation

## osmo_signalfd_cb

typedef void osmo_signalfd_cb(struct osmo_signalfd *osfd, const struct signalfd_siginfo *fdsi)

## Function Documentation

## __attribute__()

static __attribute__ | ( | (constructor) |  | ) |

static

References __attribute__, and osmo_select_init().

## _osmo_select_main()

static int _osmo_select_main | ( | int | polling | ) |

static

References _osmo_select_shutdown_requested, g_poll, OSMO_ASSERT, osmo_ctx, osmo_timers_nearest_ms(), osmo_timers_prepare(), osmo_timers_update(), poll_state::poll, poll_disp_fds(), poll_fill_fds(), osmo_talloc_contexts::select, and subsys.

Referenced by osmo_select_main(), and osmo_select_main_ctx().

## osmo_fd_close()

void osmo_fd_close | ( | struct osmo_fd * | fd | ) |

Close a file descriptor, mark it as closed + unregister from select loop abstraction.

Parameters

  [in] | fd | osmocom file descriptor to be unregistered + closed

If fd is registered, we unregister it from the select() loop abstraction. We then close the fd and set it to -1, as well as unsetting any 'when' flags

References osmo_fd::fd, osmo_fd_is_registered(), osmo_fd_unregister(), and osmo_fd::when.

Referenced by osmo_it_q_alloc(), and osmo_it_q_destroy().

## osmo_fd_disp_fds()

int osmo_fd_disp_fds | ( | void * | readset,
|  | void * | writeset,
|  | void * | exceptset
| ) |  |

inline

References flags, osmo_fd::list, llist_for_each_entry_safe, log_reset_context(), OSMO_FD_EXCEPT, OSMO_FD_READ, OSMO_FD_WRITE, osmo_fds, subsys, and unregistered_count.

## osmo_fd_fill_fds()

int osmo_fd_fill_fds | ( | void * | _rset,
|  | void * | _wset,
|  | void * | _eset
| ) |  |

inline

Populate the fd_sets and return the highest fd number.

Parameters

  [in] | _rset | The readfds to populate
  [in] | _wset | The wrtiefds to populate
  [in] | _eset | The errorfds to populate

Returns
  The highest file descriptor seen or 0 on an empty list

References osmo_fd::list, llist_for_each_entry, OSMO_FD_EXCEPT, OSMO_FD_READ, OSMO_FD_WRITE, osmo_fds, and subsys.

## osmo_fd_get_by_fd()

struct osmo_fd * osmo_fd_get_by_fd | ( | int | fd | ) |

find an osmo_fd based on the integer fd

Parameters

  [in] | fd | file descriptor to use as search key

Returns
  osmo_fd for fd; NULL in case it doesn't exist

References osmo_fd::fd, osmo_fd::list, llist_for_each_entry, and osmo_fds.

Referenced by poll_disp_fds().

## osmo_fd_is_registered()

bool osmo_fd_is_registered | ( | struct osmo_fd * | fd | ) |

Check if a file descriptor is already registered.

Parameters

  [in] | fd | osmocom file descriptor to be checked

Returns
  true if registered; otherwise false

References osmo_fd::fd, osmo_fd::list, llist_for_each_entry, and osmo_fds.

Referenced by osmo_fd_close(), and osmo_fd_register().

## osmo_fd_read_disable()

static void osmo_fd_read_disable | ( | struct osmo_fd * | ofd | ) |

inline static

References OSMO_FD_READ, and osmo_fd_update_when().

## osmo_fd_read_enable()

static void osmo_fd_read_enable | ( | struct osmo_fd * | ofd | ) |

inline static

References OSMO_FD_MASK, OSMO_FD_READ, and osmo_fd_update_when().

## osmo_fd_register()

int osmo_fd_register | ( | struct osmo_fd * | fd | ) |

Register a new file descriptor with select loop abstraction.

Parameters

  [in] | fd | osmocom file descriptor to be registered

Returns
  0 on success; negative in case of error

References osmo_fd::fd, flags, g_poll, osmo_fd::list, llist_add_tail(), maxfd, poll_state::num_registered, osmo_fd_is_registered(), osmo_fds, OTC_GLOBAL, poll_state::poll, poll_state::poll_size, and subsys.

Referenced by gsmtap_source_add_sink(), gsmtap_source_init(), log_target_create_file(), log_target_file_reopen(), log_target_file_switch_to_wqueue(), osmo_fd_init_ofd(), osmo_mnl_init(), osmo_signalfd_setup(), and osmo_timerfd_setup().

## osmo_fd_setup()

void osmo_fd_setup | ( | struct osmo_fd * | ofd,
|  | int | fd,
|  | unsigned int | when,
|  | int(*)(struct osmo_fd *fd, unsigned int what) | cb,
|  | void * | data,
|  | unsigned int | priv_nr
| ) |  |

Set up an osmo-fd.

Will not register it.

Parameters

  [in,out] | ofd | Osmo FD to be set-up
  [in] | fd | OS-level file descriptor number
  [in] | when | bit-mask of OSMO_FD_{READ,WRITE,EXECEPT}
  [in] | cb | Call-back function to be called
  [in] | data | Private context pointer
  [in] | priv_nr | Private number

References osmo_fd::cb, data, osmo_fd::data, osmo_fd::fd, osmo_fd::priv_nr, and osmo_fd::when.

Referenced by osmo_it_q_alloc(), osmo_mnl_init(), and osmo_signalfd_setup().

## osmo_fd_unregister()

void osmo_fd_unregister | ( | struct osmo_fd * | fd | ) |

Unregister a file descriptor from select loop abstraction.

Parameters

  [in] | fd | osmocom file descriptor to be unregistered

References g_poll, osmo_fd::list, llist_del(), poll_state::num_registered, osmo_stats_tcp_osmo_fd_unregister(), and unregistered_count.

Referenced by gsmtap_source_free(), log_target_destroy(), log_target_file_reopen(), log_target_file_switch_to_stream(), osmo_fd_close(), osmo_mnl_destroy(), osmo_timerfd_setup(), and signalfd_callback().

## osmo_fd_update_when()

void osmo_fd_update_when | ( | struct osmo_fd * | ofd,
|  | unsigned int | when_mask,
|  | unsigned int | when
| ) |  |

Update the 'when' field of osmo_fd.

"ofd->when = (ofd->when & when_mask) | when". Use this function instead of directly modifying ofd->when, as the latter will be removed soon.

References subsys, and osmo_fd::when.

Referenced by osmo_fd_read_disable(), osmo_fd_read_enable(), osmo_fd_write_disable(), and osmo_fd_write_enable().

## osmo_fd_write_disable()

static void osmo_fd_write_disable | ( | struct osmo_fd * | ofd | ) |

inline static

References osmo_fd_update_when(), and OSMO_FD_WRITE.

## osmo_fd_write_enable()

static void osmo_fd_write_enable | ( | struct osmo_fd * | ofd | ) |

inline static

References OSMO_FD_MASK, osmo_fd_update_when(), and OSMO_FD_WRITE.

## osmo_select_init()

void osmo_select_init | ( | void |  | ) |

initialize the osmocom select abstraction for the current thread

References INIT_LLIST_HEAD, and osmo_fds.

Referenced by __attribute__().

## osmo_select_main()

int osmo_select_main | ( | int | polling | ) |

select main loop integration

Parameters

  [in] | polling | should we pollonly (1) or block on select (0)

Returns
  0 if no fd handled; 1 if fd handled; negative in case of error

References _osmo_select_main(), osmo_ctx, osmo_panic(), osmo_talloc_contexts::select, and subsys.

## osmo_select_main_ctx()

int osmo_select_main_ctx | ( | int | polling | ) |

select main loop integration with temporary select-dispatch talloc context

Parameters

  [in] | polling | should we pollonly (1) or block on select (0)

Returns
  0 if no fd handled; 1 if fd handled; negative in case of error

References _osmo_select_main(), osmo_ctx, osmo_talloc_contexts::select, and subsys.

## osmo_select_shutdown_done()

bool osmo_select_shutdown_done | ( |  | ) |

Return true after osmo_select_shutdown_requested() was called, and after an osmo_select poll loop found no more pending OSMO_FD_WRITE on any registered socket.

References _osmo_select_shutdown_done.

## osmo_select_shutdown_request()

void osmo_select_shutdown_request | ( |  | ) |

Request osmo_select_* to only service pending OSMO_FD_WRITE requests.

Once all writes are done, osmo_select_shutdown_done() returns true. This allows for example to send all outbound packets before terminating the process.

Usage example:

static void signal_handler(int signum) { fprintf(stdout, "signal %u received\n", signum);

switch (signum) { case SIGINT: case SIGTERM: // If the user hits Ctrl-C the third time, just terminate immediately. if (osmo_select_shutdown_requested() >= 2) exit(-1); // Request write-only mode in osmo_select_main_ctx() osmo_select_shutdown_request(); break; [...] }

main() { signal(SIGINT, &signal_handler); signal(SIGTERM, &signal_handler);

[...]

// After the signal_handler issued osmo_select_shutdown_request(), osmo_select_shutdown_done() returns true // as soon as all write queues are empty. while (!osmo_select_shutdown_done()) { osmo_select_main_ctx(0); } }

References _osmo_select_shutdown_requested.

## osmo_select_shutdown_requested()

int osmo_select_shutdown_requested | ( |  | ) |

Return the number of times osmo_select_shutdown_request() was called before.

References _osmo_select_shutdown_requested.

## osmo_signalfd_setup()

struct osmo_signalfd * osmo_signalfd_setup | ( | void * | ctx,
|  | sigset_t | set,
|  | osmo_signalfd_cb * | cb,
|  | void * | data
| ) |  |

create a signalfd and register it with osmocom select loop.

Parameters

  [in] | ctx | talloc context from which osmo_signalfd is to be allocated
  [in] | set | of signals to be accept via this file descriptor
  [in] | cb | call-back function to be called for each arriving signal
  [in] | data | opaque user-provided data to pass to callback

Returns
  pointer to newly-allocated + registered osmo_signalfd; NULL on error

References osmo_signalfd::cb, osmo_signalfd::data, OSMO_FD_READ, osmo_fd_register(), osmo_fd_setup(), signalfd_callback(), and subsys.

## osmo_timerfd_disable()

int osmo_timerfd_disable | ( | struct osmo_fd * | ofd | ) |

disable the osmocom-wrapped timerfd

References osmo_fd::fd, and subsys.

Referenced by start_timer().

## osmo_timerfd_schedule()

int osmo_timerfd_schedule | ( | struct osmo_fd * | ofd,
|  | const struct timespec * | first,
|  | const struct timespec * | interval
| ) |  |

schedule the osmocom-wrapped timerfd to occur first at first, then periodically at interval

Parameters

  [in] | ofd | Osmocom wrapped timerfd
  [in] | first | Relative time at which the timer should first execute (NULL = interval)
  [in] | interval | Time interval at which subsequent timer shall fire

Returns
  0 on success; negative on error

References osmo_fd::fd, and subsys.

Referenced by start_timer().

## osmo_timerfd_setup()

int osmo_timerfd_setup | ( | struct osmo_fd * | ofd,
|  | int(*)(struct osmo_fd *, unsigned int) | cb,
|  | void * | data
| ) |  |

setup osmocom-wrapped timerfd

Parameters

  [in,out] | ofd | Osmocom-wrapped timerfd on which to operate
  [in] | cb | Call-back function called when timerfd becomes readable
  [in] | data | Opaque data to be passed on to call-back

Returns
  0 on success; negative on error

We simply initialize the data structures here, but do not yet schedule the timer.

References osmo_fd::cb, data, osmo_fd::data, osmo_fd::fd, OSMO_FD_READ, osmo_fd_register(), osmo_fd_unregister(), subsys, and osmo_fd::when.

Referenced by start_timer().

## poll_disp_fds()

static int poll_disp_fds | ( | unsigned int | n_fd | ) |

static

References _osmo_select_shutdown_done, _osmo_select_shutdown_requested, flags, g_poll, log_reset_context(), OSMO_FD_EXCEPT, osmo_fd_get_by_fd(), OSMO_FD_READ, OSMO_FD_WRITE, poll_state::poll, and subsys.

Referenced by _osmo_select_main().

## poll_fill_fds()

static unsigned int poll_fill_fds | ( | void |  | ) |

static

References g_poll, osmo_fd::list, llist_for_each_entry, OSMO_FD_EXCEPT, OSMO_FD_READ, OSMO_FD_WRITE, osmo_fds, poll_state::poll, and subsys.

Referenced by _osmo_select_main().

## signalfd_callback()

static int signalfd_callback | ( | struct osmo_fd * | ofd,
|  | unsigned int | what
| ) |  |

static

References osmo_fd::data, osmo_fd::fd, osmo_signalfd::ofd, osmo_fd_unregister(), and subsys.

Referenced by osmo_signalfd_setup().

## Variable Documentation

## _osmo_select_shutdown_done

bool _osmo_select_shutdown_done = false

static

See osmo_select_shutdown_request()

Referenced by osmo_select_shutdown_done(), and poll_disp_fds().

## _osmo_select_shutdown_requested

int _osmo_select_shutdown_requested = 0

static

See osmo_select_shutdown_request()

Referenced by _osmo_select_main(), osmo_select_shutdown_request(), osmo_select_shutdown_requested(), and poll_disp_fds().

## g_poll

__thread struct poll_state g_poll

static

Referenced by _osmo_select_main(), osmo_fd_register(), osmo_fd_unregister(), poll_disp_fds(), and poll_fill_fds().

## maxfd

__thread int maxfd = 0

static

Referenced by osmo_fd_register().

## osmo_fds

__thread struct llist_head osmo_fds

static

Referenced by osmo_fd_disp_fds(), osmo_fd_fill_fds(), osmo_fd_get_by_fd(), osmo_fd_is_registered(), osmo_fd_register(), osmo_select_init(), and poll_fill_fds().

## unregistered_count

__thread int unregistered_count

static

Referenced by osmo_fd_disp_fds(), and osmo_fd_unregister().