libpomp

libpomp is a library for event loop management (based on poll/epoll mechanism). It also contains timer and socket utilities. For further information, you can find the source code here.

POMP_API struct pomp_loop *pomp_loop_new(void)

Create a new loop structure.

Returns:

loop structure or NULL in case of error.

POMP_API int pomp_loop_destroy(struct pomp_loop *loop)

Destroy a loop.

Parameters:

loop – loop to destroy.

Returns:

0 in case of success, negative errno value in case of error.

POMP_API int pomp_loop_add(struct pomp_loop *loop, int fd, uint32_t events, pomp_fd_event_cb_t cb, void *userdata)

Register a new fd in loop.

See also

pomp_fd_event.

Parameters:
  • loop – loop.

  • fd – fd to register.

  • events – events to monitor.

  • cb – callback for notifications.

  • userdata – user data for callback.

Returns:

0 in case of success, negative errno value in case of error.

POMP_API int pomp_loop_update(struct pomp_loop *loop, int fd, uint32_t events)

Modify the set of events to monitor for a registered fd.

See also

pomp_fd_event.

Parameters:
  • loop – loop.

  • fd – fd to modify.

  • events – new events to monitor.

Returns:

0 in case of success, negative errno value in case of error.

POMP_API int pomp_loop_update2(struct pomp_loop *loop, int fd, uint32_t events_to_add, uint32_t events_to_remove)

Modify the set of events to monitor for a registered fd.

See also

pomp_fd_event.

See also

pomp_fd_event.

Parameters:
  • loop – loop.

  • fd – fd to modify.

  • events_to_add – events to add.

  • events_to_remove – events to remove.

Returns:

0 in case of success, negative errno value in case of error.

POMP_API int pomp_loop_remove(struct pomp_loop *loop, int fd)

Unregister a fd from the loop

Parameters:
  • loop – loop.

  • fd – fd to unregister.

Returns:

0 in case of success, negative errno value in case of error.

POMP_API int pomp_loop_has_fd(struct pomp_loop *loop, int fd)

Check if fd has been added in loop.

Parameters:
  • loop – loop.

  • fd – fd to check.

Returns:

1 if fd is in loop 0 otherwise.

POMP_API int pomp_loop_process_fd(struct pomp_loop *loop)

Function to be called when the loop is signaled for readiness.

Remark

this is equivalent to calling pomp_loop_wait_and_process with a timeout of 0 (no wait).

Parameters:

loop – loop.

Returns:

0 in case of success, negative errno value in case of error.

POMP_API int pomp_loop_wait_and_process(struct pomp_loop *loop, int timeout)

Wait for events to occur in loop and process them.

Parameters:
  • loop – loop.

  • timeout – timeout of wait (in ms) or -1 for infinite wait.

Returns:

0 in case of success, -ETIMEDOUT if timeout occurred, negative errno value in case of error.

POMP_API int pomp_loop_wakeup(struct pomp_loop *loop)

Wakeup a loop from a wait in pomp_loop_wait_and_process.

Remark

: this function is safe to call from another thread that the one associated normally with the loop. It is also safe to call it from a signal handler. However caller must ensure that the given context will be valid for the complete duration of the call.

Parameters:

loop – loop.

Returns:

0 in case of success, negative errno value in case of error.

POMP_API int pomp_loop_idle_add(struct pomp_loop *loop, pomp_idle_cb_t cb, void *userdata)

Register a function to be called when loop is idle, i.e. there is no event to be processed. The registered function will be called only once and in the order they are registered.

Remark

: this function is useful to register cleanup functions when called by an fd event callback for example.

Remark

: this function is safe to call from another thread that the one associated normally with the loop. However caller must ensure that the given loop will be valid for the complete duration of the call.

Parameters:
  • loop – loop.

  • cb – callback to call.

  • userdata – user data for callback.

Returns:

0 in case of success, negative errno value in case of error.

POMP_API int pomp_loop_idle_remove(struct pomp_loop *loop, pomp_idle_cb_t cb, void *userdata)

Unregister a function registered with pomp_loop_idle_add.

Remark

: if nothing match the given criteria, no error is returned.

Remark

: if several match the given criteria, all are removed.

Remark

