collection-barrier.h 1.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2020 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_COLLECTION_BARRIER_H_
#define V8_HEAP_COLLECTION_BARRIER_H_

#include <atomic>

#include "src/base/optional.h"
11
#include "src/base/platform/condition-variable.h"
12
#include "src/base/platform/elapsed-timer.h"
13
#include "src/base/platform/mutex.h"
14
#include "src/heap/local-heap.h"
15 16 17 18 19 20 21 22 23 24
#include "src/logging/counters.h"

namespace v8 {
namespace internal {

class Heap;

// This class stops and resumes all background threads waiting for GC.
class CollectionBarrier {
 public:
25
  explicit CollectionBarrier(Heap* heap) : heap_(heap) {}
26 27

  // Returns true when collection was requested.
28 29 30 31
  bool WasGCRequested();

  // Requests a GC from the main thread.
  void RequestGC();
32

33 34
  // Resumes all threads waiting for GC when tear down starts.
  void NotifyShutdownRequested();
35

36
  // Stops the TimeToCollection timer when starting the GC.
37 38 39 40 41 42
  void StopTimeToCollectionTimer();

  // Resumes threads waiting for collection.
  void ResumeThreadsAwaitingCollection();

  // This is the method use by background threads to request and wait for GC.
43 44
  bool AwaitCollectionBackground(LocalHeap* local_heap);

45 46
 private:
  // Activate stack guards and posting a task to perform the GC.
47
  void ActivateStackGuardAndPostTask();
48 49 50 51 52 53 54 55

  Heap* heap_;
  base::Mutex mutex_;
  base::ConditionVariable cv_wakeup_;
  base::ElapsedTimer timer_;
  std::atomic<bool> collection_requested_{false};
  bool block_for_collection_ = false;
  bool shutdown_requested_ = false;
56 57 58 59 60 61
};

}  // namespace internal
}  // namespace v8

#endif  // V8_HEAP_COLLECTION_BARRIER_H_