Merge pull request #3199 from lambdageek/dev/proxy-setter
[mono.git] / mono / sgen / sgen-workers.c
index 40d0ed103595b9a75ba1a338599267e5a80e44d5..adc600a090f3dc801f8d1b16b4796acb28a69988 100644 (file)
@@ -5,18 +5,7 @@
  * Copyright 2003-2010 Novell, Inc.
  * Copyright (C) 2012 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"
@@ -64,6 +53,7 @@ typedef gint32 State;
 static volatile State workers_state;
 
 static SgenObjectOperations * volatile idle_func_object_ops;
+static SgenThreadPoolJob * volatile preclean_job;
 
 static guint64 stat_workers_num_finished;
 
@@ -123,6 +113,8 @@ worker_try_finish (void)
 
                /* We are the last thread to go to sleep. */
        } while (!set_state (old_state, STATE_NOT_WORKING));
+
+       binary_protocol_worker_finish (sgen_timestamp (), forced_stop);
 }
 
 void
@@ -191,7 +183,7 @@ init_private_gray_queue (WorkerData *data)
 static void
 thread_pool_init_func (void *data_untyped)
 {
-       WorkerData *data = data_untyped;
+       WorkerData *data = (WorkerData *)data_untyped;
        SgenMajorCollector *major = sgen_get_major_collector ();
 
        sgen_client_thread_register_worker ();
@@ -211,7 +203,7 @@ continue_idle_func (void)
 static void
 marker_idle_func (void *data_untyped)
 {
-       WorkerData *data = data_untyped;
+       WorkerData *data = (WorkerData *)data_untyped;
 
        SGEN_ASSERT (0, continue_idle_func (), "Why are we called when we're not supposed to work?");
        SGEN_ASSERT (0, sgen_concurrent_collection_in_progress (), "The worker should only mark in concurrent collections.");
@@ -228,7 +220,13 @@ marker_idle_func (void *data_untyped)
 
                sgen_drain_gray_stack (ctx);
        } else {
-               worker_try_finish ();
+               SgenThreadPoolJob *job = preclean_job;
+               if (job) {
+                       sgen_thread_pool_job_enqueue (job);
+                       preclean_job = NULL;
+               } else {
+                       worker_try_finish ();
+               }
        }
 }
 
@@ -258,7 +256,7 @@ void
 sgen_workers_init (int num_workers)
 {
        int i;
-       void **workers_data_ptrs = alloca(num_workers * sizeof(void *));
+       void **workers_data_ptrs = (void **)alloca(num_workers * sizeof(void *));
 
        if (!sgen_get_major_collector ()->is_concurrent) {
                sgen_thread_pool_init (num_workers, thread_pool_init_func, NULL, NULL, NULL);
@@ -269,7 +267,7 @@ sgen_workers_init (int num_workers)
 
        workers_num = num_workers;
 
-       workers_data = sgen_alloc_internal_dynamic (sizeof (WorkerData) * num_workers, INTERNAL_MEM_WORKER_DATA, TRUE);
+       workers_data = (WorkerData *)sgen_alloc_internal_dynamic (sizeof (WorkerData) * num_workers, INTERNAL_MEM_WORKER_DATA, TRUE);
        memset (workers_data, 0, sizeof (WorkerData) * num_workers);
 
        init_distribute_gray_queue ();
@@ -285,6 +283,8 @@ sgen_workers_init (int num_workers)
 void
 sgen_workers_stop_all_workers (void)
 {
+       preclean_job = NULL;
+       mono_memory_write_barrier ();
        forced_stop = TRUE;
 
        sgen_thread_pool_wait_for_all_jobs ();
@@ -293,10 +293,11 @@ sgen_workers_stop_all_workers (void)
 }
 
 void
-sgen_workers_start_all_workers (SgenObjectOperations *object_ops)
+sgen_workers_start_all_workers (SgenObjectOperations *object_ops, SgenThreadPoolJob *job)
 {
        forced_stop = FALSE;
        idle_func_object_ops = object_ops;
+       preclean_job = job;
        mono_memory_write_barrier ();
 
        sgen_workers_ensure_awake ();