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/. */
7 #ifndef gc_ParallelMarking_h
8 #define gc_ParallelMarking_h
10 #include "mozilla/Atomics.h"
11 #include "mozilla/DoublyLinkedList.h"
12 #include "mozilla/TimeStamp.h"
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"
23 class AutoLockHelperThreadState
;
27 class ParallelMarkTask
;
29 // Per-runtime parallel marking state.
31 // This class is used on the main thread and coordinates parallel marking using
32 // several helper threads running ParallelMarkTasks.
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
{
39 explicit ParallelMarker(GCRuntime
* gc
);
41 bool mark(SliceBudget
& sliceBudget
);
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
);
49 bool markOneColor(MarkColor color
, SliceBudget
& sliceBudget
);
51 bool hasWork(MarkColor color
) const;
53 void addTask(ParallelMarkTask
* task
, const AutoLockHelperThreadState
& lock
);
55 void addTaskToWaitingList(ParallelMarkTask
* task
,
56 const AutoLockHelperThreadState
& lock
);
58 bool isTaskInWaitingList(const ParallelMarkTask
* task
,
59 const AutoLockHelperThreadState
& lock
) const;
62 bool hasActiveTasks(const AutoLockHelperThreadState
& lock
) const {
65 void incActiveTasks(ParallelMarkTask
* task
,
66 const AutoLockHelperThreadState
& lock
);
67 void decActiveTasks(ParallelMarkTask
* task
,
68 const AutoLockHelperThreadState
& lock
);
70 size_t workerCount() const;
72 friend class ParallelMarkTask
;
76 using ParallelMarkTaskList
= mozilla::DoublyLinkedList
<ParallelMarkTask
>;
77 HelperThreadLockData
<ParallelMarkTaskList
> waitingTasks
;
78 AtomicCount waitingTaskCount
;
80 HelperThreadLockData
<size_t> activeTasks
;
83 // A helper thread task that performs parallel marking.
84 class alignas(TypicalCacheLineSize
) ParallelMarkTask
85 : public GCParallelTask
,
86 public mozilla::DoublyLinkedListElement
<ParallelMarkTask
> {
88 friend class ParallelMarker
;
90 ParallelMarkTask(ParallelMarker
* pm
, GCMarker
* marker
, MarkColor color
,
91 const SliceBudget
& budget
);
94 void run(AutoLockHelperThreadState
& lock
) override
;
96 void recordDuration() override
;
99 bool tryMarking(AutoLockHelperThreadState
& lock
);
100 bool requestWork(AutoLockHelperThreadState
& lock
);
102 void waitUntilResumed(AutoLockHelperThreadState
& lock
);
104 void resumeOnFinish(const AutoLockHelperThreadState
& lock
);
106 bool hasWork() const;
108 // The following fields are only accessed by the marker thread:
109 ParallelMarker
* const pm
;
110 GCMarker
* const marker
;
111 AutoSetMarkColor color
;
113 ConditionVariable resumed
;
115 HelperThreadLockData
<bool> isWaiting
;
117 // Length of time this task spent blocked waiting for work.
118 MainThreadOrGCTaskData
<mozilla::TimeDuration
> markTime
;
119 MainThreadOrGCTaskData
<mozilla::TimeDuration
> waitTime
;
125 #endif /* gc_ParallelMarking_h */