New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loom support. #1176
Merged
Merged
Loom support. #1176
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d2a4774
[Experiment] Loom support.
yschimke 16f8d45
Simplify
yschimke 5344e6a
Merge branch 'master' into loom
yschimke a7d8ffd
Review comments
yschimke 72899bf
More fixes.
yschimke dade57f
Add a test.
yschimke 22516e5
Fix formatting.
yschimke 1dbddda
Run loom tests
yschimke ff74f80
Run loom tests
yschimke 5923071
Cleanup
yschimke 6718e0c
Merge branch 'master' into loom
yschimke a67f4fc
Update okio/src/jvmTest/java/okio/AwaitSignalTest.kt
yschimke bfd0795
Update okio/src/jvmMain/kotlin/okio/Timeout.kt
yschimke 760daa8
Update okio/src/jvmTest/java/okio/AwaitSignalTest.kt
yschimke 5335124
Update okio/src/jvmMain/kotlin/okio/Timeout.kt
yschimke 29667df
Update okio/src/jvmMain/kotlin/okio/Timeout.kt
yschimke 2d530d5
Review comments
yschimke File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// If true - tests should run for a loom environment. | ||
val loomEnabled = System.getProperty("loomEnabled", "false").toBoolean() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
okio-testing-support/src/jvmMain/kotlin/okio/TestingExecutors.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (C) 2023 Block, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package okio | ||
|
||
import java.util.concurrent.ExecutorService | ||
import java.util.concurrent.Executors | ||
import java.util.concurrent.ScheduledExecutorService | ||
import java.util.concurrent.ThreadFactory | ||
|
||
object TestingExecutors { | ||
val isLoom = System.getProperty("loomEnabled").toBoolean() | ||
|
||
fun newScheduledExecutorService(corePoolSize: Int = 0): ScheduledExecutorService = if (isLoom) { | ||
Executors.newScheduledThreadPool(corePoolSize, newVirtualThreadFactory()) | ||
} else { | ||
Executors.newScheduledThreadPool(corePoolSize) | ||
} | ||
|
||
fun newExecutorService(corePoolSize: Int = 0): ExecutorService = if (isLoom) { | ||
Executors.newScheduledThreadPool(corePoolSize, newVirtualThreadFactory()) | ||
} else { | ||
Executors.newScheduledThreadPool(corePoolSize) | ||
} | ||
|
||
fun newVirtualThreadFactory(): ThreadFactory { | ||
val threadBuilder = Thread::class.java.getMethod("ofVirtual").invoke(null) | ||
return Class.forName("java.lang.Thread\$Builder").getMethod("factory").invoke(threadBuilder) as ThreadFactory | ||
} | ||
|
||
fun newVirtualThreadPerTaskExecutor(): ExecutorService { | ||
return Executors::class.java.getMethod("newVirtualThreadPerTaskExecutor").invoke(null) as ExecutorService | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,9 @@ package okio | |
import java.io.IOException | ||
import java.io.InterruptedIOException | ||
import java.util.concurrent.TimeUnit | ||
import java.util.concurrent.locks.Condition | ||
import java.util.concurrent.locks.ReentrantLock | ||
import kotlin.concurrent.withLock | ||
|
||
/** | ||
* This timeout uses a background thread to take action exactly when the timeout occurs. Use this to | ||
|
@@ -179,7 +182,7 @@ open class AsyncTimeout : Timeout() { | |
while (true) { | ||
try { | ||
var timedOut: AsyncTimeout? = null | ||
synchronized(AsyncTimeout::class.java) { | ||
AsyncTimeout.lock.withLock { | ||
timedOut = awaitTimeout() | ||
|
||
// The queue is completely empty. Let this thread exit and let another watchdog thread | ||
|
@@ -199,6 +202,9 @@ open class AsyncTimeout : Timeout() { | |
} | ||
|
||
companion object { | ||
val lock: ReentrantLock = ReentrantLock() | ||
val condition: Condition = lock.newCondition() | ||
|
||
/** | ||
* Don't write more than 64 KiB of data at a time, give or take a segment. Otherwise slow | ||
* connections may suffer timeouts even when they're making (slow) progress. Without this, | ||
|
@@ -221,7 +227,7 @@ open class AsyncTimeout : Timeout() { | |
private var head: AsyncTimeout? = null | ||
|
||
private fun scheduleTimeout(node: AsyncTimeout, timeoutNanos: Long, hasDeadline: Boolean) { | ||
synchronized(AsyncTimeout::class.java) { | ||
AsyncTimeout.lock.withLock { | ||
check(!node.inQueue) { "Unbalanced enter/exit" } | ||
node.inQueue = true | ||
|
||
|
@@ -253,7 +259,7 @@ open class AsyncTimeout : Timeout() { | |
prev.next = node | ||
if (prev === head) { | ||
// Wake up the watchdog when inserting at the front. | ||
(AsyncTimeout::class.java as Object).notify() | ||
condition.signal() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice |
||
} | ||
break | ||
} | ||
|
@@ -264,7 +270,7 @@ open class AsyncTimeout : Timeout() { | |
|
||
/** Returns true if the timeout occurred. */ | ||
private fun cancelScheduledTimeout(node: AsyncTimeout): Boolean { | ||
synchronized(AsyncTimeout::class.java) { | ||
AsyncTimeout.lock.withLock { | ||
if (!node.inQueue) return false | ||
node.inQueue = false | ||
|
||
|
@@ -299,7 +305,7 @@ open class AsyncTimeout : Timeout() { | |
// The queue is empty. Wait until either something is enqueued or the idle timeout elapses. | ||
if (node == null) { | ||
val startNanos = System.nanoTime() | ||
(AsyncTimeout::class.java as Object).wait(IDLE_TIMEOUT_MILLIS) | ||
condition.await(IDLE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS) | ||
return if (head!!.next == null && System.nanoTime() - startNanos >= IDLE_TIMEOUT_NANOS) { | ||
head // The idle timeout elapsed. | ||
} else { | ||
|
@@ -311,11 +317,7 @@ open class AsyncTimeout : Timeout() { | |
|
||
// The head of the queue hasn't timed out yet. Await that. | ||
if (waitNanos > 0) { | ||
// Waiting is made complicated by the fact that we work in nanoseconds, | ||
// but the API wants (millis, nanos) in two arguments. | ||
val waitMillis = waitNanos / 1000000L | ||
waitNanos -= waitMillis * 1000000L | ||
(AsyncTimeout::class.java as Object).wait(waitMillis, waitNanos.toInt()) | ||
condition.await(waitNanos, TimeUnit.NANOSECONDS) | ||
return null | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is rad