File tree Expand file tree Collapse file tree 3 files changed +55
-12
lines changed
app/src/main/java/com/kylecorry/trail_sense/shared Expand file tree Collapse file tree 3 files changed +55
-12
lines changed Original file line number Diff line number Diff line change 1+ package com.kylecorry.trail_sense.shared.andromeda_temp
2+
3+ import kotlinx.coroutines.coroutineScope
4+ import kotlinx.coroutines.launch
5+ import kotlinx.coroutines.sync.Semaphore
6+ import kotlinx.coroutines.sync.withPermit
7+
8+ class ParallelCoroutineRunner2 (maxParallel : Int = 8 ) {
9+
10+ private val semaphore = Semaphore (maxParallel)
11+
12+ suspend fun run (coroutines : List <suspend () -> Any >) = coroutineScope {
13+ for (coroutine in coroutines) {
14+ launch {
15+ semaphore.withPermit { coroutine() }
16+ }
17+ }
18+ }
19+
20+ suspend fun <R > run (items : List <R >, coroutine : suspend (R ) -> Unit ) {
21+ run (items.map { { coroutine(it) } })
22+ }
23+
24+ suspend fun <T > map (coroutines : List <suspend () -> T >): List <T > {
25+ val items = mutableListOf<Pair <Int , T >>()
26+ val lock = Any ()
27+
28+ run (coroutines.mapIndexed { index, coroutine ->
29+ {
30+ val item = coroutine()
31+ synchronized(lock) {
32+ items.add(index to item)
33+ }
34+ }
35+ })
36+
37+ return items.sortedBy { it.first }.map { it.second }
38+ }
39+
40+ suspend fun <T > mapFunctions (functions : List <() -> T >): List <T > {
41+ return map(functions.map { { it() } })
42+ }
43+
44+ suspend fun <R , T > map (items : List <R >, coroutine : suspend (R ) -> T ): List <T > {
45+ return map(items.map { { coroutine(it) } })
46+ }
47+
48+ }
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import com.kylecorry.andromeda.core.cache.AppServiceRegistry
44import com.kylecorry.luna.coroutines.CoroutineQueueRunner
55import com.kylecorry.luna.coroutines.ParallelCoroutineRunner
66import com.kylecorry.sol.science.geology.CoordinateBounds
7+ import com.kylecorry.trail_sense.shared.andromeda_temp.ParallelCoroutineRunner2
78import com.kylecorry.trail_sense.shared.andromeda_temp.grow
89import com.kylecorry.trail_sense.shared.device.DeviceSubsystem
910import com.kylecorry.trail_sense.shared.map_layers.tiles.TileMath
@@ -62,12 +63,8 @@ class MapLayerBackgroundTask {
6263 val taskCopy = synchronized(taskLock) {
6364 tasks.toList()
6465 }
65- for (task in taskCopy) {
66- task(bounds, metersPerPixel)
67- }
68- // TODO: Parallel execution is not working properly - I think it's cancellation related
69- // val parallel = ParallelCoroutineRunner()
70- // parallel.run(taskCopy.map { { it(bounds, metersPerPixel) } })
66+ val parallel = ParallelCoroutineRunner2 ()
67+ parallel.run(taskCopy.map { { it(bounds, metersPerPixel) } })
7168 }
7269 ) {
7370
Original file line number Diff line number Diff line change @@ -2,8 +2,10 @@ package com.kylecorry.trail_sense.shared.map_layers
22
33import com.kylecorry.andromeda.core.cache.AppServiceRegistry
44import com.kylecorry.luna.coroutines.CoroutineQueueRunner
5+ import com.kylecorry.luna.coroutines.ParallelCoroutineRunner
56import com.kylecorry.sol.math.geometry.Rectangle
67import com.kylecorry.sol.science.geology.CoordinateBounds
8+ import com.kylecorry.trail_sense.shared.andromeda_temp.ParallelCoroutineRunner2
79import com.kylecorry.trail_sense.shared.andromeda_temp.grow
810import com.kylecorry.trail_sense.shared.device.DeviceSubsystem
911import com.kylecorry.trail_sense.shared.map_layers.tiles.TileMath
@@ -64,12 +66,8 @@ class MapLayerBackgroundTask2 {
6466 val taskCopy = synchronized(taskLock) {
6567 tasks.toList()
6668 }
67- for (task in taskCopy) {
68- task(viewBounds, bounds, projection)
69- }
70- // TODO: Parallel execution is not working properly - I think it's cancellation related
71- // val parallel = ParallelCoroutineRunner()
72- // parallel.run(taskCopy.map { { it(viewBounds, bounds, projection) } })
69+ val parallel = ParallelCoroutineRunner2 ()
70+ parallel.run(taskCopy.map { { it(viewBounds, bounds, projection) } })
7371 }
7472 ) {
7573
You can’t perform that action at this time.
0 commit comments