[f42ee6f] | 1 | #include <assert.h>
|
---|
[694253c] | 2 | #include <errno.h>
|
---|
| 3 | #include <fibril_synch.h>
|
---|
[f42ee6f] | 4 | #include <mem.h>
|
---|
[694253c] | 5 | #include <stdio.h>
|
---|
[f42ee6f] | 6 | #include <stdlib.h>
|
---|
| 7 |
|
---|
[6efec7e3] | 8 | #include "log.h"
|
---|
[f42ee6f] | 9 | #include "unit.h"
|
---|
| 10 |
|
---|
[6efec7e3] | 11 | /** Virtual method table for each unit type */
|
---|
| 12 | static unit_ops_t *unit_type_vmts[] = {
|
---|
| 13 | [UNIT_TARGET] = &unit_tgt_ops,
|
---|
| 14 | [UNIT_MOUNT] = &unit_mnt_ops,
|
---|
| 15 | [UNIT_CONFIGURATION] = &unit_cfg_ops
|
---|
| 16 | };
|
---|
| 17 |
|
---|
[f42ee6f] | 18 | static void unit_init(unit_t *unit, unit_type_t type)
|
---|
| 19 | {
|
---|
| 20 | assert(unit);
|
---|
| 21 |
|
---|
[6efec7e3] | 22 | memset(unit, 0, sizeof(unit_t));
|
---|
[f42ee6f] | 23 | link_initialize(&unit->units);
|
---|
[694253c] | 24 |
|
---|
[f42ee6f] | 25 | unit->type = type;
|
---|
| 26 | unit->state = STATE_EMBRYO;
|
---|
[694253c] | 27 | fibril_mutex_initialize(&unit->state_mtx);
|
---|
| 28 | fibril_condvar_initialize(&unit->state_cv);
|
---|
| 29 |
|
---|
| 30 | list_initialize(&unit->dependants);
|
---|
| 31 | list_initialize(&unit->dependencies);
|
---|
[6efec7e3] | 32 |
|
---|
| 33 | unit_type_vmts[unit->type]->init(unit);
|
---|
[f42ee6f] | 34 | }
|
---|
| 35 |
|
---|
| 36 | unit_t *unit_create(unit_type_t type)
|
---|
| 37 | {
|
---|
| 38 | unit_t *unit = malloc(sizeof(unit_t));
|
---|
| 39 | if (unit != NULL) {
|
---|
| 40 | unit_init(unit, type);
|
---|
| 41 | }
|
---|
| 42 | return unit;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | /** Release resources used by unit structure */
|
---|
[6efec7e3] | 46 | void unit_destroy(unit_t **unit_ptr)
|
---|
[f42ee6f] | 47 | {
|
---|
[6efec7e3] | 48 | unit_t *unit = *unit_ptr;
|
---|
| 49 | if (unit == NULL)
|
---|
[f42ee6f] | 50 | return;
|
---|
[6efec7e3] | 51 |
|
---|
| 52 | unit_type_vmts[unit->type]->destroy(unit);
|
---|
[f42ee6f] | 53 | /* TODO:
|
---|
| 54 | * edges,
|
---|
| 55 | * check it's not an active unit,
|
---|
| 56 | * other resources to come
|
---|
| 57 | */
|
---|
[6efec7e3] | 58 | free(unit);
|
---|
| 59 | unit_ptr = NULL;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | void unit_set_state(unit_t *unit, unit_state_t state)
|
---|
| 63 | {
|
---|
| 64 | fibril_mutex_lock(&unit->state_mtx);
|
---|
| 65 | unit->state = state;
|
---|
| 66 | fibril_condvar_broadcast(&unit->state_cv);
|
---|
| 67 | fibril_mutex_unlock(&unit->state_mtx);
|
---|
[f42ee6f] | 68 | }
|
---|
[694253c] | 69 |
|
---|
| 70 | /** Issue request to restarter to start a unit
|
---|
| 71 | *
|
---|
| 72 | * Return from this function only means start request was issued.
|
---|
| 73 | * If you need to wait for real start of the unit, use waiting on state_cv.
|
---|
| 74 | */
|
---|
| 75 | int unit_start(unit_t *unit)
|
---|
| 76 | {
|
---|
[6efec7e3] | 77 | sysman_log(LVL_DEBUG, "%s(%p)", __func__, unit);
|
---|
| 78 | return unit_type_vmts[unit->type]->start(unit);
|
---|
[694253c] | 79 | }
|
---|