Skip to content

Commit 6baa911

Browse files
fangererqunaibit
authored andcommitted
Disable reference queue polling during Python GC
1 parent dda81ed commit 6baa911

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

graalpython/com.oracle.graal.python.cext/src/gcmodule.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,11 @@ gc_collect_main(PyThreadState *tstate, int generation,
15851585
_PyTime_t t1 = 0; /* initialize to prevent a compiler warning */
15861586
GCState *gcstate = graalpy_get_gc_state(tstate); // GraalPy change
15871587

1588+
if (GraalPyTruffle_DisableReferneceQueuePolling()) {
1589+
// reference queue polling is currently active; cannot proceed
1590+
return m + n;
1591+
}
1592+
15881593
// gc_collect_main() must not be called before _PyGC_Init
15891594
// or after _PyGC_Fini()
15901595
assert(gcstate->garbage != NULL);
@@ -1737,6 +1742,8 @@ gc_collect_main(PyThreadState *tstate, int generation,
17371742
PyDTrace_GC_DONE(n + m);
17381743
}
17391744

1745+
GraalPyTruffle_EnableReferneceQueuePolling();
1746+
17401747
assert(!_PyErr_Occurred(tstate));
17411748
return n + m;
17421749
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextBuiltins.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTiming;
131131
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions;
132132
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.GcNativePtrToPythonNode;
133+
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.HandleContext;
133134
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.HandlePointerConverter;
134135
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.NativePtrToPythonWrapperNode;
135136
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.UpdateRefNode;
@@ -1676,6 +1677,27 @@ static Object doManaged(@SuppressWarnings("unused") Object pointer) {
16761677
}
16771678
}
16781679

1680+
@CApiBuiltin(ret = Void, call = Ignored)
1681+
abstract static class PyTruffle_EnableReferneceQueuePolling extends CApiNullaryBuiltinNode {
1682+
@Specialization
1683+
static Object doGeneric(@Bind("this") Node inliningTarget) {
1684+
assert PythonContext.get(inliningTarget).getOption(PythonOptions.PythonGC);
1685+
HandleContext handleContext = PythonContext.get(inliningTarget).nativeContext;
1686+
CApiTransitions.enableReferenceQueuePolling(handleContext);
1687+
return PNone.NO_VALUE;
1688+
}
1689+
}
1690+
1691+
@CApiBuiltin(ret = Int, call = Ignored)
1692+
abstract static class PyTruffle_DisableReferneceQueuePolling extends CApiNullaryBuiltinNode {
1693+
@Specialization
1694+
static int doGeneric(@Bind("this") Node inliningTarget) {
1695+
assert PythonContext.get(inliningTarget).getOption(PythonOptions.PythonGC);
1696+
HandleContext handleContext = PythonContext.get(inliningTarget).nativeContext;
1697+
return PInt.intValue(CApiTransitions.disableReferenceQueuePolling(handleContext));
1698+
}
1699+
}
1700+
16791701
private static final int TRACE_MEM = 0x1;
16801702
private static final int LOG_INFO = 0x2;
16811703
private static final int LOG_CONFIG = 0x4;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/transitions/CApiTransitions.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,16 @@ public static void freeClassReplacements(HandleContext handleContext) {
601601
handleContext.nativeLookup.clear();
602602
}
603603

604-
public static void disableReferenceQueuePolling(HandleContext handleContext) {
605-
handleContext.referenceQueuePollActive = true;
604+
public static boolean disableReferenceQueuePolling(HandleContext handleContext) {
605+
if (!handleContext.referenceQueuePollActive) {
606+
handleContext.referenceQueuePollActive = true;
607+
return false;
608+
}
609+
return true;
610+
}
611+
612+
public static void enableReferenceQueuePolling(HandleContext handleContext) {
613+
handleContext.referenceQueuePollActive = false;
606614
}
607615

608616
private static void freeNativeStub(PythonObjectReference ref) {

0 commit comments

Comments
 (0)