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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef gc_ParallelMarking_h
#define gc_ParallelMarking_h
#include "mozilla/Atomics.h"
#include "mozilla/DoublyLinkedList.h"
#include "mozilla/TimeStamp.h"
#include "gc/GCMarker.h"
#include "gc/GCParallelTask.h"
#include "js/HeapAPI.h"
#include "js/SliceBudget.h"
#include "threading/ConditionVariable.h"
#include "threading/ProtectedData.h"
namespace js {
class AutoLockGC;
class AutoLockHelperThreadState;
class AutoUnlockGC;
namespace gc {
class ParallelMarkTask;
// Per-runtime parallel marking state.
//
// This class is used on the main thread and coordinates parallel marking using
// several helper threads running ParallelMarkTasks.
//
// This uses a work-requesting approach. Threads mark until they run out of
// work and then add themselves to a list of waiting tasks and block. Running
// tasks with enough work may donate work to a waiting task and resume it.
class MOZ_STACK_CLASS ParallelMarker {
public:
explicit ParallelMarker(GCRuntime* gc);
bool mark(SliceBudget& sliceBudget);
using AtomicCount = mozilla::Atomic<uint32_t, mozilla::Relaxed>;
AtomicCount& waitingTaskCountRef() { return waitingTaskCount; }
bool hasWaitingTasks() { return waitingTaskCount != 0; }
void donateWorkFrom(GCMarker* src);
private:
bool markOneColor(MarkColor color, SliceBudget& sliceBudget);
bool hasWork(MarkColor color) const;
void addTask(ParallelMarkTask* task, const AutoLockGC& lock);
void addTaskToWaitingList(ParallelMarkTask* task, const AutoLockGC& lock);
#ifdef DEBUG
bool isTaskInWaitingList(const ParallelMarkTask* task,
const AutoLockGC& lock) const;
#endif
bool hasActiveTasks(const AutoLockGC& lock) const { return activeTasks; }
void incActiveTasks(ParallelMarkTask* task, const AutoLockGC& lock);
void decActiveTasks(ParallelMarkTask* task, const AutoLockGC& lock);
size_t workerCount() const;
friend class ParallelMarkTask;
GCRuntime* const gc;
using ParallelMarkTaskList = mozilla::DoublyLinkedList<ParallelMarkTask>;
GCLockData<ParallelMarkTaskList> waitingTasks;
AtomicCount waitingTaskCount;
GCLockData<size_t> activeTasks;
};
// A helper thread task that performs parallel marking.
class alignas(TypicalCacheLineSize) ParallelMarkTask
: public GCParallelTask,
public mozilla::DoublyLinkedListElement<ParallelMarkTask> {
public:
friend class ParallelMarker;
ParallelMarkTask(ParallelMarker* pm, GCMarker* marker, MarkColor color,
const SliceBudget& budget);
~ParallelMarkTask();
void run(AutoLockHelperThreadState& lock) override;
void recordDuration() override;
private:
void markOrRequestWork(AutoLockGC& lock);
bool tryMarking(AutoLockGC& lock);
bool requestWork(AutoLockGC& lock);
void waitUntilResumed(AutoLockGC& lock);
void resume();
void resumeOnFinish(const AutoLockGC& lock);
bool hasWork() const;
// The following fields are only accessed by the marker thread:
ParallelMarker* const pm;
GCMarker* const marker;
AutoSetMarkColor color;
SliceBudget budget;
ConditionVariable resumed;
GCLockData<bool> isWaiting;
// Length of time this task spent blocked waiting for work.
MainThreadOrGCTaskData<mozilla::TimeDuration> markTime;
MainThreadOrGCTaskData<mozilla::TimeDuration> waitTime;
};
} // namespace gc
} // namespace js
#endif /* gc_ParallelMarking_h */
|