[09a8006] | 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 |
|
---|
[3f7e1f24] | 29 | #include <adt/hash_table.h>
|
---|
[694253c] | 30 | #include <adt/list.h>
|
---|
[f42ee6f] | 31 | #include <errno.h>
|
---|
[3f7e1f24] | 32 | #include <fibril_synch.h>
|
---|
| 33 | #include <stdlib.h>
|
---|
[f42ee6f] | 34 |
|
---|
[3f7e1f24] | 35 | #include "log.h"
|
---|
[f42ee6f] | 36 | #include "sysman.h"
|
---|
[5559712] | 37 | #include "unit.h"
|
---|
[f42ee6f] | 38 |
|
---|
[3f7e1f24] | 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;
|
---|
[73f7c4e] | 64 | static fibril_condvar_t event_queue_nonempty_cv;
|
---|
| 65 | static fibril_condvar_t event_queue_empty_cv;
|
---|
[3f7e1f24] | 66 |
|
---|
| 67 | static hash_table_t observed_objects;
|
---|
| 68 | static fibril_mutex_t observed_objects_mtx;
|
---|
| 69 | static fibril_condvar_t observed_objects_cv;
|
---|
| 70 |
|
---|
[72c8f77] | 71 | /*
|
---|
| 72 | * Hash table functions
|
---|
| 73 | */
|
---|
[3f7e1f24] | 74 | static size_t observed_objects_ht_hash(const ht_link_t *item)
|
---|
[f42ee6f] | 75 | {
|
---|
[3f7e1f24] | 76 | observed_object_t *callbacks =
|
---|
| 77 | hash_table_get_inst(item, observed_object_t, ht_link);
|
---|
| 78 |
|
---|
| 79 | return (size_t) callbacks->object;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | static size_t observed_objects_ht_key_hash(void *key)
|
---|
| 83 | {
|
---|
| 84 | void *object = *(void **) key;
|
---|
| 85 | return (size_t) object;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | static bool observed_objects_ht_key_equal(void *key, const ht_link_t *item)
|
---|
| 89 | {
|
---|
| 90 | void *object = *(void **)key;
|
---|
| 91 | return (
|
---|
| 92 | hash_table_get_inst(item, observed_object_t, ht_link)->object ==
|
---|
| 93 | object);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | static hash_table_ops_t observed_objects_ht_ops = {
|
---|
| 97 | .hash = &observed_objects_ht_hash,
|
---|
| 98 | .key_hash = &observed_objects_ht_key_hash,
|
---|
| 99 | .equal = NULL,
|
---|
| 100 | .key_equal = &observed_objects_ht_key_equal,
|
---|
| 101 | .remove_callback = NULL
|
---|
| 102 | };
|
---|
| 103 |
|
---|
[72c8f77] | 104 | /*
|
---|
| 105 | * Static functions
|
---|
| 106 | */
|
---|
| 107 |
|
---|
| 108 | static observed_object_t *observed_object_create(void *object)
|
---|
| 109 | {
|
---|
| 110 | observed_object_t *result = malloc(sizeof(observed_object_t));
|
---|
| 111 | if (result) {
|
---|
| 112 | result->object = object;
|
---|
| 113 | list_initialize(&result->callbacks);
|
---|
| 114 | hash_table_insert(&observed_objects, &result->ht_link);
|
---|
| 115 | }
|
---|
| 116 | return result;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | static void observed_object_destroy(observed_object_t **ptr_observed_object)
|
---|
| 120 | {
|
---|
| 121 | observed_object_t *observed_object = *ptr_observed_object;
|
---|
| 122 | if (observed_object == NULL) {
|
---|
| 123 | return;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | ht_link_t *item = &observed_object->ht_link;
|
---|
| 127 | hash_table_remove_item(&observed_objects, item);
|
---|
| 128 | free(observed_object);
|
---|
| 129 | *ptr_observed_object = NULL;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[3f7e1f24] | 132 | static void notify_observers(void *object)
|
---|
| 133 | {
|
---|
| 134 | ht_link_t *item = hash_table_find(&observed_objects, &object);
|
---|
| 135 | if (item == NULL) {
|
---|
| 136 | return;
|
---|
[694253c] | 137 | }
|
---|
[3f7e1f24] | 138 | observed_object_t *observed_object =
|
---|
| 139 | hash_table_get_inst(item, observed_object_t, ht_link);
|
---|
[694253c] | 140 |
|
---|
[3f7e1f24] | 141 | list_foreach_safe(observed_object->callbacks, cur_link, next_link) {
|
---|
| 142 | obj_callback_t *callback =
|
---|
| 143 | list_get_instance(cur_link, obj_callback_t, callbacks);
|
---|
| 144 | callback->handler(object, callback->data);
|
---|
| 145 | list_remove(cur_link);
|
---|
| 146 | free(callback);
|
---|
| 147 | }
|
---|
[72c8f77] | 148 |
|
---|
| 149 | observed_object_destroy(&observed_object);
|
---|
[3f7e1f24] | 150 | }
|
---|
[694253c] | 151 |
|
---|
[3f7e1f24] | 152 | /*
|
---|
| 153 | * Non-static functions
|
---|
| 154 | */
|
---|
| 155 | void sysman_events_init(void)
|
---|
| 156 | {
|
---|
| 157 | fibril_mutex_initialize(&event_queue_mtx);
|
---|
[73f7c4e] | 158 | fibril_condvar_initialize(&event_queue_nonempty_cv);
|
---|
| 159 | fibril_condvar_initialize(&event_queue_empty_cv);
|
---|
[3f7e1f24] | 160 |
|
---|
| 161 | bool table =
|
---|
| 162 | hash_table_create(&observed_objects, 0, 0, &observed_objects_ht_ops);
|
---|
| 163 | if (!table) {
|
---|
| 164 | sysman_log(LVL_FATAL, "%s: Failed initialization", __func__);
|
---|
| 165 | abort();
|
---|
| 166 | }
|
---|
| 167 | fibril_mutex_initialize(&observed_objects_mtx);
|
---|
| 168 | fibril_condvar_initialize(&observed_objects_cv);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | int sysman_events_loop(void *unused)
|
---|
| 172 | {
|
---|
| 173 | while (1) {
|
---|
| 174 | /* Pop event */
|
---|
| 175 | fibril_mutex_lock(&event_queue_mtx);
|
---|
| 176 | while (list_empty(&event_queue)) {
|
---|
[73f7c4e] | 177 | fibril_condvar_signal(&event_queue_empty_cv);
|
---|
| 178 | fibril_condvar_wait(&event_queue_nonempty_cv,
|
---|
| 179 | &event_queue_mtx);
|
---|
[694253c] | 180 | }
|
---|
[3f7e1f24] | 181 |
|
---|
| 182 | link_t *li_event = list_first(&event_queue);
|
---|
| 183 | list_remove(li_event);
|
---|
| 184 | event_t *event =
|
---|
| 185 | list_get_instance(li_event, event_t, event_queue);
|
---|
| 186 | fibril_mutex_unlock(&event_queue_mtx);
|
---|
| 187 |
|
---|
| 188 | /* Process event */
|
---|
[dda2602] | 189 | //sysman_log(LVL_DEBUG2, "process_event(%p, %p)",
|
---|
| 190 | // event->handler, event->data);
|
---|
[3f7e1f24] | 191 | event->handler(event->data);
|
---|
| 192 | free(event);
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[5559712] | 196 | /** Create and queue job for unit
|
---|
[73f7c4e] | 197 | *
|
---|
| 198 | * If unit already has the same job assigned callback is set to it.
|
---|
[5559712] | 199 | *
|
---|
[4224ef7] | 200 | * @param[in] callback (optional) callback must explicitly delete reference
|
---|
[73f7c4e] | 201 | * to job, void callback(void *job, void *callback_arg)
|
---|
| 202 | *
|
---|
| 203 | * return EBUSY unit already has a job assigned of different type
|
---|
[5559712] | 204 | */
|
---|
[73f7c4e] | 205 | int sysman_run_job(unit_t *unit, unit_state_t target_state,
|
---|
[5559712] | 206 | callback_handler_t callback, void *callback_arg)
|
---|
| 207 | {
|
---|
[72c8f77] | 208 | job_t *job = job_create(unit, target_state);
|
---|
| 209 | if (job == NULL) {
|
---|
| 210 | return ENOMEM;
|
---|
[5559712] | 211 | }
|
---|
| 212 |
|
---|
[4224ef7] | 213 | if (callback != NULL) {
|
---|
| 214 | job_add_ref(job);
|
---|
| 215 | sysman_object_observer(job, callback, callback_arg);
|
---|
| 216 | }
|
---|
[5559712] | 217 |
|
---|
[72c8f77] | 218 | /* Pass reference to event */
|
---|
| 219 | sysman_raise_event(&sysman_event_job_process, job);
|
---|
[5559712] | 220 |
|
---|
| 221 | return EOK;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[3f7e1f24] | 224 | void sysman_raise_event(event_handler_t handler, void *data)
|
---|
| 225 | {
|
---|
[dda2602] | 226 | //sysman_log(LVL_DEBUG2, "%s(%p, %p)", __func__, handler, data);
|
---|
[3f7e1f24] | 227 | event_t *event = malloc(sizeof(event_t));
|
---|
| 228 | if (event == NULL) {
|
---|
| 229 | sysman_log(LVL_FATAL, "%s: cannot allocate event", __func__);
|
---|
| 230 | // TODO think about aborting system critical task
|
---|
| 231 | abort();
|
---|
| 232 | }
|
---|
| 233 | link_initialize(&event->event_queue);
|
---|
| 234 | event->handler = handler;
|
---|
| 235 | event->data = data;
|
---|
| 236 |
|
---|
| 237 | fibril_mutex_lock(&event_queue_mtx);
|
---|
| 238 | list_append(&event->event_queue, &event_queue);
|
---|
| 239 | /* There's only single event loop, broadcast is unnecessary */
|
---|
[73f7c4e] | 240 | fibril_condvar_signal(&event_queue_nonempty_cv);
|
---|
| 241 | fibril_mutex_unlock(&event_queue_mtx);
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | /** Empty current content of event queue
|
---|
| 245 | *
|
---|
| 246 | * This is potentially blocking call and as long as fibrils are cooperatively
|
---|
| 247 | * scheduled, queue will be empty upon return from this function.
|
---|
| 248 | */
|
---|
| 249 | void sysman_process_queue(void)
|
---|
| 250 | {
|
---|
| 251 | fibril_mutex_lock(&event_queue_mtx);
|
---|
| 252 | while (!list_empty(&event_queue)) {
|
---|
| 253 | fibril_condvar_wait(&event_queue_empty_cv, &event_queue_mtx);
|
---|
| 254 | }
|
---|
[3f7e1f24] | 255 | fibril_mutex_unlock(&event_queue_mtx);
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | /** Register single-use object observer callback
|
---|
| 259 | *
|
---|
| 260 | * TODO no one handles return value, it's quite fatal to lack memory for
|
---|
[72c8f77] | 261 | * callbacks...
|
---|
| 262 | *
|
---|
| 263 | * @return EOK on success
|
---|
[3f7e1f24] | 264 | * @return ENOMEM
|
---|
| 265 | */
|
---|
| 266 | int sysman_object_observer(void *object, callback_handler_t handler, void *data)
|
---|
| 267 | {
|
---|
| 268 | int rc;
|
---|
| 269 | observed_object_t *observed_object = NULL;
|
---|
| 270 | observed_object_t *new_observed_object = NULL;
|
---|
| 271 | ht_link_t *ht_link = hash_table_find(&observed_objects, &object);
|
---|
| 272 |
|
---|
| 273 | if (ht_link == NULL) {
|
---|
[72c8f77] | 274 | new_observed_object = observed_object_create(object);
|
---|
| 275 | if (new_observed_object == NULL) {
|
---|
[3f7e1f24] | 276 | rc = ENOMEM;
|
---|
[4fe7fcb] | 277 | goto fail;
|
---|
| 278 | }
|
---|
[72c8f77] | 279 | observed_object = new_observed_object;
|
---|
[3f7e1f24] | 280 | } else {
|
---|
| 281 | observed_object =
|
---|
| 282 | hash_table_get_inst(ht_link, observed_object_t, ht_link);
|
---|
| 283 | }
|
---|
[694253c] | 284 |
|
---|
[3f7e1f24] | 285 | obj_callback_t *obj_callback = malloc(sizeof(obj_callback_t));
|
---|
| 286 | if (obj_callback == NULL) {
|
---|
| 287 | rc = ENOMEM;
|
---|
| 288 | goto fail;
|
---|
[694253c] | 289 | }
|
---|
[3f7e1f24] | 290 |
|
---|
| 291 | obj_callback->handler = handler;
|
---|
| 292 | obj_callback->data = data;
|
---|
| 293 | list_append(&obj_callback->callbacks, &observed_object->callbacks);
|
---|
[f42ee6f] | 294 | return EOK;
|
---|
[694253c] | 295 |
|
---|
| 296 | fail:
|
---|
[3f7e1f24] | 297 | free(new_observed_object);
|
---|
[694253c] | 298 | return rc;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
[72c8f77] | 301 | int sysman_move_observers(void *src_object, void *dst_object)
|
---|
| 302 | {
|
---|
| 303 | ht_link_t *src_link = hash_table_find(&observed_objects, &src_object);
|
---|
| 304 | if (src_link == NULL) {
|
---|
| 305 | return EOK;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | ht_link_t *dst_link = hash_table_find(&observed_objects, &dst_object);
|
---|
| 309 | observed_object_t *dst_observed_object;
|
---|
| 310 | if (dst_link == NULL) {
|
---|
| 311 | dst_observed_object = observed_object_create(dst_object);
|
---|
| 312 | if (dst_observed_object == NULL) {
|
---|
| 313 | return ENOMEM;
|
---|
| 314 | }
|
---|
| 315 | } else {
|
---|
| 316 | dst_observed_object =
|
---|
| 317 | hash_table_get_inst(dst_link, observed_object_t, ht_link);
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | observed_object_t *src_observed_object =
|
---|
| 321 | hash_table_get_inst(src_link, observed_object_t, ht_link);
|
---|
| 322 |
|
---|
| 323 | list_concat(&dst_observed_object->callbacks,
|
---|
| 324 | &src_observed_object->callbacks);
|
---|
| 325 | observed_object_destroy(&src_observed_object);
|
---|
| 326 |
|
---|
| 327 | return EOK;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 |
|
---|
[3f7e1f24] | 331 | /*
|
---|
| 332 | * Event handlers
|
---|
| 333 | */
|
---|
| 334 |
|
---|
| 335 | // NOTE must run in main event loop fibril
|
---|
[5559712] | 336 | void sysman_event_job_process(void *data)
|
---|
[694253c] | 337 | {
|
---|
[5559712] | 338 | job_t *job = data;
|
---|
[3f7e1f24] | 339 | dyn_array_t job_closure;
|
---|
[72c8f77] | 340 | dyn_array_initialize(&job_closure, job_t *);
|
---|
[694253c] | 341 |
|
---|
[3f7e1f24] | 342 | int rc = job_create_closure(job, &job_closure);
|
---|
[694253c] | 343 | if (rc != EOK) {
|
---|
[3f7e1f24] | 344 | sysman_log(LVL_ERROR, "Cannot create closure for job %p (%i)",
|
---|
| 345 | job, rc);
|
---|
| 346 | goto fail;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
[8432ae1] | 349 | /*
|
---|
| 350 | * If jobs are queued, reference is passed from closure to the queue,
|
---|
| 351 | * otherwise, we still have the reference.
|
---|
| 352 | */
|
---|
[72c8f77] | 353 | rc = job_queue_add_closure(&job_closure);
|
---|
[3f7e1f24] | 354 | if (rc != EOK) {
|
---|
| 355 | goto fail;
|
---|
| 356 | }
|
---|
[8432ae1] | 357 | /* We don't need job anymore */
|
---|
| 358 | job_del_ref(&job);
|
---|
[3f7e1f24] | 359 |
|
---|
[5559712] | 360 | job_queue_process();
|
---|
[3f7e1f24] | 361 | return;
|
---|
| 362 |
|
---|
| 363 | fail:
|
---|
| 364 | job->retval = JOB_FAILED;
|
---|
| 365 | job_finish(job);
|
---|
[8432ae1] | 366 | job_del_ref(&job);
|
---|
| 367 |
|
---|
[72c8f77] | 368 | dyn_array_foreach(job_closure, job_t *, closure_job) {
|
---|
[3f7e1f24] | 369 | job_del_ref(&(*closure_job));
|
---|
[694253c] | 370 | }
|
---|
[3f7e1f24] | 371 | dyn_array_destroy(&job_closure);
|
---|
| 372 | }
|
---|
[694253c] | 373 |
|
---|
[5559712] | 374 | void sysman_event_job_finished(void *data)
|
---|
| 375 | {
|
---|
| 376 | notify_observers(data);
|
---|
| 377 | /* Unreference the event data */
|
---|
| 378 | job_t *job = data;
|
---|
| 379 | job_del_ref(&job);
|
---|
[694253c] | 380 |
|
---|
[5559712] | 381 | /* The finished job, might have been blocking */
|
---|
| 382 | job_queue_process();
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | void sysman_event_unit_exposee_created(void *data)
|
---|
[3f7e1f24] | 386 | {
|
---|
[5559712] | 387 | unit_t *unit = data;
|
---|
| 388 | unit_exposee_created(unit);
|
---|
[3f7e1f24] | 389 | }
|
---|
| 390 |
|
---|
[5559712] | 391 | void sysman_event_unit_failed(void *data)
|
---|
[3f7e1f24] | 392 | {
|
---|
[5559712] | 393 | unit_t *unit = data;
|
---|
| 394 | unit_fail(unit);
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 | void sysman_event_unit_state_changed(void *data)
|
---|
| 398 | {
|
---|
| 399 | notify_observers(data);
|
---|
[f42ee6f] | 400 | }
|
---|