[threadpool] Remove assertion on RequestWorkerThread failure (#4167)
[mono.git] / mono / sgen / sgen-workers.c
index f1212c658c4c755954fba3d3927e62742cfe836e..76eebdd86edfe066f5bfbdf1c6c51d6ad562372e 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;
 
@@ -87,7 +77,7 @@ state_is_working_or_enqueued (State state)
        return state == STATE_WORKING || state == STATE_WORK_ENQUEUED;
 }
 
-void
+static void
 sgen_workers_ensure_awake (void)
 {
        State old_state;
@@ -107,7 +97,7 @@ sgen_workers_ensure_awake (void)
 }
 
 static void
-worker_try_finish (void)
+worker_try_finish (WorkerData *data)
 {
        State old_state;
 
@@ -125,6 +115,8 @@ worker_try_finish (void)
        } while (!set_state (old_state, STATE_NOT_WORKING));
 
        binary_protocol_worker_finish (sgen_timestamp (), forced_stop);
+
+       sgen_gray_object_queue_trim_free_list (&data->private_gray_queue);
 }
 
 void
@@ -187,7 +179,8 @@ static void
 init_private_gray_queue (WorkerData *data)
 {
        sgen_gray_object_queue_init (&data->private_gray_queue,
-                       sgen_get_major_collector ()->is_concurrent ? concurrent_enqueue_check : NULL);
+                       sgen_get_major_collector ()->is_concurrent ? concurrent_enqueue_check : NULL,
+                       FALSE);
 }
 
 static void
@@ -230,7 +223,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 (data);
+               }
        }
 }
 
@@ -287,6 +286,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 ();
@@ -295,10 +296,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 ();
@@ -355,10 +357,29 @@ sgen_workers_are_working (void)
        return state_is_working_or_enqueued (workers_state);
 }
 
-SgenSectionGrayQueue*
-sgen_workers_get_distribute_section_gray_queue (void)
+void
+sgen_workers_assert_gray_queue_is_empty (void)
+{
+       SGEN_ASSERT (0, sgen_section_gray_queue_is_empty (&workers_distribute_gray_queue), "Why is the workers gray queue not empty?");
+}
+
+void
+sgen_workers_take_from_queue_and_awake (SgenGrayQueue *queue)
 {
-       return &workers_distribute_gray_queue;
+       gboolean wake = FALSE;
+
+       for (;;) {
+               GrayQueueSection *section = sgen_gray_object_dequeue_section (queue);
+               if (!section)
+                       break;
+               sgen_section_gray_queue_enqueue (&workers_distribute_gray_queue, section);
+               wake = TRUE;
+       }
+
+       if (wake) {
+               SGEN_ASSERT (0, sgen_concurrent_collection_in_progress (), "Why is there work to take when there's no concurrent collection in progress?");
+               sgen_workers_ensure_awake ();
+       }
 }
 
 #endif