Skip to content
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

Acquire fewer locks in TaskRunner #8394

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 32 additions & 37 deletions okhttp/src/main/kotlin/okhttp3/internal/concurrent/TaskRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,36 @@ class TaskRunner(
private val runnable: Runnable =
object : Runnable {
override fun run() {
var incrementedRunCallCount = false
while (true) {
val task =
this@TaskRunner.lock.withLock {
if (!incrementedRunCallCount) {
incrementedRunCallCount = true
runCallCount++
var task: Task =
lock.withLock {
runCallCount++
awaitTaskToRun()
} ?: return

val currentThread = Thread.currentThread()
val oldName = currentThread.name
try {
while (true) {
currentThread.name = task.name
val delayNanos =
logger.logElapsed(task, task.queue!!) {
task.runOnce()
}

// A task ran successfully. Update the execution state and take the next task.
task = lock.withLock {
afterRun(task, delayNanos, true)
awaitTaskToRun()
} ?: return

logger.logElapsed(task, task.queue!!) {
var completedNormally = false
try {
runTask(task)
completedNormally = true
} finally {
// If the task is crashing start another thread to service the queues.
if (!completedNormally) {
lock.withLock {
startAnotherThread()
}
}
}
}
} catch (thrown: Throwable) {
// A task failed. Update execution state and re-throw the exception.
lock.withLock {
afterRun(task, -1L, false)
}
throw thrown
} finally {
currentThread.name = oldName
}
}
}
Expand Down Expand Up @@ -130,25 +135,10 @@ class TaskRunner(
busyQueues.add(queue)
}

private fun runTask(task: Task) {
val currentThread = Thread.currentThread()
val oldName = currentThread.name
currentThread.name = task.name

var delayNanos = -1L
try {
delayNanos = task.runOnce()
} finally {
lock.withLock {
afterRun(task, delayNanos)
}
currentThread.name = oldName
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also change the thread name fewer times!

}
}

private fun afterRun(
task: Task,
delayNanos: Long,
completedNormally: Boolean,
) {
lock.assertHeld()

Expand All @@ -166,6 +156,11 @@ class TaskRunner(

if (queue.futureTasks.isNotEmpty()) {
readyQueues.add(queue)

// If the task crashed, start another thread to run the next task.
if (!completedNormally) {
startAnotherThread()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ class TaskRunnerTest {
assertThat(testLogHandler.takeAll()).containsExactly(
"FINE: Q10000 scheduled after 100 µs: task",
"FINE: Q10000 starting : task",
"FINE: Q10000 run again after 50 µs: task",
"FINE: Q10000 finished run in 0 µs: task",
"FINE: Q10000 run again after 50 µs: task",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this new order better

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 more logical

"FINE: Q10000 starting : task",
"FINE: Q10000 run again after 150 µs: task",
"FINE: Q10000 finished run in 0 µs: task",
"FINE: Q10000 run again after 150 µs: task",
"FINE: Q10000 starting : task",
"FINE: Q10000 finished run in 0 µs: task",
)
Expand Down Expand Up @@ -137,8 +137,8 @@ class TaskRunnerTest {
"FINE: Q10000 scheduled after 100 µs: task",
"FINE: Q10000 starting : task",
"FINE: Q10000 scheduled after 50 µs: task",
"FINE: Q10000 already scheduled : task",
"FINE: Q10000 finished run in 0 µs: task",
"FINE: Q10000 already scheduled : task",
"FINE: Q10000 starting : task",
"FINE: Q10000 finished run in 0 µs: task",
)
Expand Down Expand Up @@ -176,8 +176,8 @@ class TaskRunnerTest {
"FINE: Q10000 scheduled after 100 µs: task",
"FINE: Q10000 starting : task",
"FINE: Q10000 scheduled after 200 µs: task",
"FINE: Q10000 run again after 50 µs: task",
"FINE: Q10000 finished run in 0 µs: task",
"FINE: Q10000 run again after 50 µs: task",
"FINE: Q10000 starting : task",
"FINE: Q10000 finished run in 0 µs: task",
)
Expand Down Expand Up @@ -306,8 +306,8 @@ class TaskRunnerTest {
assertThat(testLogHandler.takeAll()).containsExactly(
"FINE: Q10000 scheduled after 100 µs: task",
"FINE: Q10000 starting : task",
"FINE: Q10000 run again after 50 µs: task",
"FINE: Q10000 finished run in 0 µs: task",
"FINE: Q10000 run again after 50 µs: task",
"FINE: Q10000 starting : task",
"FINE: Q10000 finished run in 0 µs: task",
)
Expand Down