diff --git a/include/types.h b/include/types.h index 5e7a9e7..3d10055 100644 --- a/include/types.h +++ b/include/types.h @@ -3,6 +3,46 @@ #include "common.h" #include "const.h" +template +class ThreadSafePtr +{ +public: + class Locker + { + 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()); } + +private: + std::shared_ptr ptr; + std::shared_ptr mtxPtr; +}; + +struct DefHandleCloser { void operator()(void* h) { CloseHandle(h); } }; +template +class AutoHandle +{ +public: + AutoHandle(HANDLE h) : h(h) {} + operator HANDLE() { return h.get(); } + 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); } + }; + std::unique_ptr h; +}; + // jichi 3/7/2014: Add guessed comment struct HookParam { @@ -40,18 +80,15 @@ struct ThreadParam template <> struct std::hash { size_t operator()(const ThreadParam& tp) const { return std::hash()((tp.processId + tp.addr) ^ (tp.ctx + tp.ctx2)); } }; static bool operator==(const ThreadParam& one, const ThreadParam& two) { return one.processId == two.processId && one.addr == two.addr && one.ctx == two.ctx && one.ctx2 == two.ctx2; } -class WinMutex +class WinMutex // Like CMutex but works with lock_guard { public: - WinMutex(std::wstring name) : mutex(CreateMutexW(nullptr, false, name.c_str())) {} - WinMutex(WinMutex&) = delete; - WinMutex& operator=(WinMutex) = delete; - ~WinMutex() { ReleaseMutex(mutex); CloseHandle(mutex); } - void lock() { WaitForSingleObject(mutex, 0); } - void unlock() { ReleaseMutex(mutex); } + WinMutex(std::wstring name) : m(CreateMutexW(nullptr, FALSE, name.c_str())) {} + void lock() { if (m) WaitForSingleObject(m, 0); } + void unlock() { if (m) ReleaseMutex(m); } private: - HANDLE mutex; + AutoHandle<> m; }; struct InsertHookCmd // From host