better threadsafeptr

This commit is contained in:
Akash Mozumdar 2018-12-01 15:52:12 -05:00
parent 2f43249aa8
commit bc644bbe4e

View File

@ -3,26 +3,25 @@
#include "common.h" #include "common.h"
#include "const.h" #include "const.h"
template<typename E, typename mtx = std::recursive_mutex> template<typename E, typename M = std::mutex, template<typename...> typename P = std::unique_ptr>
class ThreadSafePtr class ThreadSafePtr
{ {
public: public:
class Locker template <typename ...Args> ThreadSafePtr(Args ...args) : ptr(new E(args...)), mtxPtr(new M) {}
auto operator->()
{
struct
{ {
public:
Locker(E* ptr, mtx* mtxPtr) : ptr(ptr), lock(*mtxPtr) {}
E* operator->() { return ptr; } E* operator->() { return ptr; }
std::unique_lock<M> lock;
private:
E* ptr; E* ptr;
std::unique_lock<mtx> lock; } lockedProxy{ std::unique_lock<M>(*mtxPtr), ptr.get() };
}; return lockedProxy;
template <typename... Args> ThreadSafePtr(Args... args) : ptr(std::make_shared<E>(args...)), mtxPtr(std::make_shared<mtx>()) {} }
Locker operator->() { return Locker(ptr.get(), mtxPtr.get()); }
private: private:
std::shared_ptr<E> ptr; P<E> ptr;
std::shared_ptr<mtx> mtxPtr; P<M> mtxPtr;
}; };
struct DefHandleCloser { void operator()(void* h) { CloseHandle(h); } }; struct DefHandleCloser { void operator()(void* h) { CloseHandle(h); } };
@ -35,11 +34,7 @@ public:
operator bool() { return h.get() != NULL && h.get() != INVALID_HANDLE_VALUE; } operator bool() { return h.get() != NULL && h.get() != INVALID_HANDLE_VALUE; }
private: private:
struct HandleCleaner struct HandleCleaner : HandleCloser { void operator()(void* h) { if (h != INVALID_HANDLE_VALUE) HandleCloser::operator()(h); } };
{
HandleCloser hc;
void operator()(HANDLE h) { if (h != INVALID_HANDLE_VALUE) hc(h); }
};
std::unique_ptr<void, HandleCleaner> h; std::unique_ptr<void, HandleCleaner> h;
}; };
@ -67,6 +62,8 @@ struct HookParam
text_fun_t text_fun; text_fun_t text_fun;
filter_fun_t filter_fun; filter_fun_t filter_fun;
hook_fun_t hook_fun; hook_fun_t hook_fun;
char name[HOOK_NAME_SIZE];
}; };
struct ThreadParam struct ThreadParam
@ -93,10 +90,9 @@ private:
struct InsertHookCmd // From host struct InsertHookCmd // From host
{ {
InsertHookCmd(HookParam hp, std::string name = "") : hp(hp) { strcpy_s<HOOK_NAME_SIZE>(this->name, name.c_str()); }; InsertHookCmd(HookParam hp) : hp(hp) {};
int command = HOST_COMMAND_NEW_HOOK; int command = HOST_COMMAND_NEW_HOOK;
HookParam hp; HookParam hp;
char name[HOOK_NAME_SIZE] = {};
}; };
struct ConsoleOutputNotif // From hook struct ConsoleOutputNotif // From hook