Changeset be79a663 in mainline for kernel/generic/src/ipc/event.c


Ignore:
Timestamp:
2011-08-19T17:37:54Z (13 years ago)
Author:
Petr Koupy <petr.koupy@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ac7c7e36
Parents:
d7427a7e (diff), f00af83 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge libposix changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/ipc/event.c

    rd7427a7e rbe79a663  
    4040#include <synch/spinlock.h>
    4141#include <console/console.h>
     42#include <proc/task.h>
    4243#include <memstr.h>
    4344#include <errno.h>
     
    4748static event_t events[EVENT_END];
    4849
     50static void event_initialize(event_t *event)
     51{
     52        spinlock_initialize(&event->lock, "event.lock");
     53        event->answerbox = NULL;
     54        event->counter = 0;
     55        event->imethod = 0;
     56        event->masked = false;
     57        event->unmask_callback = NULL;
     58}
     59
     60static event_t *evno2event(int evno, task_t *t)
     61{
     62        ASSERT(evno < EVENT_TASK_END);
     63
     64        event_t *event;
     65
     66        if (evno < EVENT_END)
     67                event = &events[(event_type_t) evno];
     68        else
     69                event = &t->events[(event_task_type_t) evno - EVENT_END];
     70
     71        return event;
     72}
     73
    4974/** Initialize kernel events.
    5075 *
     
    5277void event_init(void)
    5378{
    54         for (unsigned int i = 0; i < EVENT_END; i++) {
    55                 spinlock_initialize(&events[i].lock, "event.lock");
    56                 events[i].answerbox = NULL;
    57                 events[i].counter = 0;
    58                 events[i].imethod = 0;
    59                 events[i].masked = false;
    60                 events[i].unmask_callback = NULL;
    61         }
    62 }
     79        for (unsigned int i = 0; i < EVENT_END; i++)
     80                event_initialize(evno2event(i, NULL));
     81}
     82
     83void event_task_init(task_t *task)
     84{
     85        for (unsigned int i = EVENT_END; i < EVENT_TASK_END; i++)
     86                event_initialize(evno2event(i, task));
     87}
     88
    6389
    6490/** Unsubscribe kernel events associated with an answerbox
     
    83109}
    84110
     111static void _event_set_unmask_callback(event_t *event, event_callback_t callback)
     112{
     113        spinlock_lock(&event->lock);
     114        event->unmask_callback = callback;
     115        spinlock_unlock(&event->lock);
     116}
     117
    85118/** Define a callback function for the event unmask event.
    86119 *
     
    94127        ASSERT(evno < EVENT_END);
    95128       
    96         spinlock_lock(&events[evno].lock);
    97         events[evno].unmask_callback = callback;
    98         spinlock_unlock(&events[evno].lock);
     129        _event_set_unmask_callback(evno2event(evno, NULL), callback);
     130}
     131
     132void event_task_set_unmask_callback(task_t *task, event_task_type_t evno,
     133    event_callback_t callback)
     134{
     135        ASSERT(evno >= (int) EVENT_END);
     136        ASSERT(evno < EVENT_TASK_END);
     137               
     138        _event_set_unmask_callback(evno2event(evno, task), callback);
     139}
     140
     141static int event_enqueue(event_t *event, bool mask, sysarg_t a1, sysarg_t a2,
     142    sysarg_t a3, sysarg_t a4, sysarg_t a5)
     143{
     144        int res;
     145
     146        spinlock_lock(&event->lock);
     147       
     148        if (event->answerbox != NULL) {
     149                if (!event->masked) {
     150                        call_t *call = ipc_call_alloc(FRAME_ATOMIC);
     151                       
     152                        if (call) {
     153                                call->flags |= IPC_CALL_NOTIF;
     154                                call->priv = ++event->counter;
     155                               
     156                                IPC_SET_IMETHOD(call->data, event->imethod);
     157                                IPC_SET_ARG1(call->data, a1);
     158                                IPC_SET_ARG2(call->data, a2);
     159                                IPC_SET_ARG3(call->data, a3);
     160                                IPC_SET_ARG4(call->data, a4);
     161                                IPC_SET_ARG5(call->data, a5);
     162                               
     163                                call->data.task_id = TASK ? TASK->taskid : 0;
     164                               
     165                                irq_spinlock_lock(&event->answerbox->irq_lock, true);
     166                                list_append(&call->link, &event->answerbox->irq_notifs);
     167                                irq_spinlock_unlock(&event->answerbox->irq_lock, true);
     168                               
     169                                waitq_wakeup(&event->answerbox->wq, WAKEUP_FIRST);
     170                               
     171                                if (mask)
     172                                        event->masked = true;
     173                               
     174                                res = EOK;
     175                        } else
     176                                res = ENOMEM;
     177                } else
     178                        res = EBUSY;
     179        } else
     180                res = ENOENT;
     181       
     182        spinlock_unlock(&event->lock);
     183        return res;
    99184}
    100185
     
    123208        ASSERT(evno < EVENT_END);
    124209       
    125         spinlock_lock(&events[evno].lock);
    126        
    127         int ret;
    128        
    129         if (events[evno].answerbox != NULL) {
    130                 if (!events[evno].masked) {
    131                         call_t *call = ipc_call_alloc(FRAME_ATOMIC);
    132                        
    133                         if (call) {
    134                                 call->flags |= IPC_CALL_NOTIF;
    135                                 call->priv = ++events[evno].counter;
    136                                
    137                                 IPC_SET_IMETHOD(call->data, events[evno].imethod);
    138                                 IPC_SET_ARG1(call->data, a1);
    139                                 IPC_SET_ARG2(call->data, a2);
    140                                 IPC_SET_ARG3(call->data, a3);
    141                                 IPC_SET_ARG4(call->data, a4);
    142                                 IPC_SET_ARG5(call->data, a5);
    143                                
    144                                 irq_spinlock_lock(&events[evno].answerbox->irq_lock, true);
    145                                 list_append(&call->link, &events[evno].answerbox->irq_notifs);
    146                                 irq_spinlock_unlock(&events[evno].answerbox->irq_lock, true);
    147                                
    148                                 waitq_wakeup(&events[evno].answerbox->wq, WAKEUP_FIRST);
    149                                
    150                                 if (mask)
    151                                         events[evno].masked = true;
    152                                
    153                                 ret = EOK;
    154                         } else
    155                                 ret = ENOMEM;
    156                 } else
    157                         ret = EBUSY;
    158         } else
    159                 ret = ENOENT;
    160        
    161         spinlock_unlock(&events[evno].lock);
    162        
    163         return ret;
     210        return event_enqueue(evno2event(evno, NULL), mask, a1, a2, a3, a4, a5);
     211}
     212
     213/** Send per-task kernel notification event
     214 *
     215 * @param task Destination task.
     216 * @param evno Event type.
     217 * @param mask Mask further notifications after a successful
     218 *             sending.
     219 * @param a1   First argument.
     220 * @param a2   Second argument.
     221 * @param a3   Third argument.
     222 * @param a4   Fourth argument.
     223 * @param a5   Fifth argument.
     224 *
     225 * @return EOK if notification was successfully sent.
     226 * @return ENOMEM if the notification IPC message failed to allocate.
     227 * @return EBUSY if the notifications of the given type are
     228 *         currently masked.
     229 * @return ENOENT if the notifications of the given type are
     230 *         currently not subscribed.
     231 *
     232 */
     233int event_task_notify(task_t *task, event_task_type_t evno, bool mask,
     234    sysarg_t a1, sysarg_t a2, sysarg_t a3, sysarg_t a4, sysarg_t a5)
     235{
     236        ASSERT(evno >= (int) EVENT_END);
     237        ASSERT(evno < EVENT_TASK_END);
     238       
     239        return event_enqueue(evno2event(evno, task), mask, a1, a2, a3, a4, a5);
    164240}
    165241
     
    176252 *
    177253 */
    178 static int event_subscribe(event_type_t evno, sysarg_t imethod,
     254static int event_subscribe(event_t *event, sysarg_t imethod,
    179255    answerbox_t *answerbox)
    180256{
    181         ASSERT(evno < EVENT_END);
    182        
    183         spinlock_lock(&events[evno].lock);
    184        
    185257        int res;
    186        
    187         if (events[evno].answerbox == NULL) {
    188                 events[evno].answerbox = answerbox;
    189                 events[evno].imethod = imethod;
    190                 events[evno].counter = 0;
    191                 events[evno].masked = false;
     258
     259        spinlock_lock(&event->lock);
     260       
     261        if (event->answerbox == NULL) {
     262                event->answerbox = answerbox;
     263                event->imethod = imethod;
     264                event->counter = 0;
     265                event->masked = false;
    192266                res = EOK;
    193267        } else
    194268                res = EEXISTS;
    195269       
    196         spinlock_unlock(&events[evno].lock);
     270        spinlock_unlock(&event->lock);
    197271       
    198272        return res;
     
    204278 *
    205279 */
    206 static void event_unmask(event_type_t evno)
    207 {
    208         ASSERT(evno < EVENT_END);
    209        
    210         spinlock_lock(&events[evno].lock);
    211         events[evno].masked = false;
    212         event_callback_t callback = events[evno].unmask_callback;
    213         spinlock_unlock(&events[evno].lock);
     280static void event_unmask(event_t *event)
     281{
     282        spinlock_lock(&event->lock);
     283        event->masked = false;
     284        event_callback_t callback = event->unmask_callback;
     285        spinlock_unlock(&event->lock);
    214286       
    215287        /*
     
    218290         */
    219291        if (callback != NULL)
    220                 callback();
     292                callback(event);
    221293}
    222294
     
    235307sysarg_t sys_event_subscribe(sysarg_t evno, sysarg_t imethod)
    236308{
    237         if (evno >= EVENT_END)
     309        if (evno >= EVENT_TASK_END)
    238310                return ELIMIT;
    239311       
    240         return (sysarg_t) event_subscribe((event_type_t) evno, (sysarg_t)
    241             imethod, &TASK->answerbox);
     312        return (sysarg_t) event_subscribe(evno2event(evno, TASK),
     313            (sysarg_t) imethod, &TASK->answerbox);
    242314}
    243315
     
    257329sysarg_t sys_event_unmask(sysarg_t evno)
    258330{
    259         if (evno >= EVENT_END)
     331        if (evno >= EVENT_TASK_END)
    260332                return ELIMIT;
    261333       
    262         event_unmask((event_type_t) evno);
     334        event_unmask(evno2event(evno, TASK));
     335
    263336        return EOK;
    264337}
Note: See TracChangeset for help on using the changeset viewer.