Skip to content
Snippets Groups Projects
Commit 3b83ceea authored by Marek Vavrusa's avatar Marek Vavrusa
Browse files

First implementation of an event scheduler API.

Scheduler works with the same event type as evqueue_t.

Example:
evsched_t *s = evsched_new();
evsched_schedule_cb(s, myfunc, data, 1000) // Schedule myfunc() after 1000ms
event_t *ev = evsched_next(); // Run event scheduler
evsched_event_free(s, ev); // Free executed event
evsched_delete(s);

Commit refs #799.
parent eb14ba53
No related branches found
No related tags found
No related merge requests found
#include <sys/time.h>
#include <stdlib.h>
#include "common/evsched.h"
evsched_t *evsched_new()
{
evsched_t *s = malloc(sizeof(evsched_t));
if (!s) {
return 0;
}
/* Initialize event calendar. */
pthread_mutex_init(&s->mx, 0);
pthread_cond_init(&s->notify, 0);
pthread_spin_init(&s->cache.lock, PTHREAD_PROCESS_PRIVATE);
slab_cache_init(&s->cache.alloc, sizeof(event_t));
init_list(&s->calendar);
return s;
}
void evsched_delete(evsched_t **s)
{
if (!s) {
return;
}
if (!*s) {
return;
}
/* Deinitialize event calendar. */
pthread_mutex_destroy(&(*s)->mx);
pthread_cond_destroy(&(*s)->notify);
node *n = 0, *nxt = 0;
WALK_LIST_DELSAFE(n, nxt, (*s)->calendar) {
evsched_event_free((*s), (event_t*)n);
}
/* Free allocator. */
slab_cache_destroy(&(*s)->cache.alloc);
/* Free scheduler. */
free(*s);
*s = 0;
}
event_t *evsched_event_new(evsched_t *s, int type)
{
if (!s) {
return 0;
}
/* Allocate. */
pthread_spin_lock(&s->cache.lock);
event_t *e = slab_cache_alloc(&s->cache.alloc);
pthread_spin_unlock(&s->cache.lock);
/* Initialize. */
memset(e, 0, sizeof(event_t));
e->type = type;
return e;
}
void evsched_event_free(evsched_t *s, event_t *ev)
{
if (!s || !ev) {
return;
}
pthread_spin_lock(&s->cache.lock);
slab_free(ev);
pthread_spin_unlock(&s->cache.lock);
}
event_t* evsched_next(evsched_t *s)
{
/* Check. */
if (!s) {
return 0;
}
/* Lock calendar. */
pthread_mutex_lock(&s->mx);
while(1) {
/* Check event queue. */
if (!EMPTY_LIST(s->calendar)) {
/* Get current time. */
struct timeval dt;
gettimeofday(&dt, 0);
/* Get next event. */
event_t *next_ev = HEAD(s->calendar);
/* Immediately return. */
if (timercmp(&dt, &next_ev->tv, >=)) {
rem_node(&next_ev->n);
pthread_mutex_unlock(&s->mx);
return next_ev;
}
/* Wait for next event or interrupt. Unlock calendar. */
struct timespec ts;
ts.tv_sec = next_ev->tv.tv_sec;
ts.tv_nsec = next_ev->tv.tv_usec * 1000L;
pthread_cond_timedwait(&s->notify, &s->mx, &ts);
} else {
/* Block until an event is scheduled. Unlock calendar.*/
pthread_cond_wait(&s->notify, &s->mx);
}
}
/* Unlock calendar, this shouldn't happen. */
pthread_mutex_unlock(&s->mx);
return 0;
}
int evsched_schedule(evsched_t *s, event_t *ev)
{
if (!s || !ev) {
return -1;
}
/* Lock calendar. */
pthread_mutex_lock(&s->mx);
/* Schedule event. */
node *n = 0, *prev = 0;
WALK_LIST(n, s->calendar) {
event_t* cur = (event_t *)n;
if (timercmp(&cur->tv, &ev->tv, <)) {
prev = n;
} else {
break;
}
}
/* Append to list. */
if (prev) {
insert_node(&ev->n, prev);
} else {
add_head(&s->calendar, &ev->n);
}
/* Unlock calendar. */
pthread_cond_signal(&s->notify);
pthread_mutex_unlock(&s->mx);
return 0;
}
event_t* evsched_schedule_cb(evsched_t *s, event_cb_t cb, void *data, int dt_msec)
{
if (!s) {
return 0;
}
event_t *e = evsched_event_new(s, EVSCHED_CB);
if (!e) {
return 0;
}
e->cb = cb;
e->data = data;
gettimeofday(&e->tv, 0);
/* Add number of seconds. */
e->tv.tv_sec += dt_msec / 1000;
/* Add the number of microseconds. */
e->tv.tv_usec += (dt_msec % 1000) * 1000;
/* Check for overflow. */
while (e->tv.tv_usec > 999999) {
e->tv.tv_sec += 1;
e->tv.tv_usec -= 1 * 1000 * 1000;
}
if (evsched_schedule(s, e) != 0) {
evsched_event_free(s, e);
e = 0;
}
return e;
}
event_t* evsched_schedule_term(evsched_t *s)
{
if (!s) {
return 0;
}
event_t *e = evsched_event_new(s, EVSCHED_TERM);
if (!e) {
return 0;
}
gettimeofday(&e->tv, 0);
if (evsched_schedule(s, e) != 0) {
evsched_event_free(s, e);
e = 0;
}
return e;
}
int evsched_cancel(evsched_t *s, event_t *ev)
{
if (!s || !ev) {
return -1;
}
/* Lock calendar. */
pthread_mutex_lock(&s->mx);
/* Remove from list. */
rem_node(&ev->n);
/* Unlock calendar. */
pthread_cond_signal(&s->notify);
pthread_mutex_unlock(&s->mx);
return 0;
}
/*!
* \file evsched.h
*
* \author Marek Vavrusa <marek.vavrusa@nic.cz>
*
* \brief Event scheduler.
*
* \addtogroup common_lib
* @{
*/
#ifndef _KNOT_COMMON_EVSCHED_H_
#define _KNOT_COMMON_EVSCHED_H_
#include <pthread.h>
#include "common/slab/slab.h"
#include "common/lists.h"
#include "common/evqueue.h"
/*!
* \brief Scheduler event types.
*/
typedef enum evsched_ev_t {
EVSCHED_NOOP = 0, /*!< No-op action, skip. */
EVSCHED_CB, /*!< Callback action. */
EVSCHED_TERM /*!< Terminal action, stop event scheduler. */
} evsched_ev_t;
/*!
* \brief Event scheduler structure.
*
* Keeps list of scheduled events. Events are executed in their scheduled
* time and kept in an ordered list (queue).
* Scheduler is terminated with a special EVSCHED_TERM event type.
*/
typedef struct {
pthread_mutex_t mx; /*!< Event queue locking. */
pthread_cond_t notify; /*!< Event queue notification. */
list calendar; /*!< Event calendar. */
struct {
slab_cache_t alloc; /*!< Events SLAB cache. */
pthread_spinlock_t lock; /*!< Events cache spin lock. */
} cache;
} evsched_t;
/*!
* \brief Create new event scheduler instance.
*
* \retval New instance on success.
* \retval NULL on error.
*/
evsched_t *evsched_new();
/*!
* \brief Deinitialize and free event scheduler instance.
*
* \param s Pointer to event scheduler instance.
* \note *sched is set to 0.
*/
void evsched_delete(evsched_t **s);
/*!
* \brief Create an empty event.
*
* \param s Pointer to event scheduler instance.
* \param type Event type.
* \retval New instance on success.
* \retval NULL on error.
*/
event_t *evsched_event_new(evsched_t *s, int type);
/*!
* \brief Dispose event instance.
*
* \param s Pointer to event scheduler instance.
* \param ev Event instance.
*/
void evsched_event_free(evsched_t *s, event_t *ev);
/*!
* \brief Fetch next-event.
*
* Scheduler may block until a next event is available.
* Send scheduler an EVSCHED_NOOP or EVSCHED_TERM event to unblock it.
*
* \param s Event scheduler.
*
* \retval Scheduled event.
* \retval NULL on error.
*/
event_t* evsched_next(evsched_t *s);
/*!
* \brief Schedule an event.
*
* \param s Event scheduler.
* \param ev Prepared event.
*
* \retval 0 on success.
* \retval <0 on error.
*/
int evsched_schedule(evsched_t *s, event_t *ev);
/*!
* \brief Schedule callback event.
*
* Execute callback after dt miliseconds has passed.
*
* \param s Event scheduler.
* \param cb Callback handler.
* \param data Data for callback.
* \param dt_msec Time difference in milliseconds from now.
*
* \retval Event instance on success.
* \retval NULL on error.
*/
event_t* evsched_schedule_cb(evsched_t *s, event_cb_t cb, void *data, int dt_msec);
/*!
* \brief Schedule termination event.
*
* Special action for scheduler termination.
*
* \param s Event scheduler.
*
* \retval Event instance on success.
* \retval NULL on error.
*/
event_t* evsched_schedule_term(evsched_t *s);
/*!
* \brief Cancel a scheduled event.
*
* \param s Event scheduler.
* \param ev Scheduled event.
*
* \retval 0 on success.
* \retval <0 on error.
*/
int evsched_cancel(evsched_t *s, event_t *ev);
#endif /* _KNOT_COMMON_EVSCHED_H_ */
/*! @} */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment