Commit 63beab6c authored by Vitezslav Kriz's avatar Vitezslav Kriz Committed by Daniel Salzman

evsched: fix race condition, remove mutex unlock

parent 51acd1f6
......@@ -85,9 +85,7 @@ static int evsched_run(dthread_t *thread)
if (timercmp_ge(&dt, &ev->tv)) {
heap_delmin(&sched->heap);
pthread_mutex_unlock(&sched->heap_lock);
ev->cb(ev);
pthread_mutex_lock(&sched->heap_lock);
} else {
/* Wait for next event or interrupt. Unlock calendar. */
struct timespec ts;
......@@ -180,14 +178,15 @@ void evsched_event_free(event_t *ev)
int evsched_schedule(event_t *ev, uint32_t dt)
{
if (ev == NULL) {
if (ev == NULL || ev->sched == NULL) {
return KNOT_EINVAL;
}
struct timeval new_time = timeval_in(dt);
/* Lock calendar. */
evsched_t *sched = ev->sched;
/* Lock calendar. */
pthread_mutex_lock(&sched->heap_lock);
ev->tv = new_time;
......@@ -201,7 +200,7 @@ int evsched_schedule(event_t *ev, uint32_t dt)
}
/* Unlock calendar. */
pthread_cond_broadcast(&sched->notify);
pthread_cond_signal(&sched->notify);
pthread_mutex_unlock(&sched->heap_lock);
return KNOT_EOK;
......@@ -213,17 +212,19 @@ int evsched_cancel(event_t *ev)
return KNOT_EINVAL;
}
evsched_t *sched = ev->sched;
/* Lock calendar. */
pthread_mutex_lock(&ev->sched->heap_lock);
pthread_mutex_lock(&sched->heap_lock);
int found = heap_find(&ev->sched->heap, ev);
int found = heap_find(&sched->heap, ev);
if (found > 0) {
heap_delete(&ev->sched->heap, found);
heap_delete(&sched->heap, found);
}
/* Unlock calendar. */
pthread_cond_broadcast(&ev->sched->notify);
pthread_mutex_unlock(&ev->sched->heap_lock);
pthread_cond_signal(&sched->notify);
pthread_mutex_unlock(&sched->heap_lock);
/* Reset event timer. */
memset(&ev->tv, 0, sizeof(struct timeval));
......@@ -240,7 +241,7 @@ void evsched_stop(evsched_t *sched)
{
pthread_mutex_lock(&sched->heap_lock);
dt_stop(sched->thread);
pthread_cond_broadcast(&sched->notify);
pthread_cond_signal(&sched->notify);
pthread_mutex_unlock(&sched->heap_lock);
}
......
......@@ -46,12 +46,12 @@ struct event;
*
* Example callback:
* \code
* int print_callback(event_t *t) {
* return printf("Callback: %s\n", t->data);
* void print_callback(event_t *t) {
* printf("Callback: %s\n", t->data);
* }
* \endcode
*/
typedef int (*event_cb_t)(struct event *);
typedef void (*event_cb_t)(struct event *);
/*!
* \brief Event structure.
......@@ -145,8 +145,6 @@ int evsched_schedule(event_t *ev, uint32_t dt);
*/
int evsched_cancel(event_t *ev);
void evsched_start(evsched_t *sched);
void evsched_stop(evsched_t *sched);
void evsched_join(evsched_t *sched);
......
......@@ -202,7 +202,7 @@ static void event_wrap(task_t *task)
/*!
* \brief Called by scheduler thread if the event occurs.
*/
static int event_dispatch(event_t *event)
static void event_dispatch(event_t *event)
{
assert(event);
assert(event->data);
......@@ -215,8 +215,6 @@ static int event_dispatch(event_t *event)
worker_pool_assign(events->pool, &events->task);
}
pthread_mutex_unlock(&events->mx);
return KNOT_EOK;
}
int zone_events_init(zone_t *zone)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment