Implement _multiprocessing.SemLock · etemcode1/graalpython@77be107 · GitHub
Skip to content

Commit 77be107

Browse files
committed
Implement _multiprocessing.SemLock
1 parent 3083b85 commit 77be107

16 files changed

Lines changed: 977 additions & 3 deletions

‎graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java‎

Lines changed: 5 additions & 0 deletions

‎graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/PythonBuiltinClassType.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ public enum PythonBuiltinClassType implements TruffleObject {
216216
PThreadLocal("_local", J__THREAD),
217217
PLock("LockType", J__THREAD),
218218
PRLock("RLock", J__THREAD),
219+
PSemLock("SemLock", "_multiprocessing"),
219220
PGraalPySemLock("SemLock", "_multiprocessing_graalpy"),
220221
PSocket("socket", J__SOCKET),
221222
PStaticmethod("staticmethod", J_BUILTINS, Flags.PUBLIC_BASE_WDICT),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.builtins.modules.multiprocessing;
42+
43+
import static com.oracle.graal.python.runtime.PosixConstants.O_CREAT;
44+
import static com.oracle.graal.python.runtime.PosixConstants.O_EXCL;
45+
46+
import java.util.List;
47+
48+
import com.oracle.graal.python.annotations.ArgumentClinic;
49+
import com.oracle.graal.python.builtins.Builtin;
50+
import com.oracle.graal.python.builtins.CoreFunctions;
51+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
52+
import com.oracle.graal.python.builtins.PythonBuiltins;
53+
import com.oracle.graal.python.builtins.objects.PNone;
54+
import com.oracle.graal.python.nodes.ErrorMessages;
55+
import com.oracle.graal.python.nodes.PConstructAndRaiseNode;
56+
import com.oracle.graal.python.nodes.PRaiseNode;
57+
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
58+
import com.oracle.graal.python.nodes.function.builtins.PythonClinicBuiltinNode;
59+
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
60+
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
61+
import com.oracle.graal.python.runtime.PosixSupport;
62+
import com.oracle.graal.python.runtime.PosixSupportLibrary;
63+
import com.oracle.graal.python.runtime.PosixSupportLibrary.PosixException;
64+
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
65+
import com.oracle.truffle.api.dsl.Bind;
66+
import com.oracle.truffle.api.dsl.Cached;
67+
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
68+
import com.oracle.truffle.api.dsl.NodeFactory;
69+
import com.oracle.truffle.api.dsl.Specialization;
70+
import com.oracle.truffle.api.frame.VirtualFrame;
71+
import com.oracle.truffle.api.library.CachedLibrary;
72+
import com.oracle.truffle.api.nodes.Node;
73+
import com.oracle.truffle.api.strings.TruffleString;
74+
75+
@CoreFunctions(defineModule = "_multiprocessing")
76+
public class MultiprocessingModuleBuiltins extends PythonBuiltins {
77+
@Override
78+
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
79+
return MultiprocessingModuleBuiltinsFactory.getFactories();
80+
}
81+
82+
@Builtin(name = "SemLock", minNumOfPositionalArgs = 1, parameterNames = {"$cls", "kind", "value", "maxvalue", "name", "unlink"}, constructsClass = PythonBuiltinClassType.PSemLock)
83+
@ArgumentClinic(name = "kind", conversion = ArgumentClinic.ClinicConversion.Int)
84+
@ArgumentClinic(name = "value", conversion = ArgumentClinic.ClinicConversion.Int)
85+
@ArgumentClinic(name = "maxvalue", conversion = ArgumentClinic.ClinicConversion.Int)
86+
@ArgumentClinic(name = "name", conversion = ArgumentClinic.ClinicConversion.TString)
87+
@ArgumentClinic(name = "unlink", conversion = ArgumentClinic.ClinicConversion.IntToBoolean)
88+
@GenerateNodeFactory
89+
abstract static class SemLockNode extends PythonClinicBuiltinNode {
90+
@Specialization
91+
static PSemLock construct(VirtualFrame frame, Object cls, int kind, int value, int maxValue, TruffleString name, boolean unlink,
92+
@Bind("this") Node inliningTarget,
93+
@Bind("getPosixSupport()") PosixSupport posixSupport,
94+
@CachedLibrary("posixSupport") PosixSupportLibrary posixLib,
95+
@Cached PythonObjectFactory factory,
96+
@Cached PConstructAndRaiseNode.Lazy constructAndRaiseNode,
97+
@Cached PRaiseNode.Lazy raiseNode) {
98+
if (kind != PGraalPySemLock.RECURSIVE_MUTEX && kind != PGraalPySemLock.SEMAPHORE) {
99+
throw raiseNode.get(inliningTarget).raise(PythonBuiltinClassType.ValueError, ErrorMessages.UNRECOGNIZED_KIND);
100+
}
101+
Object posixName = posixLib.createPathFromString(posixSupport, name);
102+
long handle;
103+
try {
104+
handle = posixLib.semOpen(posixSupport, posixName, O_CREAT.value | O_EXCL.value, 0600, value);
105+
} catch (PosixException e) {
106+
throw constructAndRaiseNode.get(inliningTarget).raiseOSErrorFromPosixException(frame, e);
107+
}
108+
if (unlink) {
109+
try {
110+
posixLib.semUnlink(posixSupport, posixName);
111+
} catch (PosixException e) {
112+
try {
113+
posixLib.semClose(posixSupport, handle);
114+
} catch (PosixException ex) {
115+
// Ignore, we're already on an error path
116+
}
117+
throw constructAndRaiseNode.get(inliningTarget).raiseOSErrorFromPosixException(frame, e);
118+
}
119+
}
120+
return factory.createSemLock(cls, handle, kind, maxValue, name);
121+
}
122+
123+
@Override
124+
protected ArgumentClinicProvider getArgumentClinic() {
125+
return MultiprocessingModuleBuiltinsClinicProviders.SemLockNodeClinicProviderGen.INSTANCE;
126+
}
127+
}
128+
129+
@GenerateNodeFactory
130+
@Builtin(name = "sem_unlink", parameterNames = {"name"})
131+
abstract static class SemUnlink extends PythonUnaryBuiltinNode {
132+
@Specialization
133+
PNone doit(VirtualFrame frame, TruffleString name,
134+
@Bind("getPosixSupport()") PosixSupport posixSupport,
135+
@CachedLibrary("posixSupport") PosixSupportLibrary posixLib,
136+
@Bind("this") Node inliningTarget,
137+
@Cached PConstructAndRaiseNode.Lazy constructAndRaiseNode) {
138+
try {
139+
posixLib.semUnlink(posixSupport, posixLib.createPathFromString(posixSupport, name));
140+
} catch (PosixException e) {
141+
throw constructAndRaiseNode.get(inliningTarget).raiseOSErrorFromPosixException(frame, e);
142+
}
143+
return PNone.NONE;
144+
}
145+
}
146+
}
Lines changed: 106 additions & 0 deletions

0 commit comments

Comments
 (0)