aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Util.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework/Util.cs')
-rw-r--r--OpenSim/Framework/Util.cs64
1 files changed, 53 insertions, 11 deletions
diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs
index 7dde6dd..d09bd6d 100644
--- a/OpenSim/Framework/Util.cs
+++ b/OpenSim/Framework/Util.cs
@@ -53,6 +53,18 @@ using Amib.Threading;
53namespace OpenSim.Framework 53namespace OpenSim.Framework
54{ 54{
55 /// <summary> 55 /// <summary>
56 /// The method used by Util.FireAndForget for asynchronously firing events
57 /// </summary>
58 public enum FireAndForgetMethod
59 {
60 UnsafeQueueUserWorkItem,
61 QueueUserWorkItem,
62 BeginInvoke,
63 SmartThreadPool,
64 Thread,
65 }
66
67 /// <summary>
56 /// Miscellaneous utility functions 68 /// Miscellaneous utility functions
57 /// </summary> 69 /// </summary>
58 public class Util 70 public class Util
@@ -74,7 +86,9 @@ namespace OpenSim.Framework
74 86
75 public static readonly Regex UUIDPattern 87 public static readonly Regex UUIDPattern
76 = 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}$"); 88 = 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}$");
77 89
90 public static FireAndForgetMethod FireAndForgetMethod = FireAndForgetMethod.SmartThreadPool;
91
78 /// <summary> 92 /// <summary>
79 /// Linear interpolates B<->C using percent A 93 /// Linear interpolates B<->C using percent A
80 /// </summary> 94 /// </summary>
@@ -1277,7 +1291,7 @@ namespace OpenSim.Framework
1277 /// <summary> 1291 /// <summary>
1278 /// Created to work around a limitation in Mono with nested delegates 1292 /// Created to work around a limitation in Mono with nested delegates
1279 /// </summary> 1293 /// </summary>
1280 /*private class FireAndForgetWrapper 1294 private class FireAndForgetWrapper
1281 { 1295 {
1282 public void FireAndForget(System.Threading.WaitCallback callback) 1296 public void FireAndForget(System.Threading.WaitCallback callback)
1283 { 1297 {
@@ -1288,17 +1302,28 @@ namespace OpenSim.Framework
1288 { 1302 {
1289 callback.BeginInvoke(obj, EndFireAndForget, callback); 1303 callback.BeginInvoke(obj, EndFireAndForget, callback);
1290 } 1304 }
1291 }*/ 1305
1306 private static void EndFireAndForget(IAsyncResult ar)
1307 {
1308 System.Threading.WaitCallback callback = (System.Threading.WaitCallback)ar.AsyncState;
1309
1310 try { callback.EndInvoke(ar); }
1311 catch (Exception ex) { m_log.Error("[UTIL]: Asynchronous method threw an exception: " + ex.Message, ex); }
1312
1313 ar.AsyncWaitHandle.Close();
1314 }
1315 }
1292 1316
1293 public static void FireAndForget(System.Threading.WaitCallback callback) 1317 public static void FireAndForget(System.Threading.WaitCallback callback)
1294 { 1318 {
1295 //FireAndForgetWrapper wrapper = Singleton.GetInstance<FireAndForgetWrapper>(); 1319 FireAndForget(callback, null);
1296 //wrapper.FireAndForget(callback);
1297 System.Threading.ThreadPool.UnsafeQueueUserWorkItem(callback, null);
1298 } 1320 }
1299 1321
1300 public static void SetMaxThreads(int maxThreads) 1322 public static void SetMaxThreads(int maxThreads)
1301 { 1323 {
1324 if (m_ThreadPool != null)
1325 return;
1326
1302 STPStartInfo startInfo = new STPStartInfo(); 1327 STPStartInfo startInfo = new STPStartInfo();
1303 startInfo.IdleTimeout = 2000; // 2 seconds 1328 startInfo.IdleTimeout = 2000; // 2 seconds
1304 startInfo.MaxWorkerThreads = maxThreads; 1329 startInfo.MaxWorkerThreads = maxThreads;
@@ -1313,11 +1338,28 @@ namespace OpenSim.Framework
1313 1338
1314 public static void FireAndForget(System.Threading.WaitCallback callback, object obj) 1339 public static void FireAndForget(System.Threading.WaitCallback callback, object obj)
1315 { 1340 {
1316 m_ThreadPool.QueueWorkItem(new WorkItemCallback(delegate(object o) 1341 switch (FireAndForgetMethod)
1317 { 1342 {
1318 callback(o); 1343 case FireAndForgetMethod.UnsafeQueueUserWorkItem:
1319 return null; 1344 System.Threading.ThreadPool.UnsafeQueueUserWorkItem(callback, obj);
1320 }), obj); 1345 break;
1346 case FireAndForgetMethod.QueueUserWorkItem:
1347 System.Threading.ThreadPool.QueueUserWorkItem(callback, obj);
1348 break;
1349 case FireAndForgetMethod.BeginInvoke:
1350 FireAndForgetWrapper wrapper = Singleton.GetInstance<FireAndForgetWrapper>();
1351 wrapper.FireAndForget(callback, obj);
1352 break;
1353 case FireAndForgetMethod.SmartThreadPool:
1354 m_ThreadPool.QueueWorkItem(delegate(object o) { callback(o); return null; }, obj);
1355 break;
1356 case FireAndForgetMethod.Thread:
1357 System.Threading.Thread thread = new System.Threading.Thread(delegate(object o) { callback(o); });
1358 thread.Start(obj);
1359 break;
1360 default:
1361 throw new NotImplementedException();
1362 }
1321 } 1363 }
1322 1364
1323 #endregion FireAndForget Threading Pattern 1365 #endregion FireAndForget Threading Pattern