Changeset 015b147 in mainline for uspace/srv/sysman/repo.c


Ignore:
Timestamp:
2019-08-17T13:52:32Z (5 years ago)
Author:
Matthieu Riolo <matthieu.riolo@…>
Children:
be07995
Parents:
31ef7c1
git-author:
Michal Koutný <xm.koutny+hos@…> (2016-01-10 17:17:19)
git-committer:
Matthieu Riolo <matthieu.riolo@…> (2019-08-17 13:52:32)
Message:

sysman: Refactored unit repo iteration and locking

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/sysman/repo.c

    r31ef7c1 r015b147  
    3838#include "log.h"
    3939
    40 LIST_INITIALIZE(units);
     40LIST_INITIALIZE(units_);
    4141
    4242static hash_table_t units_by_name;
     
    119119static void repo_remove_unit_internal(unit_t *u)
    120120{
     121        assert(fibril_rwlock_is_write_locked(&repo_lock));
     122
    121123        hash_table_remove_item(&units_by_name, &u->units_by_name);
    122124        hash_table_remove_item(&units_by_handle, &u->units_by_handle);
     
    125127        // TODO decrease refcount of unit
    126128        // unit may be referenced e.g. from running job, thus we cannot simply destroy it
     129}
     130
     131static unit_t *repo_find_unit_by_name_internal(const char *name, bool lock)
     132{
     133        sysman_log(LVL_DEBUG2, "%s(%s, %i)", __func__, name, lock);
     134        if (lock) fibril_rwlock_read_lock(&repo_lock);
     135        ht_link_t *ht_link = hash_table_find(&units_by_name, (void *)name);
     136        if (lock) fibril_rwlock_read_unlock(&repo_lock);
     137
     138        if (ht_link != NULL) {
     139                return hash_table_get_inst(ht_link, unit_t, units_by_name);
     140        } else {
     141                return NULL;
     142        }
    127143}
    128144
     
    139155        assert(unit->handle == 0);
    140156        assert(unit->name != NULL);
     157        assert(fibril_rwlock_is_write_locked(&repo_lock));
    141158        sysman_log(LVL_DEBUG2, "%s('%s')", __func__, unit_name(unit));
    142159
    143         fibril_rwlock_write_lock(&repo_lock);
    144160        if (hash_table_insert_unique(&units_by_name, &unit->units_by_name)) {
    145161                /* Pointers are same size as unit_handle_t both on 32b and 64b */
     
    147163
    148164                hash_table_insert(&units_by_handle, &unit->units_by_handle);
    149                 list_append(&unit->units, &units);
    150                 fibril_rwlock_write_unlock(&repo_lock);
     165                list_append(&unit->units, &units_);
    151166                return EOK;
    152167        } else {
    153                 fibril_rwlock_write_unlock(&repo_lock);
    154168                return EEXISTS;
    155169        }
     
    162176}
    163177
    164 void repo_begin_update(void) {
    165         sysman_log(LVL_DEBUG2, "%s", __func__);
     178void repo_begin_update_(void) {
     179        sysman_log(LVL_DEBUG2, "%s", __func__);
     180        fibril_rwlock_write_lock(&repo_lock);
    166181}
    167182
     
    196211         */
    197212        hash_table_apply(&units_by_name, &repo_commit_unit, NULL);
     213        fibril_rwlock_write_unlock(&repo_lock);
    198214}
    199215
     
    228244
    229245        hash_table_apply(&units_by_name, &repo_rollback_unit, NULL);
     246        fibril_rwlock_write_unlock(&repo_lock);
    230247}
    231248
     
    243260
    244261                unit_t *output =
    245                     repo_find_unit_by_name(e->output_name);
     262                    repo_find_unit_by_name_unsafe(e->output_name);
    246263                if (output == NULL) {
    247264                        sysman_log(LVL_ERROR,
     
    273290}
    274291
    275 unit_t *repo_find_unit_by_name(const char *name)
    276 {
    277         fibril_rwlock_read_lock(&repo_lock);
    278         ht_link_t *ht_link = hash_table_find(&units_by_name, (void *)name);
    279         fibril_rwlock_read_unlock(&repo_lock);
    280 
    281         if (ht_link != NULL) {
    282                 return hash_table_get_inst(ht_link, unit_t, units_by_name);
    283         } else {
    284                 return NULL;
    285         }
    286 }
    287 
     292/**
     293 * The function can be safely called from non-event loop fibrils
     294 */
     295unit_t *repo_find_unit_by_name_(const char *name)
     296{
     297        return repo_find_unit_by_name_internal(name, true);
     298}
     299
     300/**
     301 * @note Caller must hold repo_lock (at least reader)
     302 */
     303unit_t *repo_find_unit_by_name_unsafe(const char *name)
     304{
     305        return repo_find_unit_by_name_internal(name, false);
     306}
     307
     308/**
     309 * The function can be safely called from non-event loop fibrils
     310 */
    288311unit_t *repo_find_unit_by_handle(unit_handle_t handle)
    289312{
     313        sysman_log(LVL_DEBUG2, "%s", __func__);
    290314        fibril_rwlock_read_lock(&repo_lock);
    291315        ht_link_t *ht_link = hash_table_find(&units_by_handle, &handle);
     
    301325void repo_rlock(void)
    302326{
     327        sysman_log(LVL_DEBUG2, "%s", __func__);
    303328        fibril_rwlock_read_lock(&repo_lock);
    304329}
     
    306331void repo_runlock(void)
    307332{
     333        sysman_log(LVL_DEBUG2, "%s", __func__);
    308334        fibril_rwlock_read_unlock(&repo_lock);
    309335}
Note: See TracChangeset for help on using the changeset viewer.