: this function is safe to call from another thread that the one associated normally with the loop. However caller must ensure that the given loop will be valid for the complete duration of the call.

Parameters:
  • loop – loop.

  • cb – callback given in pomp_loop_idle_add.

  • userdata – user data given in pomp_loop_idle_add.

Returns:

0 in case of success, negative errno value in case of error.

POMP_API struct pomp_evt *pomp_evt_new(void)

Create a new event.

Remark

: this function is safe to call from another thread that the one associated normally with the loop.

Returns:

new event or NULL in case of error.

POMP_API int pomp_evt_destroy(struct pomp_evt *evt)

Destroy an event.

Remark

: this function is safe to call from another thread that the one associated normally with the loop.

Parameters:

evt – event to destroy.

Returns:

0 in case of success, negative errno value in case of error.

POMP_API int pomp_evt_attach_to_loop(struct pomp_evt *evt, struct pomp_loop *loop, pomp_evt_cb_t cb, void *userdata)

Attach a pomp_evt to a loop.

Parameters:
  • evt – event to attach.

  • loop – loop to attach to.

  • cb – callback for notifications.

  • userdata – user data for callback.

Returns:

0 in case of success, negative errno value in case of error.

POMP_API int pomp_evt_detach_from_loop(struct pomp_evt *evt, struct pomp_loop *loop)

Detach an event from a loop.

Parameters:
  • evt – event to detach.

  • loop – loop.

Returns:

0 in case of success, negative errno value in case of error.

POMP_API int pomp_evt_is_attached(struct pomp_evt *evt, struct pomp_loop *loop)

Check if the event is attached to the given loop.

Parameters:
  • evt – event to check.

  • loop – loop to check. If NULL, check if the event is attached to any loop.

Returns:

1 if event is attached to the given loop, 0 otherwise.

POMP_API int pomp_evt_signal(struct pomp_evt *evt)

Signal an event.

The fd associated with the pomp_evt will become readable.

Remark

: this function is safe to call from another thread that the one associated normally with the loop. However caller must ensure that the given event will be valid for the complete duration of the call.

Parameters:

evt – event.

Returns:

0 in case of success, negative errno value in case of error.

POMP_API int pomp_evt_clear(struct pomp_evt *event)

Clear an event.

The fd associated with the pomp_evt will no longer be readable.

Remark

: this function is safe to call from another thread that the one associated normally with the loop. However caller must ensure that the given event will be valid for the complete duration of the call.

Parameters:

evt – event.

Returns:

0 in case of success, negative errno value in case of error.

POMP_API struct pomp_timer *pomp_timer_new(struct pomp_loop *loop, pomp_timer_cb_t cb, void *userdata)

Create a new timer.

Parameters:
  • loop – fd loop to use for notifications.

  • cb – callback to use for notifications.

  • userdata – user data for callback.

Returns:

new timer or NULL in case of error.

POMP_API int pomp_timer_destroy(struct pomp_timer *timer)

Destroy a timer.

Parameters:

timer – timer to destroy.

Returns:

0 in case of success, negative errno value in case of error.

POMP_API int pomp_timer_set(struct pomp_timer *timer, uint32_t delay)

Set a one shot timer.

Remark

: this function is safe to call from another thread that the one associated normally with the loop. However caller must ensure that the given timer will be valid for the complete duration of the call.

Parameters:
  • timer – timer to set.

  • delay – expiration delay in milliseconds.

Returns:

0 in case of success, negative errno value in case of error.

POMP_API int pomp_timer_set_periodic(struct pomp_timer *timer, uint32_t delay, uint32_t period)

Set a periodic timer.

Remark

: this function is safe to call from another thread that the one associated normally with the loop. However caller must ensure that the given timer will be valid for the complete duration of the call.

Parameters:
  • timer – timer to set.

  • delay – initial expiration delay in milliseconds.

  • period – period in milliseconds.

Returns:

0 in case of success, negative errno value in case of error.

POMP_API int pomp_timer_clear(struct pomp_timer *timer)

Clear a timer.

Remark

: this function is safe to call from another thread that the one associated normally with the loop. However caller must ensure that the given timer will be valid for the complete duration of the call.

Parameters:

timer – timer to clear.

Returns:

0 in case of success, negative errno value in case of error.