Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys/event: add event_sync() #20916

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
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
sys/event: add event_sync()
  • Loading branch information
derMihai committed Oct 18, 2024
commit 9996909b653ddc6f497a5b5950af75b99f4e63fa
25 changes: 25 additions & 0 deletions sys/event/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "event.h"
#include "clist.h"
#include "mutex.h"
#include "thread.h"

#if IS_USED(MODULE_XTIMER)
Expand Down Expand Up @@ -174,3 +175,27 @@ event_t *event_wait_timeout_ztimer(event_queue_t *queue,
return result;
}
#endif

typedef struct {
event_t ev;
mutex_t synced;
} sync_ev_t;

static void sync_ev_handler(event_t *ev)
{
sync_ev_t *sync_ev = (sync_ev_t *)ev;
mutex_unlock(&sync_ev->synced);
}

void event_sync(event_queue_t *queue)
{
/* if we're on the queue, this would block forever */
assert(!queue->waiter || queue->waiter->pid != thread_getpid());

sync_ev_t sync_ev = {
.ev.handler = sync_ev_handler,
.synced = MUTEX_INIT_LOCKED,
};
event_post(queue, &sync_ev.ev);
mutex_lock(&sync_ev.synced);
}
21 changes: 21 additions & 0 deletions sys/include/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,27 @@ static inline void event_loop(event_queue_t *queue)
event_loop_multi(queue, 1);
}

/**
* @brief Synchronize with the last event on the queue
*
* Blocks until the last event on the queue at the moment of calling this is
* processed.
*
* @warning May not be called from the event queue, as it would block forever.
* @warning If the queue has no waiter, this will block until the queue is
* claimed. See @ref event_queue_claim()
*
* @param[in] queue event queue to sync with
*
* Usage example:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~ {.c}
* event_post(queue, my_event);
* // When event_sync() returns, my_event will have been processed.
* event_sync(queue);
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
derMihai marked this conversation as resolved.
Show resolved Hide resolved
void event_sync(event_queue_t *queue);

#ifdef __cplusplus
}
#endif
Expand Down
12 changes: 9 additions & 3 deletions tests/sys/events/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
#endif

#define STACKSIZE THREAD_STACKSIZE_DEFAULT
#define PRIO (THREAD_PRIORITY_MAIN - 1)
/* in order to actually test @ref event_sync(), the waiter's prio should be lower
* than main s.t. it doesn't start executing right after events are enqueued */
#define PRIO (THREAD_PRIORITY_MAIN + 1)
#define DELAYED_QUEUES_NUMOF 2

static char stack[STACKSIZE];
Expand Down Expand Up @@ -70,7 +72,7 @@
const char *text;
} custom_event_t;

static custom_event_t custom_event = { .super.handler = custom_callback, .text = "CUSTOM CALLBACK" };

Check warning on line 75 in tests/sys/events/main.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
static event_callback_t* event_callback_ptr;
static event_callback_t noevent_callback = EVENT_CALLBACK_INIT(forbidden_callback, 0);

Expand Down Expand Up @@ -164,15 +166,19 @@
event_queue_init_detached(&dqs[0]);
event_queue_init_detached(&dqs[1]);

printf("running thread that will claim event queues %p\n", (void *)&dqs);
thread_create(stack, sizeof(stack), PRIO, 0, claiming_thread, dqs, "ct");

printf("posting %p to delayed queue at index 1\n", (void *)&delayed_event1);
event_post(&dqs[1], &delayed_event1);
printf("posting %p to delayed queue at index 1\n", (void *)&delayed_event2);
event_post(&dqs[1], &delayed_event2);
printf("posting %p to delayed queue at index 0\n", (void *)&delayed_event3);
event_post(&dqs[0], &delayed_event3);

printf("running thread that will claim event queues %p\n", (void *)&dqs);
thread_create(stack, sizeof(stack), PRIO, 0, claiming_thread, dqs, "ct");
event_sync(&dqs[1]);
expect(order == 3);
printf("synced with %p\n", (void *)&delayed_event3);

/* test posting different kind of events in order to a statically
* initialized queue */
Expand Down
Loading