You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/scheduling/CoroutineScheduler.kt
+18-7Lines changed: 18 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -366,9 +366,16 @@ internal class CoroutineScheduler(
366
366
val created = createdWorkers(state)
367
367
val blocking = blockingWorkers(state)
368
368
val cpuWorkers = created - blocking
369
-
// If most of created workers are blocking, we should create one more thread to handle non-blocking work
370
-
if (cpuWorkers < corePoolSize && createNewWorker()) {
371
-
return
369
+
/*
370
+
* We check how many threads are there to handle non-blocking work,
371
+
* and create one more if we have not enough of them.
372
+
*/
373
+
if (cpuWorkers < corePoolSize) {
374
+
val newCpuWorkers = createNewWorker()
375
+
// If we've created the first cpu worker and corePoolSize > 1 then create
376
+
// one more (second) cpu worker, so that stealing between them is operational
377
+
if (newCpuWorkers ==1&& corePoolSize >1) createNewWorker()
378
+
if (newCpuWorkers >0) return
372
379
}
373
380
// Try unpark again in case there was race between permit release and parking
374
381
tryUnpark()
@@ -418,7 +425,11 @@ internal class CoroutineScheduler(
418
425
}
419
426
}
420
427
421
-
privatefuncreateNewWorker(): Boolean {
428
+
/*
429
+
* Returns the number of CPU workers after this function (including new worker) or
430
+
* 0 if no worker was created.
431
+
*/
432
+
privatefuncreateNewWorker(): Int {
422
433
synchronized(workers) {
423
434
// for test purposes make sure we're not trying to resurrect terminated scheduler
424
435
require(!isTerminated.value) { "This scheduler was terminated "}
@@ -427,14 +438,14 @@ internal class CoroutineScheduler(
427
438
val blocking = blockingWorkers(state)
428
439
val cpuWorkers = created - blocking
429
440
// Double check for overprovision
430
-
if (cpuWorkers >= corePoolSize) returnfalse
431
-
if (created >= maxPoolSize || cpuPermits.availablePermits() ==0) returnfalse
441
+
if (cpuWorkers >= corePoolSize) return0
442
+
if (created >= maxPoolSize || cpuPermits.availablePermits() ==0) return0
Copy file name to clipboardExpand all lines: core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/scheduling/SchedulerTestBase.kt
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -90,6 +90,8 @@ abstract class SchedulerTestBase : TestBase() {
0 commit comments