X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mono%2Fsgen%2Fsgen-workers.c;h=adc600a090f3dc801f8d1b16b4796acb28a69988;hb=6b8e96c44d40fa68eff6032445b3767585348b5a;hp=e7a003bcad1b523b8f9bd06f8b46b144b55bde3f;hpb=371944bed96981702f5e7a32b30b9fc214db4fbf;p=mono.git diff --git a/mono/sgen/sgen-workers.c b/mono/sgen/sgen-workers.c index e7a003bcad1..adc600a090f 100644 --- a/mono/sgen/sgen-workers.c +++ b/mono/sgen/sgen-workers.c @@ -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" @@ -31,6 +20,7 @@ #include "mono/sgen/sgen-client.h" static int workers_num; +static volatile gboolean forced_stop; static WorkerData *workers_data; static SgenSectionGrayQueue workers_distribute_gray_queue; @@ -39,12 +29,11 @@ static gboolean workers_distribute_gray_queue_inited; /* * Allowed transitions: * - * | from \ to | NOT WORKING | WORKING | WORK ENQUEUED | NURSERY COLLECTION | - * |--------------------+-------------+---------+---------------+--------------------| - * | NOT WORKING | - | - | main | main | - * | WORKING | worker | - | main | main | - * | WORK ENQUEUED | - | worker | - | main | - * | NURSERY COLLECTION | - | - | main | - | + * | from \ to | NOT WORKING | WORKING | WORK ENQUEUED | + * |--------------------+-------------+---------+---------------+ + * | NOT WORKING | - | - | main | + * | WORKING | worker | - | main | + * | WORK ENQUEUED | - | worker | - | * * The WORK ENQUEUED state guarantees that the worker thread will inspect the queue again at * least once. Only after looking at the queue will it go back to WORKING, and then, @@ -56,8 +45,7 @@ static gboolean workers_distribute_gray_queue_inited; enum { STATE_NOT_WORKING, STATE_WORKING, - STATE_WORK_ENQUEUED, - STATE_NURSERY_COLLECTION + STATE_WORK_ENQUEUED }; typedef gint32 State; @@ -65,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; @@ -82,20 +71,14 @@ set_state (State old_state, State new_state) return InterlockedCompareExchange (&workers_state, new_state, old_state) == old_state; } -static void -assert_nursery_collection (State state) -{ - SGEN_ASSERT (0, state == STATE_NURSERY_COLLECTION, "Must be in the nursery collection state"); -} - static gboolean state_is_working_or_enqueued (State state) { return state == STATE_WORKING || state == STATE_WORK_ENQUEUED; } -static void -workers_signal_enqueue_work (gboolean from_nursery_collection) +void +sgen_workers_ensure_awake (void) { State old_state; gboolean did_set_state; @@ -103,30 +86,16 @@ workers_signal_enqueue_work (gboolean from_nursery_collection) do { old_state = workers_state; - if (from_nursery_collection) - assert_nursery_collection (old_state); - else - SGEN_ASSERT (0, old_state != STATE_NURSERY_COLLECTION, "If we're not in a nursery collection, how come the state is NURSERY COLLECTION?"); - if (old_state == STATE_WORK_ENQUEUED) break; did_set_state = set_state (old_state, STATE_WORK_ENQUEUED); - if (from_nursery_collection) - SGEN_ASSERT (0, did_set_state, "Nobody else should be mutating the state"); } while (!did_set_state); if (!state_is_working_or_enqueued (old_state)) sgen_thread_pool_idle_signal (); } -void -sgen_workers_ensure_awake (void) -{ - SGEN_ASSERT (0, workers_state != STATE_NURSERY_COLLECTION, "Can't wake workers during nursery collection"); - workers_signal_enqueue_work (FALSE); -} - static void worker_try_finish (void) { @@ -138,26 +107,20 @@ worker_try_finish (void) old_state = workers_state; SGEN_ASSERT (0, old_state != STATE_NOT_WORKING, "How did we get from doing idle work to NOT WORKING without setting it ourselves?"); - if (old_state == STATE_NURSERY_COLLECTION) - return; if (old_state == STATE_WORK_ENQUEUED) return; SGEN_ASSERT (0, old_state == STATE_WORKING, "What other possibility is there?"); /* We are the last thread to go to sleep. */ } while (!set_state (old_state, STATE_NOT_WORKING)); -} -static gboolean -collection_needs_workers (void) -{ - return sgen_collection_is_concurrent (); + binary_protocol_worker_finish (sgen_timestamp (), forced_stop); } void -sgen_workers_enqueue_job (SgenThreadPoolJob *job) +sgen_workers_enqueue_job (SgenThreadPoolJob *job, gboolean enqueue) { - if (!collection_needs_workers ()) { + if (!enqueue) { job->func (NULL, job); sgen_thread_pool_job_free (job); return; @@ -180,30 +143,6 @@ sgen_workers_wait_for_jobs_finished (void) sgen_workers_ensure_awake (); } -void -sgen_workers_signal_start_nursery_collection_and_wait (void) -{ - State old_state; - - do { - old_state = workers_state; - - if (old_state != STATE_NOT_WORKING) - SGEN_ASSERT (0, old_state != STATE_NURSERY_COLLECTION, "Why are we transitioning to NURSERY COLLECTION when we're already there?"); - } while (!set_state (old_state, STATE_NURSERY_COLLECTION)); - - sgen_thread_pool_idle_wait (); - - assert_nursery_collection (workers_state); -} - -void -sgen_workers_signal_finish_nursery_collection (void) -{ - assert_nursery_collection (workers_state); - workers_signal_enqueue_work (TRUE); -} - static gboolean workers_get_work (WorkerData *data) { @@ -227,7 +166,7 @@ workers_get_work (WorkerData *data) } static void -concurrent_enqueue_check (char *obj) +concurrent_enqueue_check (GCObject *obj) { g_assert (sgen_concurrent_collection_in_progress ()); g_assert (!sgen_ptr_in_nursery (obj)); @@ -244,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 (); @@ -264,27 +203,30 @@ continue_idle_func (void) static void marker_idle_func (void *data_untyped) { - WorkerData *data = data_untyped; - - if (!continue_idle_func ()) - return; + 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."); - SGEN_ASSERT (0, sgen_get_current_collection_generation () != GENERATION_NURSERY, "Why are we doing work while there's a nursery collection happening?"); if (workers_state == STATE_WORK_ENQUEUED) { set_state (STATE_WORK_ENQUEUED, STATE_WORKING); SGEN_ASSERT (0, workers_state != STATE_NOT_WORKING, "How did we get from WORK ENQUEUED to NOT WORKING?"); } - if (!sgen_gray_object_queue_is_empty (&data->private_gray_queue) || workers_get_work (data)) { + if (!forced_stop && (!sgen_gray_object_queue_is_empty (&data->private_gray_queue) || workers_get_work (data))) { ScanCopyContext ctx = CONTEXT_FROM_OBJECT_OPERATIONS (idle_func_object_ops, &data->private_gray_queue); SGEN_ASSERT (0, !sgen_gray_object_queue_is_empty (&data->private_gray_queue), "How is our gray queue empty if we just got work?"); - sgen_drain_gray_stack (32, ctx); + 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 (); + } } } @@ -305,7 +247,7 @@ init_distribute_gray_queue (void) void sgen_workers_init_distribute_gray_queue (void) { - SGEN_ASSERT (0, sgen_get_major_collector ()->is_concurrent && collection_needs_workers (), + SGEN_ASSERT (0, sgen_get_major_collector ()->is_concurrent, "Why should we init the distribute gray queue if we don't need it?"); init_distribute_gray_queue (); } @@ -314,7 +256,7 @@ void sgen_workers_init (int num_workers) { int i; - void *workers_data_ptrs [num_workers]; + 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); @@ -325,13 +267,13 @@ 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 (); for (i = 0; i < workers_num; ++i) - workers_data_ptrs [i] = &workers_data [i]; + workers_data_ptrs [i] = (void *) &workers_data [i]; sgen_thread_pool_init (num_workers, thread_pool_init_func, marker_idle_func, continue_idle_func, workers_data_ptrs); @@ -339,15 +281,26 @@ sgen_workers_init (int num_workers) } void -sgen_workers_start_all_workers (SgenObjectOperations *object_ops) +sgen_workers_stop_all_workers (void) { - if (!collection_needs_workers ()) - return; + preclean_job = NULL; + mono_memory_write_barrier (); + forced_stop = TRUE; + + sgen_thread_pool_wait_for_all_jobs (); + sgen_thread_pool_idle_wait (); + SGEN_ASSERT (0, workers_state == STATE_NOT_WORKING, "Can only signal enqueue work when in no work state"); +} +void +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 (); - workers_signal_enqueue_work (FALSE); + sgen_workers_ensure_awake (); } void @@ -355,11 +308,6 @@ sgen_workers_join (void) { int i; - SGEN_ASSERT (0, workers_state != STATE_NURSERY_COLLECTION, "Can't be in nursery collection when joining"); - - if (!collection_needs_workers ()) - return; - sgen_thread_pool_wait_for_all_jobs (); sgen_thread_pool_idle_wait (); SGEN_ASSERT (0, workers_state == STATE_NOT_WORKING, "Can only signal enqueue work when in no work state"); @@ -371,6 +319,28 @@ sgen_workers_join (void) SGEN_ASSERT (0, sgen_gray_object_queue_is_empty (&workers_data [i].private_gray_queue), "Why is there still work left to do?"); } +/* + * Can only be called if the workers are stopped. + * If we're stopped, there are also no pending jobs. + */ +gboolean +sgen_workers_have_idle_work (void) +{ + int i; + + SGEN_ASSERT (0, forced_stop && sgen_workers_all_done (), "Checking for idle work should only happen if the workers are stopped."); + + if (!sgen_section_gray_queue_is_empty (&workers_distribute_gray_queue)) + return TRUE; + + for (i = 0; i < workers_num; ++i) { + if (!sgen_gray_object_queue_is_empty (&workers_data [i].private_gray_queue)) + return TRUE; + } + + return FALSE; +} + gboolean sgen_workers_all_done (void) { @@ -384,13 +354,6 @@ sgen_workers_are_working (void) return state_is_working_or_enqueued (workers_state); } -void -sgen_workers_wait (void) -{ - sgen_thread_pool_idle_wait (); - SGEN_ASSERT (0, sgen_workers_all_done (), "Why are the workers not done after we wait for them?"); -} - SgenSectionGrayQueue* sgen_workers_get_distribute_section_gray_queue (void) {