1 | /*
|
---|
2 | * Copyright (c) 2015 Michal Koutny
|
---|
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 | #include <adt/hash_table.h>
|
---|
30 | #include <adt/list.h>
|
---|
31 | #include <errno.h>
|
---|
32 | #include <fibril_synch.h>
|
---|
33 | #include <stdlib.h>
|
---|
34 |
|
---|
35 | #include "log.h"
|
---|
36 | #include "sysman.h"
|
---|
37 | #include "unit.h"
|
---|
38 |
|
---|
39 |
|
---|
40 | /* Do not expose this generally named type */
|
---|
41 | typedef struct {
|
---|
42 | link_t event_queue;
|
---|
43 |
|
---|
44 | event_handler_t handler;
|
---|
45 | void *data;
|
---|
46 | } event_t;
|
---|
47 |
|
---|
48 | typedef struct {
|
---|
49 | link_t callbacks;
|
---|
50 |
|
---|
51 | callback_handler_t handler;
|
---|
52 | void *data;
|
---|
53 | } obj_callback_t;
|
---|
54 |
|
---|
55 | typedef struct {
|
---|
56 | ht_link_t ht_link;
|
---|
57 |
|
---|
58 | void *object;
|
---|
59 | list_t callbacks;
|
---|
60 | } observed_object_t;
|
---|
61 |
|
---|
62 | static LIST_INITIALIZE(event_queue);
|
---|
63 | static fibril_mutex_t event_queue_mtx;
|
---|
64 | static fibril_condvar_t event_queue_cv;
|
---|
65 |
|
---|
66 | static hash_table_t observed_objects;
|
---|
67 | static fibril_mutex_t observed_objects_mtx;
|
---|
68 | static fibril_condvar_t observed_objects_cv;
|
---|
69 |
|
---|
70 | /* Hash table functions */
|
---|
71 | static size_t observed_objects_ht_hash(const ht_link_t *item)
|
---|
72 | {
|
---|
73 | observed_object_t *callbacks =
|
---|
74 | hash_table_get_inst(item, observed_object_t, ht_link);
|
---|
75 |
|
---|
76 | return (size_t) callbacks->object;
|
---|
77 | }
|
---|
78 |
|
---|
79 | static size_t observed_objects_ht_key_hash(void *key)
|
---|
80 | {
|
---|
81 | void *object = *(void **) key;
|
---|
82 | return (size_t) object;
|
---|
83 | }
|
---|
84 |
|
---|
85 | static bool observed_objects_ht_key_equal(void *key, const ht_link_t *item)
|
---|
86 | {
|
---|
87 | void *object = *(void **)key;
|
---|
88 | return (
|
---|
89 | hash_table_get_inst(item, observed_object_t, ht_link)->object ==
|
---|
90 | object);
|
---|
91 | }
|
---|
92 |
|
---|
93 | static hash_table_ops_t observed_objects_ht_ops = {
|
---|
94 | .hash = &observed_objects_ht_hash,
|
---|
95 | .key_hash = &observed_objects_ht_key_hash,
|
---|
96 | .equal = NULL,
|
---|
97 | .key_equal = &observed_objects_ht_key_equal,
|
---|
98 | .remove_callback = NULL
|
---|
99 | };
|
---|
100 |
|
---|
101 | static void notify_observers(void *object)
|
---|
102 | {
|
---|
103 | ht_link_t *item = hash_table_find(&observed_objects, &object);
|
---|
104 | if (item == NULL) {
|
---|
105 | return;
|
---|
106 | }
|
---|
107 | observed_object_t *observed_object =
|
---|
108 | hash_table_get_inst(item, observed_object_t, ht_link);
|
---|
109 |
|
---|
110 | list_foreach_safe(observed_object->callbacks, cur_link, next_link) {
|
---|
111 | obj_callback_t *callback =
|
---|
112 | list_get_instance(cur_link, obj_callback_t, callbacks);
|
---|
113 | callback->handler(object, callback->data);
|
---|
114 | list_remove(cur_link);
|
---|
115 | free(callback);
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | /*
|
---|
120 | * Non-static functions
|
---|
121 | */
|
---|
122 | void sysman_events_init(void)
|
---|
123 | {
|
---|
124 | fibril_mutex_initialize(&event_queue_mtx);
|
---|
125 | fibril_condvar_initialize(&event_queue_cv);
|
---|
126 |
|
---|
127 | bool table =
|
---|
128 | hash_table_create(&observed_objects, 0, 0, &observed_objects_ht_ops);
|
---|
129 | if (!table) {
|
---|
130 | sysman_log(LVL_FATAL, "%s: Failed initialization", __func__);
|
---|
131 | abort();
|
---|
132 | }
|
---|
133 | fibril_mutex_initialize(&observed_objects_mtx);
|
---|
134 | fibril_condvar_initialize(&observed_objects_cv);
|
---|
135 | }
|
---|
136 |
|
---|
137 | int sysman_events_loop(void *unused)
|
---|
138 | {
|
---|
139 | while (1) {
|
---|
140 | /* Pop event */
|
---|
141 | fibril_mutex_lock(&event_queue_mtx);
|
---|
142 | while (list_empty(&event_queue)) {
|
---|
143 | fibril_condvar_wait(&event_queue_cv, &event_queue_mtx);
|
---|
144 | }
|
---|
145 |
|
---|
146 | link_t *li_event = list_first(&event_queue);
|
---|
147 | list_remove(li_event);
|
---|
148 | event_t *event =
|
---|
149 | list_get_instance(li_event, event_t, event_queue);
|
---|
150 | fibril_mutex_unlock(&event_queue_mtx);
|
---|
151 |
|
---|
152 | /* Process event */
|
---|
153 | //sysman_log(LVL_DEBUG2, "process_event(%p, %p)",
|
---|
154 | // event->handler, event->data);
|
---|
155 | event->handler(event->data);
|
---|
156 | free(event);
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | /** Create and queue job for unit
|
---|
161 | *
|
---|
162 | * @param[in] callback (optional) callback must explicitly delete reference
|
---|
163 | * to job
|
---|
164 | */
|
---|
165 | int sysman_queue_job(unit_t *unit, unit_state_t target_state,
|
---|
166 | callback_handler_t callback, void *callback_arg)
|
---|
167 | {
|
---|
168 | job_t *job = job_create(unit, target_state);
|
---|
169 | if (job == NULL) {
|
---|
170 | return ENOMEM;
|
---|
171 | }
|
---|
172 |
|
---|
173 | if (callback != NULL) {
|
---|
174 | job_add_ref(job);
|
---|
175 | sysman_object_observer(job, callback, callback_arg);
|
---|
176 | }
|
---|
177 |
|
---|
178 | job_add_ref(job);
|
---|
179 | sysman_raise_event(&sysman_event_job_process, job);
|
---|
180 |
|
---|
181 | job_del_ref(&job);
|
---|
182 | return EOK;
|
---|
183 | }
|
---|
184 |
|
---|
185 | void sysman_raise_event(event_handler_t handler, void *data)
|
---|
186 | {
|
---|
187 | //sysman_log(LVL_DEBUG2, "%s(%p, %p)", __func__, handler, data);
|
---|
188 | event_t *event = malloc(sizeof(event_t));
|
---|
189 | if (event == NULL) {
|
---|
190 | sysman_log(LVL_FATAL, "%s: cannot allocate event", __func__);
|
---|
191 | // TODO think about aborting system critical task
|
---|
192 | abort();
|
---|
193 | }
|
---|
194 | link_initialize(&event->event_queue);
|
---|
195 | event->handler = handler;
|
---|
196 | event->data = data;
|
---|
197 |
|
---|
198 | fibril_mutex_lock(&event_queue_mtx);
|
---|
199 | list_append(&event->event_queue, &event_queue);
|
---|
200 | /* There's only single event loop, broadcast is unnecessary */
|
---|
201 | fibril_condvar_signal(&event_queue_cv);
|
---|
202 | fibril_mutex_unlock(&event_queue_mtx);
|
---|
203 | }
|
---|
204 |
|
---|
205 | /** Register single-use object observer callback
|
---|
206 | *
|
---|
207 | * TODO no one handles return value, it's quite fatal to lack memory for
|
---|
208 | * callbacks... @return EOK on success
|
---|
209 | * @return ENOMEM
|
---|
210 | */
|
---|
211 | int sysman_object_observer(void *object, callback_handler_t handler, void *data)
|
---|
212 | {
|
---|
213 | int rc;
|
---|
214 | observed_object_t *observed_object = NULL;
|
---|
215 | observed_object_t *new_observed_object = NULL;
|
---|
216 | ht_link_t *ht_link = hash_table_find(&observed_objects, &object);
|
---|
217 |
|
---|
218 | if (ht_link == NULL) {
|
---|
219 | observed_object = malloc(sizeof(observed_object_t));
|
---|
220 | if (observed_object == NULL) {
|
---|
221 | rc = ENOMEM;
|
---|
222 | goto fail;
|
---|
223 | }
|
---|
224 | new_observed_object = observed_object;
|
---|
225 |
|
---|
226 | observed_object->object = object;
|
---|
227 | list_initialize(&observed_object->callbacks);
|
---|
228 | hash_table_insert(&observed_objects, &observed_object->ht_link);
|
---|
229 | } else {
|
---|
230 | observed_object =
|
---|
231 | hash_table_get_inst(ht_link, observed_object_t, ht_link);
|
---|
232 | }
|
---|
233 |
|
---|
234 | obj_callback_t *obj_callback = malloc(sizeof(obj_callback_t));
|
---|
235 | if (obj_callback == NULL) {
|
---|
236 | rc = ENOMEM;
|
---|
237 | goto fail;
|
---|
238 | }
|
---|
239 |
|
---|
240 | obj_callback->handler = handler;
|
---|
241 | obj_callback->data = data;
|
---|
242 | list_append(&obj_callback->callbacks, &observed_object->callbacks);
|
---|
243 | return EOK;
|
---|
244 |
|
---|
245 | fail:
|
---|
246 | free(new_observed_object);
|
---|
247 | return rc;
|
---|
248 | }
|
---|
249 |
|
---|
250 | /*
|
---|
251 | * Event handlers
|
---|
252 | */
|
---|
253 |
|
---|
254 | // NOTE must run in main event loop fibril
|
---|
255 | void sysman_event_job_process(void *data)
|
---|
256 | {
|
---|
257 | job_t *job = data;
|
---|
258 | dyn_array_t job_closure;
|
---|
259 | dyn_array_initialize(&job_closure, job_ptr_t);
|
---|
260 |
|
---|
261 | int rc = job_create_closure(job, &job_closure);
|
---|
262 | if (rc != EOK) {
|
---|
263 | sysman_log(LVL_ERROR, "Cannot create closure for job %p (%i)",
|
---|
264 | job, rc);
|
---|
265 | goto fail;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /*
|
---|
269 | * If jobs are queued, reference is passed from closure to the queue,
|
---|
270 | * otherwise, we still have the reference.
|
---|
271 | */
|
---|
272 | rc = job_queue_add_jobs(&job_closure);
|
---|
273 | if (rc != EOK) {
|
---|
274 | goto fail;
|
---|
275 | }
|
---|
276 | /* We don't need job anymore */
|
---|
277 | job_del_ref(&job);
|
---|
278 |
|
---|
279 | job_queue_process();
|
---|
280 | return;
|
---|
281 |
|
---|
282 | fail:
|
---|
283 | job->retval = JOB_FAILED;
|
---|
284 | job_finish(job);
|
---|
285 | job_del_ref(&job);
|
---|
286 |
|
---|
287 | dyn_array_foreach(job_closure, job_ptr_t, closure_job) {
|
---|
288 | job_del_ref(&(*closure_job));
|
---|
289 | }
|
---|
290 | dyn_array_destroy(&job_closure);
|
---|
291 | }
|
---|
292 |
|
---|
293 | void sysman_event_job_finished(void *data)
|
---|
294 | {
|
---|
295 | notify_observers(data);
|
---|
296 | /* Unreference the event data */
|
---|
297 | job_t *job = data;
|
---|
298 | job_del_ref(&job);
|
---|
299 |
|
---|
300 | /* The finished job, might have been blocking */
|
---|
301 | job_queue_process();
|
---|
302 | }
|
---|
303 |
|
---|
304 | void sysman_event_unit_exposee_created(void *data)
|
---|
305 | {
|
---|
306 | unit_t *unit = data;
|
---|
307 | unit_exposee_created(unit);
|
---|
308 | }
|
---|
309 |
|
---|
310 | void sysman_event_unit_failed(void *data)
|
---|
311 | {
|
---|
312 | unit_t *unit = data;
|
---|
313 | unit_fail(unit);
|
---|
314 | }
|
---|
315 |
|
---|
316 | void sysman_event_unit_state_changed(void *data)
|
---|
317 | {
|
---|
318 | notify_observers(data);
|
---|
319 | }
|
---|