diff options
author | Melanie Thielker | 2014-06-21 00:39:55 +0200 |
---|---|---|
committer | Melanie Thielker | 2014-06-21 00:39:55 +0200 |
commit | 159fcbf150b7da0e229b29aa7b94793484543d12 (patch) | |
tree | b8c0ff3b4c758a3fba8315b556c923ef4c02a185 /ThirdParty/SmartThreadPool/EventWaitHandle.cs | |
parent | Merge commit '68c8633ba18f0a11cfc0ed04d1d0c7c59e6cec76' (diff) | |
parent | Merge branch 'master' into careminster (diff) | |
download | opensim-SC-159fcbf150b7da0e229b29aa7b94793484543d12.zip opensim-SC-159fcbf150b7da0e229b29aa7b94793484543d12.tar.gz opensim-SC-159fcbf150b7da0e229b29aa7b94793484543d12.tar.bz2 opensim-SC-159fcbf150b7da0e229b29aa7b94793484543d12.tar.xz |
Merge branch 'master' of ssh://3dhosting.de/var/git/careminster
Conflicts:
OpenSim/Region/Framework/Scenes/SceneObjectGroup.cs
Diffstat (limited to 'ThirdParty/SmartThreadPool/EventWaitHandle.cs')
-rw-r--r-- | ThirdParty/SmartThreadPool/EventWaitHandle.cs | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/ThirdParty/SmartThreadPool/EventWaitHandle.cs b/ThirdParty/SmartThreadPool/EventWaitHandle.cs new file mode 100644 index 0000000..25be07a --- /dev/null +++ b/ThirdParty/SmartThreadPool/EventWaitHandle.cs | |||
@@ -0,0 +1,104 @@ | |||
1 | #if (_WINDOWS_CE) | ||
2 | |||
3 | using System; | ||
4 | using System.Runtime.InteropServices; | ||
5 | using System.Threading; | ||
6 | |||
7 | namespace Amib.Threading.Internal | ||
8 | { | ||
9 | /// <summary> | ||
10 | /// EventWaitHandle class | ||
11 | /// In WindowsCE this class doesn't exist and I needed the WaitAll and WaitAny implementation. | ||
12 | /// So I wrote this class to implement these two methods with some of their overloads. | ||
13 | /// It uses the WaitForMultipleObjects API to do the WaitAll and WaitAny. | ||
14 | /// Note that this class doesn't even inherit from WaitHandle! | ||
15 | /// </summary> | ||
16 | public class STPEventWaitHandle | ||
17 | { | ||
18 | #region Public Constants | ||
19 | |||
20 | public const int WaitTimeout = Timeout.Infinite; | ||
21 | |||
22 | #endregion | ||
23 | |||
24 | #region Private External Constants | ||
25 | |||
26 | private const Int32 WAIT_FAILED = -1; | ||
27 | private const Int32 WAIT_TIMEOUT = 0x102; | ||
28 | private const UInt32 INFINITE = 0xFFFFFFFF; | ||
29 | |||
30 | #endregion | ||
31 | |||
32 | #region WaitAll and WaitAny | ||
33 | |||
34 | internal static bool WaitOne(WaitHandle waitHandle, int millisecondsTimeout, bool exitContext) | ||
35 | { | ||
36 | return waitHandle.WaitOne(millisecondsTimeout, exitContext); | ||
37 | } | ||
38 | |||
39 | private static IntPtr[] PrepareNativeHandles(WaitHandle[] waitHandles) | ||
40 | { | ||
41 | IntPtr[] nativeHandles = new IntPtr[waitHandles.Length]; | ||
42 | for (int i = 0; i < waitHandles.Length; i++) | ||
43 | { | ||
44 | nativeHandles[i] = waitHandles[i].Handle; | ||
45 | } | ||
46 | return nativeHandles; | ||
47 | } | ||
48 | |||
49 | public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext) | ||
50 | { | ||
51 | uint timeout = millisecondsTimeout < 0 ? INFINITE : (uint)millisecondsTimeout; | ||
52 | |||
53 | IntPtr[] nativeHandles = PrepareNativeHandles(waitHandles); | ||
54 | |||
55 | int result = WaitForMultipleObjects((uint)waitHandles.Length, nativeHandles, true, timeout); | ||
56 | |||
57 | if (result == WAIT_TIMEOUT || result == WAIT_FAILED) | ||
58 | { | ||
59 | return false; | ||
60 | } | ||
61 | |||
62 | return true; | ||
63 | } | ||
64 | |||
65 | |||
66 | public static int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext) | ||
67 | { | ||
68 | uint timeout = millisecondsTimeout < 0 ? INFINITE : (uint)millisecondsTimeout; | ||
69 | |||
70 | IntPtr[] nativeHandles = PrepareNativeHandles(waitHandles); | ||
71 | |||
72 | int result = WaitForMultipleObjects((uint)waitHandles.Length, nativeHandles, false, timeout); | ||
73 | |||
74 | if (result >= 0 && result < waitHandles.Length) | ||
75 | { | ||
76 | return result; | ||
77 | } | ||
78 | |||
79 | return -1; | ||
80 | } | ||
81 | |||
82 | public static int WaitAny(WaitHandle[] waitHandles) | ||
83 | { | ||
84 | return WaitAny(waitHandles, Timeout.Infinite, false); | ||
85 | } | ||
86 | |||
87 | public static int WaitAny(WaitHandle[] waitHandles, TimeSpan timeout, bool exitContext) | ||
88 | { | ||
89 | int millisecondsTimeout = (int)timeout.TotalMilliseconds; | ||
90 | |||
91 | return WaitAny(waitHandles, millisecondsTimeout, false); | ||
92 | } | ||
93 | |||
94 | #endregion | ||
95 | |||
96 | #region External methods | ||
97 | |||
98 | [DllImport("coredll.dll", SetLastError = true)] | ||
99 | public static extern int WaitForMultipleObjects(uint nCount, IntPtr[] lpHandles, bool fWaitAll, uint dwMilliseconds); | ||
100 | |||
101 | #endregion | ||
102 | } | ||
103 | } | ||
104 | #endif \ No newline at end of file | ||