Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 42 additions & 16 deletions sys/include/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,24 @@ extern "C" {
/**
* @brief Thread flag use to notify available events in an event queue
*/
#define THREAD_FLAG_EVENT (0x1)
# define THREAD_FLAG_EVENT (0x1)
#endif

/**
* @brief event_queue_t static initializer
*/
#define EVENT_QUEUE_INIT { .waiter = thread_get_active() }
#define EVENT_QUEUE_INIT \
{ \
.waiter = thread_get_active() \
}

/**
* @brief static initializer for detached event queues
*/
#define EVENT_QUEUE_INIT_DETACHED { .waiter = NULL }
#define EVENT_QUEUE_INIT_DETACHED \
{ \
.waiter = NULL \
}

/**
* @brief event structure forward declaration
Expand All @@ -143,16 +149,16 @@ typedef void (*event_handler_t)(event_t *);
* @brief event structure
*/
struct event {
clist_node_t list_node; /**< event queue list entry */
event_handler_t handler; /**< pointer to event handler function */
clist_node_t list_node; /**< event queue list entry */
event_handler_t handler; /**< pointer to event handler function */
};

/**
* @brief event queue structure
*/
typedef struct PTRTAG {
clist_node_t event_list; /**< list of queued events */
thread_t *waiter; /**< thread owning event queue */
clist_node_t event_list; /**< list of queued events */
thread_t *waiter; /**< thread owning event queue */
} event_queue_t;

/**
Expand All @@ -164,7 +170,7 @@ typedef struct PTRTAG {
* @param[in] n_queues number of queues in @p queues
*/
static inline void event_queues_init(event_queue_t *queues,
size_t n_queues)
size_t n_queues)
{
assert(queues && n_queues);
thread_t *me = thread_get_active();
Expand Down Expand Up @@ -193,7 +199,7 @@ static inline void event_queue_init(event_queue_t *queue)
* @param[in] n_queues number of queues in @p queues
*/
static inline void event_queues_init_detached(event_queue_t *queues,
size_t n_queues)
size_t n_queues)
{
assert(queues);
for (size_t i = 0; i < n_queues; i++) {
Expand Down Expand Up @@ -405,6 +411,19 @@ event_t *event_wait_timeout_ztimer(event_queue_t *queue,
ztimer_clock_t *clock, uint32_t timeout);
#endif

/**
* @brief Threshold in microseconds below which event_handler
* are not printed while debugging.
*
* This can be used for debugging event loops. Setting a value
* other than zero will also remove the event_handler start message.
*
* Use this to prevent *a lot* of output when debugging.
*/
#ifndef CONFIG_EVENT_LOOP_DEBUG_THRESHOLD_US
# define CONFIG_EVENT_LOOP_DEBUG_THRESHOLD_US (0)
#endif

/**
* @brief Simple event loop with multiple queues
*
Expand Down Expand Up @@ -438,21 +457,28 @@ static inline void event_loop_multi(event_queue_t *queues, size_t n_queues)
while (1) {
event_t *event = event_wait_multi(queues, n_queues);
if (IS_USED(MODULE_EVENT_LOOP_DEBUG)) {
uint32_t now;
ztimer_acquire(ZTIMER_USEC);

void _event_callback_handler(event_t *event);
if (!IS_USED(MODULE_EVENT_CALLBACK) ||
event->handler != _event_callback_handler) {
void _event_callback_handler(event_t * event);
if ((CONFIG_EVENT_LOOP_DEBUG_THRESHOLD_US == 0) &&
(!IS_USED(MODULE_EVENT_CALLBACK) ||
event->handler != _event_callback_handler)) {
printf("event: executing %p->%p\n",
(void *)event, (void *)(uintptr_t)event->handler);
}
now = ztimer_now(ZTIMER_USEC);
uint32_t now = ztimer_now(ZTIMER_USEC);
/* events might modify themself */
void *handler_ptr = (void *)(uintptr_t)event->handler;

event->handler(event);

printf("event: %p took %" PRIu32 " µs\n",
(void *)event, ztimer_now(ZTIMER_USEC) - now);
uint32_t dt = ztimer_now(ZTIMER_USEC) - now;

if ((CONFIG_EVENT_LOOP_DEBUG_THRESHOLD_US == 0) ||
dt > CONFIG_EVENT_LOOP_DEBUG_THRESHOLD_US) {
printf("event: %p->%p ran: %" PRIu32 " usec\n",
(void *)event, handler_ptr, dt);
}
ztimer_release(ZTIMER_USEC);
}
else {
Expand Down