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 d36ca05 commit 6cc6a42
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
31 changes: 30 additions & 1 deletion include/Vutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2396,7 +2396,7 @@ class RegistryW : public RegistryX
};

/**
* CriticalSection
* ThreadLock
*/

class ThreadLock
Expand All @@ -2412,6 +2412,35 @@ class ThreadLock
CRITICAL_SECTION m_cs;
};

/**
* ThreadSignal
*/

class ThreadSignal
{
public:
ThreadSignal(const bool waiting = false);
ThreadSignal(const ThreadSignal& right);

virtual ~ThreadSignal();

operator bool() const;
bool get() const;
void set(bool waiting);
void notify();
bool wait(ulong time_out = INFINITE) const;

bool operator==(ThreadSignal& right);
bool operator!=(ThreadSignal& right);
bool operator==(bool waiting);
bool operator!=(bool waiting);
const ThreadSignal& operator=(bool waiting);
const ThreadSignal& operator=(const ThreadSignal& right);

private:
HANDLE m_event;
};

/**
* Stop Watch
*/
Expand Down
79 changes: 79 additions & 0 deletions src/details/crisec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,83 @@ void vuapi ThreadLock::unlock()
LeaveCriticalSection(&m_cs);
}

ThreadSignal::ThreadSignal(bool waiting)
{
m_event = CreateEventA(nullptr, TRUE, waiting, nullptr);
}

ThreadSignal::ThreadSignal(const ThreadSignal& right)
{
m_event = CreateEventA(nullptr, TRUE, right.get(), nullptr);
}

ThreadSignal::~ThreadSignal()
{
CloseHandle(m_event);
}

const ThreadSignal& ThreadSignal::operator=(const ThreadSignal& right)
{
this->set(right.get());
return *this;
}

const ThreadSignal& ThreadSignal::operator=(bool waiting)
{
this->set(waiting);
return *this;
}

ThreadSignal::operator bool() const
{
return this->get();
}

bool ThreadSignal::operator==(ThreadSignal& right)
{
return right.get() == this->get();
}

bool ThreadSignal::operator!=(ThreadSignal& right)
{
return !this->operator==(right);
}

bool ThreadSignal::operator==(bool waiting)
{
return this->get() == waiting;
}

bool ThreadSignal::operator!=(bool waiting)
{
return !this->operator==(waiting);
}

void ThreadSignal::set(bool state)
{
if (state)
{
SetEvent(m_event);
}
else
{
ResetEvent(m_event);
}
}

void ThreadSignal::notify()
{
this->set(true);
}

bool ThreadSignal::get() const
{
return this->wait(0);
}

bool ThreadSignal::wait(ulong time_out) const
{
return WaitForSingleObject(m_event, time_out) == WAIT_OBJECT_0;
}

} // namespace vu

0 comments on commit 6cc6a42

Please sign in to comment.