[05641a9e] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Jakub Jermar
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup generic
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
[13a638d] | 34 | * @brief Kernel event notifications.
|
---|
[05641a9e] | 35 | */
|
---|
| 36 |
|
---|
[13a638d] | 37 | #include <ipc/event.h>
|
---|
| 38 | #include <ipc/event_types.h>
|
---|
[05641a9e] | 39 | #include <mm/slab.h>
|
---|
[d99c1d2] | 40 | #include <typedefs.h>
|
---|
[05641a9e] | 41 | #include <synch/spinlock.h>
|
---|
| 42 | #include <console/console.h>
|
---|
| 43 | #include <memstr.h>
|
---|
| 44 | #include <errno.h>
|
---|
| 45 | #include <arch.h>
|
---|
| 46 |
|
---|
[e49b57b2] | 47 | /** The events array. */
|
---|
[13a638d] | 48 | static event_t events[EVENT_END];
|
---|
[05641a9e] | 49 |
|
---|
| 50 | /** Initialize kernel events. */
|
---|
| 51 | void event_init(void)
|
---|
| 52 | {
|
---|
[13a638d] | 53 | unsigned int i;
|
---|
| 54 |
|
---|
[05641a9e] | 55 | for (i = 0; i < EVENT_END; i++) {
|
---|
[13a638d] | 56 | spinlock_initialize(&events[i].lock, "event.lock");
|
---|
| 57 | events[i].answerbox = NULL;
|
---|
| 58 | events[i].counter = 0;
|
---|
[228e490] | 59 | events[i].imethod = 0;
|
---|
[05641a9e] | 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[228e490] | 63 | static int event_subscribe(event_type_t evno, sysarg_t imethod,
|
---|
[98000fb] | 64 | answerbox_t *answerbox)
|
---|
[05641a9e] | 65 | {
|
---|
[13a638d] | 66 | if (evno >= EVENT_END)
|
---|
[05641a9e] | 67 | return ELIMIT;
|
---|
[13a638d] | 68 |
|
---|
| 69 | spinlock_lock(&events[evno].lock);
|
---|
| 70 |
|
---|
| 71 | int res;
|
---|
| 72 |
|
---|
| 73 | if (events[evno].answerbox == NULL) {
|
---|
| 74 | events[evno].answerbox = answerbox;
|
---|
[228e490] | 75 | events[evno].imethod = imethod;
|
---|
[13a638d] | 76 | events[evno].counter = 0;
|
---|
[05641a9e] | 77 | res = EOK;
|
---|
[13a638d] | 78 | } else
|
---|
| 79 | res = EEXISTS;
|
---|
| 80 |
|
---|
| 81 | spinlock_unlock(&events[evno].lock);
|
---|
| 82 |
|
---|
[05641a9e] | 83 | return res;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[228e490] | 86 | sysarg_t sys_event_subscribe(sysarg_t evno, sysarg_t imethod)
|
---|
[05641a9e] | 87 | {
|
---|
[96b02eb9] | 88 | return (sysarg_t) event_subscribe((event_type_t) evno, (sysarg_t)
|
---|
[228e490] | 89 | imethod, &TASK->answerbox);
|
---|
[05641a9e] | 90 | }
|
---|
| 91 |
|
---|
[13a638d] | 92 | bool event_is_subscribed(event_type_t evno)
|
---|
[05641a9e] | 93 | {
|
---|
| 94 | bool res;
|
---|
[13a638d] | 95 |
|
---|
| 96 | ASSERT(evno < EVENT_END);
|
---|
| 97 |
|
---|
| 98 | spinlock_lock(&events[evno].lock);
|
---|
| 99 | res = events[evno].answerbox != NULL;
|
---|
| 100 | spinlock_unlock(&events[evno].lock);
|
---|
| 101 |
|
---|
[05641a9e] | 102 | return res;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 | void event_cleanup_answerbox(answerbox_t *answerbox)
|
---|
| 107 | {
|
---|
[13a638d] | 108 | unsigned int i;
|
---|
| 109 |
|
---|
[05641a9e] | 110 | for (i = 0; i < EVENT_END; i++) {
|
---|
[13a638d] | 111 | spinlock_lock(&events[i].lock);
|
---|
| 112 | if (events[i].answerbox == answerbox) {
|
---|
| 113 | events[i].answerbox = NULL;
|
---|
| 114 | events[i].counter = 0;
|
---|
[228e490] | 115 | events[i].imethod = 0;
|
---|
[05641a9e] | 116 | }
|
---|
[13a638d] | 117 | spinlock_unlock(&events[i].lock);
|
---|
[05641a9e] | 118 | }
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[96b02eb9] | 121 | void event_notify(event_type_t evno, sysarg_t a1, sysarg_t a2, sysarg_t a3,
|
---|
| 122 | sysarg_t a4, sysarg_t a5)
|
---|
[05641a9e] | 123 | {
|
---|
[13a638d] | 124 | ASSERT(evno < EVENT_END);
|
---|
| 125 |
|
---|
| 126 | spinlock_lock(&events[evno].lock);
|
---|
| 127 | if (events[evno].answerbox != NULL) {
|
---|
[05641a9e] | 128 | call_t *call = ipc_call_alloc(FRAME_ATOMIC);
|
---|
| 129 | if (call) {
|
---|
| 130 | call->flags |= IPC_CALL_NOTIF;
|
---|
[13a638d] | 131 | call->priv = ++events[evno].counter;
|
---|
[228e490] | 132 | IPC_SET_IMETHOD(call->data, events[evno].imethod);
|
---|
[05641a9e] | 133 | IPC_SET_ARG1(call->data, a1);
|
---|
| 134 | IPC_SET_ARG2(call->data, a2);
|
---|
| 135 | IPC_SET_ARG3(call->data, a3);
|
---|
| 136 | IPC_SET_ARG4(call->data, a4);
|
---|
| 137 | IPC_SET_ARG5(call->data, a5);
|
---|
[13a638d] | 138 |
|
---|
[da1bafb] | 139 | irq_spinlock_lock(&events[evno].answerbox->irq_lock, true);
|
---|
[13a638d] | 140 | list_append(&call->link, &events[evno].answerbox->irq_notifs);
|
---|
[da1bafb] | 141 | irq_spinlock_unlock(&events[evno].answerbox->irq_lock, true);
|
---|
[13a638d] | 142 |
|
---|
| 143 | waitq_wakeup(&events[evno].answerbox->wq, WAKEUP_FIRST);
|
---|
[05641a9e] | 144 | }
|
---|
| 145 | }
|
---|
[13a638d] | 146 | spinlock_unlock(&events[evno].lock);
|
---|
[05641a9e] | 147 | }
|
---|
| 148 |
|
---|
| 149 | /** @}
|
---|
| 150 | */
|
---|