Commit 86cc2386 authored by Matthieu Dorier's avatar Matthieu Dorier
Browse files

added useful classes to help handling mutex and rwlocks

parent ba5600ed
/*
* Copyright (c) 2017 UChicago Argonne, LLC
*
* See COPYRIGHT in top-level directory.
*/
#ifndef ABT_UTILS_MUTEX_H
#define ABT_UTILS_MUTEX_H
#include <abt.h>
namespace sdsu
{
namespace abt {
class mutex {
ABT_mutex mutex_m;
public:
explicit mutex(bool recursive = false) {
ABT_mutex_attr attr;
ABT_mutex_attr_create(&attr);
if(recursive)
ABT_mutex_attr_set_recursive(attr, ABT_TRUE);
ABT_mutex_create_with_attr(attr, &mutex_m);
ABT_mutex_attr_free(&attr);
}
mutex(const mutex& other) = delete;
mutex(mutex&& other) {
mutex_m = other.mutex_m;
other.mutex_m = ABT_MUTEX_NULL;
}
~mutex() noexcept {
if(mutex_m != ABT_MUTEX_NULL)
ABT_mutex_free(&mutex_m);
}
int lock() noexcept {
return ABT_mutex_lock(mutex_m);
}
int lock_low() noexcept {
return ABT_mutex_lock_low(mutex_m);
}
int spinlock() noexcept {
return ABT_mutex_spinlock(mutex_m);
}
int trylock() noexcept {
return ABT_mutex_trylock(mutex_m);
}
int unlock() noexcept {
return ABT_mutex_unlock(mutex_m);
}
int unlock_se() noexcept {
return ABT_mutex_unlock_se(mutex_m);
}
};
} // namespace abt
} // namespace sdsu
#endif /* end of include guard */
/*
* Copyright (c) 2017 UChicago Argonne, LLC
*
* See COPYRIGHT in top-level directory.
*/
#ifndef ABT_UTILS_RWLOCK_H
#define ABT_UTILS_RWLOCK_H
#include <abt.h>
namespace sdsu
{
namespace abt {
class rwlock {
ABT_rwlock lock_m;
public:
explicit rwlock() {
ABT_rwlock_create(&lock_m);
}
rwlock(const rwlock&) = delete;
rwlock(rwlock&& other) {
lock_m = other.lock_m;
other.lock_m = ABT_RWLOCK_NULL;
}
~rwlock() noexcept {
if(lock_m != ABT_RWLOCK_NULL)
ABT_rwlock_free(&lock_m);
}
int rdlock() noexcept {
return ABT_rwlock_rdlock(lock_m);
}
int wrlock() noexcept {
return ABT_rwlock_wrlock(lock_m);
}
int unlock() noexcept {
return ABT_rwlock_unlock(lock_m);
}
};
} // namespace abt
} // namespace sdsu
#endif /* end of include guard */
/*
* Copyright (c) 2017 UChicago Argonne, LLC
*
* See COPYRIGHT in top-level directory.
*/
#ifndef ABT_UTILS_SCOPED_LOCK_H
#define ABT_UTILS_SCOPED_LOCK_H
namespace sdsu
{
namespace abt {
template<typename Lockable,
int(Lockable::*lockfunc)() = &Lockable::lock,
int(Lockable::*unlockfunc)() = &Lockable::unlock>
class scoped_lock {
Lockable& lockable_m;
public:
explicit scoped_lock(Lockable& l)
: lockable_m(l) {
(lockable_m.*lockfunc)();
}
scoped_lock(const scoped_lock&) = delete;
scoped_lock(scoped_lock&&) = delete;
~scoped_lock() {
(lockable_m.*unlockfunc)();
}
};
} // namespace abt
} // namespace sdsu
#endif /* end of include guard */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment