source: trunk/Include/Classes/System/Threading/WaitHandle.ab@ 412

Last change on this file since 412 was 408, checked in by NoWest, 17 years ago

あまりに久しぶりの参加で調子でないです(笑
一先ず肩慣らしにWaitHandleの実装を進めておきました。

File size: 4.4 KB
RevLine 
[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]8Namespace System
9Namespace Threading
10
[258]11Namespace Detail
12 TypeDef PFNSignalObjectAndWait = *Function(hObjectToSignal As HANDLE, hObjectToWaitOn As HANDLE, dwMilliseconds As DWord, bAlertable As DWord) As DWord
13End Namespace
14
[46]15Class WaitHandle
[381]16 Implements System.IDisposable
[46]17Public
[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
[408]31 Virtual Sub WaitHandle()
32 End Sub
[77]33
34 Virtual Sub ~WaitHandle()
[408]35 This.Dispose()
[77]36 End Sub
37
[46]38 Virtual Sub Close()
[408]39 This.Dispose()
[46]40 End Sub
41
[388]42 Virtual Sub Dispose()
[261]43 Dim hDisposing = InterlockedExchangePointer(h, 0)
44 If hDisposing <> 0 Then
45 CloseHandle(hDisposing)
46 End If
47 End Sub
48
49 Function WaitOne() As Boolean
[47]50 Return WaitOne(INFINITE, FALSE)
51 End Function
[46]52
[388]53 Function WaitOne(millisecondsTimeout As Long, exitContext As Boolean) As Boolean
[47]54 Return WaitHandle.AfterWait(WaitForSingleObject(h, millisecondsTimeout As DWord), 1)
55 End Function
[46]56
[408]57 Function WaitOne(timeout As System.TimeSpan, exitContext As Boolean) As Boolean
58 Return WaitHandle.AfterWait(WaitForSingleObject(h, timeout.TotalMilliseconds() As DWord), 1)
59 End Function
60
61 Static Function WaitAll(count As DWord, handles As *HANDLE) As Boolean
[47]62 Return WaitAll(count, handles, INFINITE, FALSE)
63 End Function
[46]64
[408]65 Static Function WaitAll(count As DWord, handles As *HANDLE, millisecondsTimeout As Long, exitContext As Boolean) As Boolean
[47]66 Return WaitHandle.AfterWait(WaitForMultipleObjects(count, handles, TRUE, millisecondsTimeout), count)
67 End Function
[46]68
[408]69 Static Function WaitAll(count As DWord, handles As *HANDLE, timeout As System.TimeSpan, exitContext As Boolean) As Boolean
70 Return WaitHandle.AfterWait(WaitForMultipleObjects(count, handles, TRUE, timeout.TotalMilliseconds() As DWord), count)
71 End Function
[46]72
[408]73 Static Function WaitAny(count As DWord, handles As *HANDLE) As Boolean
[47]74 Return WaitAny(count, handles, INFINITE, FALSE)
75 End Function
[46]76
[408]77 Static Function WaitAny(count As DWord, handles As *HANDLE, millisecondsTimeout As Long, exitContext As Boolean) As Boolean
[47]78 Return WaitHandle.AfterWait(WaitForMultipleObjects(count, handles, FALSE, millisecondsTimeout), count)
79 End Function
[46]80
[408]81 Static Function WaitAny(count As DWord, handles As *HANDLE, timeout As System.TimeSpan, exitContext As Boolean) As Boolean
82 Return WaitHandle.AfterWait(WaitForMultipleObjects(count, handles, FALSE, timeout.TotalMilliseconds() As DWord), count)
83 End Function
84
[261]85 Static Function SignalAndWait(toSignal As WaitHandle, toWaitOn As WaitHandle) As Boolean
[47]86 Return SignalAndWait(toSignal, toWaitOn, INFINITE, FALSE)
87 End Function
[46]88
[408]89 Static Function SignalAndWait (toSignal As WaitHandle, toWaitOn As WaitHandle, timeout As System.TimeSpan, exitContext As Boolean) As Boolean
90 Return SignalAndWait(toSignal, toWaitOn, timeout.TotalMilliseconds() As Long, FALSE)
91 End Function
92
[388]93 Static Function SignalAndWait(toSignal As WaitHandle, toWaitOn As WaitHandle, millisecondsTimeout As Long, exitContext As Boolean) As Boolean
[258]94 Dim pSignalObjectAndWait = GetProcAddress(GetModuleHandle("Kernel32.dll"), "SignalObjectAndWait") As Detail.PFNSignalObjectAndWait
[46]95 If pSignalObjectAndWait = 0 Then
[388]96 Throw New PlatformNotSupportedException("WaitHandle.SignalAndWait: This platform doesn't supoort this operation.")
[46]97 End If
[237]98 Return WaitHandle.AfterWait(pSignalObjectAndWait(toSignal.Handle, toWaitOn.Handle, millisecondsTimeout As DWord, FALSE), 1)
[47]99 End Function
[46]100
[408]101Public
102 Function WaitTimeout() As Long
103 Return WAIT_TIMEOUT
104 End Function
[46]105
106Protected
107 Static Const InvalidHandle = INVALID_HANDLE_VALUE As HANDLE
108
109Private
110 h As HANDLE
111
[261]112 Static Function AfterWait(ret As DWord, n As DWord) As Boolean
[46]113 Select Case ret
114 Case WAIT_TIMEOUT
[261]115 Return False
[46]116 Case WAIT_ABANDONED
117 ' Throw AbandonedMutexException
118 Debug
119 ExitThread(0)
120 'Case WAIT_FAILED
121 Case Else
[47]122 If WAIT_OBJECT_0 <= ret And ret < WAIT_OBJECT_0 + n Then
[261]123 Return True
[47]124 End If
[46]125 ' ObjectDisposedException?
126 Debug
127 ExitThread(0)
128 End Select
[58]129 End Function
[46]130End Class
131
[261]132End Namespace 'Threading
133End Namespace 'System
[258]134
[46]135#endif '__SYSTEM_THREADING_WAITHANDLE_AB__
Note: See TracBrowser for help on using the repository browser.