forked from erikkaashoek/NanoVNA-App
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCriticalSection.h
106 lines (94 loc) · 2.2 KB
/
CriticalSection.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
/*
* Copyright 2020 C Moss G6AMU
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
//#pragma once
#ifndef CriticalSectionH
#define CriticalSectionH
#ifdef _MSC_VER
#include <afxwin.h> // for CRITICAL_SECTION
#endif
class CCriticalSectionObj
{
private:
// CRITICAL_SECTION m_cs;
public:
CCriticalSectionObj()
{
::InitializeCriticalSection(&m_cs);
}
~CCriticalSectionObj()
{
::DeleteCriticalSection(&m_cs);
}
CRITICAL_SECTION m_cs;
// __property CRITICAL_SECTION cs = {read = m_cs};
};
class CCriticalSection
{
private:
volatile LONG m_count;
CCriticalSectionObj *m_cso;
public:
__fastcall CCriticalSection(CCriticalSectionObj &cso, bool enter = true)
{
m_count = 0;
m_cso = &cso;
if (m_cso && enter)
{
::EnterCriticalSection(&m_cso->m_cs);
::InterlockedIncrement(&m_count);
}
}
__fastcall ~CCriticalSection()
{
if (m_cso && m_count > 0)
{
::InterlockedDecrement(&m_count);
::LeaveCriticalSection(&m_cso->m_cs);
}
}
void __fastcall enter()
{
if (m_cso)
{
::EnterCriticalSection(&m_cso->m_cs);
::InterlockedIncrement(&m_count);
}
}
bool __fastcall tryEnter()
{
if (m_cso)
{
if (m_count > 0)
return false;
if (::TryEnterCriticalSection(&m_cso->m_cs) == FALSE)
return false;
::InterlockedIncrement(&m_count);
}
return true;
}
void __fastcall leave()
{
if (m_cso && m_count > 0)
{
::InterlockedDecrement(&m_count);
::LeaveCriticalSection(&m_cso->m_cs);
}
}
};
#endif