From bc644bbe4ed37f2b842a02f939c4486b89d4715d Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Sat, 1 Dec 2018 15:52:12 -0500 Subject: [PATCH] better threadsafeptr --- include/types.h | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/include/types.h b/include/types.h index d09f749..0f91bd6 100644 --- a/include/types.h +++ b/include/types.h @@ -3,26 +3,25 @@ #include "common.h" #include "const.h" -template +template typename P = std::unique_ptr> class ThreadSafePtr { public: - class Locker + template ThreadSafePtr(Args ...args) : ptr(new E(args...)), mtxPtr(new M) {} + auto operator->() { - public: - Locker(E* ptr, mtx* mtxPtr) : ptr(ptr), lock(*mtxPtr) {} - E* operator->() { return ptr; } - - private: - E* ptr; - std::unique_lock lock; - }; - template ThreadSafePtr(Args... args) : ptr(std::make_shared(args...)), mtxPtr(std::make_shared()) {} - Locker operator->() { return Locker(ptr.get(), mtxPtr.get()); } + struct + { + E* operator->() { return ptr; } + std::unique_lock lock; + E* ptr; + } lockedProxy{ std::unique_lock(*mtxPtr), ptr.get() }; + return lockedProxy; + } private: - std::shared_ptr ptr; - std::shared_ptr mtxPtr; + P ptr; + P mtxPtr; }; struct DefHandleCloser { void operator()(void* h) { CloseHandle(h); } }; @@ -35,11 +34,7 @@ public: operator bool() { return h.get() != NULL && h.get() != INVALID_HANDLE_VALUE; } private: - struct HandleCleaner - { - HandleCloser hc; - void operator()(HANDLE h) { if (h != INVALID_HANDLE_VALUE) hc(h); } - }; + struct HandleCleaner : HandleCloser { void operator()(void* h) { if (h != INVALID_HANDLE_VALUE) HandleCloser::operator()(h); } }; std::unique_ptr h; }; @@ -67,6 +62,8 @@ struct HookParam text_fun_t text_fun; filter_fun_t filter_fun; hook_fun_t hook_fun; + + char name[HOOK_NAME_SIZE]; }; struct ThreadParam @@ -93,10 +90,9 @@ private: struct InsertHookCmd // From host { - InsertHookCmd(HookParam hp, std::string name = "") : hp(hp) { strcpy_s(this->name, name.c_str()); }; + InsertHookCmd(HookParam hp) : hp(hp) {}; int command = HOST_COMMAND_NEW_HOOK; HookParam hp; - char name[HOOK_NAME_SIZE] = {}; }; struct ConsoleOutputNotif // From hook