summaryrefslogtreecommitdiff
path: root/chromium/v8/src/heap/progress-bar.h
blob: b00558b684cec540c1d5bd41978124c4ca3e6db3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_HEAP_PROGRESS_BAR_H_
#define V8_HEAP_PROGRESS_BAR_H_

#include <atomic>
#include <cstdint>

#include "src/base/logging.h"

namespace v8 {
namespace internal {

// The progress bar allows for keeping track of the bytes processed of a single
// object. The progress bar itself must be enabled before it's used.
//
// Only large objects use the progress bar which is stored in their page header.
// These objects are scanned in increments and will be kept black while being
// scanned. Even if the mutator writes to them they will be kept black and a
// white to grey transition is performed in the value.
//
// The progress bar starts as disabled. After enabling (through `Enable()`), it
// can never be disabled again.
class ProgressBar final {
 public:
  void Initialize() { value_ = kDisabledSentinel; }
  void Enable() { value_ = 0; }
  bool IsEnabled() const {
    return value_.load(std::memory_order_acquire) != kDisabledSentinel;
  }

  size_t Value() const {
    DCHECK(IsEnabled());
    return value_.load(std::memory_order_acquire);
  }

  bool TrySetNewValue(size_t old_value, size_t new_value) {
    DCHECK(IsEnabled());
    DCHECK_NE(kDisabledSentinel, new_value);
    return value_.compare_exchange_strong(old_value, new_value,
                                          std::memory_order_acq_rel);
  }

  void ResetIfEnabled() {
    if (IsEnabled()) {
      value_.store(0, std::memory_order_release);
    }
  }

 private:
  static constexpr size_t kDisabledSentinel = SIZE_MAX;

  std::atomic<size_t> value_;
};

}  // namespace internal
}  // namespace v8

#endif  // V8_HEAP_PROGRESS_BAR_H_