diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Framework/Util.cs | 70 |
1 files changed, 51 insertions, 19 deletions
diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index d5ae3b7..02f6d12 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs | |||
@@ -51,6 +51,18 @@ using OpenMetaverse.StructuredData; | |||
51 | namespace OpenSim.Framework | 51 | namespace OpenSim.Framework |
52 | { | 52 | { |
53 | /// <summary> | 53 | /// <summary> |
54 | /// The method used by Util.FireAndForget for asynchronously firing events | ||
55 | /// </summary> | ||
56 | public enum FireAndForgetMethod | ||
57 | { | ||
58 | UnsafeQueueUserWorkItem, | ||
59 | QueueUserWorkItem, | ||
60 | BeginInvoke, | ||
61 | SmartThreadPool, | ||
62 | Thread, | ||
63 | } | ||
64 | |||
65 | /// <summary> | ||
54 | /// Miscellaneous utility functions | 66 | /// Miscellaneous utility functions |
55 | /// </summary> | 67 | /// </summary> |
56 | public class Util | 68 | public class Util |
@@ -70,7 +82,9 @@ namespace OpenSim.Framework | |||
70 | 82 | ||
71 | public static readonly Regex UUIDPattern | 83 | public static readonly Regex UUIDPattern |
72 | = new Regex("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"); | 84 | = new Regex("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"); |
73 | 85 | ||
86 | public static FireAndForgetMethod FireAndForgetMethod = FireAndForgetMethod.UnsafeQueueUserWorkItem; | ||
87 | |||
74 | /// <summary> | 88 | /// <summary> |
75 | /// Linear interpolates B<->C using percent A | 89 | /// Linear interpolates B<->C using percent A |
76 | /// </summary> | 90 | /// </summary> |
@@ -1273,7 +1287,7 @@ namespace OpenSim.Framework | |||
1273 | /// <summary> | 1287 | /// <summary> |
1274 | /// Created to work around a limitation in Mono with nested delegates | 1288 | /// Created to work around a limitation in Mono with nested delegates |
1275 | /// </summary> | 1289 | /// </summary> |
1276 | /*private class FireAndForgetWrapper | 1290 | private class FireAndForgetWrapper |
1277 | { | 1291 | { |
1278 | public void FireAndForget(System.Threading.WaitCallback callback) | 1292 | public void FireAndForget(System.Threading.WaitCallback callback) |
1279 | { | 1293 | { |
@@ -1284,32 +1298,50 @@ namespace OpenSim.Framework | |||
1284 | { | 1298 | { |
1285 | callback.BeginInvoke(obj, EndFireAndForget, callback); | 1299 | callback.BeginInvoke(obj, EndFireAndForget, callback); |
1286 | } | 1300 | } |
1287 | }*/ | 1301 | |
1302 | private static void EndFireAndForget(IAsyncResult ar) | ||
1303 | { | ||
1304 | System.Threading.WaitCallback callback = (System.Threading.WaitCallback)ar.AsyncState; | ||
1305 | |||
1306 | try { callback.EndInvoke(ar); } | ||
1307 | catch (Exception ex) { m_log.Error("[UTIL]: Asynchronous method threw an exception: " + ex.Message, ex); } | ||
1308 | |||
1309 | ar.AsyncWaitHandle.Close(); | ||
1310 | } | ||
1311 | } | ||
1288 | 1312 | ||
1289 | public static void FireAndForget(System.Threading.WaitCallback callback) | 1313 | public static void FireAndForget(System.Threading.WaitCallback callback) |
1290 | { | 1314 | { |
1291 | //FireAndForgetWrapper wrapper = Singleton.GetInstance<FireAndForgetWrapper>(); | 1315 | FireAndForget(callback, null); |
1292 | //wrapper.FireAndForget(callback); | ||
1293 | System.Threading.ThreadPool.UnsafeQueueUserWorkItem(callback, null); | ||
1294 | } | 1316 | } |
1295 | 1317 | ||
1296 | public static void FireAndForget(System.Threading.WaitCallback callback, object obj) | 1318 | public static void FireAndForget(System.Threading.WaitCallback callback, object obj) |
1297 | { | 1319 | { |
1298 | //FireAndForgetWrapper wrapper = Singleton.GetInstance<FireAndForgetWrapper>(); | 1320 | switch (FireAndForgetMethod) |
1299 | //wrapper.FireAndForget(callback, obj); | 1321 | { |
1300 | System.Threading.ThreadPool.UnsafeQueueUserWorkItem(callback, obj); | 1322 | case FireAndForgetMethod.UnsafeQueueUserWorkItem: |
1323 | System.Threading.ThreadPool.UnsafeQueueUserWorkItem(callback, obj); | ||
1324 | break; | ||
1325 | case FireAndForgetMethod.QueueUserWorkItem: | ||
1326 | System.Threading.ThreadPool.QueueUserWorkItem(callback, obj); | ||
1327 | break; | ||
1328 | case FireAndForgetMethod.BeginInvoke: | ||
1329 | FireAndForgetWrapper wrapper = Singleton.GetInstance<FireAndForgetWrapper>(); | ||
1330 | wrapper.FireAndForget(callback, obj); | ||
1331 | break; | ||
1332 | case FireAndForgetMethod.SmartThreadPool: | ||
1333 | Amib.Threading.SmartThreadPool stp = Singleton.GetInstance<Amib.Threading.SmartThreadPool>(); | ||
1334 | stp.QueueWorkItem(delegate(object o) { callback(o); return null; }, obj); | ||
1335 | break; | ||
1336 | case FireAndForgetMethod.Thread: | ||
1337 | System.Threading.Thread thread = new System.Threading.Thread(delegate(object o) { callback(o); }); | ||
1338 | thread.Start(obj); | ||
1339 | break; | ||
1340 | default: | ||
1341 | throw new NotImplementedException(); | ||
1342 | } | ||
1301 | } | 1343 | } |
1302 | 1344 | ||
1303 | /*private static void EndFireAndForget(IAsyncResult ar) | ||
1304 | { | ||
1305 | System.Threading.WaitCallback callback = (System.Threading.WaitCallback)ar.AsyncState; | ||
1306 | |||
1307 | try { callback.EndInvoke(ar); } | ||
1308 | catch (Exception ex) { m_log.Error("[UTIL]: Asynchronous method threw an exception: " + ex.Message, ex); } | ||
1309 | |||
1310 | ar.AsyncWaitHandle.Close(); | ||
1311 | }*/ | ||
1312 | |||
1313 | #endregion FireAndForget Threading Pattern | 1345 | #endregion FireAndForget Threading Pattern |
1314 | } | 1346 | } |
1315 | } | 1347 | } |