Added ability to use SmartThreadPool

This commit is contained in:
Latif Khalifa
2013-07-18 20:00:11 +02:00
parent 21f82ab380
commit f1b8fcaf47
10 changed files with 248 additions and 14 deletions

View File

@@ -3943,7 +3943,7 @@ namespace OpenMetaverse
if (m_AnimationsChanged != null)
{
ThreadPool.QueueUserWorkItem(delegate(object o)
WorkPool.QueueUserWorkItem(delegate(object o)
{ OnAnimationsChanged(new AnimationsChangedEventArgs(this.SignaledAnimations)); });
}
@@ -4338,7 +4338,7 @@ namespace OpenMetaverse
return;
}
ThreadPool.QueueUserWorkItem(sync =>
WorkPool.QueueUserWorkItem(sync =>
{
using (AutoResetEvent gotMuteList = new AutoResetEvent(false))
{

View File

@@ -901,7 +901,7 @@ namespace OpenMetaverse
{
Logger.Log("UploadBakedTexture not available, falling back to UDP method", Helpers.LogLevel.Info, Client);
ThreadPool.QueueUserWorkItem(
WorkPool.QueueUserWorkItem(
delegate(object o)
{
UUID transactionID = UUID.Random();

View File

@@ -144,7 +144,7 @@ namespace OpenMetaverse
wrapper.Callback = callback.Callback;
wrapper.Packet = packet;
wrapper.Simulator = simulator;
ThreadPool.QueueUserWorkItem(ThreadPoolDelegate, wrapper);
WorkPool.QueueUserWorkItem(ThreadPoolDelegate, wrapper);
}
else
{
@@ -164,7 +164,7 @@ namespace OpenMetaverse
wrapper.Callback = callback.Callback;
wrapper.Packet = packet;
wrapper.Simulator = simulator;
ThreadPool.QueueUserWorkItem(ThreadPoolDelegate, wrapper);
WorkPool.QueueUserWorkItem(ThreadPoolDelegate, wrapper);
}
else
{
@@ -329,7 +329,7 @@ namespace OpenMetaverse
wrapper.CapsEvent = capsEvent;
wrapper.Message = message;
wrapper.Simulator = simulator;
ThreadPool.QueueUserWorkItem(_ThreadPoolCallback, wrapper);
WorkPool.QueueUserWorkItem(_ThreadPoolCallback, wrapper);
}
}
@@ -341,7 +341,7 @@ namespace OpenMetaverse
wrapper.CapsEvent = capsEvent;
wrapper.Message = message;
wrapper.Simulator = simulator;
ThreadPool.QueueUserWorkItem(_ThreadPoolCallback, wrapper);
WorkPool.QueueUserWorkItem(_ThreadPoolCallback, wrapper);
specialHandler = true;
}

View File

@@ -108,6 +108,9 @@ namespace OpenMetaverse
/// </summary>
public GridClient()
{
// Initialise ThreadPool
WorkPool.Init(true);
// These are order-dependant
Network = new NetworkManager(this);
Settings = new Settings(this);

View File

@@ -838,7 +838,7 @@ namespace OpenMetaverse
if (m_CoarseLocationUpdate != null)
{
ThreadPool.QueueUserWorkItem(delegate(object o)
WorkPool.QueueUserWorkItem(delegate(object o)
{ OnCoarseLocationUpdate(new CoarseLocationUpdateEventArgs(e.Simulator, newEntries, removedEntries)); });
}
}

View File

@@ -2101,7 +2101,7 @@ namespace OpenMetaverse
EventHandler<PrimEventArgs> handler = m_ObjectUpdate;
if (handler != null)
{
ThreadPool.QueueUserWorkItem(delegate(object o)
WorkPool.QueueUserWorkItem(delegate(object o)
{ handler(this, new PrimEventArgs(simulator, prim, update.RegionData.TimeDilation, isNewObject, attachment)); });
}
@@ -2320,7 +2320,7 @@ namespace OpenMetaverse
EventHandler<TerseObjectUpdateEventArgs> handler = m_TerseObjectUpdate;
if (handler != null)
{
ThreadPool.QueueUserWorkItem(delegate(object o)
WorkPool.QueueUserWorkItem(delegate(object o)
{ handler(this, new TerseObjectUpdateEventArgs(simulator, obj, update, terse.RegionData.TimeDilation)); });
}

View File

@@ -515,7 +515,7 @@ namespace OpenMetaverse
nextTask.RequestSlot = slot;
//Logger.DebugLog(String.Format("Sending Worker thread new download request {0}", slot));
ThreadPool.QueueUserWorkItem(TextureRequestDoWork, nextTask);
WorkPool.QueueUserWorkItem(TextureRequestDoWork, nextTask);
continue;
}
}

View File

@@ -65,7 +65,7 @@ namespace OpenMetaverse
for (int i = 0; i < threadCount; i++)
{
ThreadPool.QueueUserWorkItem(
WorkPool.QueueUserWorkItem(
delegate(object o)
{
int threadIndex = (int)o;
@@ -120,7 +120,7 @@ namespace OpenMetaverse
for (int i = 0; i < threadCount; i++)
{
ThreadPool.QueueUserWorkItem(
WorkPool.QueueUserWorkItem(
delegate(object o)
{
int threadIndex = (int)o;
@@ -175,7 +175,7 @@ namespace OpenMetaverse
for (int i = 0; i < threadCount; i++)
{
ThreadPool.QueueUserWorkItem(
WorkPool.QueueUserWorkItem(
delegate(object o)
{
int threadIndex = (int)o;

View File

@@ -0,0 +1,231 @@
/*
* Copyright (c) 2007-2009, openmetaverse.org
* All rights reserved.
*
* - Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Neither the name of the openmetaverse.org nor the names
* of its contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
//#define SMARTHREADPOOL_REF
using System;
using System.IO;
using System.Reflection;
#if SMARTHREADPOOL_REF
using Amib.Threading;
#else
using System.Threading;
#endif
namespace OpenMetaverse
{
// Use statically referenced SmartThreadPool.dll
#if SMARTHREADPOOL_REF
public static class WorkPool
{
internal static SmartThreadPool Pool = null;
public static bool Init(bool useSmartThredPool)
{
if (Pool == null)
{
STPStartInfo param = new STPStartInfo();
param.MinWorkerThreads = 2;
param.MaxWorkerThreads = 50;
param.ThreadPoolName = "LibOpenMetaverse Main ThreadPool";
param.AreThreadsBackground = true;
Pool = new SmartThreadPool(param);
}
return true;
}
public static void Shutdown()
{
Pool.Shutdown();
Pool = null;
}
public static void QueueUserWorkItem(System.Threading.WaitCallback callback)
{
if (Pool != null)
{
Pool.QueueWorkItem(state => { callback.Invoke(state); return null; });
}
else
{
System.Threading.ThreadPool.QueueUserWorkItem(state => callback.Invoke(state));
}
}
public static void QueueUserWorkItem(System.Threading.WaitCallback callback, object state)
{
if (Pool != null)
{
Pool.QueueWorkItem(sync => { callback.Invoke(sync); return null; }, state);
}
else
{
System.Threading.ThreadPool.QueueUserWorkItem(sync => callback.Invoke(sync), state);
}
}
}
#else
// Try to load SmartThreadPool.dll during initialization
// Fallback to System.Threading.ThreadPool if that fails
public static class WorkPoolDynamic
{
internal static object Pool = null;
private static Type SmartThreadPoolType;
private static Type WorkItemCallbackType;
private static MethodInfo QueueWorkItemFunc, QueueWorkItemFunc2;
private static MethodInfo ShutdownFunc;
private static Func<System.Threading.WaitCallback, object, object> Invoker;
public static bool Init()
{
try
{
string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Assembly assembly = Assembly.LoadFile(Path.Combine(dir, "SmartThreadPool.dll"));
Type STPStartInfo = assembly.GetType("Amib.Threading.STPStartInfo");
SmartThreadPoolType = assembly.GetType("Amib.Threading.SmartThreadPool");
WorkItemCallbackType = assembly.GetType("Amib.Threading.WorkItemCallback");
var param = Activator.CreateInstance(STPStartInfo);
STPStartInfo.GetProperty("MinWorkerThreads").SetValue(param, 2, null);
STPStartInfo.GetProperty("MaxWorkerThreads").SetValue(param, 50, null);
STPStartInfo.GetProperty("ThreadPoolName").SetValue(param, "LibOpenMetaverse Main ThreadPool", null);
STPStartInfo.GetProperty("AreThreadsBackground").SetValue(param, true, null);
STPStartInfo.GetProperty("MinWorkerThreads").SetValue(param, 2, null);
Pool = Activator.CreateInstance(SmartThreadPoolType, new object[] { param });
QueueWorkItemFunc = SmartThreadPoolType.GetMethod("QueueWorkItem", new Type[] { WorkItemCallbackType });
QueueWorkItemFunc2 = SmartThreadPoolType.GetMethod("QueueWorkItem", new Type[] { WorkItemCallbackType, typeof(object) });
ShutdownFunc = SmartThreadPoolType.GetMethod("Shutdown", new Type[] { });
Invoker = (inv, state) =>
{
inv.Invoke(state);
return null;
};
return true;
}
catch
{
Pool = null;
return false;
}
}
public static void Shutdown()
{
if (Pool != null)
{
ShutdownFunc.Invoke(Pool, null);
Pool = null;
}
}
public static void QueueUserWorkItem(System.Threading.WaitCallback callback)
{
if (Pool != null)
{
QueueWorkItemFunc.Invoke(Pool, new object[] { Delegate.CreateDelegate(WorkItemCallbackType, callback, Invoker.Method) });
}
else
{
System.Threading.ThreadPool.QueueUserWorkItem(state => callback.Invoke(state));
}
}
public static void QueueUserWorkItem(System.Threading.WaitCallback callback, object state)
{
if (Pool != null)
{
QueueWorkItemFunc2.Invoke(Pool, new object[] { Delegate.CreateDelegate(WorkItemCallbackType, callback, Invoker.Method), state });
}
else
{
System.Threading.ThreadPool.QueueUserWorkItem(sync => callback.Invoke(sync), state);
}
}
}
public static class WorkPool
{
private static bool UseSmartThreadPool = false;
public static bool Init(bool useSmartThredPool)
{
if (useSmartThredPool)
{
if (WorkPoolDynamic.Init())
{
UseSmartThreadPool = true;
return true;
}
return false;
}
return true;
}
public static void Shutdown()
{
if (UseSmartThreadPool)
{
WorkPoolDynamic.Shutdown();
UseSmartThreadPool = false;
}
}
public static void QueueUserWorkItem(System.Threading.WaitCallback callback)
{
if (UseSmartThreadPool)
{
WorkPoolDynamic.QueueUserWorkItem(sync => callback.Invoke(sync));
}
else
{
ThreadPool.QueueUserWorkItem(sync => callback.Invoke(sync));
}
}
public static void QueueUserWorkItem(System.Threading.WaitCallback callback, object state)
{
if (UseSmartThreadPool)
{
WorkPoolDynamic.QueueUserWorkItem(sync => callback.Invoke(sync), state);
}
else
{
ThreadPool.QueueUserWorkItem(sync => callback.Invoke(sync), state);
}
}
}
#endif
}

BIN
bin/SmartThreadPool.dll Normal file

Binary file not shown.