GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix epoll_wait retry loop (#14425) · netty/netty@b68a71d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • transport-native-epoll
    • pom.xml
    • src/main/c
      • netty_epoll_native.c
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎transport-native-epoll/pom.xml‎

Copy file name to clipboardExpand all lines: transport-native-epoll/pom.xml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<unix.common.include.unpacked.dir>${unix.common.lib.dir}/META-INF/native/include</unix.common.include.unpacked.dir>
4040
<jni.compiler.args.cflags>CFLAGS=-O2 -pipe -Werror -fno-omit-frame-pointer -Wunused-variable -fvisibility=hidden -D_FORTIFY_SOURCE=2 -ffunction-sections -fdata-sections -I${unix.common.include.unpacked.dir}</jni.compiler.args.cflags>
4141
<jni.compiler.args.ldflags>LDFLAGS=-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -Wl,--gc-sections -L${unix.common.lib.unpacked.dir}</jni.compiler.args.ldflags>
42-
<jni.compiler.args.libs>LIBS=-Wl,--whole-archive -l${unix.common.lib.name} -Wl,--no-whole-archive -ldl</jni.compiler.args.libs>
42+
<jni.compiler.args.libs>LIBS=-Wl,--whole-archive -l${unix.common.lib.name} -Wl,--no-whole-archive -ldl -lrt</jni.compiler.args.libs>
4343
<nativeSourceDirectory>${project.basedir}/src/main/c</nativeSourceDirectory>
4444
<skipTests>true</skipTests>
4545
<japicmp.skip>true</japicmp.skip>
Collapse file

‎transport-native-epoll/src/main/c/netty_epoll_native.c‎

Copy file name to clipboardExpand all lines: transport-native-epoll/src/main/c/netty_epoll_native.c
+124-21Lines changed: 124 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -249,23 +249,133 @@ static jint netty_epoll_native_epollCreate(JNIEnv* env, jclass clazz) {
249249
return efd;
250250
}
251251

252-
static jint netty_epoll_native_epollWait(JNIEnv* env, jclass clazz, jint efd, jlong address, jint len, jint timeout) {
253-
struct epoll_event *ev = (struct epoll_event*) (intptr_t) address;
254-
int result, err;
252+
static inline jint netty_epoll_wait(JNIEnv* env, jint efd, struct epoll_event *ev, jint len, jint timeout)
253+
{
254+
int rc;
255+
256+
if (timeout <= 0) {
257+
// avoid making unnecessary syscalls when doing non-blocking
258+
// check (timeout = 0), or waiting indefinitely (timeout = -1)
259+
while ((rc = epoll_wait(efd, ev, len, timeout)) < 0) {
260+
if (errno != EINTR) {
261+
return -errno;
262+
}
263+
}
264+
} else {
265+
struct timespec ts;
266+
long deadline, now;
255267

256-
do {
257-
result = epoll_wait(efd, ev, len, timeout);
258-
if (result >= 0) {
259-
return result;
268+
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
269+
netty_unix_errors_throwRuntimeExceptionErrorNo(env, "clock_gettime() failed: ", errno);
270+
return -1;
260271
}
261-
} while((err = errno) == EINTR);
262-
return -err;
272+
deadline = ts.tv_sec * 1000 + ts.tv_nsec / 1000 + timeout;
273+
274+
while ((rc = epoll_wait(efd, ev, len, timeout)) < 0) {
275+
if (errno != EINTR) {
276+
return -errno;
277+
}
278+
279+
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
280+
netty_unix_errors_throwRuntimeExceptionErrorNo(env, "clock_gettime() failed: ", errno);
281+
return -1;
282+
}
283+
284+
now = ts.tv_sec * 1000 + ts.tv_nsec / 1000;
285+
if (now >= deadline) {
286+
return 0;
287+
}
288+
timeout = deadline - now;
289+
}
290+
}
291+
292+
return rc;
293+
}
294+
295+
static jint netty_epoll_native_epollWait(JNIEnv* env, jclass clazz, jint efd, jlong address, jint len, jint timeout)
296+
{
297+
return netty_epoll_wait(env, efd, (struct epoll_event *)(intptr_t) address, len, timeout);
298+
}
299+
300+
static inline void timespec_add(struct timespec *t1, const struct timespec *t2)
301+
{
302+
t1->tv_sec += t2->tv_sec;
303+
t1->tv_nsec += t2->tv_nsec;
304+
if (t1->tv_nsec > 1000000000) {
305+
t1->tv_sec++;
306+
t1->tv_nsec -= 1000000000;
307+
}
308+
}
309+
310+
static inline void timespec_sub(struct timespec *t1, const struct timespec *t2)
311+
{
312+
t1->tv_sec -= t2->tv_sec;
313+
t1->tv_nsec -= t2->tv_nsec;
314+
if (t1->tv_nsec < 0) {
315+
t1->tv_sec--;
316+
t1->tv_nsec += 1000000000;
317+
}
318+
}
319+
320+
static inline int timespec_after(const struct timespec *t1, const struct timespec *t2)
321+
{
322+
return (t1->tv_sec > t2->tv_sec) ||
323+
(t1->tv_sec == t2->tv_sec && t1->tv_nsec > t2->tv_nsec);
324+
}
325+
326+
static inline jint netty_epoll_pwait2(JNIEnv *env, jint efd, struct epoll_event *ev, jint len, const struct timespec *timeout)
327+
{
328+
int rc;
329+
330+
if (timeout == NULL || (timeout->tv_sec == 0 && timeout->tv_nsec == 0)) {
331+
// avoid making unnecessary syscalls when doing non-blocking
332+
// check (timeout = { 0, 0 }), or waiting indefinitely
333+
// (timeout = NULL)
334+
while ((rc = epoll_pwait2(efd, ev, len, timeout, NULL)) < 0) {
335+
if (errno != EINTR) {
336+
return -errno;
337+
}
338+
}
339+
} else {
340+
struct timespec deadline, now, decaying_timeout;
341+
342+
if (clock_gettime(CLOCK_MONOTONIC, &deadline) != 0) {
343+
netty_unix_errors_throwRuntimeExceptionErrorNo(env, "clock_gettime() failed: ", errno);
344+
return -1;
345+
}
346+
347+
timespec_add(&deadline, timeout);
348+
decaying_timeout = *timeout;
349+
350+
while ((rc = epoll_pwait2(efd, ev, len, &decaying_timeout, NULL)) < 0) {
351+
if (errno != EINTR) {
352+
return -errno;
353+
}
354+
355+
if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
356+
netty_unix_errors_throwRuntimeExceptionErrorNo(env, "clock_gettime() failed: ", errno);
357+
return -1;
358+
}
359+
360+
if (timespec_after(&now, &deadline)) {
361+
return 0;
362+
}
363+
364+
decaying_timeout = deadline;
365+
timespec_sub(&decaying_timeout, &now);
366+
}
367+
}
368+
369+
return rc;
263370
}
264371

