|
121 | 121 | import com.oracle.truffle.api.TruffleLanguage.Env; |
122 | 122 | import com.oracle.truffle.api.TruffleLogger; |
123 | 123 | import com.oracle.truffle.api.TruffleSafepoint; |
| 124 | +import com.oracle.truffle.api.dsl.Bind; |
124 | 125 | import com.oracle.truffle.api.dsl.Cached; |
125 | 126 | import com.oracle.truffle.api.dsl.Cached.Shared; |
126 | 127 | import com.oracle.truffle.api.dsl.NeverDefault; |
|
130 | 131 | import com.oracle.truffle.api.interop.UnknownIdentifierException; |
131 | 132 | import com.oracle.truffle.api.interop.UnsupportedMessageException; |
132 | 133 | import com.oracle.truffle.api.interop.UnsupportedTypeException; |
| 134 | +import com.oracle.truffle.api.library.CachedLibrary; |
133 | 135 | import com.oracle.truffle.api.library.ExportLibrary; |
134 | 136 | import com.oracle.truffle.api.library.ExportMessage; |
135 | 137 | import com.oracle.truffle.api.nodes.Node; |
@@ -2276,16 +2278,34 @@ boolean semTryWait(long handle, |
2276 | 2278 |
|
2277 | 2279 | @ExportMessage |
2278 | 2280 | boolean semTimedWait(long handle, long deadlineNs, |
2279 | | - @Shared("invoke") @Cached InvokeNativeFunction invokeNode) throws PosixException { |
2280 | | - int res = invokeNode.callInt(this, PosixNativeFunction.call_sem_timedwait, handle, deadlineNs); |
2281 | | - if (res < 0) { |
2282 | | - int errno = getErrno(invokeNode); |
2283 | | - if (errno == OSErrorEnum.ETIMEDOUT.getNumber()) { |
2284 | | - return false; |
| 2281 | + @Bind("$node") Node node, |
| 2282 | + @CachedLibrary("this") PosixSupportLibrary thisLib, |
| 2283 | + @Shared("invoke") @Cached InvokeNativeFunction invokeNode) throws PosixException { |
| 2284 | + if (PythonOS.getPythonOS() == PythonOS.PLATFORM_LINUX) { |
| 2285 | + int res = invokeNode.callInt(this, PosixNativeFunction.call_sem_timedwait, handle, deadlineNs); |
| 2286 | + if (res < 0) { |
| 2287 | + int errno = getErrno(invokeNode); |
| 2288 | + if (errno == OSErrorEnum.ETIMEDOUT.getNumber()) { |
| 2289 | + return false; |
| 2290 | + } |
| 2291 | + throw newPosixException(invokeNode, errno); |
| 2292 | + } |
| 2293 | + return true; |
| 2294 | + } else { |
| 2295 | + long deadlineMs = deadlineNs / 1_000_000; |
| 2296 | + while (true) { |
| 2297 | + if (thisLib.semTryWait(this, handle)) { |
| 2298 | + return true; |
| 2299 | + } |
| 2300 | + long currentMs = System.currentTimeMillis(); |
| 2301 | + if (currentMs > deadlineMs) { |
| 2302 | + return false; |
| 2303 | + } |
| 2304 | + long delayMs = Math.min(deadlineMs - currentMs, 20); |
| 2305 | + TruffleSafepoint.setBlockedThreadInterruptible(node, Thread::sleep, delayMs); |
| 2306 | + TruffleSafepoint.poll(node); |
2285 | 2307 | } |
2286 | | - throw newPosixException(invokeNode, errno); |
2287 | 2308 | } |
2288 | | - return true; |
2289 | 2309 | } |
2290 | 2310 |
|
2291 | 2311 | @ExportMessage |
|
0 commit comments