diff options
Diffstat (limited to 'CRT/DriverThread.hpp')
-rw-r--r-- | CRT/DriverThread.hpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/CRT/DriverThread.hpp b/CRT/DriverThread.hpp new file mode 100644 index 0000000..d00db1b --- /dev/null +++ b/CRT/DriverThread.hpp @@ -0,0 +1,83 @@ +#ifndef DDK_THREAD +#define DDK_THREAD 1 + +#include <ntddk.h> + +extern "C" void InterceptorThreadRoutine(PVOID threadContext); + +typedef NTSTATUS (*threadRoutine_t)(PVOID); + +namespace DriverThread +{ + +class Mutex +{ +public: + Mutex(void); + ~Mutex(void); + +private: + void Lock(); + void Unlock(); + + volatile long int m_interlock; + + friend class LockGuard; +}; + +class LockGuard +{ +public: + LockGuard(Mutex & m); + ~LockGuard(void); + +private: + Mutex m_Lock; +}; + +class Thread +{ +public: + Thread(void); + ~Thread(void); + NTSTATUS Start(threadRoutine_t routine, PVOID threadContext); + NTSTATUS WaitForTermination(LONGLONG timeout = 0); + HANDLE GetThreadId(void); + +private: + friend void ::InterceptorThreadRoutine(PVOID threadContext); + + HANDLE m_threadId = nullptr; + PETHREAD m_threadObject = nullptr; + Mutex m_mutex; + threadRoutine_t m_routine; + PVOID m_threadContext; +}; + +class Spinlock +{ +public: + Spinlock(void); + NTSTATUS Acquire(void); + void Release(void); + KIRQL GetOldIrql(void); + +private: + KIRQL m_oldIrql; + KSPIN_LOCK m_spinLock; +}; + +class Semaphore +{ +public: + Semaphore(LONG initialValue = 0, LONG maxValue = MAXLONG); + NTSTATUS Wait(LONGLONG timeout = 0); + LONG Release(LONG adjustment = 1); + +private: + KSEMAPHORE m_semaphore; +}; + +}; // namespace DriverThread + +#endif |