1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #8338 from haukepetersen/add_event_timeoutclear

sys/event/timeout: add timeout_clear() function
This commit is contained in:
Martine Lenders 2018-01-09 15:40:40 +01:00 committed by GitHub
commit 03fdaaeb8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 8 deletions

View File

@ -26,3 +26,8 @@ void event_timeout_set(event_timeout_t *event_timeout, uint32_t timeout)
{
xtimer_set(&event_timeout->timer, timeout);
}
void event_timeout_clear(event_timeout_t *event_timeout)
{
xtimer_remove(&event_timeout->timer);
}

View File

@ -54,11 +54,12 @@ typedef struct {
/**
* @brief Initialize timeout event object
*
* @param[in] event_timeout event_timeout object to initilalize
* @param[in] event_timeout event_timeout object to initialize
* @param[in] queue queue that the timed-out event will be added to
* @param[in] event event to add to queue after timeout
*/
void event_timeout_init(event_timeout_t *event_timeout, event_queue_t *queue, event_t *event);
void event_timeout_init(event_timeout_t *event_timeout, event_queue_t *queue,
event_t *event);
/**
* @brief Set a timeout
@ -69,11 +70,23 @@ void event_timeout_init(event_timeout_t *event_timeout, event_queue_t *queue, ev
* @note: the used event_timeout struct must stay valid until after the timeout
* event has been processed!
*
* @param[in] event_timeout event_timout context onject to use
* @param[in] event_timeout event_timout context object to use
* @param[in] timeout timeout in miliseconds
*/
void event_timeout_set(event_timeout_t *event_timeout, uint32_t timeout);
/**
* @brief Clear a timeout event
*
* Calling this function will cancel the timeout by removing its underlying
* timer. If the timer has already fired before calling this function, the
* connected event will be put already into the given event queue and this
* function does not have any effect.
*
* @param[in] event_timeout event_timeout context object to use
*/
void event_timeout_clear(event_timeout_t *event_timeout);
#ifdef __cplusplus
}
#endif

View File

@ -31,6 +31,7 @@ static uint32_t before;
static void callback(event_t *arg);
static void custom_callback(event_t *event);
static void timed_callback(void *arg);
static void forbidden_callback(void *arg);
static event_t event = { .handler = callback };
@ -51,6 +52,7 @@ typedef struct {
static custom_event_t custom_event = { .super.handler = custom_callback, .text = "CUSTOM CALLBACK" };
static event_callback_t event_callback = EVENT_CALLBACK_INIT(timed_callback, 0x12345678);
static event_callback_t noevent_callback = EVENT_CALLBACK_INIT(forbidden_callback, 0);
static void custom_callback(event_t *event)
{
@ -69,7 +71,18 @@ static void timed_callback(void *arg)
uint32_t now = xtimer_now_usec();
assert((now - before >= 100000LU));
printf("triggered timed callback with arg 0x%08x after %" PRIu32 "us\n", (unsigned)arg, now - before);
printf("[SUCCESS]\n");
puts("[SUCCESS]");
}
static void forbidden_callback(void *arg)
{
(void)arg;
/* this callback should never be triggered! */
puts("call to forbidden callback");
puts("[FAILED]");
while (1) {
assert(false);
}
}
int main(void)
@ -85,17 +98,25 @@ int main(void)
printf("canceling 0x%08x\n", (unsigned)&event2);
event_cancel(&queue, &event2);
printf("posting custom event\n");
puts("posting custom event");
event_post(&queue, (event_t *)&custom_event);
event_timeout_t event_timeout;
printf("posting timed callback with timeout 1sec\n");
puts("posting timed callback with timeout 1sec");
event_timeout_init(&event_timeout, &queue, (event_t *)&event_callback);
before = xtimer_now_usec();
event_timeout_set(&event_timeout, 100000LU);
event_timeout_set(&event_timeout, (1 * US_PER_SEC));
printf("launching event queue\n");
event_timeout_t event_timeout_canceled;
puts("posting timed callback with timeout 0.5sec and canceling it again");
event_timeout_init(&event_timeout_canceled, &queue,
(event_t *)&noevent_callback);
event_timeout_set(&event_timeout_canceled, 500 * US_PER_MS);
event_timeout_clear(&event_timeout_canceled);
puts("launching event queue");
event_loop(&queue);
return 0;