[sgen] Always use one concurrent worker
[mono.git] / mono / sgen / sgen-thread-pool.c
index 056c248789e60ff668ce414e6be81d5fb7e4836d..20c5e17d3d7464fc90ac156b0efdaeb3d45b8b6a 100644 (file)
@@ -3,18 +3,7 @@
  *
  * Copyright (C) 2015 Xamarin Inc
  *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License 2.0 as published by the Free Software Foundation;
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public
- * License 2.0 along with this library; if not, write to the Free
- * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 
 #include "config.h"
 #include "mono/utils/mono-threads.h"
 #endif
 
+#define MAX_NUM_THREADS 8
+
 static mono_mutex_t lock;
 static mono_cond_t work_cond;
 static mono_cond_t done_cond;
 
-static MonoNativeThreadId thread;
+static int threads_num = 0;
+static MonoNativeThreadId threads [MAX_NUM_THREADS];
 
 /* Only accessed with the lock held. */
 static SgenPointerQueue job_queue;
@@ -40,6 +32,10 @@ static SgenPointerQueue job_queue;
 static SgenThreadPoolThreadInitFunc thread_init_func;
 static SgenThreadPoolIdleJobFunc idle_job_func;
 static SgenThreadPoolContinueIdleJobFunc continue_idle_job_func;
