source: trunk/Include/Classes/ActiveBasic/Windows/CriticalSection.ab@ 353

Last change on this file since 353 was 337, checked in by dai, 17 years ago

index.abを一つにまとめた。

File size: 1.4 KB
Line 
1'Classes/ActiveBasic/Windows/CriticalSection.ab
2
3Namespace ActiveBasic
4Namespace Windows
5
6Namespace Detail
7 Const InterlockedExchangeAnyPointer(p, x) = InterlockedExchangePointer(ByVal VarPtr(p) As *VoidPtr, x)
8End Namespace
9
10Class CriticalSection
11' Inherits System.IDisposable
12Public
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
31Private
32 cs As CRITICAL_SECTION
33 disposed As Long
34End Class
35
36Class CriticalSectionLock
37' Inherits System.IDisposable
38Public
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
59Private
60 pcs As *CRITICAL_SECTION
61End Class
62
63End Namespace 'Windows
64End Namespace 'ActiveBasic
Note: See TracBrowser for help on using the repository browser.