1 // Copyright 2012 the V8 project authors. All rights reserved. 2 // Redistribution and use in source and binary forms, with or without 3 // modification, are permitted provided that the following conditions are 4 // met: 5 // 6 // * Redistributions of source code must retain the above copyright 7 // notice, this list of conditions and the following disclaimer. 8 // * Redistributions in binary form must reproduce the above 9 // copyright notice, this list of conditions and the following 10 // disclaimer in the documentation and/or other materials provided 11 // with the distribution. 12 // * Neither the name of Google Inc. nor the names of its 13 // contributors may be used to endorse or promote products derived 14 // from this software without specific prior written permission. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28 #ifndef V8_OPTIMIZING_COMPILER_THREAD_H_ 29 #define V8_OPTIMIZING_COMPILER_THREAD_H_ 30 31 #include "atomicops.h" 32 #include "flags.h" 33 #include "platform.h" 34 #include "unbound-queue-inl.h" 35 36 namespace v8 { 37 namespace internal { 38 39 class HOptimizedGraphBuilder; 40 class OptimizingCompiler; 41 class SharedFunctionInfo; 42 43 class OptimizingCompilerThread : public Thread { 44 public: 45 explicit OptimizingCompilerThread(Isolate *isolate) : 46 Thread("OptimizingCompilerThread"), 47 #ifdef DEBUG 48 thread_id_(0), 49 thread_id_mutex_(OS::CreateMutex()), 50 #endif 51 isolate_(isolate), 52 stop_semaphore_(OS::CreateSemaphore(0)), 53 input_queue_semaphore_(OS::CreateSemaphore(0)), 54 install_mutex_(OS::CreateMutex()), 55 time_spent_compiling_(0), 56 time_spent_total_(0) { 57 NoBarrier_Store(&stop_thread_, static_cast<AtomicWord>(CONTINUE)); 58 NoBarrier_Store(&queue_length_, static_cast<AtomicWord>(0)); 59 } 60 61 void Run(); 62 void Stop(); 63 void Flush(); 64 void QueueForOptimization(OptimizingCompiler* optimizing_compiler); 65 void InstallOptimizedFunctions(); 66 67 inline bool IsQueueAvailable() { 68 // We don't need a barrier since we have a data dependency right 69 // after. 70 Atomic32 current_length = NoBarrier_Load(&queue_length_); 71 72 // This can be queried only from the execution thread. 73 ASSERT(!IsOptimizerThread()); 74 // Since only the execution thread increments queue_length_ and 75 // only one thread can run inside an Isolate at one time, a direct 76 // doesn't introduce a race -- queue_length_ may decreased in 77 // meantime, but not increased. 78 return (current_length < FLAG_parallel_recompilation_queue_length); 79 } 80 81 #ifdef DEBUG 82 bool IsOptimizerThread(); 83 #endif 84 85 ~OptimizingCompilerThread() { 86 delete install_mutex_; 87 delete input_queue_semaphore_; 88 delete stop_semaphore_; 89 #ifdef DEBUG 90 delete thread_id_mutex_; 91 #endif 92 } 93 94 private: 95 enum StopFlag { CONTINUE, STOP, FLUSH }; 96 97 void FlushInputQueue(bool restore_function_code); 98 void FlushOutputQueue(bool restore_function_code); 99 100 void CompileNext(); 101 102 #ifdef DEBUG 103 int thread_id_; 104 Mutex* thread_id_mutex_; 105 #endif 106 107 Isolate* isolate_; 108 Semaphore* stop_semaphore_; 109 Semaphore* input_queue_semaphore_; 110 UnboundQueue<OptimizingCompiler*> input_queue_; 111 UnboundQueue<OptimizingCompiler*> output_queue_; 112 Mutex* install_mutex_; 113 volatile AtomicWord stop_thread_; 114 volatile Atomic32 queue_length_; 115 int64_t time_spent_compiling_; 116 int64_t time_spent_total_; 117 }; 118 119 } } // namespace v8::internal 120 121 #endif // V8_OPTIMIZING_COMPILER_THREAD_H_ 122