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