blob: 7aad78ca5e86a25f6324ef7b8dbf476fc65e8153 [file] [log] [blame]
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001// 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.org304cc332012-07-24 07:59:48 +000032#include "flags.h"
yangguo@chromium.orgfb377212012-11-16 14:43:43 +000033#include "platform.h"
yangguo@chromium.org304cc332012-07-24 07:59:48 +000034#include "unbound-queue.h"
35
36namespace v8 {
37namespace internal {
38
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000039class HOptimizedGraphBuilder;
yangguo@chromium.org304cc332012-07-24 07:59:48 +000040class OptimizingCompiler;
yangguo@chromium.orgfb377212012-11-16 14:43:43 +000041class SharedFunctionInfo;
yangguo@chromium.org304cc332012-07-24 07:59:48 +000042
43class OptimizingCompilerThread : public Thread {
44 public:
45 explicit OptimizingCompilerThread(Isolate *isolate) :
46 Thread("OptimizingCompilerThread"),
danno@chromium.org1f34ad32012-11-26 14:53:56 +000047#ifdef DEBUG
48 thread_id_(0),
49#endif
yangguo@chromium.org304cc332012-07-24 07:59:48 +000050 isolate_(isolate),
51 stop_semaphore_(OS::CreateSemaphore(0)),
52 input_queue_semaphore_(OS::CreateSemaphore(0)),
yangguo@chromium.orgfb377212012-11-16 14:43:43 +000053 output_queue_semaphore_(OS::CreateSemaphore(0)),
yangguo@chromium.org304cc332012-07-24 07:59:48 +000054 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.orgfb377212012-11-16 14:43:43 +000065 // Wait for the next optimized function and install it.
66 Handle<SharedFunctionInfo> InstallNextOptimizedFunction();
67
yangguo@chromium.org304cc332012-07-24 07:59:48 +000068 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.orgfb377212012-11-16 14:43:43 +000087 delete output_queue_semaphore_; // Only used for manual mode.
danno@chromium.org1f34ad32012-11-26 14:53:56 +000088 delete input_queue_semaphore_;
yangguo@chromium.org304cc332012-07-24 07:59:48 +000089 delete stop_semaphore_;
90 }
91
92 private:
danno@chromium.org1f34ad32012-11-26 14:53:56 +000093#ifdef DEBUG
94 int thread_id_;
95#endif
96
yangguo@chromium.org304cc332012-07-24 07:59:48 +000097 Isolate* isolate_;
98 Semaphore* stop_semaphore_;
99 Semaphore* input_queue_semaphore_;
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000100 Semaphore* output_queue_semaphore_;
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000101 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.org304cc332012-07-24 07:59:48 +0000107};
108
109} } // namespace v8::internal
110
111#endif // V8_OPTIMIZING_COMPILER_THREAD_H_