-
Notifications
You must be signed in to change notification settings - Fork 75
/
threadclasses.h
109 lines (88 loc) · 2.57 KB
/
threadclasses.h
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// -------------------------------
// Copyright (c) Corman Technologies Inc.
// See LICENSE.txt for license information.
// -------------------------------
//
// File: ThreadClasses.h
// Contents: Thread synchronization classes for Corman Lisp.
// History: 8/5/97 RGC Created.
//
#ifndef THREADCLASSES_H
#define THREADCLASSES_H
#include <windows.h>
#include <assert.h>
class PLSingleLock;
class PLSyncObject
{
public:
PLSyncObject(LPCTSTR pstrName);
virtual ~PLSyncObject();
operator HANDLE() const;
HANDLE m_hObject;
virtual BOOL Lock(DWORD dwTimeout = INFINITE);
virtual BOOL Unlock() = 0;
virtual BOOL Unlock(LONG, LPLONG) { return TRUE; }
friend class PLSingleLock;
};
class PLEvent : public PLSyncObject
{
public:
PLEvent(BOOL bInitiallyOwn = FALSE, BOOL bManualReset = FALSE,
LPCTSTR lpszNAme = NULL, LPSECURITY_ATTRIBUTES lpsaAttribute = NULL);
virtual ~PLEvent();
BOOL SetEvent();
BOOL PulseEvent();
BOOL ResetEvent();
BOOL Unlock();
};
class PLSemaphore : public PLSyncObject
{
public:
PLSemaphore(LONG lInitialCount = 1, LONG lMaxCount = 1,
LPCTSTR pstrName=NULL, LPSECURITY_ATTRIBUTES lpsaAttributes = NULL);
virtual ~PLSemaphore();
virtual BOOL Unlock();
virtual BOOL Unlock(LONG lCount, LPLONG lprevCount = NULL);
};
class PLSingleLock
{
public:
PLSingleLock(PLSyncObject* pObject, BOOL bInitialLock = FALSE);
~PLSingleLock();
BOOL Lock(DWORD dwTimeOut = INFINITE);
BOOL Unlock();
BOOL Unlock(LONG lCount, LPLONG lPrevCount = NULL);
BOOL IsLocked();
protected:
PLSyncObject* m_pObject;
HANDLE m_hObject;
BOOL m_bAcquired;
};
class CriticalSection
{
public:
CriticalSection() { InitializeCriticalSection(&m_sect); }
~CriticalSection(){ DeleteCriticalSection(&m_sect); }
void Enter() { EnterCriticalSection(&m_sect); }
void Leave() { LeaveCriticalSection(&m_sect); }
public:
CRITICAL_SECTION m_sect;
};
class ScopedLock
{
public:
ScopedLock(CriticalSection &cs) : m_cs(cs) {m_cs.Enter();}
~ScopedLock() { m_cs.Leave(); }
public:
CriticalSection &m_cs;
};
inline PLSyncObject::operator HANDLE() const
{ return m_hObject;}
inline BOOL PLSemaphore::Unlock()
{ return Unlock(1, NULL); }
inline BOOL PLEvent::SetEvent() { return ::SetEvent(m_hObject); }
inline BOOL PLEvent::PulseEvent() { return ::PulseEvent(m_hObject); }
inline BOOL PLEvent::ResetEvent() { return ::ResetEvent(m_hObject); }
inline PLSingleLock::~PLSingleLock() { Unlock(); }
inline BOOL PLSingleLock::IsLocked() { return m_bAcquired; }
#endif // THREADCLASSES_H