diff options
author | Teravus Ovares (Dan Olivares) | 2009-12-12 01:24:25 -0500 |
---|---|---|
committer | Teravus Ovares (Dan Olivares) | 2009-12-12 01:24:25 -0500 |
commit | 35961caec6de48ec79e9af79fa1a2edf176b4389 (patch) | |
tree | 17872ae62dc84525420ff8a248f3eaf8aac6dbba /OpenSim/Framework/Parallel.cs | |
parent | * Commit some sit code that's commented out for now. (diff) | |
parent | Getting rid of SimpleRegionInfo and SerializableRegionInfo per Mel (diff) | |
download | opensim-SC_OLD-35961caec6de48ec79e9af79fa1a2edf176b4389.zip opensim-SC_OLD-35961caec6de48ec79e9af79fa1a2edf176b4389.tar.gz opensim-SC_OLD-35961caec6de48ec79e9af79fa1a2edf176b4389.tar.bz2 opensim-SC_OLD-35961caec6de48ec79e9af79fa1a2edf176b4389.tar.xz |
Merge branch 'master' of ssh://MyConnection/var/git/opensim
Diffstat (limited to 'OpenSim/Framework/Parallel.cs')
-rw-r--r-- | OpenSim/Framework/Parallel.cs | 211 |
1 files changed, 0 insertions, 211 deletions
diff --git a/OpenSim/Framework/Parallel.cs b/OpenSim/Framework/Parallel.cs deleted file mode 100644 index a0394f2..0000000 --- a/OpenSim/Framework/Parallel.cs +++ /dev/null | |||
@@ -1,211 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Threading; | ||
31 | |||
32 | namespace OpenSim.Framework | ||
33 | { | ||
34 | /// <summary> | ||
35 | /// Provides helper methods for parallelizing loops | ||
36 | /// </summary> | ||
37 | public static class Parallel | ||
38 | { | ||
39 | public static readonly int ProcessorCount = System.Environment.ProcessorCount; | ||
40 | |||
41 | /// <summary> | ||
42 | /// Executes a for loop in which iterations may run in parallel | ||
43 | /// </summary> | ||
44 | /// <param name="fromInclusive">The loop will be started at this index</param> | ||
45 | /// <param name="toExclusive">The loop will be terminated before this index is reached</param> | ||
46 | /// <param name="body">Method body to run for each iteration of the loop</param> | ||
47 | public static void For(int fromInclusive, int toExclusive, Action<int> body) | ||
48 | { | ||
49 | For(ProcessorCount, fromInclusive, toExclusive, body); | ||
50 | } | ||
51 | |||
52 | /// <summary> | ||
53 | /// Executes a for loop in which iterations may run in parallel | ||
54 | /// </summary> | ||
55 | /// <param name="threadCount">The number of concurrent execution threads to run</param> | ||
56 | /// <param name="fromInclusive">The loop will be started at this index</param> | ||
57 | /// <param name="toExclusive">The loop will be terminated before this index is reached</param> | ||
58 | /// <param name="body">Method body to run for each iteration of the loop</param> | ||
59 | public static void For(int threadCount, int fromInclusive, int toExclusive, Action<int> body) | ||
60 | { | ||
61 | int counter = threadCount; | ||
62 | AutoResetEvent threadFinishEvent = new AutoResetEvent(false); | ||
63 | Exception exception = null; | ||
64 | |||
65 | --fromInclusive; | ||
66 | |||
67 | for (int i = 0; i < threadCount; i++) | ||
68 | { | ||
69 | Util.FireAndForget( | ||
70 | delegate(object o) | ||
71 | { | ||
72 | // int threadIndex = (int)o; | ||
73 | |||
74 | while (exception == null) | ||
75 | { | ||
76 | int currentIndex = Interlocked.Increment(ref fromInclusive); | ||
77 | |||
78 | if (currentIndex >= toExclusive) | ||
79 | break; | ||
80 | |||
81 | try { body(currentIndex); } | ||
82 | catch (Exception ex) { exception = ex; break; } | ||
83 | } | ||
84 | |||
85 | if (Interlocked.Decrement(ref counter) == 0) | ||
86 | threadFinishEvent.Set(); | ||
87 | }, i | ||
88 | ); | ||
89 | } | ||
90 | |||
91 | threadFinishEvent.WaitOne(); | ||
92 | threadFinishEvent.Close(); | ||
93 | |||
94 | if (exception != null) | ||
95 | throw new Exception(exception.Message, exception); | ||
96 | } | ||
97 | |||
98 | /// <summary> | ||
99 | /// Executes a foreach loop in which iterations may run in parallel | ||
100 | /// </summary> | ||
101 | /// <typeparam name="T">Object type that the collection wraps</typeparam> | ||
102 | /// <param name="enumerable">An enumerable collection to iterate over</param> | ||
103 | /// <param name="body">Method body to run for each object in the collection</param> | ||
104 | public static void ForEach<T>(IEnumerable<T> enumerable, Action<T> body) | ||
105 | { | ||
106 | ForEach<T>(ProcessorCount, enumerable, body); | ||
107 | } | ||
108 | |||
109 | /// <summary> | ||
110 | /// Executes a foreach loop in which iterations may run in parallel | ||
111 | /// </summary> | ||
112 | /// <typeparam name="T">Object type that the collection wraps</typeparam> | ||
113 | /// <param name="threadCount">The number of concurrent execution threads to run</param> | ||
114 | /// <param name="enumerable">An enumerable collection to iterate over</param> | ||
115 | /// <param name="body">Method body to run for each object in the collection</param> | ||
116 | public static void ForEach<T>(int threadCount, IEnumerable<T> enumerable, Action<T> body) | ||
117 | { | ||
118 | int counter = threadCount; | ||
119 | AutoResetEvent threadFinishEvent = new AutoResetEvent(false); | ||
120 | IEnumerator<T> enumerator = enumerable.GetEnumerator(); | ||
121 | object syncRoot = new object(); | ||
122 | Exception exception = null; | ||
123 | |||
124 | for (int i = 0; i < threadCount; i++) | ||
125 | { | ||
126 | Util.FireAndForget( | ||
127 | delegate(object o) | ||
128 | { | ||
129 | // int threadIndex = (int)o; | ||
130 | |||
131 | while (exception == null) | ||
132 | { | ||
133 | T entry; | ||
134 | |||
135 | lock (syncRoot) | ||
136 | { | ||
137 | if (!enumerator.MoveNext()) | ||
138 | break; | ||
139 | entry = (T)enumerator.Current; // Explicit typecast for Mono's sake | ||
140 | } | ||
141 | |||
142 | try { body(entry); } | ||
143 | catch (Exception ex) { exception = ex; break; } | ||
144 | } | ||
145 | |||
146 | if (Interlocked.Decrement(ref counter) == 0) | ||
147 | threadFinishEvent.Set(); | ||
148 | }, i | ||
149 | ); | ||
150 | } | ||
151 | |||
152 | threadFinishEvent.WaitOne(); | ||
153 | threadFinishEvent.Close(); | ||
154 | |||
155 | if (exception != null) | ||
156 | throw new Exception(exception.Message, exception); | ||
157 | } | ||
158 | |||
159 | /// <summary> | ||
160 | /// Executes a series of tasks in parallel | ||
161 | /// </summary> | ||
162 | /// <param name="actions">A series of method bodies to execute</param> | ||
163 | public static void Invoke(params Action[] actions) | ||
164 | { | ||
165 | Invoke(ProcessorCount, actions); | ||
166 | } | ||
167 | |||
168 | /// <summary> | ||
169 | /// Executes a series of tasks in parallel | ||
170 | /// </summary> | ||
171 | /// <param name="threadCount">The number of concurrent execution threads to run</param> | ||
172 | /// <param name="actions">A series of method bodies to execute</param> | ||
173 | public static void Invoke(int threadCount, params Action[] actions) | ||
174 | { | ||
175 | int counter = threadCount; | ||
176 | AutoResetEvent threadFinishEvent = new AutoResetEvent(false); | ||
177 | int index = -1; | ||
178 | Exception exception = null; | ||
179 | |||
180 | for (int i = 0; i < threadCount; i++) | ||
181 | { | ||
182 | Util.FireAndForget( | ||
183 | delegate(object o) | ||
184 | { | ||
185 | // int threadIndex = (int)o; | ||
186 | |||
187 | while (exception == null) | ||
188 | { | ||
189 | int currentIndex = Interlocked.Increment(ref index); | ||
190 | |||
191 | if (currentIndex >= actions.Length) | ||
192 | break; | ||
193 | |||
194 | try { actions[currentIndex](); } | ||
195 | catch (Exception ex) { exception = ex; break; } | ||
196 | } | ||
197 | |||
198 | if (Interlocked.Decrement(ref counter) == 0) | ||
199 | threadFinishEvent.Set(); | ||
200 | }, i | ||
201 | ); | ||
202 | } | ||
203 | |||
204 | threadFinishEvent.WaitOne(); | ||
205 | threadFinishEvent.Close(); | ||
206 | |||
207 | if (exception != null) | ||
208 | throw new Exception(exception.Message, exception); | ||
209 | } | ||
210 | } | ||
211 | } | ||