1 // Copyright 2012 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_OPTIMIZING_COMPILE_DISPATCHER_H_ 6 #define V8_OPTIMIZING_COMPILE_DISPATCHER_H_ 7 8 #include <queue> 9 10 #include "src/base/atomicops.h" 11 #include "src/base/platform/condition-variable.h" 12 #include "src/base/platform/mutex.h" 13 #include "src/base/platform/platform.h" 14 #include "src/flags.h" 15 #include "src/list.h" 16 17 namespace v8 { 18 namespace internal { 19 20 class CompilationJob; 21 class SharedFunctionInfo; 22 23 class OptimizingCompileDispatcher { 24 public: OptimizingCompileDispatcher(Isolate * isolate)25 explicit OptimizingCompileDispatcher(Isolate* isolate) 26 : isolate_(isolate), 27 input_queue_capacity_(FLAG_concurrent_recompilation_queue_length), 28 input_queue_length_(0), 29 input_queue_shift_(0), 30 blocked_jobs_(0), 31 ref_count_(0), 32 recompilation_delay_(FLAG_concurrent_recompilation_delay) { 33 base::NoBarrier_Store(&mode_, static_cast<base::AtomicWord>(COMPILE)); 34 input_queue_ = NewArray<CompilationJob*>(input_queue_capacity_); 35 } 36 37 ~OptimizingCompileDispatcher(); 38 39 void Run(); 40 void Stop(); 41 void Flush(); 42 void QueueForOptimization(CompilationJob* job); 43 void Unblock(); 44 void InstallOptimizedFunctions(); 45 IsQueueAvailable()46 inline bool IsQueueAvailable() { 47 base::LockGuard<base::Mutex> access_input_queue(&input_queue_mutex_); 48 return input_queue_length_ < input_queue_capacity_; 49 } 50 Enabled()51 static bool Enabled() { return FLAG_concurrent_recompilation; } 52 53 private: 54 class CompileTask; 55 56 enum ModeFlag { COMPILE, FLUSH }; 57 58 void FlushOutputQueue(bool restore_function_code); 59 void CompileNext(CompilationJob* job); 60 CompilationJob* NextInput(bool check_if_flushing = false); 61 InputQueueIndex(int i)62 inline int InputQueueIndex(int i) { 63 int result = (i + input_queue_shift_) % input_queue_capacity_; 64 DCHECK_LE(0, result); 65 DCHECK_LT(result, input_queue_capacity_); 66 return result; 67 } 68 69 Isolate* isolate_; 70 71 // Circular queue of incoming recompilation tasks (including OSR). 72 CompilationJob** input_queue_; 73 int input_queue_capacity_; 74 int input_queue_length_; 75 int input_queue_shift_; 76 base::Mutex input_queue_mutex_; 77 78 // Queue of recompilation tasks ready to be installed (excluding OSR). 79 std::queue<CompilationJob*> output_queue_; 80 // Used for job based recompilation which has multiple producers on 81 // different threads. 82 base::Mutex output_queue_mutex_; 83 84 volatile base::AtomicWord mode_; 85 86 int blocked_jobs_; 87 88 int ref_count_; 89 base::Mutex ref_count_mutex_; 90 base::ConditionVariable ref_count_zero_; 91 92 // Copy of FLAG_concurrent_recompilation_delay that will be used from the 93 // background thread. 94 // 95 // Since flags might get modified while the background thread is running, it 96 // is not safe to access them directly. 97 int recompilation_delay_; 98 }; 99 } // namespace internal 100 } // namespace v8 101 102 #endif // V8_OPTIMIZING_COMPILE_DISPATCHER_H_ 103