Skip to content

Commit 154375b

Browse files
authored
Reduce calls to nanoTime in Timeout.awaitSignal() (#1390)
The function returns a non-positive value if the call timed out, so there's no need to measure that independently.
1 parent c922e82 commit 154375b

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

okio/src/jvmMain/kotlin/okio/Timeout.kt

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,27 +161,21 @@ actual open class Timeout {
161161
}
162162

163163
// Compute how long we'll wait.
164-
val start = System.nanoTime()
165164
val waitNanos = if (hasDeadline && timeoutNanos != 0L) {
166-
val deadlineNanos = deadlineNanoTime() - start
165+
val deadlineNanos = deadlineNanoTime() - System.nanoTime()
167166
minOf(timeoutNanos, deadlineNanos)
168167
} else if (hasDeadline) {
169-
deadlineNanoTime() - start
168+
deadlineNanoTime() - System.nanoTime()
170169
} else {
171170
timeoutNanos
172171
}
173172

174-
// Attempt to wait that long. This will break out early if the monitor is notified.
175-
var elapsedNanos = 0L
176-
if (waitNanos > 0L) {
177-
condition.await(waitNanos, TimeUnit.NANOSECONDS)
178-
elapsedNanos = System.nanoTime() - start
179-
}
173+
if (waitNanos <= 0) throw InterruptedIOException("timeout")
180174

181-
// Throw if the timeout elapsed before the monitor was notified.
182-
if (elapsedNanos >= waitNanos) {
183-
throw InterruptedIOException("timeout")
184-
}
175+
// Attempt to wait that long. This will return early if the monitor is notified.
176+
val nanosRemaining = condition.awaitNanos(waitNanos)
177+
178+
if (nanosRemaining <= 0) throw InterruptedIOException("timeout")
185179
} catch (e: InterruptedException) {
186180
Thread.currentThread().interrupt() // Retain interrupted status.
187181
throw InterruptedIOException("interrupted")

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy