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
|
---|
34 | * @brief Kernel event notifications.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <ipc/event.h>
|
---|
38 | #include <ipc/event_types.h>
|
---|
39 | #include <mm/slab.h>
|
---|
40 | #include <typedefs.h>
|
---|
41 | #include <synch/spinlock.h>
|
---|
42 | #include <console/console.h>
|
---|
43 | #include <memstr.h>
|
---|
44 | #include <errno.h>
|
---|
45 | #include <arch.h>
|
---|
46 |
|
---|
47 | /** The events array. */
|
---|
48 | static event_t events[EVENT_END];
|
---|
49 |
|
---|
50 | /** Initialize kernel events. */
|
---|
51 | void event_init(void)
|
---|
52 | {
|
---|
53 | unsigned int i;
|
---|
54 |
|
---|
55 | for (i = 0; i < EVENT_END; i++) {
|
---|
56 | spinlock_initialize(&events[i].lock, "event.lock");
|
---|
57 | events[i].answerbox = NULL;
|
---|
58 | events[i].counter = 0;
|
---|
59 | events[i].imethod = 0;
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | static int event_subscribe(event_type_t evno, sysarg_t imethod,
|
---|
64 | answerbox_t *answerbox)
|
---|
65 | {
|
---|
66 | if (evno >= EVENT_END)
|
---|
67 | return ELIMIT;
|
---|
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].imethod = imethod;
|
---|
76 | events[evno].counter = 0;
|
---|
77 | res = EOK;
|
---|
78 | } else
|
---|
79 | res = EEXISTS;
|
---|
80 |
|
---|
81 | spinlock_unlock(&events[evno].lock);
|
---|
82 |
|
---|
83 | return res;
|
---|
84 | }
|
---|
85 |
|
---|
86 | sysarg_t sys_event_subscribe(sysarg_t evno, sysarg_t imethod)
|
---|
87 | {
|
---|
88 | return (sysarg_t) event_subscribe((event_type_t) evno, (sysarg_t)
|
---|
89 | imethod, &TASK->answerbox);
|
---|
90 | }
|
---|
91 |
|
---|
92 | bool event_is_subscribed(event_type_t evno)
|
---|
93 | {
|
---|
94 | bool res;
|
---|
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 |
|
---|
102 | return res;
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | void event_cleanup_answerbox(answerbox_t *answerbox)
|
---|
107 | {
|
---|
108 | unsigned int i;
|
---|
109 |
|
---|
110 | for (i = 0; i < EVENT_END; i++) {
|
---|
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].imethod = 0;
|
---|
116 | }
|
---|
117 | spinlock_unlock(&events[i].lock);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | void event_notify(event_type_t evno, sysarg_t a1, sysarg_t a2, sysarg_t a3,
|
---|
122 | sysarg_t a4, sysarg_t a5)
|
---|
123 | {
|
---|
124 | ASSERT(evno < EVENT_END);
|
---|
125 |
|
---|
126 | spinlock_lock(&events[evno].lock);
|
---|
127 | if (events[evno].answerbox != NULL) {
|
---|
128 | call_t *call = ipc_call_alloc(FRAME_ATOMIC);
|
---|
129 | if (call) {
|
---|
130 | call->flags |= IPC_CALL_NOTIF;
|
---|
131 | call->priv = ++events[evno].counter;
|
---|
132 | IPC_SET_IMETHOD(call->data, events[evno].imethod);
|
---|
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);
|
---|
138 |
|
---|
139 | irq_spinlock_lock(&events[evno].answerbox->irq_lock, true);
|
---|
140 | list_append(&call->link, &events[evno].answerbox->irq_notifs);
|
---|
141 | irq_spinlock_unlock(&events[evno].answerbox->irq_lock, true);
|
---|
142 |
|
---|
143 | waitq_wakeup(&events[evno].answerbox->wq, WAKEUP_FIRST);
|
---|
144 | }
|
---|
145 | }
|
---|
146 | spinlock_unlock(&events[evno].lock);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /** @}
|
---|
150 | */
|
---|