1 | #include <assert.h>
|
---|
2 | #include <errno.h>
|
---|
3 | #include <fibril_synch.h>
|
---|
4 | #include <mem.h>
|
---|
5 | #include <stdio.h>
|
---|
6 | #include <stdlib.h>
|
---|
7 |
|
---|
8 | #include "log.h"
|
---|
9 | #include "unit.h"
|
---|
10 |
|
---|
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 |
|
---|
18 | static void unit_init(unit_t *unit, unit_type_t type)
|
---|
19 | {
|
---|
20 | assert(unit);
|
---|
21 |
|
---|
22 | memset(unit, 0, sizeof(unit_t));
|
---|
23 | link_initialize(&unit->units);
|
---|
24 |
|
---|
25 | unit->type = type;
|
---|
26 | unit->state = STATE_EMBRYO;
|
---|
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);
|
---|
32 |
|
---|
33 | unit_type_vmts[unit->type]->init(unit);
|
---|
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 */
|
---|
46 | void unit_destroy(unit_t **unit_ptr)
|
---|
47 | {
|
---|
48 | unit_t *unit = *unit_ptr;
|
---|
49 | if (unit == NULL)
|
---|
50 | return;
|
---|
51 |
|
---|
52 | unit_type_vmts[unit->type]->destroy(unit);
|
---|
53 | /* TODO:
|
---|
54 | * edges,
|
---|
55 | * check it's not an active unit,
|
---|
56 | * other resources to come
|
---|
57 | */
|
---|
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);
|
---|
68 | }
|
---|
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 | {
|
---|
77 | sysman_log(LVL_DEBUG, "%s(%p)", __func__, unit);
|
---|
78 | return unit_type_vmts[unit->type]->start(unit);
|
---|
79 | }
|
---|