source: mainline/uspace/srv/sysman/unit.c@ 6efec7e3

Last change on this file since 6efec7e3 was 6efec7e3, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

Unit polymorphism (simple mount), debug logging

  • Property mode set to 100644
File size: 1.7 KB
Line 
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 */
12static 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
18static 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
36unit_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 */
46void 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
62void 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 */
75int 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}
Note: See TracBrowser for help on using the repository browser.