diff --git a/src/optimizing-compiler-thread.cc b/src/optimizing-compiler-thread.cc index 99f7bc6fdf..b9ff7d83ec 100644 --- a/src/optimizing-compiler-thread.cc +++ b/src/optimizing-compiler-thread.cc @@ -89,8 +89,9 @@ void OptimizingCompilerThread::CompileNext() { ASSERT(status != OptimizingCompiler::FAILED); // The function may have already been optimized by OSR. Simply continue. - // Mark it for installing before queuing so that we can be sure of the write - // order: marking first and (after being queued) installing code second. + // Use a mutex to make sure that functions marked for install + // are always also queued. + ScopedLock mark_and_queue(install_mutex_); { Heap::RelocationLock relocation_lock(isolate_->heap()); AllowHandleDereference ahd; optimizing_compiler->info()->closure()->MarkForInstallingRecompiledCode(); @@ -130,11 +131,13 @@ void OptimizingCompilerThread::Stop() { void OptimizingCompilerThread::InstallOptimizedFunctions() { ASSERT(!IsOptimizerThread()); HandleScope handle_scope(isolate_); - int functions_installed = 0; OptimizingCompiler* compiler; - while (output_queue_.Dequeue(&compiler)) { + while (true) { + { // Memory barrier to ensure marked functions are queued. + ScopedLock marked_and_queued(install_mutex_); + if (!output_queue_.Dequeue(&compiler)) return; + } Compiler::InstallOptimizedCode(compiler); - functions_installed++; } } diff --git a/src/optimizing-compiler-thread.h b/src/optimizing-compiler-thread.h index 8cb5e2dd59..004fce7ada 100644 --- a/src/optimizing-compiler-thread.h +++ b/src/optimizing-compiler-thread.h @@ -50,6 +50,7 @@ class OptimizingCompilerThread : public Thread { isolate_(isolate), stop_semaphore_(OS::CreateSemaphore(0)), input_queue_semaphore_(OS::CreateSemaphore(0)), + install_mutex_(OS::CreateMutex()), time_spent_compiling_(0), time_spent_total_(0) { NoBarrier_Store(&stop_thread_, static_cast(false)); @@ -95,6 +96,7 @@ class OptimizingCompilerThread : public Thread { Semaphore* input_queue_semaphore_; UnboundQueue input_queue_; UnboundQueue output_queue_; + Mutex* install_mutex_; volatile AtomicWord stop_thread_; volatile Atomic32 queue_length_; int64_t time_spent_compiling_; diff --git a/src/runtime.cc b/src/runtime.cc index 0046ff667e..fc6ebb8bb5 100644 --- a/src/runtime.cc +++ b/src/runtime.cc @@ -7912,12 +7912,7 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_InstallRecompiledCode) { ASSERT(args.length() == 1); CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); ASSERT(V8::UseCrankshaft() && FLAG_parallel_recompilation); - OptimizingCompilerThread* opt_thread = isolate->optimizing_compiler_thread(); - do { - // The function could have been marked for installing, but not queued just - // yet. In this case, retry until installed. - opt_thread->InstallOptimizedFunctions(); - } while (function->IsMarkedForInstallingRecompiledCode()); + isolate->optimizing_compiler_thread()->InstallOptimizedFunctions(); return function->code(); }