optimizing-compile-dispatcher.h 2.87 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 7 8
#ifndef V8_OPTIMIZING_COMPILE_DISPATCHER_H_
#define V8_OPTIMIZING_COMPILE_DISPATCHER_H_

#include <queue>
9

10
#include "src/base/atomicops.h"
11
#include "src/base/platform/condition-variable.h"
12 13
#include "src/base/platform/mutex.h"
#include "src/base/platform/platform.h"
14 15
#include "src/flags.h"
#include "src/list.h"
16 17 18 19

namespace v8 {
namespace internal {

20
class CompilationJob;
21
class SharedFunctionInfo;
22

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

37
  ~OptimizingCompileDispatcher();
38

39 40
  void Run();
  void Stop();
41
  void Flush();
42
  void QueueForOptimization(CompilationJob* job);
43
  void Unblock();
44 45 46
  void InstallOptimizedFunctions();

  inline bool IsQueueAvailable() {
47
    base::LockGuard<base::Mutex> access_input_queue(&input_queue_mutex_);
48 49 50
    return input_queue_length_ < input_queue_capacity_;
  }

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

53
 private:
54 55
  class CompileTask;

56
  enum ModeFlag { COMPILE, FLUSH };
57 58

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

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

69
  Isolate* isolate_;
70

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

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

84
  volatile base::AtomicWord mode_;
85

86
  int blocked_jobs_;
87

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

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

102
#endif  // V8_OPTIMIZING_COMPILE_DISPATCHER_H_