265372
// This needs to be consistent with Native.java
266373
#define EPOLL_WAIT_RESULT(V, ARM_TIMER) ((jlong) ((uint64_t) ((uint32_t) V) << 32 | ARM_TIMER))
267374

268-
static jlong netty_epoll_native_epollWait0(JNIEnv* env, jclass clazz, jint efd, jlong address, jint len, jint timerFd, jint tvSec, jint tvNsec, jlong millisThreshold) {
375+
static jlong netty_epoll_native_epollWait0(JNIEnv* env, jclass clazz, jint efd, jlong address, jint len, jint timerFd, jint tvSec, jint tvNsec, jlong millisThreshold)
376+
{
377+
struct epoll_event *ev = (struct epoll_event *)(intptr_t) address;
378+
int result;
269379
// only reschedule the timer if there is a newer event.
270380
// -1 is a special value used by EpollEventLoop.
271381
uint32_t armTimer = millisThreshold <= 0 ? 1 : 0;
@@ -278,15 +388,8 @@ static jlong netty_epoll_native_epollWait0(JNIEnv* env, jclass clazz, jint efd,
278388
// We have epoll_pwait2(...) and it is supported, this means we can just pass in the itimerspec directly and not need an
279389
// extra syscall even for very small timeouts.
280390
struct timespec ts = { tvSec, tvNsec };
281-
struct epoll_event *ev = (struct epoll_event*) (intptr_t) address;
282-
int result, err;
283-
do {
284-
result = epoll_pwait2(efd, ev, len, &ts, NULL);
285-
if (result >= 0) {
286-
return EPOLL_WAIT_RESULT(result, armTimer);
287-
}
288-
} while((err = errno) == EINTR);
289-
return EPOLL_WAIT_RESULT(-err, armTimer);
391+
result = netty_epoll_pwait2(env, efd, ev, len, &ts);
392+
return EPOLL_WAIT_RESULT(result, armTimer);
290393
}
291394

292395
int millis = tvNsec / 1000000;
@@ -298,7 +401,7 @@ static jlong netty_epoll_native_epollWait0(JNIEnv* env, jclass clazz, jint efd,
298401
millis >= millisThreshold ||
299402
tvSec > 0) {
300403
millis += tvSec * 1000;
301-
int result = netty_epoll_native_epollWait(env, clazz, efd, address, len, millis);
404+
result = netty_epoll_wait(env, efd, ev, len, millis);
302405
return EPOLL_WAIT_RESULT(result, armTimer);
303406
}
304407
}
@@ -312,7 +415,7 @@ static jlong netty_epoll_native_epollWait0(JNIEnv* env, jclass clazz, jint efd,
312415
}
313416
armTimer = 1;
314417
}
315-
int result = netty_epoll_native_epollWait(env, clazz, efd, address, len, -1);
418+
result = netty_epoll_wait(env, efd, ev, len, -1);
316419
return EPOLL_WAIT_RESULT(result, armTimer);
317420
}
318421

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.