4040 */
4141package com .oracle .graal .python .builtins .modules .multiprocessing ;
4242
43+ import static com .oracle .graal .python .builtins .PythonBuiltinClassType .NotImplementedError ;
4344import static com .oracle .graal .python .builtins .PythonBuiltinClassType .ValueError ;
4445import static com .oracle .graal .python .nodes .SpecialMethodNames .J___ENTER__ ;
4546import static com .oracle .graal .python .nodes .SpecialMethodNames .J___EXIT__ ;
6970import com .oracle .graal .python .runtime .PosixSupport ;
7071import com .oracle .graal .python .runtime .PosixSupportLibrary ;
7172import com .oracle .graal .python .runtime .PosixSupportLibrary .PosixException ;
73+ import com .oracle .graal .python .runtime .PosixSupportLibrary .UnsupportedPosixFeatureException ;
7274import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
7375import com .oracle .truffle .api .dsl .Bind ;
7476import com .oracle .truffle .api .dsl .Cached ;
@@ -97,7 +99,7 @@ public void initialize(Python3Core core) {
9799 @ GenerateNodeFactory
98100 abstract static class HandleNode extends PythonUnaryBuiltinNode {
99101 @ Specialization
100- Object get (PSemLock self ) {
102+ static Object get (PSemLock self ) {
101103 return self .getHandle ();
102104 }
103105 }
@@ -106,7 +108,7 @@ Object get(PSemLock self) {
106108 @ GenerateNodeFactory
107109 abstract static class KindNode extends PythonUnaryBuiltinNode {
108110 @ Specialization
109- Object get (PSemLock self ) {
111+ static Object get (PSemLock self ) {
110112 return self .getKind ();
111113 }
112114 }
@@ -115,7 +117,7 @@ Object get(PSemLock self) {
115117 @ GenerateNodeFactory
116118 abstract static class NameNode extends PythonUnaryBuiltinNode {
117119 @ Specialization
118- Object get (PSemLock self ) {
120+ static Object get (PSemLock self ) {
119121 return self .getName ();
120122 }
121123 }
@@ -124,7 +126,7 @@ Object get(PSemLock self) {
124126 @ GenerateNodeFactory
125127 abstract static class MaxValueNode extends PythonUnaryBuiltinNode {
126128 @ Specialization
127- Object get (PSemLock self ) {
129+ static Object get (PSemLock self ) {
128130 return self .getMaxValue ();
129131 }
130132 }
@@ -199,7 +201,7 @@ protected ArgumentClinicProvider getArgumentClinic() {
199201 @ GenerateNodeFactory
200202 abstract static class ReleaseNode extends PythonUnaryBuiltinNode {
201203 @ Specialization
202- PNone release (VirtualFrame frame , PSemLock self ,
204+ static PNone release (VirtualFrame frame , PSemLock self ,
203205 @ Bind ("this" ) Node inliningTarget ,
204206 @ Bind ("getPosixSupport()" ) PosixSupport posixSupport ,
205207 @ CachedLibrary ("posixSupport" ) PosixSupportLibrary posixLib ,
@@ -216,13 +218,22 @@ PNone release(VirtualFrame frame, PSemLock self,
216218 } else {
217219 int sval ;
218220 try {
219- sval = posixLib .semGetValue (posixSupport , self .getHandle ());
221+ try {
222+ sval = posixLib .semGetValue (posixSupport , self .getHandle ());
223+ if (sval >= self .getMaxValue ()) {
224+ throw raiseNode .get (inliningTarget ).raise (ValueError , ErrorMessages .SEMAPHORE_RELEASED_TOO_MANY_TIMES );
225+ }
226+ } catch (UnsupportedPosixFeatureException e ) {
227+ /* We will only check properly the maxvalue == 1 case */
228+ if (posixLib .semTryWait (posixSupport , self .getHandle ())) {
229+ /* it was not locked so undo wait and raise */
230+ posixLib .semPost (posixSupport , self .getHandle ());
231+ throw raiseNode .get (inliningTarget ).raise (ValueError , ErrorMessages .SEMAPHORE_RELEASED_TOO_MANY_TIMES );
232+ }
233+ }
220234 } catch (PosixException e ) {
221235 throw constructAndRaiseNode .get (inliningTarget ).raiseOSErrorFromPosixException (frame , e );
222236 }
223- if (sval >= self .getMaxValue ()) {
224- throw raiseNode .get (inliningTarget ).raise (ValueError , ErrorMessages .SEMAPHORE_RELEASED_TOO_MANY_TIMES );
225- }
226237 }
227238 try {
228239 posixLib .semPost (posixSupport , self .getHandle ());
@@ -238,7 +249,7 @@ PNone release(VirtualFrame frame, PSemLock self,
238249 @ GenerateNodeFactory
239250 abstract static class EnterNode extends PythonUnaryBuiltinNode {
240251 @ Specialization
241- Object enter (VirtualFrame frame , PSemLock self ,
252+ static Object enter (VirtualFrame frame , PSemLock self ,
242253 @ Cached AcquireNode acquireNode ) {
243254 return acquireNode .execute (frame , self , true , PNone .NO_VALUE );
244255 }
@@ -248,7 +259,7 @@ Object enter(VirtualFrame frame, PSemLock self,
248259 @ GenerateNodeFactory
249260 abstract static class ExitNode extends PythonQuaternaryBuiltinNode {
250261 @ Specialization
251- Object exit (VirtualFrame frame , PSemLock self , @ SuppressWarnings ("unused" ) Object type , @ SuppressWarnings ("unused" ) Object value , @ SuppressWarnings ("unused" ) Object traceback ,
262+ static Object exit (VirtualFrame frame , PSemLock self , @ SuppressWarnings ("unused" ) Object type , @ SuppressWarnings ("unused" ) Object value , @ SuppressWarnings ("unused" ) Object traceback ,
252263 @ Cached ReleaseNode releaseNode ) {
253264 return releaseNode .execute (frame , self );
254265 }
@@ -258,7 +269,7 @@ Object exit(VirtualFrame frame, PSemLock self, @SuppressWarnings("unused") Objec
258269 @ GenerateNodeFactory
259270 abstract static class CountNode extends PythonUnaryBuiltinNode {
260271 @ Specialization
261- Object get (PSemLock self ) {
272+ static Object get (PSemLock self ) {
262273 return self .getCount ();
263274 }
264275 }
@@ -267,7 +278,7 @@ Object get(PSemLock self) {
267278 @ GenerateNodeFactory
268279 abstract static class IsMineNode extends PythonUnaryBuiltinNode {
269280 @ Specialization
270- boolean get (PSemLock self ) {
281+ static boolean get (PSemLock self ) {
271282 return self .isMine ();
272283 }
273284 }
@@ -280,7 +291,8 @@ int get(VirtualFrame frame, PSemLock self,
280291 @ Bind ("this" ) Node inliningTarget ,
281292 @ Bind ("getPosixSupport()" ) PosixSupport posixSupport ,
282293 @ CachedLibrary ("posixSupport" ) PosixSupportLibrary posixLib ,
283- @ Cached PConstructAndRaiseNode .Lazy constructAndRaiseNode ) {
294+ @ Cached PConstructAndRaiseNode .Lazy constructAndRaiseNode ,
295+ @ Cached PRaiseNode .Lazy raiseNode ) {
284296 try {
285297 int sval = posixLib .semGetValue (posixSupport , self .getHandle ());
286298 /*
@@ -293,6 +305,9 @@ int get(VirtualFrame frame, PSemLock self,
293305 return sval ;
294306 } catch (PosixException e ) {
295307 throw constructAndRaiseNode .get (inliningTarget ).raiseOSErrorFromPosixException (frame , e );
308+ } catch (UnsupportedPosixFeatureException e ) {
309+ // Not available on Darwin
310+ throw raiseNode .get (inliningTarget ).raise (NotImplementedError );
296311 }
297312 }
298313 }
@@ -301,14 +316,22 @@ int get(VirtualFrame frame, PSemLock self,
301316 @ GenerateNodeFactory
302317 abstract static class IsZeroNode extends PythonUnaryBuiltinNode {
303318 @ Specialization
304- boolean get (VirtualFrame frame , PSemLock self ,
319+ static boolean get (VirtualFrame frame , PSemLock self ,
305320 @ Bind ("this" ) Node inliningTarget ,
306321 @ Bind ("getPosixSupport()" ) PosixSupport posixSupport ,
307322 @ CachedLibrary ("posixSupport" ) PosixSupportLibrary posixLib ,
308323 @ Cached PConstructAndRaiseNode .Lazy constructAndRaiseNode ) {
309324 try {
310- int sval = posixLib .semGetValue (posixSupport , self .getHandle ());
311- return sval == 0 ;
325+ try {
326+ return posixLib .semGetValue (posixSupport , self .getHandle ()) == 0 ;
327+ } catch (UnsupportedPosixFeatureException e ) {
328+ if (posixLib .semTryWait (posixSupport , self .getHandle ())) {
329+ posixLib .semPost (posixSupport , self .getHandle ());
330+ return false ;
331+ } else {
332+ return true ;
333+ }
334+ }
312335 } catch (PosixException e ) {
313336 throw constructAndRaiseNode .get (inliningTarget ).raiseOSErrorFromPosixException (frame , e );
314337 }
@@ -319,7 +342,7 @@ boolean get(VirtualFrame frame, PSemLock self,
319342 @ GenerateNodeFactory
320343 abstract static class AfterForkNode extends PythonUnaryBuiltinNode {
321344 @ Specialization
322- Object afterFork (PSemLock self ) {
345+ static Object afterFork (PSemLock self ) {
323346 self .setCount (0 );
324347 return PNone .NONE ;
325348 }
@@ -333,7 +356,7 @@ Object afterFork(PSemLock self) {
333356 @ GenerateNodeFactory
334357 abstract static class RebuildNode extends PythonClinicBuiltinNode {
335358 @ Specialization
336- Object rebuild (VirtualFrame frame , Object cls , @ SuppressWarnings ("unused" ) long origHandle , int kind , int maxValue , TruffleString name ,
359+ static Object rebuild (VirtualFrame frame , Object cls , @ SuppressWarnings ("unused" ) long origHandle , int kind , int maxValue , TruffleString name ,
337360 @ Bind ("this" ) Node inliningTarget ,
338361 @ Bind ("getPosixSupport()" ) PosixSupport posixSupport ,
339362 @ CachedLibrary ("posixSupport" ) PosixSupportLibrary posixLib ,
0 commit comments