optimizing-compile-dispatcher.h 3.01 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#ifndef V8_COMPILER_DISPATCHER_OPTIMIZING_COMPILE_DISPATCHER_H_
#define V8_COMPILER_DISPATCHER_OPTIMIZING_COMPILE_DISPATCHER_H_
7

8
#include <atomic>
9
#include <queue>
10

11
#include "src/allocation.h"
12
#include "src/base/platform/condition-variable.h"
13 14
#include "src/base/platform/mutex.h"
#include "src/base/platform/platform.h"
15
#include "src/flags.h"
16
#include "src/globals.h"
17 18 19 20

namespace v8 {
namespace internal {

21
class OptimizedCompilationJob;
22
class SharedFunctionInfo;
23

24
class V8_EXPORT_PRIVATE OptimizingCompileDispatcher {
25
 public:
26 27
  explicit OptimizingCompileDispatcher(Isolate* isolate)
      : isolate_(isolate),
28 29 30
        input_queue_capacity_(FLAG_concurrent_recompilation_queue_length),
        input_queue_length_(0),
        input_queue_shift_(0),
31
        mode_(COMPILE),
32
        blocked_jobs_(0),
33
        ref_count_(0),
34
        recompilation_delay_(FLAG_concurrent_recompilation_delay) {
35
    input_queue_ = NewArray<OptimizedCompilationJob*>(input_queue_capacity_);
36 37
  }

38
  ~OptimizingCompileDispatcher();
39

40
  void Stop();
41
  void Flush(BlockingBehavior blocking_behavior);
42
  // Takes ownership of |job|.
43
  void QueueForOptimization(OptimizedCompilationJob* job);
44
  void Unblock();
45 46 47
  void InstallOptimizedFunctions();

  inline bool IsQueueAvailable() {
48
    base::MutexGuard access_input_queue(&input_queue_mutex_);
49 50 51
    return input_queue_length_ < input_queue_capacity_;
  }

52
  static bool Enabled() { return FLAG_concurrent_recompilation; }
53

54
 private:
55 56
  class CompileTask;

57
  enum ModeFlag { COMPILE, FLUSH };
58 59

  void FlushOutputQueue(bool restore_function_code);
60 61
  void CompileNext(OptimizedCompilationJob* job);
  OptimizedCompilationJob* NextInput(bool check_if_flushing = false);
62

63 64
  inline int InputQueueIndex(int i) {
    int result = (i + input_queue_shift_) % input_queue_capacity_;
65 66
    DCHECK_LE(0, result);
    DCHECK_LT(result, input_queue_capacity_);
67
    return result;
68 69
  }

70
  Isolate* isolate_;
71

72
  // Circular queue of incoming recompilation tasks (including OSR).
73
  OptimizedCompilationJob** input_queue_;
74 75 76
  int input_queue_capacity_;
  int input_queue_length_;
  int input_queue_shift_;
77
  base::Mutex input_queue_mutex_;
78

79
  // Queue of recompilation tasks ready to be installed (excluding OSR).
80
  std::queue<OptimizedCompilationJob*> output_queue_;
81 82 83
  // Used for job based recompilation which has multiple producers on
  // different threads.
  base::Mutex output_queue_mutex_;
84

85
  std::atomic<ModeFlag> mode_;
86

87
  int blocked_jobs_;
88

89 90 91
  int ref_count_;
  base::Mutex ref_count_mutex_;
  base::ConditionVariable ref_count_zero_;
92

93 94
  // Copy of FLAG_concurrent_recompilation_delay that will be used from the
  // background thread.
95 96 97
  //
  // Since flags might get modified while the background thread is running, it
  // is not safe to access them directly.
98
  int recompilation_delay_;
99
};
100 101
}  // namespace internal
}  // namespace v8
102

103
#endif  // V8_COMPILER_DISPATCHER_OPTIMIZING_COMPILE_DISPATCHER_H_