Skip to content

Commit f8ed5cf

Browse files
committed
Share named semaphores between both contexts sharing an engine and also main and children contexts in the multiprocessing scenario
1 parent bccf6c7 commit f8ed5cf

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.IOException;
2929
import java.util.Arrays;
3030
import java.util.concurrent.ConcurrentHashMap;
31+
import java.util.concurrent.Semaphore;
3132
import java.util.logging.Level;
3233

3334
import org.graalvm.options.OptionDescriptors;
@@ -193,6 +194,20 @@ public final class PythonLanguage extends TruffleLanguage<PythonContext> {
193194

194195
@CompilationFinal(dimensions = 1) private static final Object[] CONTEXT_INSENSITIVE_SINGLETONS = new Object[]{PNone.NONE, PNone.NO_VALUE, PEllipsis.INSTANCE, PNotImplemented.NOT_IMPLEMENTED};
195196

197+
/**
198+
* Named semaphores are shared between all processes in a system, and they persist until the
199+
* system is shut down, unless explicitly removed. We interpret this as meaning they all exist
200+
* globally per language instance, that is, they are shared between different Contexts in the
201+
* same engine.
202+
*
203+
* Top level contexts use this map to initialize their shared multiprocessing data. Inner
204+
* children contexts created for the multiprocessing module ignore this map in
205+
* {@link PythonLanguage} and instead inherit it in the shared multiprocessing data from their
206+
* parent context. This way, the child inner contexts do not have to run in the same engine
207+
* (have the same language instance), but can still share the named semaphores.
208+
*/
209+
public final ConcurrentHashMap<String, Semaphore> namedSemaphores = new ConcurrentHashMap<>();
210+
196211
@CompilationFinal(dimensions = 1) private volatile Object[] engineOptionsStorage;
197212
@CompilationFinal private volatile OptionValues engineOptions;
198213

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,10 @@ public static final class SharedMultiprocessingData {
554554
*/
555555
private final Map<Integer, Integer> fdRefCount = new HashMap<>();
556556

557+
public SharedMultiprocessingData(ConcurrentHashMap<String, Semaphore> namedSemaphores) {
558+
this.namedSemaphores = namedSemaphores;
559+
}
560+
557561
/**
558562
* Increases reference count for the given file descriptor.
559563
*/
@@ -676,11 +680,9 @@ private boolean isClosed(int fd) {
676680
}
677681

678682
/**
679-
* Named semaphores are shared between all processes in a system, and they persist until the
680-
* system is shut down, unless explicitly removed. We interpret this as meaning they all
681-
* exist globally per the main context and all its children.
683+
* @see PythonLanguage#namedSemaphores
682684
*/
683-
private final ConcurrentHashMap<String, Semaphore> namedSemaphores = new ConcurrentHashMap<>();
685+
private final ConcurrentHashMap<String, Semaphore> namedSemaphores;
684686

685687
@TruffleBoundary
686688
public void putNamedSemaphore(String name, Semaphore sem) {
@@ -732,7 +734,7 @@ public PythonContext(PythonLanguage language, TruffleLanguage.Env env, Python3Co
732734
this.core = core;
733735
this.env = env;
734736
this.childContextData = (ChildContextData) env.getConfig().get(CHILD_CONTEXT_DATA);
735-
this.sharedMultiprocessingData = this.childContextData == null ? new SharedMultiprocessingData() : childContextData.parentCtx.sharedMultiprocessingData;
737+
this.sharedMultiprocessingData = this.childContextData == null ? new SharedMultiprocessingData(language.namedSemaphores) : childContextData.parentCtx.sharedMultiprocessingData;
736738
this.handler = new AsyncHandler(this);
737739
this.sharedFinalizer = new AsyncHandler.SharedFinalizer(this);
738740
this.optionValues = PythonOptions.createOptionValuesStorage(env);

0 commit comments

Comments
 (0)