Merge pull request #165 from robertbaldyga/ctx-get-put

Give ocf_ctx get/put semantics
This commit is contained in:
Jan Musiał 2019-05-24 14:29:04 +02:00 committed by GitHub
commit 93a06686a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 42 additions and 28 deletions

View File

@ -90,7 +90,7 @@ int my_cache_init(void)
{ {
int result; int result;
result ocf_ctx_init(&ctx, &ctx_ops) result = ocf_ctx_create(&ctx, &ctx_ops)
if (result) { if (result) {
/* Cannot initialze context of OCF library */ /* Cannot initialze context of OCF library */
return result; return result;
@ -116,7 +116,7 @@ int my_cache_init(void)
err: err:
/* In case of failure we destroy context and propagate error code */ /* In case of failure we destroy context and propagate error code */
ocf_ctx_exit(ctx); ocf_ctx_put(ctx);
return result; return result;
} }

View File

@ -278,13 +278,13 @@ int ctx_init(ocf_ctx_t *ctx)
{ {
int ret; int ret;
ret = ocf_ctx_init(ctx, &ctx_cfg); ret = ocf_ctx_create(ctx, &ctx_cfg);
if (ret) if (ret)
return ret; return ret;
ret = volume_init(*ctx); ret = volume_init(*ctx);
if (ret) { if (ret) {
ocf_ctx_exit(*ctx); ocf_ctx_put(*ctx);
return ret; return ret;
} }
@ -298,5 +298,5 @@ int ctx_init(ocf_ctx_t *ctx)
void ctx_cleanup(ocf_ctx_t ctx) void ctx_cleanup(ocf_ctx_t ctx)
{ {
volume_cleanup(ctx); volume_cleanup(ctx);
ocf_ctx_exit(ctx); ocf_ctx_put(ctx);
} }

View File

@ -285,24 +285,27 @@ int ocf_ctx_volume_create(ocf_ctx_t ctx, ocf_volume_t *volume,
struct ocf_volume_uuid *uuid, uint8_t type_id); struct ocf_volume_uuid *uuid, uint8_t type_id);
/** /**
* @brief Initialize OCF context * @brief Create and initialize OCF context
* *
* @param[out] ctx OCF context * @param[out] ctx OCF context
* @param[in] ops OCF context operations * @param[in] ops OCF context operations
* *
* @return Zero when success, otherwise an error * @return Zero when success, otherwise an error
*/ */
int ocf_ctx_init(ocf_ctx_t *ctx, const struct ocf_ctx_config *cfg); int ocf_ctx_create(ocf_ctx_t *ctx, const struct ocf_ctx_config *cfg);
/** /**
* @brief De-Initialize OCF context * @brief Increase reference counter of ctx
* *
* @param[in] ctx OCF context * @param[in] ctx OCF context
*
* @note Precondition is stopping all cache instances
*
* @return Zero when success, otherwise an error
*/ */
int ocf_ctx_exit(ocf_ctx_t ctx); void ocf_ctx_get(ocf_ctx_t ctx);
/**
* @brief Decrease reference counter of ctx
*
* @param[in] ctx OCF context
*/
void ocf_ctx_put(ocf_ctx_t ctx);
#endif /* __OCF_CTX_H__ */ #endif /* __OCF_CTX_H__ */

View File

@ -1216,6 +1216,8 @@ static int _ocf_mngt_cache_start(ocf_ctx_t ctx, ocf_cache_t *cache,
if (result) if (result)
goto _cache_mng_init_instance_ERROR; goto _cache_mng_init_instance_ERROR;
ocf_ctx_get(ctx);
if (params.locked) { if (params.locked) {
/* Increment reference counter to match cache_lock / /* Increment reference counter to match cache_lock /
cache_unlock convention. User is expected to call cache_unlock convention. User is expected to call

View File

@ -119,11 +119,15 @@ void cache_mng_core_remove_from_cache(struct ocf_cache *cache, int core_id)
void ocf_mngt_cache_put(ocf_cache_t cache) void ocf_mngt_cache_put(ocf_cache_t cache)
{ {
ocf_ctx_t ctx;
OCF_CHECK_NULL(cache); OCF_CHECK_NULL(cache);
if (ocf_refcnt_dec(&cache->refcnt.cache) == 0) { if (ocf_refcnt_dec(&cache->refcnt.cache) == 0) {
ctx = cache->owner;
ocf_metadata_deinit(cache); ocf_metadata_deinit(cache);
env_vfree(cache); env_vfree(cache);
ocf_ctx_put(ctx);
} }
} }

View File

@ -114,7 +114,7 @@ int ocf_ctx_volume_create(ocf_ctx_t ctx, ocf_volume_t *volume,
/* /*
* *
*/ */
int ocf_ctx_init(ocf_ctx_t *ctx, const struct ocf_ctx_config *cfg) int ocf_ctx_create(ocf_ctx_t *ctx, const struct ocf_ctx_config *cfg)
{ {
ocf_ctx_t ocf_ctx; ocf_ctx_t ocf_ctx;
int ret; int ret;
@ -127,6 +127,7 @@ int ocf_ctx_init(ocf_ctx_t *ctx, const struct ocf_ctx_config *cfg)
return -ENOMEM; return -ENOMEM;
INIT_LIST_HEAD(&ocf_ctx->caches); INIT_LIST_HEAD(&ocf_ctx->caches);
env_atomic_set(&ocf_ctx->ref_count, 1);
ret = env_mutex_init(&ocf_ctx->lock); ret = env_mutex_init(&ocf_ctx->lock);
if (ret) if (ret)
goto err_ctx; goto err_ctx;
@ -166,25 +167,30 @@ err_ctx:
/* /*
* *
*/ */
int ocf_ctx_exit(ocf_ctx_t ctx) void ocf_ctx_get(ocf_ctx_t ctx)
{ {
int result = 0;
OCF_CHECK_NULL(ctx); OCF_CHECK_NULL(ctx);
/* Check if caches are setup */ env_atomic_inc(&ctx->ref_count);
}
/*
*
*/
void ocf_ctx_put(ocf_ctx_t ctx)
{
OCF_CHECK_NULL(ctx);
if (env_atomic_dec_return(&ctx->ref_count))
return;
env_mutex_lock(&ctx->lock); env_mutex_lock(&ctx->lock);
if (!list_empty(&ctx->caches)) ENV_BUG_ON(!list_empty(&ctx->caches));
result = -EEXIST;
env_mutex_unlock(&ctx->lock); env_mutex_unlock(&ctx->lock);
if (result)
return result;
ocf_mngt_core_pool_deinit(ctx); ocf_mngt_core_pool_deinit(ctx);
ocf_core_volume_type_deinit(ctx); ocf_core_volume_type_deinit(ctx);
ocf_utils_deinit(ctx); ocf_utils_deinit(ctx);
ocf_logger_close(&ctx->logger); ocf_logger_close(&ctx->logger);
env_free(ctx); env_free(ctx);
return 0;
} }

View File

@ -20,6 +20,7 @@ struct ocf_ctx {
const struct ocf_ctx_config *cfg; const struct ocf_ctx_config *cfg;
struct ocf_logger logger; struct ocf_logger logger;
struct ocf_volume_type *volume_type[OCF_VOLUME_TYPE_MAX]; struct ocf_volume_type *volume_type[OCF_VOLUME_TYPE_MAX];
env_atomic ref_count;
env_mutex lock; env_mutex lock;
struct list_head caches; struct list_head caches;
struct { struct {

View File

@ -51,7 +51,7 @@ class OcfCtx:
logger_priv=cast(pointer(logger.get_priv()), c_void_p), logger_priv=cast(pointer(logger.get_priv()), c_void_p),
) )
result = self.lib.ocf_ctx_init(byref(self.ctx_handle), byref(self.cfg)) result = self.lib.ocf_ctx_create(byref(self.ctx_handle), byref(self.cfg))
if result != 0: if result != 0:
raise OcfError("Context initialization failed", result) raise OcfError("Context initialization failed", result)
@ -95,9 +95,7 @@ class OcfCtx:
self.stop_caches() self.stop_caches()
self.cleanup_volume_types() self.cleanup_volume_types()
result = self.lib.ocf_ctx_exit(self.ctx_handle) self.lib.ocf_ctx_put(self.ctx_handle)
if result != 0:
raise OcfError("Failed quitting OcfCtx", result)
self.cfg = None self.cfg = None
self.logger = None self.logger = None