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

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

sysman: Rename configuration to repo (forgotten change)

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