optimizing-compile-dispatcher.h 3.08 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 <queue>
9

10
#include "src/allocation.h"
11
#include "src/base/atomicops.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 CompilationJob;
22
class SharedFunctionInfo;
23

24
class V8_EXPORT_PRIVATE OptimizingCompileDispatcher {
25
 public:
26 27
  enum class BlockingBehavior { kBlock, kDontBlock };

28 29
  explicit OptimizingCompileDispatcher(Isolate* isolate)
      : isolate_(isolate),
30 31 32
        input_queue_capacity_(FLAG_concurrent_recompilation_queue_length),
        input_queue_length_(0),
        input_queue_shift_(0),
33
        blocked_jobs_(0),
34
        ref_count_(0),
35
        recompilation_delay_(FLAG_concurrent_recompilation_delay) {
36
    base::Relaxed_Store(&mode_, static_cast<base::AtomicWord>(COMPILE));
37
    input_queue_ = NewArray<CompilationJob*>(input_queue_capacity_);
38 39
  }

40
  ~OptimizingCompileDispatcher();
41

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

  inline bool IsQueueAvailable() {
50
    base::LockGuard<base::Mutex> access_input_queue(&input_queue_mutex_);
51 52 53
    return input_queue_length_ < input_queue_capacity_;
  }

54
  static bool Enabled() { return FLAG_concurrent_recompilation; }
55

56
 private:
57 58
  class CompileTask;

59
  enum ModeFlag { COMPILE, FLUSH };
60 61

  void FlushOutputQueue(bool restore_function_code);
62 63
  void CompileNext(CompilationJob* job);
  CompilationJob* NextInput(bool check_if_flushing = false);
64

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

72
  Isolate* isolate_;
73

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

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

87
  volatile base::AtomicWord mode_;
88

89
  int blocked_jobs_;
90

91 92 93
  int ref_count_;
  base::Mutex ref_count_mutex_;
  base::ConditionVariable ref_count_zero_;
94

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

105
#endif  // V8_COMPILER_DISPATCHER_OPTIMIZING_COMPILE_DISPATCHER_H_