yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 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" |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 32 | #include "flags.h" |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 33 | #include "platform.h" |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 34 | #include "unbound-queue.h" |
| 35 | |
| 36 | namespace v8 { |
| 37 | namespace internal { |
| 38 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 39 | class HOptimizedGraphBuilder; |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 40 | class OptimizingCompiler; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 41 | class SharedFunctionInfo; |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 42 | |
| 43 | class OptimizingCompilerThread : public Thread { |
| 44 | public: |
| 45 | explicit OptimizingCompilerThread(Isolate *isolate) : |
| 46 | Thread("OptimizingCompilerThread"), |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 47 | #ifdef DEBUG |
| 48 | thread_id_(0), |
| 49 | #endif |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 50 | isolate_(isolate), |
| 51 | stop_semaphore_(OS::CreateSemaphore(0)), |
| 52 | input_queue_semaphore_(OS::CreateSemaphore(0)), |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 53 | output_queue_semaphore_(OS::CreateSemaphore(0)), |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 54 | time_spent_compiling_(0), |
| 55 | time_spent_total_(0) { |
| 56 | NoBarrier_Store(&stop_thread_, static_cast<AtomicWord>(false)); |
| 57 | NoBarrier_Store(&queue_length_, static_cast<AtomicWord>(0)); |
| 58 | } |
| 59 | |
| 60 | void Run(); |
| 61 | void Stop(); |
| 62 | void QueueForOptimization(OptimizingCompiler* optimizing_compiler); |
| 63 | void InstallOptimizedFunctions(); |
| 64 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 65 | // Wait for the next optimized function and install it. |
| 66 | Handle<SharedFunctionInfo> InstallNextOptimizedFunction(); |
| 67 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 68 | inline bool IsQueueAvailable() { |
| 69 | // We don't need a barrier since we have a data dependency right |
| 70 | // after. |
| 71 | Atomic32 current_length = NoBarrier_Load(&queue_length_); |
| 72 | |
| 73 | // This can be queried only from the execution thread. |
| 74 | ASSERT(!IsOptimizerThread()); |
| 75 | // Since only the execution thread increments queue_length_ and |
| 76 | // only one thread can run inside an Isolate at one time, a direct |
| 77 | // doesn't introduce a race -- queue_length_ may decreased in |
| 78 | // meantime, but not increased. |
| 79 | return (current_length < FLAG_parallel_recompilation_queue_length); |
| 80 | } |
| 81 | |
| 82 | #ifdef DEBUG |
| 83 | bool IsOptimizerThread(); |
| 84 | #endif |
| 85 | |
| 86 | ~OptimizingCompilerThread() { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 87 | delete output_queue_semaphore_; // Only used for manual mode. |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 88 | delete input_queue_semaphore_; |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 89 | delete stop_semaphore_; |
| 90 | } |
| 91 | |
| 92 | private: |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 93 | #ifdef DEBUG |
| 94 | int thread_id_; |
| 95 | #endif |
| 96 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 97 | Isolate* isolate_; |
| 98 | Semaphore* stop_semaphore_; |
| 99 | Semaphore* input_queue_semaphore_; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 100 | Semaphore* output_queue_semaphore_; |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 101 | UnboundQueue<OptimizingCompiler*> input_queue_; |
| 102 | UnboundQueue<OptimizingCompiler*> output_queue_; |
| 103 | volatile AtomicWord stop_thread_; |
| 104 | volatile Atomic32 queue_length_; |
| 105 | int64_t time_spent_compiling_; |
| 106 | int64_t time_spent_total_; |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | } } // namespace v8::internal |
| 110 | |
| 111 | #endif // V8_OPTIMIZING_COMPILER_THREAD_H_ |