Function anjay_event_loop_run
Defined in File core.h
Function Documentation
-
int anjay_event_loop_run(anjay_t *anjay, avs_time_duration_t max_wait_time)
Runs Anjay’s main event loop that executes anjay_serve and anjay_sched_run as appropriate.
This function will only return after either:
anjay_event_loop_interrupt is called (from a different thread or from a user callback), or
a fatal error (e.g. out-of-memory) occurs in the loop itself (errors from anjay_serve are not considered fatal)
IMPORTANT: The preimplemented event loop is primarily intended to be run in a dedicated thread, while other threads may handle tasks not related to LwM2M. It is safe to control the same instance of Anjay concurrently from a different thread only if the thread safety features have been enabled at Anjay’s compile time:
AVS_COMMONS_SCHED_THREAD_SAFE(WITH_SCHEDULER_THREAD_SAFEin CMake) is required to safely use Anjay’s internal scheduler (see anjay_get_scheduler)Please note that only managing scheduler tasks from another thread is safe. Attempting to call anjay_sched_run (or
avs_sched_run(anjay_get_scheduler(anjay))while the event loop is running may lead to undefined behavior in the loop
ANJAY_WITH_THREAD_SAFETY(WITH_THREAD_SAFETYin CMake) is required to call any other functions, aside from the cleanup functions (which are only safe to call after the event loop has finished) and anjay_event_loop_interrupt (which is always safe to call)
CAUTION: The preimplemented event loop will only work if all the sockets that may be created by Anjay will return file descriptors compatible with
poll()orselect()throughavs_net_socket_get_system().In particular, please be cautious when using SMS or NIDD transports (in versions of Anjay that include the relevant commercial features) - your
anjay_smsdrv_system_socket_tandanjay_nidd_driver_system_descriptor_tfunctions need to populate the output argument with a valid file descriptor (e.g.int fd = ...; *out = &fd;), and that file descriptor must be valid to use with thepoll()orselect()functions (which may not true if the socket API is separate from other IO APIs, as is the case e.g. on Windows). Otherwise attempting to use this event loop implementation will not handle NIDD and SMS transports properly, and may even lead to undefined behavior.- Parameters:
anjay – Anjay object to operate on.
max_wait_time – Maximum time to spend in each single call to
poll()orselect(). Larger times will prevent the event loop thread from waking up too frequently. However, during this wait time, the call to anjay_event_loop_interrupt may not be handled immediately, and scheduler jobs requested from other threads (see anjay_get_scheduler) will not be executed, so shortening this time will make the scheduler more responsive.
- Returns:
0 after having been successfully interrupted by anjay_event_loop_interrupt, or a negative value in case of a fatal error.