Skip to content
Snippets Groups Projects
Commit 498ce9d5 authored by Nicolas Pope's avatar Nicolas Pope
Browse files

Add a transactional class

parent 96c2d3a1
No related branches found
No related tags found
1 merge request!316Resolves #343 GUI and Frame Refactor
...@@ -31,6 +31,8 @@ ...@@ -31,6 +31,8 @@
#define SHARED_LOCK(M,L) std::shared_lock<std::remove_reference<decltype(M)>::type> L(M); #define SHARED_LOCK(M,L) std::shared_lock<std::remove_reference<decltype(M)>::type> L(M);
#endif // DEBUG_MUTEX #endif // DEBUG_MUTEX
#define SHARED_LOCK_TYPE(M) std::shared_lock<M>
namespace ftl { namespace ftl {
extern ctpl::thread_pool pool; extern ctpl::thread_pool pool;
} }
......
#ifndef _FTL_TRANSACTIONAL_HPP_
#define _FTL_TRANSACTIONAL_HPP_
#include <ftl/threads.hpp>
namespace ftl {
/**
* Use RAII style transactional objects with shared locking. This wraps an
* object with a lock and provides a release notification mechanism to allow
* completion code.
*/
template <typename T>
class Transactional {
static_assert(std::is_pointer<T>::value, "Transactional type must be a pointer");
public:
Transactional(T obj, SHARED_MUTEX &mtx) : ref_(obj), mtx_(mtx), lock_(mtx_) {}
Transactional(T obj, SHARED_MUTEX &mtx, const std::function<void(T)> &complete) : ref_(obj), mtx_(mtx), lock_(mtx_), completed_(complete) {}
Transactional(const Transactional &)=delete;
Transactional()=delete;
~Transactional() {
lock_.unlock();
if (completed_) completed_(ref_);
}
Transactional &operator=(const Transactional &)=delete;
T operator->() { return ref_; }
const T operator->() const { return ref_; }
private:
T ref_;
SHARED_MUTEX &mtx_;
SHARED_LOCK_TYPE(SHARED_MUTEX) lock_;
std::function<void(T)> completed_;
};
}
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment