• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_HEAP_COLLECTION_BARRIER_H_
6 #define V8_HEAP_COLLECTION_BARRIER_H_
7 
8 #include <atomic>
9 
10 #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 #include "src/logging/counters.h"
16 
17 namespace v8 {
18 namespace internal {
19 
20 class Heap;
21 
22 // This class stops and resumes all background threads waiting for GC.
23 class CollectionBarrier {
24  public:
CollectionBarrier(Heap * heap)25   explicit CollectionBarrier(Heap* heap) : heap_(heap) {}
26 
27   // Returns true when collection was requested.
28   bool WasGCRequested();
29 
30   // Requests a GC from the main thread. Returns whether GC was successfully
31   // requested. Requesting a GC can fail when isolate shutdown was already
32   // initiated.
33   bool TryRequestGC();
34 
35   // Resumes all threads waiting for GC when tear down starts.
36   void NotifyShutdownRequested();
37 
38   // Stops the TimeToCollection timer when starting the GC.
39   void StopTimeToCollectionTimer();
40 
41   // Resumes threads waiting for collection.
42   void ResumeThreadsAwaitingCollection();
43 
44   // Cancels collection if one was requested and resumes threads waiting for GC.
45   void CancelCollectionAndResumeThreads();
46 
47   // This is the method use by background threads to request and wait for GC.
48   // Returns whether a GC was performed.
49   bool AwaitCollectionBackground(LocalHeap* local_heap);
50 
51  private:
52   // Activate stack guards and posting a task to perform the GC.
53   void ActivateStackGuardAndPostTask();
54 
55   Heap* heap_;
56   base::Mutex mutex_;
57   base::ConditionVariable cv_wakeup_;
58   base::ElapsedTimer timer_;
59 
60   // Flag that main thread checks whether a GC was requested from the background
61   // thread.
62   std::atomic<bool> collection_requested_{false};
63 
64   // This flag is used to detect whether to block for the GC. Only set if the
65   // main thread was actually running and is unset when GC resumes background
66   // threads.
67   bool block_for_collection_ = false;
68 
69   // Set to true when a GC was performed, false in case it was canceled because
70   // the main thread parked itself without running the GC.
71   bool collection_performed_ = false;
72 
73   // Will be set as soon as Isolate starts tear down.
74   bool shutdown_requested_ = false;
75 };
76 
77 }  // namespace internal
78 }  // namespace v8
79 
80 #endif  // V8_HEAP_COLLECTION_BARRIER_H_
81