+static SgenThreadPoolShouldWorkFunc should_work_func;
+
+static volatile gboolean threadpool_shutdown;
+static volatile int threads_finished = 0;
 
 enum {
        STATE_WAITING,
@@ -52,7 +48,7 @@ static SgenThreadPoolJob*
 get_job_and_set_in_progress (void)
 {
        for (size_t i = 0; i < job_queue.next_slot; ++i) {
-               SgenThreadPoolJob *job = job_queue.data [i];
+               SgenThreadPoolJob *job = (SgenThreadPoolJob *)job_queue.data [i];
                if (job->state == STATE_WAITING) {
                        job->state = STATE_IN_PROGRESS;
                        return job;
@@ -86,11 +82,19 @@ remove_job (SgenThreadPoolJob *job)
 }
 
 static gboolean
-continue_idle_job (void)
+continue_idle_job (void *thread_data)
 {
        if (!continue_idle_job_func)
                return FALSE;
-       return continue_idle_job_func ();
+       return continue_idle_job_func (thread_data);
+}
+
+static gboolean
+should_work (void *thread_data)
+{
+       if (!should_work_func)
+               return TRUE;
+       return should_work_func (thread_data);
 }
 
 static mono_native_thread_return_t
@@ -100,16 +104,23 @@ thread_func (void *thread_data)
 
        mono_os_mutex_lock (&lock);
        for (;;) {
+               gboolean do_idle;
+               SgenThreadPoolJob *job;
+
+               if (!should_work (thread_data)) {
+                       mono_os_cond_wait (&work_cond, &lock);
+                       continue;
+               }
                /*
                 * It's important that we check the continue idle flag with the lock held.
                 * Suppose we didn't check with the lock held, and the result is FALSE.  The
                 * main thread might then set continue idle and signal us before we can take
                 * the lock, and we'd lose the signal.
                 */
-               gboolean do_idle = continue_idle_job ();
-               SgenThreadPoolJob *job = get_job_and_set_in_progress ();
+               do_idle = continue_idle_job (thread_data);
+               job = get_job_and_set_in_progress ();
 
-               if (!job && !do_idle) {
+               if (!job && !do_idle && !threadpool_shutdown) {
                        /*
                         * pthread_cond_wait() can return successfully despite the condition
                         * not being signalled, so we have to run this in a loop until we
@@ -134,18 +145,24 @@ thread_func (void *thread_data)
                         * have to broadcast.
                         */
                        mono_os_cond_signal (&done_cond);
-               } else {
-                       SGEN_ASSERT (0, do_idle, "Why did we unlock if we still have to wait for idle?");
+               } else if (do_idle) {
                        SGEN_ASSERT (0, idle_job_func, "Why do we have idle work when there's no idle job function?");
                        do {
                                idle_job_func (thread_data);
-                               do_idle = continue_idle_job ();
+                               do_idle = continue_idle_job (thread_data);
                        } while (do_idle && !job_queue.next_slot);
 
                        mono_os_mutex_lock (&lock);
 
                        if (!do_idle)
                                mono_os_cond_signal (&done_cond);
+               } else {
+                       SGEN_ASSERT (0, threadpool_shutdown, "Why did we unlock if no jobs and not shutting down?");
+                       mono_os_mutex_lock (&lock);
+                       threads_finished++;
+                       mono_os_cond_signal (&done_cond);
+                       mono_os_mutex_unlock (&lock);
+                       return 0;
                }
        }
 
@@ -153,9 +170,11 @@ thread_func (void *thread_data)
 }
 
 void
-sgen_thread_pool_init (int num_threads, SgenThreadPoolThreadInitFunc init_func, SgenThreadPoolIdleJobFunc idle_func, SgenThreadPoolContinueIdleJobFunc continue_idle_func, void **thread_datas)
+sgen_thread_pool_init (int num_threads, SgenThreadPoolThreadInitFunc init_func, SgenThreadPoolIdleJobFunc idle_func, SgenThreadPoolContinueIdleJobFunc continue_idle_func, SgenThreadPoolShouldWorkFunc should_work_func_p, void **thread_datas)
 {
-       SGEN_ASSERT (0, num_threads == 1, "We only support 1 thread pool thread for now.");
+       int i;
+
+       threads_num = (num_threads < MAX_NUM_THREADS) ? num_threads : MAX_NUM_THREADS;
 
        mono_os_mutex_init (&lock);
        mono_os_cond_init (&work_cond);
@@ -164,14 +183,34 @@ sgen_thread_pool_init (int num_threads, SgenThreadPoolThreadInitFunc init_func,
        thread_init_func = init_func;
        idle_job_func = idle_func;
        continue_idle_job_func = continue_idle_func;
+       should_work_func = should_work_func_p;
+
+       for (i = 0; i < threads_num; i++)
+               mono_native_thread_create (&threads [i], thread_func, thread_datas ? thread_datas [i] : NULL);
+}
+
+void
+sgen_thread_pool_shutdown (void)
+{
+       if (!threads_num)
+               return;
 
-       mono_native_thread_create (&thread, thread_func, thread_datas ? thread_datas [0] : NULL);
+       mono_os_mutex_lock (&lock);
+       threadpool_shutdown = TRUE;
+       mono_os_cond_broadcast (&work_cond);
+       while (threads_finished < threads_num)
+               mono_os_cond_wait (&done_cond, &lock);
+       mono_os_mutex_unlock (&lock);
+
+       mono_os_mutex_destroy (&lock);
+       mono_os_cond_destroy (&work_cond);
+       mono_os_cond_destroy (&done_cond);
 }
 
 SgenThreadPoolJob*
 sgen_thread_pool_job_alloc (const char *name, SgenThreadPoolJobFunc func, size_t size)
 {
-       SgenThreadPoolJob *job = sgen_alloc_internal_dynamic (size, INTERNAL_MEM_THREAD_POOL_JOB, TRUE);
+       SgenThreadPoolJob *job = (SgenThreadPoolJob *)sgen_alloc_internal_dynamic (size, INTERNAL_MEM_THREAD_POOL_JOB, TRUE);
        job->name = name;
        job->size = size;
        job->state = STATE_WAITING;
@@ -191,10 +230,6 @@ sgen_thread_pool_job_enqueue (SgenThreadPoolJob *job)
        mono_os_mutex_lock (&lock);
 
        sgen_pointer_queue_add (&job_queue, job);
-       /*
-        * FIXME: We could check whether there is a job in progress.  If there is, there's
-        * no need to signal the condition, at least as long as we have only one thread.
-        */
        mono_os_cond_signal (&work_cond);
 
        mono_os_mutex_unlock (&lock);
@@ -220,8 +255,8 @@ sgen_thread_pool_idle_signal (void)
 
        mono_os_mutex_lock (&lock);
 
-       if (continue_idle_job_func ())
-               mono_os_cond_signal (&work_cond);
+       if (continue_idle_job_func (NULL))
+               mono_os_cond_broadcast (&work_cond);
 
        mono_os_mutex_unlock (&lock);
 }
@@ -233,7 +268,7 @@ sgen_thread_pool_idle_wait (void)
 
        mono_os_mutex_lock (&lock);
 
-       while (continue_idle_job_func ())
+       while (continue_idle_job_func (NULL))
                mono_os_cond_wait (&done_cond, &lock);
 
        mono_os_mutex_unlock (&lock);
@@ -250,10 +285,18 @@ sgen_thread_pool_wait_for_all_jobs (void)
        mono_os_mutex_unlock (&lock);
 }
 
-gboolean
+/* Return 0 if is not a thread pool thread or the thread number otherwise */
+int
 sgen_thread_pool_is_thread_pool_thread (MonoNativeThreadId some_thread)
 {
-       return some_thread == thread;
+       int i;
+
+       for (i = 0; i < threads_num; i++) {
+               if (some_thread == threads [i])
+                       return i + 1;
+       }
+
+       return 0;
 }
 
 #endif