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

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

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

File size: 4.4 KB
Line 
1' Classes/System/Threading/WaitHandle.ab
2
3#ifndef __SYSTEM_THREADING_WAITHANDLE_AB__
4#define __SYSTEM_THREADING_WAITHANDLE_AB__
5
6#require <Classes/System/misc.ab>
7
8Namespace System
9Namespace Threading
10
11Namespace Detail
12 TypeDef PFNSignalObjectAndWait = *Function(hObjectToSignal As HANDLE, hObjectToWaitOn As HANDLE, dwMilliseconds As DWord, bAlertable As DWord) As DWord
13End Namespace
14
15Class WaitHandle
16 Implements System.IDisposable
17Public
18 ' Properties
19' Const Function SafeWaitHandle() As SafeWaitHandle
20' Sub SafeWaitHandle(h As SafeWaitHandle)
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
30 ' Methods
31 Virtual Sub WaitHandle()
32 End Sub
33
34 Virtual Sub ~WaitHandle()
35 This.Dispose()
36 End Sub
37
38 Virtual Sub Close()
39 This.Dispose()
40 End Sub
41
42 Virtual Sub Dispose()
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
50 Return WaitOne(INFINITE, FALSE)
51 End Function
52
53 Function WaitOne(millisecondsTimeout As Long, exitContext As Boolean) As Boolean
54 Return WaitHandle.AfterWait(WaitForSingleObject(h, millisecondsTimeout As DWord), 1)
55 End Function
56
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
62 Return WaitAll(count, handles, INFINITE, FALSE)
63 End Function
64
65 Static Function WaitAll(count As DWord, handles As *HANDLE, millisecondsTimeout As Long, exitContext As Boolean) As Boolean
66 Return WaitHandle.AfterWait(WaitForMultipleObjects(count, handles, TRUE, millisecondsTimeout), count)
67 End Function
68
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
72
73 Static Function WaitAny(count As DWord, handles As *HANDLE) As Boolean
74 Return WaitAny(count, handles, INFINITE, FALSE)
75 End Function
76
77 Static Function WaitAny(count As DWord, handles As *HANDLE, millisecondsTimeout As Long, exitContext As Boolean) As Boolean
78 Return WaitHandle.AfterWait(WaitForMultipleObjects(count, handles, FALSE, millisecondsTimeout), count)
79 End Function
80
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
85 Static Function SignalAndWait(toSignal As WaitHandle, toWaitOn As WaitHandle) As Boolean
86 Return SignalAndWait(toSignal, toWaitOn, INFINITE, FALSE)
87 End Function
88
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
93 Static Function SignalAndWait(toSignal As WaitHandle, toWaitOn As WaitHandle, millisecondsTimeout As Long, exitContext As Boolean) As Boolean
94 Dim pSignalObjectAndWait = GetProcAddress(GetModuleHandle("Kernel32.dll"), "SignalObjectAndWait") As Detail.PFNSignalObjectAndWait
95 If pSignalObjectAndWait = 0 Then
96 Throw New PlatformNotSupportedException("WaitHandle.SignalAndWait: This platform doesn't supoort this operation.")
97 End If
98 Return WaitHandle.AfterWait(pSignalObjectAndWait(toSignal.Handle, toWaitOn.Handle, millisecondsTimeout As DWord, FALSE), 1)
99 End Function
100
101Public
102 Function WaitTimeout() As Long
103 Return WAIT_TIMEOUT
104 End Function
105
106Protected
107 Static Const InvalidHandle = INVALID_HANDLE_VALUE As HANDLE
108
109Private
110 h As HANDLE
111
112 Static Function AfterWait(ret As DWord, n As DWord) As Boolean
113 Select Case ret
114 Case WAIT_TIMEOUT
115 Return False
116 Case WAIT_ABANDONED
117 ' Throw AbandonedMutexException
118 Debug
119 ExitThread(0)
120 'Case WAIT_FAILED
121 Case Else
122 If WAIT_OBJECT_0 <= ret And ret < WAIT_OBJECT_0 + n Then
123 Return True
124 End If
125 ' ObjectDisposedException?
126 Debug
127 ExitThread(0)
128 End Select
129 End Function
130End Class
131
132End Namespace 'Threading
133End Namespace 'System
134
135#endif '__SYSTEM_THREADING_WAITHANDLE_AB__
Note: See TracBrowser for help on using the repository browser.