Return post modification value from ocf_refcnt_inc/dec

If counter is frozen then increment returns 0.

Signed-off-by: Adam Rutkowski <adam.j.rutkowski@intel.com>
This commit is contained in:
Adam Rutkowski 2019-04-09 16:29:24 -04:00
parent 962f9d17d1
commit 0e15c693fd
3 changed files with 17 additions and 12 deletions

View File

@ -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, static inline int ocf_io_set_dirty(ocf_cache_t cache,
struct ocf_core_io *core_io) 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; return core_io->dirty ? 0 : -EBUSY;
} }

View File

@ -13,26 +13,30 @@ void ocf_refcnt_init(struct ocf_refcnt *rc)
rc->cb = NULL; 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); int val = env_atomic_dec_return(&rc->counter);
ENV_BUG_ON(val < 0); ENV_BUG_ON(val < 0);
if (!val && env_atomic_cmpxchg(&rc->callback, 1, 0)) if (!val && env_atomic_cmpxchg(&rc->callback, 1, 0))
rc->cb(rc->priv); 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)) { if (!env_atomic_read(&rc->freeze)) {
env_atomic_inc(&rc->counter); val = env_atomic_inc_return(&rc->counter);
if (!env_atomic_read(&rc->freeze)) if (!env_atomic_read(&rc->freeze))
return true; return val;
else else
ocf_refcnt_dec(rc); ocf_refcnt_dec(rc);
} }
return false; return 0;
} }

View File

@ -22,15 +22,16 @@ struct ocf_refcnt
/* Initialize reference counter */ /* Initialize reference counter */
void ocf_refcnt_init(struct ocf_refcnt *rc); void ocf_refcnt_init(struct ocf_refcnt *rc);
/* Try to increment counter. Returns true if successfull, false if freezed */ /* Try to increment counter. Returns counter value (> 0) if successfull, 0
bool ocf_refcnt_inc(struct ocf_refcnt *rc); * if counter is frozen */
int ocf_refcnt_inc(struct ocf_refcnt *rc);
/* Decrement reference counter */ /* Decrement reference counter and return post-decrement value */
void ocf_refcnt_dec(struct ocf_refcnt *rc); int ocf_refcnt_dec(struct ocf_refcnt *rc);
/* Disallow incrementing of underlying counter - attempts to increment counter /* Disallow incrementing of underlying counter - attempts to increment counter
* will be failing until ocf_refcnt_unfreeze is calleed. * 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.*/ * until all freeze calls are offset by a corresponding unfreeze.*/
void ocf_refcnt_freeze(struct ocf_refcnt *rc); 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); bool ocf_refcnt_frozen(struct ocf_refcnt *rc);
/* Register callback to be called when reference counter drops to 0. /* 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. */ * 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 ocf_refcnt_register_zero_cb(struct ocf_refcnt *rc, ocf_refcnt_cb_t cb,
void *priv); void *priv);