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

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

sysman: Synchronize access to unit repository from other fibrils

  • Property mode set to 100644
File size: 8.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
[dda2602]40LIST_INITIALIZE(units);
41
42static hash_table_t units_by_name;
[b55f62a]43static hash_table_t units_by_handle;
[ff20afc]44/** Lock to protect units_by_name and units_by_handle, so that
45 * repo_find_unit_by_* can be called also from non-event loop fibrils.
46 */
47static FIBRIL_RWLOCK_INITIALIZE(repo_lock);
[bb154c6]48
49/* Hash table functions */
[b55f62a]50static size_t units_by_handle_ht_hash(const ht_link_t *item)
51{
52 unit_t *unit =
53 hash_table_get_inst(item, unit_t, units_by_handle);
54 return unit->handle;
55}
56
57static size_t units_by_handle_ht_key_hash(void *key)
58{
59 return *(unit_handle_t *)key;
60}
61
62static bool units_by_handle_ht_equal(const ht_link_t *item1, const ht_link_t *item2)
63{
64 return
65 hash_table_get_inst(item1, unit_t, units_by_handle) ==
66 hash_table_get_inst(item2, unit_t, units_by_handle);
67}
68
69static bool units_by_handle_ht_key_equal(void *key, const ht_link_t *item)
70{
71 return *(unit_handle_t *)key ==
72 hash_table_get_inst(item, unit_t, units_by_handle)->handle;
73}
74
75static hash_table_ops_t units_by_handle_ht_ops = {
76 .hash = &units_by_handle_ht_hash,
77 .key_hash = &units_by_handle_ht_key_hash,
78 .equal = &units_by_handle_ht_equal,
79 .key_equal = &units_by_handle_ht_key_equal,
[8ae8262]80 .remove_callback = NULL
[b55f62a]81};
82
[dda2602]83static size_t units_by_name_ht_hash(const ht_link_t *item)
[bb154c6]84{
85 unit_t *unit =
[dda2602]86 hash_table_get_inst(item, unit_t, units_by_name);
[bb154c6]87 return hash_string(unit->name);
88}
89
[dda2602]90static size_t units_by_name_ht_key_hash(void *key)
[bb154c6]91{
92 return hash_string((const char *)key);
93}
94
[dda2602]95static bool units_by_name_ht_equal(const ht_link_t *item1, const ht_link_t *item2)
[bb154c6]96{
[b55f62a]97 return
98 hash_table_get_inst(item1, unit_t, units_by_handle) ==
99 hash_table_get_inst(item2, unit_t, units_by_handle);
[bb154c6]100}
101
[dda2602]102static bool units_by_name_ht_key_equal(void *key, const ht_link_t *item)
[bb154c6]103{
104 return str_cmp((const char *)key,
[dda2602]105 hash_table_get_inst(item, unit_t, units_by_name)->name) == 0;
[bb154c6]106}
107
108
[dda2602]109static hash_table_ops_t units_by_name_ht_ops = {
110 .hash = &units_by_name_ht_hash,
111 .key_hash = &units_by_name_ht_key_hash,
112 .equal = &units_by_name_ht_equal,
113 .key_equal = &units_by_name_ht_key_equal,
[8ae8262]114 .remove_callback = NULL
[bb154c6]115};
116
[af92309]117/* Repository functions */
[f42ee6f]118
[8ae8262]119static void repo_remove_unit_internal(unit_t *u)
120{
121 hash_table_remove_item(&units_by_name, &u->units_by_name);
122 hash_table_remove_item(&units_by_handle, &u->units_by_handle);
123 list_remove(&u->units);
124
125 // TODO decrease refcount of unit
126 // unit may be referenced e.g. from running job, thus we cannot simply destroy it
127}
128
[af92309]129void repo_init(void)
[f42ee6f]130{
[dda2602]131 hash_table_create(&units_by_name, 0, 0, &units_by_name_ht_ops);
[b55f62a]132 hash_table_create(&units_by_handle, 0, 0, &units_by_handle_ht_ops);
[f42ee6f]133}
134
[af92309]135int repo_add_unit(unit_t *unit)
[f42ee6f]136{
137 assert(unit);
[8ae8262]138 assert(unit->repo_state == REPO_EMBRYO);
[b55f62a]139 assert(unit->handle == 0);
[bb154c6]140 assert(unit->name != NULL);
141 sysman_log(LVL_DEBUG2, "%s('%s')", __func__, unit_name(unit));
[f42ee6f]142
[ff20afc]143 fibril_rwlock_write_lock(&repo_lock);
[dda2602]144 if (hash_table_insert_unique(&units_by_name, &unit->units_by_name)) {
[b55f62a]145 /* Pointers are same size as unit_handle_t both on 32b and 64b */
146 unit->handle = (unit_handle_t)unit;
147
148 hash_table_insert(&units_by_handle, &unit->units_by_handle);
[dda2602]149 list_append(&unit->units, &units);
[ff20afc]150 fibril_rwlock_write_unlock(&repo_lock);
[bb154c6]151 return EOK;
152 } else {
[ff20afc]153 fibril_rwlock_write_unlock(&repo_lock);
[bb154c6]154 return EEXISTS;
155 }
156}
157
[8ae8262]158int repo_remove_unit(unit_t *unit)
159{
160 unit->repo_state = REPO_ZOMBIE;
161 return EOK; /* We could check that unit is present in repo etc... */
162}
163
[af92309]164void repo_begin_update(void) {
[bb154c6]165 sysman_log(LVL_DEBUG2, "%s", __func__);
166}
167
[af92309]168static bool repo_commit_unit(ht_link_t *ht_link, void *arg)
[bb154c6]169{
[dda2602]170 unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
[8ae8262]171 if (unit->repo_state == REPO_ZOMBIE) {
172 repo_remove_unit_internal(unit);
173 return true;
174 }
175
176 if (unit->repo_state == REPO_EMBRYO) {
177 unit->repo_state = REPO_LIVING;
[bb154c6]178 }
179
[9532981]180 list_foreach(unit->edges_out, edges_out, unit_edge_t, e) {
181 e->commited = true;
[bb154c6]182 }
183 return true;
[f42ee6f]184}
185
[dda2602]186/** Marks newly added units_by_name as usable (via state change) */
[af92309]187void repo_commit(void)
[f42ee6f]188{
[6efec7e3]189 sysman_log(LVL_DEBUG2, "%s", __func__);
190
[bb154c6]191 /*
[8ae8262]192 * Apply commit to all units_by_name, each commited unit commits its
193 * outgoing deps, thus eventually commiting all embryo deps as well.
194 *
195 * TODO why not iterate over units list?
[bb154c6]196 */
[af92309]197 hash_table_apply(&units_by_name, &repo_commit_unit, NULL);
[bb154c6]198}
199
[af92309]200static bool repo_rollback_unit(ht_link_t *ht_link, void *arg)
[bb154c6]201{
[dda2602]202 unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
[bb154c6]203
[9532981]204 list_foreach_safe(unit->edges_out, cur_link, next_link) {
205 unit_edge_t *e =
206 list_get_instance(cur_link, unit_edge_t, edges_out);
207 if (!e->commited) {
208 edge_remove(&e);
[bb154c6]209 }
210 }
211
[8ae8262]212 if (unit->repo_state == REPO_EMBRYO) {
213 repo_remove_unit_internal(unit);
214 } else if (unit->repo_state == REPO_ZOMBIE) {
215 unit->repo_state = REPO_LIVING;
[bb154c6]216 }
217
218 return true;
219}
220
[dda2602]221/** Remove all uncommited units_by_name and edges from configuratio
[5559712]222 *
223 * Memory used by removed object is released.
224 */
[af92309]225void repo_rollback(void)
[bb154c6]226{
227 sysman_log(LVL_DEBUG2, "%s", __func__);
228
[af92309]229 hash_table_apply(&units_by_name, &repo_rollback_unit, NULL);
[bb154c6]230}
231
[af92309]232static bool repo_resolve_unit(ht_link_t *ht_link, void *arg)
[bb154c6]233{
234 bool *has_error_ptr = arg;
[dda2602]235 unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
[bb154c6]236
[9532981]237 list_foreach(unit->edges_out, edges_out, unit_edge_t, e) {
238 assert(e->input == unit);
239 assert((e->output != NULL) != (e->output_name != NULL));
240 if (e->output) {
[bb154c6]241 continue;
242 }
243
[9532981]244 unit_t *output =
245 repo_find_unit_by_name(e->output_name);
246 if (output == NULL) {
[bb154c6]247 sysman_log(LVL_ERROR,
248 "Cannot resolve dependency of '%s' to unit '%s'",
[9532981]249 unit_name(unit), e->output_name);
[bb154c6]250 *has_error_ptr = true;
251 // TODO should we just leave the sprout untouched?
252 } else {
[9532981]253 edge_resolve_output(e, output);
[f42ee6f]254 }
255 }
256
[bb154c6]257 return true;
258}
259
[dda2602]260/** Resolve unresolved dependencies between any pair of units_by_name
[bb154c6]261 *
262 * @return EOK on success
[5559712]263 * @return ENOENT when one or more resolution fails, information is logged
[bb154c6]264 */
[9532981]265int repo_resolve_references(void)
[bb154c6]266{
267 sysman_log(LVL_DEBUG2, "%s", __func__);
268
269 bool has_error = false;
[af92309]270 hash_table_apply(&units_by_name, &repo_resolve_unit, &has_error);
[bb154c6]271
272 return has_error ? ENOENT : EOK;
[f42ee6f]273}
[bb154c6]274
[af92309]275unit_t *repo_find_unit_by_name(const char *name)
[bb154c6]276{
[ff20afc]277 fibril_rwlock_read_lock(&repo_lock);
[dda2602]278 ht_link_t *ht_link = hash_table_find(&units_by_name, (void *)name);
[ff20afc]279 fibril_rwlock_read_unlock(&repo_lock);
280
[bb154c6]281 if (ht_link != NULL) {
[dda2602]282 return hash_table_get_inst(ht_link, unit_t, units_by_name);
[bb154c6]283 } else {
284 return NULL;
285 }
286}
287
[af92309]288unit_t *repo_find_unit_by_handle(unit_handle_t handle)
[b55f62a]289{
[ff20afc]290 fibril_rwlock_read_lock(&repo_lock);
[b55f62a]291 ht_link_t *ht_link = hash_table_find(&units_by_handle, &handle);
[ff20afc]292 fibril_rwlock_read_unlock(&repo_lock);
293
[b55f62a]294 if (ht_link != NULL) {
295 return hash_table_get_inst(ht_link, unit_t, units_by_handle);
296 } else {
297 return NULL;
298 }
299}
300
[ff20afc]301void repo_rlock(void)
302{
303 fibril_rwlock_read_lock(&repo_lock);
304}
305
306void repo_runlock(void)
307{
308 fibril_rwlock_read_unlock(&repo_lock);
309}
Note: See TracBrowser for help on using the repository browser.