source: mainline/kernel/generic/src/ipc/event.c@ a000878c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a000878c was e49b57b2, checked in by Jakub Jermar <jakub@…>, 16 years ago

Update comment.

  • Property mode set to 100644
File size: 4.0 KB
RevLine 
[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>
40#include <arch/types.h>
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]48static event_t events[EVENT_END];
[05641a9e]49
50/** Initialize kernel events. */
51void 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;
59 events[i].method = 0;
[05641a9e]60 }
61}
62
[98000fb]63static int event_subscribe(event_type_t evno, unative_t method,
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;
75 events[evno].method = method;
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
86unative_t sys_event_subscribe(unative_t evno, unative_t method)
87{
88 return (unative_t) event_subscribe((event_type_t) evno, (unative_t)
89 method, &TASK->answerbox);
90}
91
[13a638d]92bool 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
106void 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;
115 events[i].method = 0;
[05641a9e]116 }
[13a638d]117 spinlock_unlock(&events[i].lock);
[05641a9e]118 }
119}
120
[98000fb]121void event_notify(event_type_t evno, unative_t a1, unative_t a2, unative_t a3,
[05641a9e]122 unative_t a4, unative_t a5)
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;
132 IPC_SET_METHOD(call->data, events[evno].method);
[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
[5e73815]139 ipl_t ipl = interrupts_disable();
[13a638d]140 spinlock_lock(&events[evno].answerbox->irq_lock);
141 list_append(&call->link, &events[evno].answerbox->irq_notifs);
142 spinlock_unlock(&events[evno].answerbox->irq_lock);
[5e73815]143 interrupts_restore(ipl);
[13a638d]144
145 waitq_wakeup(&events[evno].answerbox->wq, WAKEUP_FIRST);
[05641a9e]146 }
147 }
[13a638d]148 spinlock_unlock(&events[evno].lock);
[05641a9e]149}
150
151/** @}
152 */
Note: See TracBrowser for help on using the repository browser.