2004-10-29 Gonzalo Paniagua Javier <gonzalo@ximian.com>
authorGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Fri, 29 Oct 2004 19:54:38 +0000 (19:54 -0000)
committerGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Fri, 29 Oct 2004 19:54:38 +0000 (19:54 -0000)
* man/mono.1: documented MONO_THREADS_PER_CPU.
* mono/metadata/appdomain.c: call mono_thread_pool_init.
* mono/metadata/threadpool.[ch]: new mono_thread_pool_init that sets
the max. number of worker threads based on the number of CPUs and the
environment variable MONO_THREADS_PER_CPU if present. The defaults are
50 (25) for non-windows (windows) systems.

svn path=/trunk/mono/; revision=35476

ChangeLog
man/mono.1
mono/metadata/ChangeLog
mono/metadata/appdomain.c
mono/metadata/threadpool.c
mono/metadata/threadpool.h

index 5b29f765ad18f0c2914919923ada47ec548f0da1..2e3e7735669c6bf843ea86b1a11694cd75ba309a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2004-10-29 Gonzalo Paniagua Javier <gonzalo@ximian.com>
+
+       * man/mono.1: documented MONO_THREADS_PER_CPU.
+
 2004-10-29  Zoltan Varga  <vargaz@freemail.hu>
 
        * configure.in: Add INCLUDED_LIBGC automake conditional.
index b4d454a25935c0cfe455b4164ded2a139dbbad87..1379258a04fa929f7213fee018b383c2728eab0e 100644 (file)
@@ -341,6 +341,10 @@ managed implementation (slow). If unset, mono will try to use FAM under
 Unix systems and native API calls on Windows, falling back to the
 managed implementation on error.
 .TP
+.I "MONO_THREADS_PER_CPU"
+Sets the maximum number of threads in the threadpool per CPU. The default is
+50 for non-windows systems and 25 for windows.
+.TP
 .I "MONO_TRACE"
 If set, enables the System.Diagnostics.DefaultTraceListener, which will 
 print the output of the System.Diagnostics Trace and Debug classes.  
index 4be052285c513d1c50c0921880092bd708745df8..ee134a8b2c1548c5b0d7baaa07e9c42c124c1a26 100644 (file)
@@ -1,3 +1,11 @@
+2004-10-29 Gonzalo Paniagua Javier <gonzalo@ximian.com>
+
+       * appdomain.c: call mono_thread_pool_init.
+       * threadpool.[ch]: new mono_thread_pool_init that sets the max. number
+       of worker threads based on the number of CPUs and the environment
+       variable MONO_THREADS_PER_CPU if present. The defaults are 50 (25)
+       for non-windows (windows) systems.
+
 2004-10-27  Chris Toshok  <toshok@ximian.com>
 
        * mono-debug-debugger.c (write_class): don't call mono_class_init
index 9c955b2ba6895013493769e69bdee3b7b83d1f70..1b76ed79c7e0a23fedfaebac1c6b2e0258f7fe81 100644 (file)
@@ -79,6 +79,7 @@ mono_runtime_init (MonoDomain *domain, MonoThreadStartCB start_cb,
        MonoString *arg;
        
        MONO_GC_PRE_INIT ();
+       mono_thread_pool_init ();
        mono_marshal_init ();
 
        mono_install_assembly_preload_hook (mono_domain_assembly_preload, NULL);
index 9a0548480c81bb896db925264bc7bc001cf52f23..26362ba60b20db6548842941205b961f18b54a0f 100644 (file)
@@ -15,6 +15,9 @@
 #ifdef PLATFORM_WIN32
 #define WINVER 0x0500
 #define _WIN32_WINNT 0x0500
+#define THREADS_PER_CPU        25
+#else
+#define THREADS_PER_CPU        50
 #endif
 
 #include <mono/metadata/domain-internals.h>
@@ -31,7 +34,7 @@
 #include "threadpool.h"
 
 /* maximum number of worker threads */
-int mono_max_worker_threads = 25; /* per available CPU? */
+int mono_max_worker_threads = THREADS_PER_CPU;
 static int mono_min_worker_threads = 0;
 
 /* current number of worker threads */
@@ -40,6 +43,9 @@ static int mono_worker_threads = 0;
 /* current number of busy threads */
 static int busy_worker_threads = 0;
 
+/* mono_thread_pool_init called */
+static int tp_inited;
+
 /* we use this to store a reference to the AsyncResult to avoid GC */
 static MonoGHashTable *ares_htable = NULL;
 
@@ -97,6 +103,28 @@ mono_async_invoke (MonoAsyncResult *ares)
        LeaveCriticalSection (&ares_lock);
 }
 
+void
+mono_thread_pool_init ()
+{
+       SYSTEM_INFO info;
+       int threads_per_cpu = THREADS_PER_CPU;
+
+       if ((int) InterlockedCompareExchange (&tp_inited, 1, 0) == 1)
+               return;
+
+       MONO_GC_REGISTER_ROOT (ares_htable);
+       InitializeCriticalSection (&ares_lock);
+       ares_htable = mono_g_hash_table_new (NULL, NULL);
+       job_added = CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
+       GetSystemInfo (&info);
+       if (getenv ("MONO_THREADS_PER_CPU") != NULL) {
+               threads_per_cpu = atoi (getenv ("MONO_THREADS_PER_CPU"));
+               if (threads_per_cpu <= 0)
+                       threads_per_cpu = THREADS_PER_CPU;
+       }
+
+       mono_max_worker_threads = threads_per_cpu * info.dwNumberOfProcessors;
+}
 
 MonoAsyncResult *
 mono_thread_pool_add (MonoObject *target, MonoMethodMessage *msg, MonoDelegate *async_callback,
@@ -125,13 +153,6 @@ mono_thread_pool_add (MonoObject *target, MonoMethodMessage *msg, MonoDelegate *
        ares = mono_async_result_new (domain, NULL, ac->state, ac);
        ares->async_delegate = target;
 
-       if (!ares_htable) {
-               MONO_GC_REGISTER_ROOT (ares_htable);
-               InitializeCriticalSection (&ares_lock);
-               ares_htable = mono_g_hash_table_new (NULL, NULL);
-               job_added = CreateSemaphore (NULL, 0, 0x7fffffff, NULL);
-       }
-
        EnterCriticalSection (&ares_lock);
        mono_g_hash_table_insert (ares_htable, ares, ares);
        LeaveCriticalSection (&ares_lock);
index 8ae1445ffdf9cfda790d8524561c35532eaf22d3..c92f070bbb1140521db68a4be864097b4fcb6f6f 100644 (file)
@@ -6,6 +6,9 @@
 
 extern int mono_max_worker_threads;
 
+/* No managed code here */
+void mono_thread_pool_init (void);
+
 MonoAsyncResult *
 mono_thread_pool_add     (MonoObject *target, MonoMethodMessage *msg, 
                          MonoDelegate *async_callback, MonoObject *state);