Skip to content

Commit bd46f4a

Browse files
committed
Document queuing of kept objects from completed threads.
1 parent 04db6e1 commit bd46f4a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/main/java/org/truffleruby/core/MarkingService.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@ private boolean isFull() {
177177
return counter == service.cacheSize;
178178
}
179179

180+
/**
181+
* This method is called when finalizing the {@link MarkerThreadLocalData} of another
182+
* thread. we want to either queue the running of mark functions for any objects still kept
183+
* alive by that other thread, or copy them into our kept objects. If our keptObjects buffer
184+
* is already full we should queue and reset it first, and then consider one of four cases.
185+
*
186+
* 1. The other threads kept object buffer is full. We'll just queue that.
187+
* 2. The other thread has an empty kept object buffer. We don't need to do anything.
188+
* 3. The whole of the other thread's kept object buffer will fit into our buffer.
189+
* We can just copy its contents into our buffer.
190+
* 4. The other buffer will not fit into our buffer. We need to copy across as much as will
191+
* fit, queue the now full buffer, and then copy the rest.
192+
*/
180193
public void keepObjects(MarkerKeptObjects otherObjects) {
181194
if (isFull()) {
182195
queueAndReset();
@@ -245,6 +258,13 @@ public MarkingService(RubyContext context, ReferenceProcessor referenceProcessor
245258
public MarkerThreadLocalData makeThreadLocalData() {
246259
MarkerThreadLocalData data = new MarkerThreadLocalData(this);
247260
MarkerKeptObjects keptObjects = data.getKeptObjects();
261+
/*
262+
* This finalizer will ensure all the objects remaining in our kept objects buffer will be
263+
* queue at some point. We don't need to do a queue and reset as the MarkerKeptObjects is
264+
* going to be GC'ed anyway. We also don't simply queue the keptObjects buffer because it
265+
* may only have zero, or very few, objects in it and we don't want to run mark functions
266+
* more often than we have to.
267+
*/
248268
context.getFinalizationService().addFinalizer(data, null, MarkingService.class, () -> getThreadLocalData().keptObjects.keepObjects(keptObjects), null);
249269
return data;
250270
}

0 commit comments

Comments
 (0)