HelenOS sources
This source file includes following definitions.
- event_initialize
- evno2event
- event_init
- event_task_init
- event_cleanup_answerbox
- _event_set_unmask_callback
- event_set_unmask_callback
- event_task_set_unmask_callback
- event_enqueue
- event_notify
- event_task_notify
- event_subscribe
- event_unsubscribe
- event_unmask
- sys_ipc_event_subscribe
- sys_ipc_event_unsubscribe
- sys_ipc_event_unmask
#include <assert.h>
#include <ipc/event.h>
#include <mm/slab.h>
#include <typedefs.h>
#include <synch/spinlock.h>
#include <console/console.h>
#include <proc/task.h>
#include <errno.h>
#include <arch.h>
static event_t events[EVENT_END];
static void event_initialize(event_t *event)
{
spinlock_initialize(&event->lock, "event.lock");
event->answerbox = NULL;
event->counter = 0;
event->imethod = 0;
event->masked = false;
event->unmask_callback = NULL;
}
static event_t *evno2event(int evno, task_t *task)
{
assert(evno < EVENT_TASK_END);
event_t *event;
if (evno < EVENT_END)
event = &events[(event_type_t) evno];
else
event = &task->events[(event_task_type_t) evno - EVENT_END];
return event;
}
void event_init(void)
{
for (unsigned int i = 0; i < EVENT_END; i++)
event_initialize(evno2event(i, NULL));
}
void event_task_init(task_t *task)
{
for (unsigned int i = EVENT_END; i < EVENT_TASK_END; i++)
event_initialize(evno2event(i, task));
}
void event_cleanup_answerbox(answerbox_t *answerbox)
{
for (unsigned int i = 0; i < EVENT_END; i++) {
spinlock_lock(&events[i].lock);
if (events[i].answerbox == answerbox) {
events[i].answerbox = NULL;
events[i].counter = 0;
events[i].imethod = 0;
events[i].masked = false;
}
spinlock_unlock(&events[i].lock);
}
}
static void _event_set_unmask_callback(event_t *event, event_callback_t callback)
{
spinlock_lock(&event->lock);
event->unmask_callback = callback;
spinlock_unlock(&event->lock);
}
void event_set_unmask_callback(event_type_t evno, event_callback_t callback)
{
assert(evno < EVENT_END);
_event_set_unmask_callback(evno2event(evno, NULL), callback);
}
void event_task_set_unmask_callback(task_t *task, event_task_type_t evno,
event_callback_t callback)
{
assert(evno >= (int) EVENT_END);
assert(evno < EVENT_TASK_END);
_event_set_unmask_callback(evno2event(evno, task), callback);
}
static errno_t event_enqueue(event_t *event, bool mask, sysarg_t a1, sysarg_t a2,
sysarg_t a3, sysarg_t a4, sysarg_t a5)
{
errno_t res;
spinlock_lock(&event->lock);
if (event->answerbox != NULL) {
if (!event->masked) {
call_t *call = ipc_call_alloc();
if (call) {
call->flags |= IPC_CALL_NOTIF;
call->priv = ++event->counter;
ipc_set_imethod(&call->data, event->imethod);
ipc_set_arg1(&call->data, a1);
ipc_set_arg2(&call->data, a2);
ipc_set_arg3(&call->data, a3);
ipc_set_arg4(&call->data, a4);
ipc_set_arg5(&call->data, a5);
call->data.task_id = TASK ? TASK->taskid : 0;
irq_spinlock_lock(&event->answerbox->irq_lock,
true);
list_append(&call->ab_link,
&event->answerbox->irq_notifs);
irq_spinlock_unlock(&event->answerbox->irq_lock,
true);
waitq_wake_one(&event->answerbox->wq);
if (mask)
event->masked = true;
res = EOK;
} else
res = ENOMEM;
} else
res = EBUSY;
} else
res = ENOENT;
spinlock_unlock(&event->lock);
return res;
}
errno_t event_notify(event_type_t evno, bool mask, sysarg_t a1, sysarg_t a2,
sysarg_t a3, sysarg_t a4, sysarg_t a5)
{
assert(evno < EVENT_END);
return event_enqueue(evno2event(evno, NULL), mask, a1, a2, a3, a4, a5);
}
errno_t event_task_notify(task_t *task, event_task_type_t evno, bool mask,
sysarg_t a1, sysarg_t a2, sysarg_t a3, sysarg_t a4, sysarg_t a5)
{
assert(evno >= (int) EVENT_END);
assert(evno < EVENT_TASK_END);
return event_enqueue(evno2event(evno, task), mask, a1, a2, a3, a4, a5);
}
static errno_t event_subscribe(event_t *event, sysarg_t imethod,
answerbox_t *answerbox)
{
errno_t res;
spinlock_lock(&event->lock);
if (event->answerbox == NULL) {
event->answerbox = answerbox;
event->imethod = imethod;
event->counter = 0;
event->masked = false;
res = EOK;
} else
res = EEXIST;
spinlock_unlock(&event->lock);
return res;
}
static errno_t event_unsubscribe(event_t *event, answerbox_t *answerbox)
{
errno_t res;
spinlock_lock(&event->lock);
if (event->answerbox == answerbox) {
event->answerbox = NULL;
event->counter = 0;
event->imethod = 0;
event->masked = false;
res = EOK;
} else
res = ENOENT;
spinlock_unlock(&event->lock);
return res;
}
static void event_unmask(event_t *event)
{
spinlock_lock(&event->lock);
event->masked = false;
event_callback_t callback = event->unmask_callback;
spinlock_unlock(&event->lock);
if (callback != NULL)
callback(event);
}
sys_errno_t sys_ipc_event_subscribe(sysarg_t evno, sysarg_t imethod)
{
if (evno >= EVENT_TASK_END)
return ELIMIT;
return (sys_errno_t) event_subscribe(evno2event(evno, TASK),
(sysarg_t) imethod, &TASK->answerbox);
}
sys_errno_t sys_ipc_event_unsubscribe(sysarg_t evno)
{
if (evno >= EVENT_TASK_END)
return ELIMIT;
return (sys_errno_t) event_unsubscribe(evno2event(evno, TASK),
&TASK->answerbox);
}
sys_errno_t sys_ipc_event_unmask(sysarg_t evno)
{
if (evno >= EVENT_TASK_END)
return ELIMIT;
event_unmask(evno2event(evno, TASK));
return EOK;
}
HelenOS homepage, sources at GitHub