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
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
40// TODO rename to repository (dynamic nature of units storage, and do not name it godlike Manager :-)
41
42LIST_INITIALIZE(units);
43
44static hash_table_t units_by_name;
45static hash_table_t units_by_handle;
46
47/* Hash table functions */
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
81static size_t units_by_name_ht_hash(const ht_link_t *item)
82{
83 unit_t *unit =
84 hash_table_get_inst(item, unit_t, units_by_name);
85 return hash_string(unit->name);
86}
87
88static size_t units_by_name_ht_key_hash(void *key)
89{
90 return hash_string((const char *)key);
91}
92
93static bool units_by_name_ht_equal(const ht_link_t *item1, const ht_link_t *item2)
94{
95 return
96 hash_table_get_inst(item1, unit_t, units_by_handle) ==
97 hash_table_get_inst(item2, unit_t, units_by_handle);
98}
99
100static bool units_by_name_ht_key_equal(void *key, const ht_link_t *item)
101{
102 return str_cmp((const char *)key,
103 hash_table_get_inst(item, unit_t, units_by_name)->name) == 0;
104}
105
106
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,
112 .remove_callback = NULL // TODO realy unneeded?
113};
114
115/* Repository functions */
116
117void repo_init(void)
118{
119 hash_table_create(&units_by_name, 0, 0, &units_by_name_ht_ops);
120 hash_table_create(&units_by_handle, 0, 0, &units_by_handle_ht_ops);
121}
122
123int repo_add_unit(unit_t *unit)
124{
125 assert(unit);
126 assert(unit->state == STATE_EMBRYO);
127 assert(unit->handle == 0);
128 assert(unit->name != NULL);
129 sysman_log(LVL_DEBUG2, "%s('%s')", __func__, unit_name(unit));
130
131 if (hash_table_insert_unique(&units_by_name, &unit->units_by_name)) {
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);
136 list_append(&unit->units, &units);
137 return EOK;
138 } else {
139 return EEXISTS;
140 }
141}
142
143void repo_begin_update(void) {
144 sysman_log(LVL_DEBUG2, "%s", __func__);
145}
146
147static bool repo_commit_unit(ht_link_t *ht_link, void *arg)
148{
149 unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
150 if (unit->state == STATE_EMBRYO) {
151 unit->state = STATE_STOPPED;
152 }
153
154 list_foreach(unit->edges_out, edges_out, unit_edge_t, e) {
155 e->commited = true;
156 }
157 return true;
158}
159
160/** Marks newly added units_by_name as usable (via state change) */
161void repo_commit(void)
162{
163 sysman_log(LVL_DEBUG2, "%s", __func__);
164
165 /*
166 * Apply commit to all units_by_name, each commited unit commits its outgoing
167 * deps, thus eventually commiting all embryo deps as well.
168 */
169 hash_table_apply(&units_by_name, &repo_commit_unit, NULL);
170}
171
172static bool repo_rollback_unit(ht_link_t *ht_link, void *arg)
173{
174 unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
175
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);
181 }
182 }
183
184 if (unit->state == STATE_EMBRYO) {
185 hash_table_remove_item(&units_by_name, ht_link);
186 list_remove(&unit->units);
187 unit_destroy(&unit);
188 }
189
190 return true;
191}
192
193/** Remove all uncommited units_by_name and edges from configuratio
194 *
195 * Memory used by removed object is released.
196 */
197void repo_rollback(void)
198{
199 sysman_log(LVL_DEBUG2, "%s", __func__);
200
201 hash_table_apply(&units_by_name, &repo_rollback_unit, NULL);
202}
203
204static bool repo_resolve_unit(ht_link_t *ht_link, void *arg)
205{
206 bool *has_error_ptr = arg;
207 unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
208
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) {
213 continue;
214 }
215
216 unit_t *output =
217 repo_find_unit_by_name(e->output_name);
218 if (output == NULL) {
219 sysman_log(LVL_ERROR,
220 "Cannot resolve dependency of '%s' to unit '%s'",
221 unit_name(unit), e->output_name);
222 *has_error_ptr = true;
223 // TODO should we just leave the sprout untouched?
224 } else {
225 edge_resolve_output(e, output);
226 }
227 }
228
229 return true;
230}
231
232/** Resolve unresolved dependencies between any pair of units_by_name
233 *
234 * @return EOK on success
235 * @return ENOENT when one or more resolution fails, information is logged
236 */
237int repo_resolve_references(void)
238{
239 sysman_log(LVL_DEBUG2, "%s", __func__);
240
241 bool has_error = false;
242 hash_table_apply(&units_by_name, &repo_resolve_unit, &has_error);
243
244 return has_error ? ENOENT : EOK;
245}
246
247unit_t *repo_find_unit_by_name(const char *name)
248{
249 ht_link_t *ht_link = hash_table_find(&units_by_name, (void *)name);
250 if (ht_link != NULL) {
251 return hash_table_get_inst(ht_link, unit_t, units_by_name);
252 } else {
253 return NULL;
254 }
255}
256
257unit_t *repo_find_unit_by_handle(unit_handle_t handle)
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.