Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / sgen / sgen-thread-pool.c
index 6a03bc3d69d164b5cd949b0b71760fc84f41d04b..c9aaaa2667b5cb9b45cf5b7427f43e8a8bd96060 100644 (file)
@@ -21,6 +21,7 @@ static mono_cond_t done_cond;
 
 static int threads_num;
 static MonoNativeThreadId threads [SGEN_THREADPOOL_MAX_NUM_THREADS];
+static int threads_context [SGEN_THREADPOOL_MAX_NUM_THREADS];
 
 static volatile gboolean threadpool_shutdown;
 static volatile int threads_finished;
@@ -166,8 +167,9 @@ get_work (int worker_index, int *work_context, int *do_idle, SgenThreadPoolJob *
 }
 
 static mono_native_thread_return_t
-thread_func (int worker_index)
+thread_func (void *data)
 {
+       int worker_index = (int)(gsize)data;
        int current_context;
        void *thread_data = NULL;
 
@@ -190,7 +192,9 @@ thread_func (int worker_index)
                SgenThreadPoolJob *job = NULL;
                SgenThreadPoolContext *context = NULL;
 
+               threads_context [worker_index] = -1;
                get_work (worker_index, &current_context, &do_idle, &job);
+               threads_context [worker_index] = current_context;
 
                if (!threadpool_shutdown) {
                        context = &pool_contexts [current_context];
@@ -301,6 +305,10 @@ sgen_thread_pool_shutdown (void)
        mono_os_mutex_destroy (&lock);
        mono_os_cond_destroy (&work_cond);
        mono_os_cond_destroy (&done_cond);
+
+       for (int i = 0; i < threads_num; i++) {
+               mono_threads_add_joinable_thread ((gpointer)threads [i]);
+       }
 }
 
 SgenThreadPoolJob*
@@ -358,13 +366,13 @@ sgen_thread_pool_idle_signal (int context_id)
 }
 
 void
-sgen_thread_pool_idle_wait (int context_id)
+sgen_thread_pool_idle_wait (int context_id, SgenThreadPoolContinueIdleWaitFunc continue_wait)
 {
        SGEN_ASSERT (0, pool_contexts [context_id].idle_job_func, "Why are we waiting for idle without an idle function?");
 
        mono_os_mutex_lock (&lock);
 
-       while (pool_contexts [context_id].continue_idle_job_func (NULL, context_id))
+       while (continue_wait (context_id, threads_context))
                mono_os_cond_wait (&done_cond, &lock);
 
        mono_os_mutex_unlock (&lock);