Add cache locking functions in trylock variant

- Add cache trylock and read trylock functions.
- Introduce new error code -OCF_ERR_NO_LOCK.
- Change trylock functions in env to return this code in case of
  lock contention.

[ENV CHANGES REQUIRED]
Following functions should return 0 on success or -OCF_ERR_NO_LOCK
in case of lock contention:
- env_mutex_trylock()
- env_rwsem_up_read_trylock()
- env_rwsem_up_write_trylock()

Signed-off-by: Robert Baldyga <robert.baldyga@intel.com>
This commit is contained in:
Robert Baldyga
2019-03-01 15:52:05 +01:00
parent 71d30e948d
commit 9b9b965b55
7 changed files with 78 additions and 42 deletions

View File

@@ -133,7 +133,7 @@ void ocf_cleaner_run(ocf_cleaner_t cleaner, ocf_queue_t queue)
}
/* Sleep in case there is management operation in progress. */
if (env_rwsem_down_write_trylock(&cache->lock) == 0) {
if (env_rwsem_down_write_trylock(&cache->lock)) {
cleaner->end(cleaner, SLEEP_TIME_MS);
return;
}

View File

@@ -48,7 +48,7 @@ static inline void ocf_metadata_unlock(struct ocf_cache *cache, int rw)
static inline int ocf_metadata_try_lock(struct ocf_cache *cache, int rw)
{
int result = -1;
int result = 0;
if (rw == OCF_METADATA_WR) {
result = env_rwsem_down_write_trylock(
@@ -60,7 +60,7 @@ static inline int ocf_metadata_try_lock(struct ocf_cache *cache, int rw)
ENV_BUG();
}
if (!result)
if (result)
return -1;
return 0;

View File

@@ -232,21 +232,27 @@ bool ocf_mngt_is_cache_locked(ocf_cache_t cache)
return false;
}
static void _ocf_mngt_cache_unlock(ocf_cache_t cache,
void (*unlock_fn)(env_rwsem *s))
{
unlock_fn(&cache->lock);
ocf_mngt_cache_put(cache);
}
void ocf_mngt_cache_unlock(ocf_cache_t cache)
{
OCF_CHECK_NULL(cache);
env_rwsem_up_write(&cache->lock);
ocf_mngt_cache_put(cache);
_ocf_mngt_cache_unlock(cache, env_rwsem_up_write);
}
void ocf_mngt_cache_read_unlock(ocf_cache_t cache)
{
OCF_CHECK_NULL(cache);
env_rwsem_up_read(&cache->lock);
ocf_mngt_cache_put(cache);
_ocf_mngt_cache_unlock(cache, env_rwsem_up_read);
}
int _ocf_mngt_cache_lock(ocf_cache_t cache, bool read)
static int _ocf_mngt_cache_lock(ocf_cache_t cache, int (*lock_fn)(env_rwsem *s),
void (*unlock_fn)(env_rwsem *s))
{
int ret;
@@ -254,10 +260,7 @@ int _ocf_mngt_cache_lock(ocf_cache_t cache, bool read)
env_atomic_inc(&cache->ref_count);
env_atomic_inc(&cache->lock_waiter);
if (read)
ret = env_rwsem_down_read_interruptible(&cache->lock);
else
ret = env_rwsem_down_write_interruptible(&cache->lock);
ret = lock_fn(&cache->lock);
env_atomic_dec(&cache->lock_waiter);
if (ret) {
@@ -283,10 +286,7 @@ int _ocf_mngt_cache_lock(ocf_cache_t cache, bool read)
return 0;
unlock:
if (read)
ocf_mngt_cache_read_unlock(cache);
else
ocf_mngt_cache_unlock(cache);
_ocf_mngt_cache_unlock(cache, unlock_fn);
return ret;
}
@@ -294,13 +294,29 @@ unlock:
int ocf_mngt_cache_lock(ocf_cache_t cache)
{
OCF_CHECK_NULL(cache);
return _ocf_mngt_cache_lock(cache, false);
return _ocf_mngt_cache_lock(cache, env_rwsem_down_write_interruptible,
env_rwsem_up_write);
}
int ocf_mngt_cache_read_lock(ocf_cache_t cache)
{
OCF_CHECK_NULL(cache);
return _ocf_mngt_cache_lock(cache, true);
return _ocf_mngt_cache_lock(cache, env_rwsem_down_read_interruptible,
env_rwsem_up_read);
}
int ocf_mngt_cache_trylock(ocf_cache_t cache)
{
OCF_CHECK_NULL(cache);
return _ocf_mngt_cache_lock(cache, env_rwsem_down_write_trylock,
env_rwsem_up_write);
}
int ocf_mngt_cache_read_trylock(ocf_cache_t cache)
{
OCF_CHECK_NULL(cache);
return _ocf_mngt_cache_lock(cache, env_rwsem_down_read_trylock,
env_rwsem_up_read);
}
/* if cache is either fully initialized or during recovery */