Line | |
---|
1 | 'Classes/ActiveBasic/Windows/CriticalSection.ab
|
---|
2 |
|
---|
3 | Namespace ActiveBasic
|
---|
4 | Namespace Windows
|
---|
5 |
|
---|
6 | Namespace Detail
|
---|
7 | Const InterlockedExchangeAnyPointer(p, x) = InterlockedExchangePointer(ByVal VarPtr(p) As *VoidPtr, x)
|
---|
8 | End Namespace
|
---|
9 |
|
---|
10 | Class CriticalSection
|
---|
11 | ' Inherits System.IDisposable
|
---|
12 | Public
|
---|
13 | Sub CriticalSection()
|
---|
14 | disposed = 0
|
---|
15 | InitializeCriticalSection(cs)
|
---|
16 | End Sub
|
---|
17 |
|
---|
18 | Sub ~CriticalSection()
|
---|
19 | Dispose()
|
---|
20 | End Sub
|
---|
21 |
|
---|
22 | /*Override*/ Sub Dispose()
|
---|
23 | If InterlockedIncrement(disposed) = 0 Then
|
---|
24 | DeleteCriticalSection(cs)
|
---|
25 | End If
|
---|
26 | End Sub
|
---|
27 |
|
---|
28 | Function Enter() As ActiveBasic.Windows.CriticalSectionLock
|
---|
29 | Return New CriticalSectionLock(cs)
|
---|
30 | End Function
|
---|
31 | Private
|
---|
32 | cs As CRITICAL_SECTION
|
---|
33 | disposed As Long
|
---|
34 | End Class
|
---|
35 |
|
---|
36 | Class CriticalSectionLock
|
---|
37 | ' Inherits System.IDisposable
|
---|
38 | Public
|
---|
39 | Sub CriticalSectionLock(ByRef cs As CRITICAL_SECTION)
|
---|
40 | pcs = VarPtr(cs)
|
---|
41 | EnterCriticalSection(cs)
|
---|
42 | End Sub
|
---|
43 |
|
---|
44 | Sub ~CriticalSectionLock()
|
---|
45 | Dispose()
|
---|
46 | End Sub
|
---|
47 |
|
---|
48 | Sub Leave()
|
---|
49 | Dispose()
|
---|
50 | End Sub
|
---|
51 |
|
---|
52 | /*Override*/ Sub Dispose()
|
---|
53 | ' Dim pcsOld = ActiveBasic.Windows.Detail.InterlockedExchangeAnyPointer(pcs, 0)
|
---|
54 | Dim pcsOld = InterlockedExchangePointer(pcs As VoidPtr, 0) As *CRITICAL_SECTION
|
---|
55 | If pcsOld <> 0 Then
|
---|
56 | LeaveCriticalSection(ByVal pcsOld)
|
---|
57 | End If
|
---|
58 | End Sub
|
---|
59 | Private
|
---|
60 | pcs As *CRITICAL_SECTION
|
---|
61 | End Class
|
---|
62 |
|
---|
63 | End Namespace 'Windows
|
---|
64 | End Namespace 'ActiveBasic
|
---|
Note:
See
TracBrowser
for help on using the repository browser.