diff options
Diffstat (limited to 'OpenSim/Framework/Util.cs')
-rw-r--r-- | OpenSim/Framework/Util.cs | 42 |
1 files changed, 24 insertions, 18 deletions
diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index d09bd6d..10f38ab 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs | |||
@@ -69,8 +69,6 @@ namespace OpenSim.Framework | |||
69 | /// </summary> | 69 | /// </summary> |
70 | public class Util | 70 | public class Util |
71 | { | 71 | { |
72 | private static SmartThreadPool m_ThreadPool = null; | ||
73 | |||
74 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | 72 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
75 | 73 | ||
76 | private static uint nextXferID = 5000; | 74 | private static uint nextXferID = 5000; |
@@ -79,6 +77,9 @@ namespace OpenSim.Framework | |||
79 | private static string regexInvalidFileChars = "[" + new String(Path.GetInvalidFileNameChars()) + "]"; | 77 | private static string regexInvalidFileChars = "[" + new String(Path.GetInvalidFileNameChars()) + "]"; |
80 | private static string regexInvalidPathChars = "[" + new String(Path.GetInvalidPathChars()) + "]"; | 78 | private static string regexInvalidPathChars = "[" + new String(Path.GetInvalidPathChars()) + "]"; |
81 | private static object XferLock = new object(); | 79 | private static object XferLock = new object(); |
80 | /// <summary>Thread pool used for Util.FireAndForget if | ||
81 | /// FireAndForgetMethod.SmartThreadPool is used</summary> | ||
82 | private static SmartThreadPool m_ThreadPool; | ||
82 | 83 | ||
83 | // Unix-epoch starts at January 1st 1970, 00:00:00 UTC. And all our times in the server are (or at least should be) in UTC. | 84 | // Unix-epoch starts at January 1st 1970, 00:00:00 UTC. And all our times in the server are (or at least should be) in UTC. |
84 | private static readonly DateTime unixEpoch = | 85 | private static readonly DateTime unixEpoch = |
@@ -1319,21 +1320,14 @@ namespace OpenSim.Framework | |||
1319 | FireAndForget(callback, null); | 1320 | FireAndForget(callback, null); |
1320 | } | 1321 | } |
1321 | 1322 | ||
1322 | public static void SetMaxThreads(int maxThreads) | 1323 | public static void InitThreadPool(int maxThreads) |
1323 | { | 1324 | { |
1325 | if (maxThreads < 2) | ||
1326 | throw new ArgumentOutOfRangeException("maxThreads", "maxThreads must be greater than 2"); | ||
1324 | if (m_ThreadPool != null) | 1327 | if (m_ThreadPool != null) |
1325 | return; | 1328 | throw new InvalidOperationException("SmartThreadPool is already initialized"); |
1326 | |||
1327 | STPStartInfo startInfo = new STPStartInfo(); | ||
1328 | startInfo.IdleTimeout = 2000; // 2 seconds | ||
1329 | startInfo.MaxWorkerThreads = maxThreads; | ||
1330 | startInfo.MinWorkerThreads = 2; | ||
1331 | startInfo.StackSize = 524288; | ||
1332 | startInfo.ThreadPriority = ThreadPriority.Normal; | ||
1333 | 1329 | ||
1334 | startInfo.StartSuspended = false; | 1330 | m_ThreadPool = new SmartThreadPool(2000, maxThreads, 2); |
1335 | |||
1336 | m_ThreadPool = new SmartThreadPool(startInfo); | ||
1337 | } | 1331 | } |
1338 | 1332 | ||
1339 | public static void FireAndForget(System.Threading.WaitCallback callback, object obj) | 1333 | public static void FireAndForget(System.Threading.WaitCallback callback, object obj) |
@@ -1341,20 +1335,22 @@ namespace OpenSim.Framework | |||
1341 | switch (FireAndForgetMethod) | 1335 | switch (FireAndForgetMethod) |
1342 | { | 1336 | { |
1343 | case FireAndForgetMethod.UnsafeQueueUserWorkItem: | 1337 | case FireAndForgetMethod.UnsafeQueueUserWorkItem: |
1344 | System.Threading.ThreadPool.UnsafeQueueUserWorkItem(callback, obj); | 1338 | ThreadPool.UnsafeQueueUserWorkItem(callback, obj); |
1345 | break; | 1339 | break; |
1346 | case FireAndForgetMethod.QueueUserWorkItem: | 1340 | case FireAndForgetMethod.QueueUserWorkItem: |
1347 | System.Threading.ThreadPool.QueueUserWorkItem(callback, obj); | 1341 | ThreadPool.QueueUserWorkItem(callback, obj); |
1348 | break; | 1342 | break; |
1349 | case FireAndForgetMethod.BeginInvoke: | 1343 | case FireAndForgetMethod.BeginInvoke: |
1350 | FireAndForgetWrapper wrapper = Singleton.GetInstance<FireAndForgetWrapper>(); | 1344 | FireAndForgetWrapper wrapper = Singleton.GetInstance<FireAndForgetWrapper>(); |
1351 | wrapper.FireAndForget(callback, obj); | 1345 | wrapper.FireAndForget(callback, obj); |
1352 | break; | 1346 | break; |
1353 | case FireAndForgetMethod.SmartThreadPool: | 1347 | case FireAndForgetMethod.SmartThreadPool: |
1354 | m_ThreadPool.QueueWorkItem(delegate(object o) { callback(o); return null; }, obj); | 1348 | if (m_ThreadPool == null) |
1349 | m_ThreadPool = new SmartThreadPool(2000, 15, 2); | ||
1350 | m_ThreadPool.QueueWorkItem(SmartThreadPoolCallback, new object[] { callback, obj }); | ||
1355 | break; | 1351 | break; |
1356 | case FireAndForgetMethod.Thread: | 1352 | case FireAndForgetMethod.Thread: |
1357 | System.Threading.Thread thread = new System.Threading.Thread(delegate(object o) { callback(o); }); | 1353 | Thread thread = new Thread(delegate(object o) { callback(o); }); |
1358 | thread.Start(obj); | 1354 | thread.Start(obj); |
1359 | break; | 1355 | break; |
1360 | default: | 1356 | default: |
@@ -1362,6 +1358,16 @@ namespace OpenSim.Framework | |||
1362 | } | 1358 | } |
1363 | } | 1359 | } |
1364 | 1360 | ||
1361 | private static object SmartThreadPoolCallback(object o) | ||
1362 | { | ||
1363 | object[] array = (object[])o; | ||
1364 | WaitCallback callback = (WaitCallback)array[0]; | ||
1365 | object obj = array[1]; | ||
1366 | |||
1367 | callback(obj); | ||
1368 | return null; | ||
1369 | } | ||
1370 | |||
1365 | #endregion FireAndForget Threading Pattern | 1371 | #endregion FireAndForget Threading Pattern |
1366 | } | 1372 | } |
1367 | } | 1373 | } |