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

23
env/posix/ocf_env.h vendored
View File

@@ -35,6 +35,7 @@
#include "ocf_env_list.h"
#include "ocf_env_headers.h"
#include "ocf/ocf_err.h"
/* linux sector 512-bytes */
#define ENV_SECTOR_SHIFT 9
@@ -146,9 +147,7 @@ static inline int env_mutex_lock_interruptible(env_mutex *mutex)
static inline int env_mutex_trylock(env_mutex *mutex)
{
if (pthread_mutex_trylock(&mutex->m) == 0)
return 1;
return 0;
return pthread_mutex_trylock(&mutex->m) ? -OCF_ERR_NO_LOCK : 0;
}
static inline void env_mutex_unlock(env_mutex *mutex)
@@ -158,7 +157,7 @@ static inline void env_mutex_unlock(env_mutex *mutex)
static inline int env_mutex_is_locked(env_mutex *mutex)
{
if (env_mutex_trylock(mutex)) {
if (env_mutex_trylock(mutex) == 0) {
env_mutex_unlock(mutex);
return 1;
}
@@ -223,12 +222,7 @@ static inline void env_rwsem_down_read(env_rwsem *s)
static inline int env_rwsem_down_read_trylock(env_rwsem *s)
{
int result = pthread_rwlock_tryrdlock(&s->lock);
if (result == 0)
return 1;
else
return 0;
return pthread_rwlock_tryrdlock(&s->lock) ? -OCF_ERR_NO_LOCK : 0;
}
static inline void env_rwsem_up_write(env_rwsem *s)
@@ -243,17 +237,12 @@ static inline void env_rwsem_down_write(env_rwsem *s)
static inline int env_rwsem_down_write_trylock(env_rwsem *s)
{
int result = pthread_rwlock_trywrlock(&s->lock);
if (result == 0)
return 1;
else
return 0;
return pthread_rwlock_trywrlock(&s->lock) ? -OCF_ERR_NO_LOCK : 0;
}
static inline int env_rwsem_is_locked(env_rwsem *s)
{
if (env_rwsem_down_read_trylock(s)) {
if (env_rwsem_down_read_trylock(s) == 0) {
env_rwsem_up_read(s);
return 0;
}