source: mainline/uspace/srv/sysman/repo.c@ 9532981

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

sysman: Rename dependency to edge (more generic)

  • Property mode set to 100644
File size: 7.4 KB
RevLine 
[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
[bb154c6]29#include <adt/hash.h>
30#include <adt/hash_table.h>
[f42ee6f]31#include <adt/list.h>
32#include <assert.h>
33#include <errno.h>
34#include <fibril_synch.h>
35
[af92309]36#include "repo.h"
[9532981]37#include "edge.h"
[6efec7e3]38#include "log.h"
[f42ee6f]39
[e8747bd8]40// TODO rename to repository (dynamic nature of units storage, and do not name it godlike Manager :-)
41
[dda2602]42LIST_INITIALIZE(units);
43
44static hash_table_t units_by_name;
[b55f62a]45static hash_table_t units_by_handle;
[bb154c6]46
47/* Hash table functions */
[b55f62a]48static size_t units_by_handle_ht_hash(const ht_link_t *item)
49{
50 unit_t *unit =
51 hash_table_get_inst(item, unit_t, units_by_handle);
52 return unit->handle;
53}
54
55static size_t units_by_handle_ht_key_hash(void *key)
56{
57 return *(unit_handle_t *)key;
58}
59
60static bool units_by_handle_ht_equal(const ht_link_t *item1, const ht_link_t *item2)
61{
62 return
63 hash_table_get_inst(item1, unit_t, units_by_handle) ==
64 hash_table_get_inst(item2, unit_t, units_by_handle);
65}
66
67static bool units_by_handle_ht_key_equal(void *key, const ht_link_t *item)
68{
69 return *(unit_handle_t *)key ==
70 hash_table_get_inst(item, unit_t, units_by_handle)->handle;
71}
72
73static hash_table_ops_t units_by_handle_ht_ops = {
74 .hash = &units_by_handle_ht_hash,
75 .key_hash = &units_by_handle_ht_key_hash,
76 .equal = &units_by_handle_ht_equal,
77 .key_equal = &units_by_handle_ht_key_equal,
78 .remove_callback = NULL // TODO realy unneeded?
79};
80
[dda2602]81static size_t units_by_name_ht_hash(const ht_link_t *item)
[bb154c6]82{
83 unit_t *unit =
[dda2602]84 hash_table_get_inst(item, unit_t, units_by_name);
[bb154c6]85 return hash_string(unit->name);
86}
87
[dda2602]88static size_t units_by_name_ht_key_hash(void *key)
[bb154c6]89{
90 return hash_string((const char *)key);
91}
92
[dda2602]93static bool units_by_name_ht_equal(const ht_link_t *item1, const ht_link_t *item2)
[bb154c6]94{
[b55f62a]95 return
96 hash_table_get_inst(item1, unit_t, units_by_handle) ==
97 hash_table_get_inst(item2, unit_t, units_by_handle);
[bb154c6]98}
99
[dda2602]100static bool units_by_name_ht_key_equal(void *key, const ht_link_t *item)
[bb154c6]101{
102 return str_cmp((const char *)key,
[dda2602]103 hash_table_get_inst(item, unit_t, units_by_name)->name) == 0;
[bb154c6]104}
105
106
[dda2602]107static hash_table_ops_t units_by_name_ht_ops = {
108 .hash = &units_by_name_ht_hash,
109 .key_hash = &units_by_name_ht_key_hash,
110 .equal = &units_by_name_ht_equal,
111 .key_equal = &units_by_name_ht_key_equal,
[bb154c6]112 .remove_callback = NULL // TODO realy unneeded?
113};
114
[af92309]115/* Repository functions */
[f42ee6f]116
[af92309]117void repo_init(void)
[f42ee6f]118{
[dda2602]119 hash_table_create(&units_by_name, 0, 0, &units_by_name_ht_ops);
[b55f62a]120 hash_table_create(&units_by_handle, 0, 0, &units_by_handle_ht_ops);
[f42ee6f]121}
122
[af92309]123int repo_add_unit(unit_t *unit)
[f42ee6f]124{
125 assert(unit);
126 assert(unit->state == STATE_EMBRYO);
[b55f62a]127 assert(unit->handle == 0);
[bb154c6]128 assert(unit->name != NULL);
129 sysman_log(LVL_DEBUG2, "%s('%s')", __func__, unit_name(unit));
[f42ee6f]130
[dda2602]131 if (hash_table_insert_unique(&units_by_name, &unit->units_by_name)) {
[b55f62a]132 /* Pointers are same size as unit_handle_t both on 32b and 64b */
133 unit->handle = (unit_handle_t)unit;
134
135 hash_table_insert(&units_by_handle, &unit->units_by_handle);
[dda2602]136 list_append(&unit->units, &units);
[bb154c6]137 return EOK;
138 } else {
139 return EEXISTS;
140 }
141}
142
[af92309]143void repo_begin_update(void) {
[bb154c6]144 sysman_log(LVL_DEBUG2, "%s", __func__);
145}
146
[af92309]147static bool repo_commit_unit(ht_link_t *ht_link, void *arg)
[bb154c6]148{
[dda2602]149 unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
[bb154c6]150 if (unit->state == STATE_EMBRYO) {
151 unit->state = STATE_STOPPED;
152 }
153
[9532981]154 list_foreach(unit->edges_out, edges_out, unit_edge_t, e) {
155 e->commited = true;
[bb154c6]156 }
157 return true;
[f42ee6f]158}
159
[dda2602]160/** Marks newly added units_by_name as usable (via state change) */
[af92309]161void repo_commit(void)
[f42ee6f]162{
[6efec7e3]163 sysman_log(LVL_DEBUG2, "%s", __func__);
164
[bb154c6]165 /*
[dda2602]166 * Apply commit to all units_by_name, each commited unit commits its outgoing
[bb154c6]167 * deps, thus eventually commiting all embryo deps as well.
168 */
[af92309]169 hash_table_apply(&units_by_name, &repo_commit_unit, NULL);
[bb154c6]170}
171
[af92309]172static bool repo_rollback_unit(ht_link_t *ht_link, void *arg)
[bb154c6]173{
[dda2602]174 unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
[bb154c6]175
[9532981]176 list_foreach_safe(unit->edges_out, cur_link, next_link) {
177 unit_edge_t *e =
178 list_get_instance(cur_link, unit_edge_t, edges_out);
179 if (!e->commited) {
180 edge_remove(&e);
[bb154c6]181 }
182 }
183
184 if (unit->state == STATE_EMBRYO) {
[dda2602]185 hash_table_remove_item(&units_by_name, ht_link);
186 list_remove(&unit->units);
[bb154c6]187 unit_destroy(&unit);
188 }
189
190 return true;
191}
192
[dda2602]193/** Remove all uncommited units_by_name and edges from configuratio
[5559712]194 *
195 * Memory used by removed object is released.
196 */
[af92309]197void repo_rollback(void)
[bb154c6]198{
199 sysman_log(LVL_DEBUG2, "%s", __func__);
200
[af92309]201 hash_table_apply(&units_by_name, &repo_rollback_unit, NULL);
[bb154c6]202}
203
[af92309]204static bool repo_resolve_unit(ht_link_t *ht_link, void *arg)
[bb154c6]205{
206 bool *has_error_ptr = arg;
[dda2602]207 unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
[bb154c6]208
[9532981]209 list_foreach(unit->edges_out, edges_out, unit_edge_t, e) {
210 assert(e->input == unit);
211 assert((e->output != NULL) != (e->output_name != NULL));
212 if (e->output) {
[bb154c6]213 continue;
214 }
215
[9532981]216 unit_t *output =
217 repo_find_unit_by_name(e->output_name);
218 if (output == NULL) {
[bb154c6]219 sysman_log(LVL_ERROR,
220 "Cannot resolve dependency of '%s' to unit '%s'",
[9532981]221 unit_name(unit), e->output_name);
[bb154c6]222 *has_error_ptr = true;
223 // TODO should we just leave the sprout untouched?
224 } else {
[9532981]225 edge_resolve_output(e, output);
[f42ee6f]226 }
227 }
228
[bb154c6]229 return true;
230}
231
[dda2602]232/** Resolve unresolved dependencies between any pair of units_by_name
[bb154c6]233 *
234 * @return EOK on success
[5559712]235 * @return ENOENT when one or more resolution fails, information is logged
[bb154c6]236 */
[9532981]237int repo_resolve_references(void)
[bb154c6]238{
239 sysman_log(LVL_DEBUG2, "%s", __func__);
240
241 bool has_error = false;
[af92309]242 hash_table_apply(&units_by_name, &repo_resolve_unit, &has_error);
[bb154c6]243
244 return has_error ? ENOENT : EOK;
[f42ee6f]245}
[bb154c6]246
[af92309]247unit_t *repo_find_unit_by_name(const char *name)
[bb154c6]248{
[dda2602]249 ht_link_t *ht_link = hash_table_find(&units_by_name, (void *)name);
[bb154c6]250 if (ht_link != NULL) {
[dda2602]251 return hash_table_get_inst(ht_link, unit_t, units_by_name);
[bb154c6]252 } else {
253 return NULL;
254 }
255}
256
[af92309]257unit_t *repo_find_unit_by_handle(unit_handle_t handle)
[b55f62a]258{
259 ht_link_t *ht_link = hash_table_find(&units_by_handle, &handle);
260 if (ht_link != NULL) {
261 return hash_table_get_inst(ht_link, unit_t, units_by_handle);
262 } else {
263 return NULL;
264 }
265}
266
Note: See TracBrowser for help on using the repository browser.