@@ -249,23 +249,133 @@ static jint netty_epoll_native_epollCreate(JNIEnv* env, jclass clazz) { |
249 | 249 | return efd; |
250 | 250 | } |
251 | 251 |
|
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; |
255 | 267 |
|
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; |
260 | 271 | } |
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; |
263 | 370 | } |
264 | 371 |
|
265 | 372 | // This needs to be consistent with Native.java |
266 | 373 | #define EPOLL_WAIT_RESULT(V, ARM_TIMER) ((jlong) ((uint64_t) ((uint32_t) V) << 32 | ARM_TIMER)) |
267 | 374 |
|
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; |
269 | 379 | // only reschedule the timer if there is a newer event. |
270 | 380 | // -1 is a special value used by EpollEventLoop. |
271 | 381 | uint32_t armTimer = millisThreshold <= 0 ? 1 : 0; |
@@ -278,15 +388,8 @@ static jlong netty_epoll_native_epollWait0(JNIEnv* env, jclass clazz, jint efd, |
278 | 388 | // We have epoll_pwait2(...) and it is supported, this means we can just pass in the itimerspec directly and not need an |
279 | 389 | // extra syscall even for very small timeouts. |
280 | 390 | 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); |
290 | 393 | } |
291 | 394 |
|
292 | 395 | int millis = tvNsec / 1000000; |
@@ -298,7 +401,7 @@ static jlong netty_epoll_native_epollWait0(JNIEnv* env, jclass clazz, jint efd, |
298 | 401 | millis >= millisThreshold || |
299 | 402 | tvSec > 0) { |
300 | 403 | 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); |
302 | 405 | return EPOLL_WAIT_RESULT(result, armTimer); |
303 | 406 | } |
304 | 407 | } |
@@ -312,7 +415,7 @@ static jlong netty_epoll_native_epollWait0(JNIEnv* env, jclass clazz, jint efd, |
312 | 415 | } |
313 | 416 | armTimer = 1; |
314 | 417 | } |
315 | | - int result = netty_epoll_native_epollWait(env, clazz, efd, address, len, -1); |
| 418 | + result = netty_epoll_wait(env, efd, ev, len, -1); |
316 | 419 | return EPOLL_WAIT_RESULT(result, armTimer); |
317 | 420 | } |
318 | 421 |
|
|