Skip to content

Commit

Permalink
* Allow SmartThreadPool to be initialized without setting max stack s…
Browse files Browse the repository at this point in the history
…ize (like the original implementation)

* Only initialize Util's SmartThreadPool if it is actually being used
* No longer initializing Util's SmartThreadPool with a custom max stack size. From MSDN: "Avoid using this constructor overload. The default stack size used by the Thread(ThreadStart) constructor overload is the recommended stack size for threads."
  • Loading branch information
jhurliman committed Oct 22, 2009
1 parent 1e71e3f commit 2f394b7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
12 changes: 7 additions & 5 deletions OpenSim/Framework/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ public enum FireAndForgetMethod
/// </summary>
public class Util
{
private static SmartThreadPool m_ThreadPool = null;

private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

private static uint nextXferID = 5000;
Expand All @@ -79,6 +77,9 @@ public class Util
private static string regexInvalidFileChars = "[" + new String(Path.GetInvalidFileNameChars()) + "]";
private static string regexInvalidPathChars = "[" + new String(Path.GetInvalidPathChars()) + "]";
private static object XferLock = new object();
/// <summary>Thread pool used for Util.FireAndForget if
/// FireAndForgetMethod.SmartThreadPool is used</summary>
private static SmartThreadPool m_ThreadPool;

// 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.
private static readonly DateTime unixEpoch =
Expand Down Expand Up @@ -1319,18 +1320,19 @@ public static void FireAndForget(System.Threading.WaitCallback callback)
FireAndForget(callback, null);
}

public static void SetMaxThreads(int maxThreads)
public static void InitThreadPool(int maxThreads)
{
if (maxThreads < 2)
throw new ArgumentOutOfRangeException("maxThreads", "maxThreads must be greater than 2");

if (m_ThreadPool != null)
return;

STPStartInfo startInfo = new STPStartInfo();
startInfo.IdleTimeout = 2000; // 2 seconds
startInfo.MaxWorkerThreads = maxThreads;
startInfo.MinWorkerThreads = 2;
startInfo.StackSize = 524288;
startInfo.ThreadPriority = ThreadPriority.Normal;

startInfo.StartSuspended = false;

m_ThreadPool = new SmartThreadPool(startInfo);
Expand Down
7 changes: 6 additions & 1 deletion OpenSim/Region/Application/OpenSim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected override void ReadExtraConfigSettings()

IConfig startupConfig = m_config.Source.Configs["Startup"];

Util.SetMaxThreads(startupConfig.GetInt("MaxPoolThreads", 15));
int stpMaxThreads = 15;

if (startupConfig != null)
{
Expand Down Expand Up @@ -100,8 +100,13 @@ protected override void ReadExtraConfigSettings()
FireAndForgetMethod asyncCallMethod;
if (!String.IsNullOrEmpty(asyncCallMethodStr) && Utils.EnumTryParse<FireAndForgetMethod>(asyncCallMethodStr, out asyncCallMethod))
Util.FireAndForgetMethod = asyncCallMethod;

stpMaxThreads = startupConfig.GetInt("MaxPoolThreads", 15);
}

if (Util.FireAndForgetMethod == FireAndForgetMethod.SmartThreadPool)
Util.InitThreadPool(stpMaxThreads);

m_log.Info("[OPENSIM MAIN]: Using async_call_method " + Util.FireAndForgetMethod);
}

Expand Down
6 changes: 5 additions & 1 deletion ThirdParty/SmartThreadPool/SmartThreadPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,11 @@ private void StartThreads(int threadsCount)
}

// Create a new thread
Thread workerThread = new Thread(new ThreadStart(ProcessQueuedItems), _stpStartInfo.StackSize);
Thread workerThread;
if (_stpStartInfo.StackSize > 0)
workerThread = new Thread(ProcessQueuedItems, _stpStartInfo.StackSize);
else
workerThread = new Thread(ProcessQueuedItems);

// Configure the new thread and start it
workerThread.Name = "STP " + Name + " Thread #" + _threadCounter;
Expand Down
14 changes: 9 additions & 5 deletions bin/OpenSim.ini.example
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@

; Sets the method that OpenSim will use to fire asynchronous
; events. Valid values are UnsafeQueueUserWorkItem,
; QueueUserWorkItem, BeginInvoke, SmartThreadPool, and Thread
; async_call_method = SmartThreadPool
; QueueUserWorkItem, BeginInvoke, SmartThreadPool, and Thread.
; SmartThreadPool is reported to work well on Mono/Linux, but
; UnsafeQueueUserWorkItem has been benchmarked with better
; performance on .NET/Windows
;async_call_method = SmartThreadPool

; Max threads to allocate on the FireAndForget thread pool
; when running with the SmartThreadPool option above
MaxPoolThreads = 15

; ##
; ## CLIENTS
Expand All @@ -51,9 +58,6 @@
; Set this to the DLL containing the client stack to use.
clientstack_plugin="OpenSim.Region.ClientStack.LindenUDP.dll"

; Max threads to allocate on the FireAndForget pool
MaxPoolThreads = 15

; ##
; ## REGIONS
; ##
Expand Down

0 comments on commit 2f394b7

Please sign in to comment.