Skip to content

Commit

Permalink
Vutils
Browse files Browse the repository at this point in the history
  • Loading branch information
vic4key committed Oct 11, 2023
1 parent 6cc6a42 commit 6356d92
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ It helps your programming go easier, faster and simpler.
* [File Mapping](<https://docs.microsoft.com/en-us/windows/win32/memory/file-mapping>)
* [IAT/INL API Hooking](<https://en.wikipedia.org/wiki/Hooking>)
* [Windows Message Hooking](<https://docs.microsoft.com/en-us/windows/win32/winmsg/hooks>)
* [Critical Section](<https://en.wikipedia.org/wiki/Critical_section>)
* [Thread Lock, Thread Signal](<https://en.wikipedia.org/wiki/Critical_section>)
* [Debouncer](<https://www.educative.io/edpresso/how-to-use-the-debounce-function-in-javascript>)
* [Singleton Template](<https://en.wikipedia.org/wiki/Singleton_pattern>)
* [Input Dialog](<https://www.google.com/search?q=input+dialog&source=lnms&tbm=isch>)
Expand Down
18 changes: 18 additions & 0 deletions include/Vutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
#endif // _WIN_SVC_

#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <mutex>
Expand Down Expand Up @@ -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<int, std::unique_ptr<ThreadLock>> m_list;
};

#define ScopedThreadLock() _GlobalThreadLock _scoped_thread_lock(__COUNTER__)

/**
* ThreadSignal
*/
Expand Down
35 changes: 35 additions & 0 deletions src/details/crisec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
namespace vu
{

/**
* ThreadLock
*/

ThreadLock::ThreadLock()
{
memset(&m_cs, 0, sizeof(m_cs));
Expand All @@ -30,6 +34,37 @@ void vuapi ThreadLock::unlock()
LeaveCriticalSection(&m_cs);
}

/**
* GlobalThreadLock
*/

std::map<int, std::unique_ptr<ThreadLock>> _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);
Expand Down

0 comments on commit 6356d92

Please sign in to comment.