aboutsummaryrefslogtreecommitdiff
path: root/CRT/DriverThread.hpp
blob: d00db1b9ab81ae4bcee9c3994750cd3e0b500676 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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