diff --git a/README.md b/README.md index 8fb3c4d..1bec2cf 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ It helps your programming go easier, faster and simpler. * [File Mapping]() * [IAT/INL API Hooking]() * [Windows Message Hooking]() -* [Critical Section]() +* [Thread Lock, Thread Signal]() * [Debouncer]() * [Singleton Template]() * [Input Dialog]() diff --git a/include/Vutils.h b/include/Vutils.h index f82514c..e3eac34 100644 --- a/include/Vutils.h +++ b/include/Vutils.h @@ -90,6 +90,7 @@ #endif // _WIN_SVC_ #include +#include #include #include #include @@ -2412,6 +2413,23 @@ class ThreadLock CRITICAL_SECTION m_cs; }; +/** + * GlobalThreadLock + */ + +class _GlobalThreadLock +{ +public: + _GlobalThreadLock(int thread_lock_id); + virtual ~_GlobalThreadLock(); + +private: + int m_thread_lock_id; + static std::map> m_list; +}; + +#define ScopedThreadLock() _GlobalThreadLock _scoped_thread_lock(__COUNTER__) + /** * ThreadSignal */ diff --git a/src/details/crisec.cpp b/src/details/crisec.cpp index 3e23fc8..d9b409c 100644 --- a/src/details/crisec.cpp +++ b/src/details/crisec.cpp @@ -9,6 +9,10 @@ namespace vu { +/** + * ThreadLock + */ + ThreadLock::ThreadLock() { memset(&m_cs, 0, sizeof(m_cs)); @@ -30,6 +34,37 @@ void vuapi ThreadLock::unlock() LeaveCriticalSection(&m_cs); } +/** + * GlobalThreadLock + */ + +std::map> _GlobalThreadLock::m_list; + +_GlobalThreadLock::_GlobalThreadLock(int thread_lock_id) : m_thread_lock_id(thread_lock_id) +{ + auto it = m_list.find(thread_lock_id); + if (it == m_list.cend()) + { + m_thread_lock_id = thread_lock_id; + m_list[m_thread_lock_id].reset(new ThreadLock()); + } + + m_list[m_thread_lock_id]->lock(); +} + +_GlobalThreadLock::~_GlobalThreadLock() +{ + if (m_thread_lock_id != -1) + { + m_list[m_thread_lock_id]->unlock(); + //m_list.erase(m_thread_lock_id); + } +} + +/** + * ThreadSignal + */ + ThreadSignal::ThreadSignal(bool waiting) { m_event = CreateEventA(nullptr, TRUE, waiting, nullptr);