From 0e15c693fd05962921959d93bdc93e846e94c9be Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Tue, 9 Apr 2019 16:29:24 -0400 Subject: [PATCH] Return post modification value from ocf_refcnt_inc/dec If counter is frozen then increment returns 0. Signed-off-by: Adam Rutkowski --- src/ocf_core.c | 2 +- src/utils/utils_refcnt.c | 14 +++++++++----- src/utils/utils_refcnt.h | 13 +++++++------ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/ocf_core.c b/src/ocf_core.c index 020e271..383cacd 100644 --- a/src/ocf_core.c +++ b/src/ocf_core.c @@ -156,7 +156,7 @@ static inline ocf_core_t ocf_volume_to_core(ocf_volume_t volume) static inline int ocf_io_set_dirty(ocf_cache_t cache, struct ocf_core_io *core_io) { - core_io->dirty = ocf_refcnt_inc(&cache->refcnt.dirty); + core_io->dirty = !!ocf_refcnt_inc(&cache->refcnt.dirty); return core_io->dirty ? 0 : -EBUSY; } diff --git a/src/utils/utils_refcnt.c b/src/utils/utils_refcnt.c index 43fda33..983a3f5 100644 --- a/src/utils/utils_refcnt.c +++ b/src/utils/utils_refcnt.c @@ -13,26 +13,30 @@ void ocf_refcnt_init(struct ocf_refcnt *rc) rc->cb = NULL; } -void ocf_refcnt_dec(struct ocf_refcnt *rc) +int ocf_refcnt_dec(struct ocf_refcnt *rc) { int val = env_atomic_dec_return(&rc->counter); ENV_BUG_ON(val < 0); if (!val && env_atomic_cmpxchg(&rc->callback, 1, 0)) rc->cb(rc->priv); + + return val; } -bool ocf_refcnt_inc(struct ocf_refcnt *rc) +int ocf_refcnt_inc(struct ocf_refcnt *rc) { + int val; + if (!env_atomic_read(&rc->freeze)) { - env_atomic_inc(&rc->counter); + val = env_atomic_inc_return(&rc->counter); if (!env_atomic_read(&rc->freeze)) - return true; + return val; else ocf_refcnt_dec(rc); } - return false; + return 0; } diff --git a/src/utils/utils_refcnt.h b/src/utils/utils_refcnt.h index ed00872..5567560 100644 --- a/src/utils/utils_refcnt.h +++ b/src/utils/utils_refcnt.h @@ -22,15 +22,16 @@ struct ocf_refcnt /* Initialize reference counter */ void ocf_refcnt_init(struct ocf_refcnt *rc); -/* Try to increment counter. Returns true if successfull, false if freezed */ -bool ocf_refcnt_inc(struct ocf_refcnt *rc); +/* Try to increment counter. Returns counter value (> 0) if successfull, 0 + * if counter is frozen */ +int ocf_refcnt_inc(struct ocf_refcnt *rc); -/* Decrement reference counter */ -void ocf_refcnt_dec(struct ocf_refcnt *rc); +/* Decrement reference counter and return post-decrement value */ +int ocf_refcnt_dec(struct ocf_refcnt *rc); /* Disallow incrementing of underlying counter - attempts to increment counter * will be failing until ocf_refcnt_unfreeze is calleed. - * It's ok to call freeze multiple times, in which case counter is freezed + * It's ok to call freeze multiple times, in which case counter is frozen * until all freeze calls are offset by a corresponding unfreeze.*/ void ocf_refcnt_freeze(struct ocf_refcnt *rc); @@ -40,7 +41,7 @@ void ocf_refcnt_unfreeze(struct ocf_refcnt *rc); bool ocf_refcnt_frozen(struct ocf_refcnt *rc); /* Register callback to be called when reference counter drops to 0. - * Must be called after counter is freezed. + * Must be called after counter is frozen. * Cannot be called until previously regsitered callback had fired. */ void ocf_refcnt_register_zero_cb(struct ocf_refcnt *rc, ocf_refcnt_cb_t cb, void *priv);