Debian Codegraph @

js/src/gc/ParallelMarking.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef gc_ParallelMarking_h
8 #define gc_ParallelMarking_h
9
10 #include "mozilla/Atomics.h"
11 #include "mozilla/DoublyLinkedList.h"
12 #include "mozilla/TimeStamp.h"
13
14 #include "gc/GCMarker.h"
15 #include "gc/GCParallelTask.h"
16 #include "js/HeapAPI.h"
17 #include "js/SliceBudget.h"
18 #include "threading/ConditionVariable.h"
19 #include "threading/ProtectedData.h"
20
21 namespace js {
22
23 class AutoLockHelperThreadState;
24
25 namespace gc {
26
27 class ParallelMarkTask;
28
29 // Per-runtime parallel marking state.
30 //
31 // This class is used on the main thread and coordinates parallel marking using
32 // several helper threads running ParallelMarkTasks.
33 //
34 // This uses a work-requesting approach. Threads mark until they run out of
35 // work and then add themselves to a list of waiting tasks and block. Running
36 // tasks with enough work may donate work to a waiting task and resume it.
37 class MOZ_STACK_CLASS ParallelMarker {
38 public:
39 explicit ParallelMarker(GCRuntime* gc);
40
41 bool mark(const JS::SliceBudget& sliceBudget);
42
43 using AtomicCount = mozilla::Atomic<uint32_t, mozilla::Relaxed>;
44 AtomicCount& waitingTaskCountRef() { return waitingTaskCount; }
45 bool hasWaitingTasks() { return waitingTaskCount != 0; }
46 void donateWorkFrom(GCMarker* src);
47
48 private:
49 bool markOneColor(MarkColor color, const JS::SliceBudget& sliceBudget);
50
51 bool hasWork(MarkColor color) const;
52
53 void addTask(ParallelMarkTask* task, const AutoLockHelperThreadState& lock);
54
55 void addTaskToWaitingList(ParallelMarkTask* task,
56 const AutoLockHelperThreadState& lock);
57 #ifdef DEBUG
58 bool isTaskInWaitingList(const ParallelMarkTask* task,
59 const AutoLockHelperThreadState& lock) const;
60 #endif
61
62 bool hasActiveTasks(const AutoLockHelperThreadState& lock) const {
63 return activeTasks;
64 }
65 void incActiveTasks(ParallelMarkTask* task,
66 const AutoLockHelperThreadState& lock);
67 void decActiveTasks(ParallelMarkTask* task,
68 const AutoLockHelperThreadState& lock);
69
70 size_t workerCount() const;
71
72 friend class ParallelMarkTask;
73
74 GCRuntime* const gc;
75
76 using ParallelMarkTaskList = mozilla::DoublyLinkedList<ParallelMarkTask>;
77 HelperThreadLockData<ParallelMarkTaskList> waitingTasks;
78 AtomicCount waitingTaskCount;
79
80 HelperThreadLockData<size_t> activeTasks;
81 };
82
83 // A helper thread task that performs parallel marking.
84 class alignas(TypicalCacheLineSize) ParallelMarkTask
85 : public GCParallelTask,
86 public mozilla::DoublyLinkedListElement<ParallelMarkTask> {
87 public:
88 friend class ParallelMarker;
89
90 ParallelMarkTask(ParallelMarker* pm, GCMarker* marker, MarkColor color,
91 const JS::SliceBudget& budget);
92 ~ParallelMarkTask();
93
94 void run(AutoLockHelperThreadState& lock) override;
95
96 void recordDuration() override;
97
98 private:
99 bool tryMarking(AutoLockHelperThreadState& lock);
100 bool requestWork(AutoLockHelperThreadState& lock);
101
102 void waitUntilResumed(AutoLockHelperThreadState& lock);
103 void resume();
104 void resumeOnFinish(const AutoLockHelperThreadState& lock);
105
106 bool hasWork() const;
107
108 // The following fields are only accessed by the marker thread:
109 ParallelMarker* const pm;
110 GCMarker* const marker;
111 AutoSetMarkColor color;
112 JS::SliceBudget budget;
113 ConditionVariable resumed;
114
115 HelperThreadLockData<bool> isWaiting;
116
117 // Length of time this task spent blocked waiting for work.
118 MainThreadOrGCTaskData<mozilla::TimeDuration> markTime;
119 MainThreadOrGCTaskData<mozilla::TimeDuration> waitTime;
120 };
121
122 } // namespace gc
123 } // namespace js
124
125 #endif /* gc_ParallelMarking_h */