|
23 | 23 | * questions. |
24 | 24 | */ |
25 | 25 |
|
| 26 | +/* |
| 27 | + * =========================================================================== |
| 28 | + * (c) Copyright IBM Corp. 2026, 2026 All Rights Reserved |
| 29 | + * =========================================================================== |
| 30 | + */ |
| 31 | + |
26 | 32 | #include <dlfcn.h> |
27 | 33 | #include <unistd.h> |
28 | 34 | #include <sys/types.h> |
29 | 35 | #include <sys/epoll.h> |
| 36 | + #include <time.h> |
30 | 37 |
|
31 | 38 | #include "jni.h" |
32 | 39 | #include "jni_util.h" |
@@ -83,11 +90,41 @@ Java_sun_nio_ch_EPoll_wait(JNIEnv *env, jclass clazz, jint epfd, |
83 | 90 | jlong address, jint numfds, jint timeout) |
84 | 91 | { |
85 | 92 | struct epoll_event *events = jlong_to_ptr(address); |
| 93 | + struct timespec starttime; |
| 94 | + if (-1 != timeout) { |
| 95 | + clock_gettime(CLOCK_MONOTONIC, &starttime); |
| 96 | + } |
86 | 97 | int res = epoll_wait(epfd, events, numfds, timeout); |
87 | 98 | if (res < 0) { |
88 | 99 | if (errno == EINTR) { |
89 | 100 | return IOS_INTERRUPTED; |
90 | 101 | } else { |
| 102 | + if (EINVAL == errno) { |
| 103 | + jint newtimeout = -1; |
| 104 | + if (-1 != timeout) { |
| 105 | + time_t elapsedsec = 0; |
| 106 | + long elapsedmillis = 0; |
| 107 | + struct timespec endtime; |
| 108 | + clock_gettime(CLOCK_MONOTONIC, &endtime); |
| 109 | + elapsedsec = endtime.tv_sec - starttime.tv_sec - 1; |
| 110 | + elapsedmillis = (endtime.tv_nsec + 1000000000 - starttime.tv_nsec) / 1000000; |
| 111 | + newtimeout = timeout - (elapsedsec * 1000) - elapsedmillis; |
| 112 | + if (newtimeout < 0) { |
| 113 | + newtimeout = 0; |
| 114 | + } |
| 115 | + } |
| 116 | + res = epoll_wait(epfd, events, numfds, newtimeout); |
| 117 | + if (res < 0) { |
| 118 | + if (errno == EINTR) { |
| 119 | + return IOS_INTERRUPTED; |
| 120 | + } else { |
| 121 | + JNU_ThrowIOExceptionWithLastError(env, "epoll_wait failed"); |
| 122 | + return IOS_THROWN; |
| 123 | + } |
| 124 | + } else { |
| 125 | + return res; |
| 126 | + } |
| 127 | + } |
91 | 128 | JNU_ThrowIOExceptionWithLastError(env, "epoll_wait failed"); |
92 | 129 | return IOS_THROWN; |
93 | 130 | } |
|
0 commit comments