[46] | 1 | ' Classes/System/Threading/WaitHandle.ab
|
---|
| 2 |
|
---|
| 3 | #ifndef __SYSTEM_THREADING_WAITHANDLE_AB__
|
---|
| 4 | #define __SYSTEM_THREADING_WAITHANDLE_AB__
|
---|
| 5 |
|
---|
[261] | 6 | #require <Classes/System/misc.ab>
|
---|
[258] | 7 |
|
---|
[261] | 8 | Namespace System
|
---|
| 9 | Namespace Threading
|
---|
| 10 |
|
---|
[258] | 11 | Namespace Detail
|
---|
| 12 | TypeDef PFNSignalObjectAndWait = *Function(hObjectToSignal As HANDLE, hObjectToWaitOn As HANDLE, dwMilliseconds As DWord, bAlertable As DWord) As DWord
|
---|
| 13 | End Namespace
|
---|
| 14 |
|
---|
[46] | 15 | Class WaitHandle
|
---|
[381] | 16 | Implements System.IDisposable
|
---|
[46] | 17 | Public
|
---|
[77] | 18 | ' Properties
|
---|
| 19 | ' Const Function SafeWaitHandle() As SafeWaitHandle
|
---|
| 20 | ' Sub SafeWaitHandle(h As SafeWaitHandle)
|
---|
[46] | 21 |
|
---|
| 22 | Const Virtual Function Handle() As HANDLE
|
---|
| 23 | Return h
|
---|
| 24 | End Function
|
---|
| 25 |
|
---|
| 26 | Virtual Sub Handle(newHandle As HANDLE)
|
---|
| 27 | h = newHandle
|
---|
| 28 | End Sub
|
---|
| 29 |
|
---|
[77] | 30 | ' Methods
|
---|
| 31 |
|
---|
| 32 | Virtual Sub ~WaitHandle()
|
---|
[261] | 33 | Dispose()
|
---|
[77] | 34 | End Sub
|
---|
| 35 |
|
---|
[46] | 36 | Virtual Sub Close()
|
---|
[261] | 37 | Dispose()
|
---|
[46] | 38 | End Sub
|
---|
| 39 |
|
---|
[261] | 40 | Override Sub Dispose()
|
---|
| 41 | Dim hDisposing = InterlockedExchangePointer(h, 0)
|
---|
| 42 | If hDisposing <> 0 Then
|
---|
| 43 | CloseHandle(hDisposing)
|
---|
| 44 | End If
|
---|
| 45 | End Sub
|
---|
| 46 |
|
---|
| 47 | Function WaitOne() As Boolean
|
---|
[47] | 48 | Return WaitOne(INFINITE, FALSE)
|
---|
| 49 | End Function
|
---|
[46] | 50 |
|
---|
[261] | 51 | Function WaitOne(millisecondsTimeout As Long, exitContext As BOOL) As Boolean
|
---|
[47] | 52 | Return WaitHandle.AfterWait(WaitForSingleObject(h, millisecondsTimeout As DWord), 1)
|
---|
| 53 | End Function
|
---|
[46] | 54 |
|
---|
[47] | 55 | ' Function WaitOne(timeout As TimeSpan, exitContext As BOOL) As BOOL
|
---|
[46] | 56 | /*
|
---|
[47] | 57 | Static Function WaitAll(count As DWord, handles As *HANDLE) As BOOL
|
---|
| 58 | Return WaitAll(count, handles, INFINITE, FALSE)
|
---|
| 59 | End Function
|
---|
[46] | 60 |
|
---|
[47] | 61 | Static Function WaitAll(count As DWord, handles As *HANDLE, millisecondsTimeout As Long, exitContext As BOOL) As BOOL
|
---|
| 62 | Return WaitHandle.AfterWait(WaitForMultipleObjects(count, handles, TRUE, millisecondsTimeout), count)
|
---|
| 63 | End Function
|
---|
[46] | 64 |
|
---|
[47] | 65 | ' Static Function WaitAll(timeout As TimeSpan, handles As *HANDLE, millisecondsTimeout As Long, exitContext As BOOL) As BOOL
|
---|
[46] | 66 |
|
---|
[47] | 67 | Static Function WaitAny(count As DWord, handles As *HANDLE) As BOOL
|
---|
| 68 | Return WaitAny(count, handles, INFINITE, FALSE)
|
---|
| 69 | End Function
|
---|
[46] | 70 |
|
---|
[47] | 71 | Static Function WaitAny(count As DWord, handles As *HANDLE, millisecondsTimeout As Long, exitContext As BOOL) As BOOL
|
---|
| 72 | Return WaitHandle.AfterWait(WaitForMultipleObjects(count, handles, FALSE, millisecondsTimeout), count)
|
---|
| 73 | End Function
|
---|
[46] | 74 |
|
---|
[47] | 75 | ' Static Function WaitAny(timeout As TimeSpan, handles As *HANDLE, millisecondsTimeout As Long, exitContext As BOOL) As BOOL
|
---|
[46] | 76 | */
|
---|
[261] | 77 | Static Function SignalAndWait(toSignal As WaitHandle, toWaitOn As WaitHandle) As Boolean
|
---|
[47] | 78 | Return SignalAndWait(toSignal, toWaitOn, INFINITE, FALSE)
|
---|
| 79 | End Function
|
---|
[46] | 80 |
|
---|
[258] | 81 | Public
|
---|
[261] | 82 | Static Function SignalAndWait(toSignal As WaitHandle, toWaitOn As WaitHandle, millisecondsTimeout As Long, exitContext As BOOL) As Boolean
|
---|
[258] | 83 | Dim pSignalObjectAndWait = GetProcAddress(GetModuleHandle("Kernel32.dll"), "SignalObjectAndWait") As Detail.PFNSignalObjectAndWait
|
---|
[46] | 84 | If pSignalObjectAndWait = 0 Then
|
---|
| 85 | ' PlatformNotSupportedException
|
---|
| 86 | Debug
|
---|
[237] | 87 | ExitThread(-1)
|
---|
[46] | 88 | End If
|
---|
[237] | 89 | Return WaitHandle.AfterWait(pSignalObjectAndWait(toSignal.Handle, toWaitOn.Handle, millisecondsTimeout As DWord, FALSE), 1)
|
---|
[47] | 90 | End Function
|
---|
[46] | 91 |
|
---|
[47] | 92 | ' Static Function SignalAndWait(ByRef toSignal As WaitHandle, ByRef toWaitOn As WaitHandle, timeout As TimeSpan, exitContext As BOOL) As BOOL
|
---|
[46] | 93 |
|
---|
| 94 | ' Protected
|
---|
| 95 | Sub WaitHandle()
|
---|
| 96 | End Sub
|
---|
| 97 |
|
---|
| 98 | Protected
|
---|
| 99 | Static Const InvalidHandle = INVALID_HANDLE_VALUE As HANDLE
|
---|
| 100 |
|
---|
| 101 | Private
|
---|
| 102 | h As HANDLE
|
---|
| 103 |
|
---|
[261] | 104 | Static Function AfterWait(ret As DWord, n As DWord) As Boolean
|
---|
[46] | 105 | Select Case ret
|
---|
| 106 | Case WAIT_TIMEOUT
|
---|
[261] | 107 | Return False
|
---|
[46] | 108 | Case WAIT_ABANDONED
|
---|
| 109 | ' Throw AbandonedMutexException
|
---|
| 110 | Debug
|
---|
| 111 | ExitThread(0)
|
---|
| 112 | 'Case WAIT_FAILED
|
---|
| 113 | Case Else
|
---|
[47] | 114 | If WAIT_OBJECT_0 <= ret And ret < WAIT_OBJECT_0 + n Then
|
---|
[261] | 115 | Return True
|
---|
[47] | 116 | End If
|
---|
[46] | 117 | ' ObjectDisposedException?
|
---|
| 118 | Debug
|
---|
| 119 | ExitThread(0)
|
---|
| 120 | End Select
|
---|
[58] | 121 | End Function
|
---|
[46] | 122 | End Class
|
---|
| 123 |
|
---|
[261] | 124 | End Namespace 'Threading
|
---|
| 125 | End Namespace 'System
|
---|
[258] | 126 |
|
---|
[46] | 127 | #endif '__SYSTEM_THREADING_WAITHANDLE_AB__
|
---|