@@ -14,29 +14,33 @@ import dev.opencode.mobile.markdown.emptyResult |
14 | 14 | import kotlinx.coroutines.CompletableDeferred |
15 | 15 | import kotlinx.coroutines.CoroutineDispatcher |
16 | 16 | import kotlinx.coroutines.CoroutineScope |
17 | | -import kotlinx.coroutines.Job |
18 | 17 | import kotlinx.coroutines.SupervisorJob |
19 | 18 | import kotlinx.coroutines.launch |
20 | 19 | import kotlinx.coroutines.sync.Mutex |
21 | 20 | import kotlinx.coroutines.sync.withLock |
22 | 21 | import kotlinx.coroutines.withContext |
| 22 | +import kotlinx.coroutines.yield |
23 | 23 | import kotlin.coroutines.cancellation.CancellationException |
24 | 24 |
|
25 | 25 | /** |
26 | 26 | * Android's asynchronous platform-to-KMP adapter. The dispatcher keeps TextMate |
27 | 27 | * off the UI executor. The shared [TextMateRequestScheduler] owns queue policy; |
28 | 28 | * this adapter drives at most one active highlight job. |
| 29 | + * |
| 30 | + * Only highlight jobs and cache trims run on [ownerDispatcher]. Admits take [owner] |
| 31 | + * on the caller's thread, so a newer revision can cancel the job that is running. |
29 | 32 | */ |
30 | 33 | class AndroidTextMateHighlightingEngine( |
31 | 34 | private val delegateFactory: () -> TextMateHighlightingEngine, |
32 | 35 | private val ownerDispatcher: CoroutineDispatcher, |
33 | 36 | private val scheduler: TextMateRequestScheduler = TextMateRequestScheduler(), |
34 | 37 | ) : TextMateHighlightingEngine { |
| 38 | + // Guards scheduler, waiters, and pumping; never held while a job runs. |
35 | 39 | private val owner = Mutex() |
36 | 40 | private val scope = CoroutineScope(SupervisorJob() + ownerDispatcher) |
37 | 41 | private val waiters = mutableMapOf<Long, CompletableDeferred<HighlightResult>>() |
38 | 42 | private var delegate: TextMateHighlightingEngine? = null |
39 | | - private var pumpJob: Job? = null |
| 43 | + private var pumping = false |
40 | 44 |
|
41 | 45 | constructor( |
42 | 46 | delegate: TextMateHighlightingEngine, |
@@ -50,102 +54,100 @@ class AndroidTextMateHighlightingEngine( |
50 | 54 |
|
51 | 55 | override suspend fun highlight(request: HighlightRequest): HighlightResult { |
52 | 56 | val deferred = |
53 | | - withContext(ownerDispatcher) { |
54 | | - owner.withLock { |
55 | | - val admit = scheduler.admit(request) |
56 | | - for (completion in admit.completed) { |
57 | | - waiters.remove(completion.ticketId)?.complete(completion.result) |
58 | | - } |
59 | | - val ticketId = admit.ticketId |
60 | | - if (ticketId == null) { |
61 | | - val rejected = |
62 | | - admit.completed |
63 | | - .lastOrNull { |
64 | | - it.request.key == request.key && it.request.revision == request.revision |
65 | | - }?.result |
66 | | - ?: admit.completed.last().result |
67 | | - return@withLock CompletableDeferred(rejected) |
68 | | - } |
69 | | - val waiter = CompletableDeferred<HighlightResult>() |
70 | | - waiters[ticketId] = waiter |
71 | | - ensurePumpLocked() |
72 | | - waiter |
| 57 | + owner.withLock { |
| 58 | + val admit = scheduler.admit(request) |
| 59 | + for (completion in admit.completed) { |
| 60 | + waiters.remove(completion.ticketId)?.complete(completion.result) |
73 | 61 | } |
| 62 | + val ticketId = admit.ticketId |
| 63 | + if (ticketId == null) { |
| 64 | + val rejected = |
| 65 | + admit.completed |
| 66 | + .lastOrNull { |
| 67 | + it.request.key == request.key && it.request.revision == request.revision |
| 68 | + }?.result |
| 69 | + ?: admit.completed.last().result |
| 70 | + return@withLock CompletableDeferred(rejected) |
| 71 | + } |
| 72 | + val waiter = CompletableDeferred<HighlightResult>() |
| 73 | + waiters[ticketId] = waiter |
| 74 | + ensurePumpLocked() |
| 75 | + waiter |
74 | 76 | } |
75 | 77 | return deferred.await() |
76 | 78 | } |
77 | 79 |
|
78 | 80 | override suspend fun trim(level: MemoryPressure) { |
79 | | - withContext(ownerDispatcher) { |
80 | | - owner.withLock { |
81 | | - for (completion in scheduler.trim(level)) { |
82 | | - waiters.remove(completion.ticketId)?.complete(completion.result) |
83 | | - } |
84 | | - ownedDelegate().trim(level) |
| 81 | + owner.withLock { |
| 82 | + for (completion in scheduler.trim(level)) { |
| 83 | + waiters.remove(completion.ticketId)?.complete(completion.result) |
85 | 84 | } |
86 | 85 | } |
| 86 | + withContext(ownerDispatcher) { |
| 87 | + ownedDelegate().trim(level) |
| 88 | + } |
87 | 89 | } |
88 | 90 |
|
89 | 91 | suspend fun setLowPowerOrThermal(disabled: Boolean) { |
90 | | - withContext(ownerDispatcher) { |
91 | | - owner.withLock { |
92 | | - scheduler.setLowPowerOrThermal(disabled) |
93 | | - } |
| 92 | + owner.withLock { |
| 93 | + scheduler.setLowPowerOrThermal(disabled) |
94 | 94 | } |
95 | 95 | } |
96 | 96 |
|
97 | 97 | override fun schedulerSnapshot(): TextMateSchedulerSnapshot = scheduler.snapshot() |
98 | 98 |
|
99 | 99 | private fun ensurePumpLocked() { |
100 | | - if (pumpJob?.isActive == true) return |
101 | | - pumpJob = |
102 | | - scope.launch { |
103 | | - while (true) { |
104 | | - val job = |
105 | | - owner.withLock { |
106 | | - scheduler.pickNext() |
107 | | - } ?: break |
108 | | - val raw = |
109 | | - try { |
110 | | - when (val active = ownedDelegate()) { |
111 | | - is OpenCodeTextMateHighlightingEngine -> |
112 | | - active.highlightNow(job.request, shouldCancel = job.cancelRequested) |
113 | | - else -> active.highlight(job.request) |
114 | | - } |
115 | | - } catch (cancelled: CancellationException) { |
116 | | - throw cancelled |
117 | | - } catch (error: Throwable) { |
118 | | - emptyResult( |
119 | | - request = job.request, |
120 | | - outcome = TextMateParseOutcome.Failed, |
121 | | - queueWaitNanoseconds = job.queueWaitNanoseconds, |
122 | | - schedulingReason = null, |
123 | | - ).copy( |
124 | | - diagnostics = |
125 | | - listOf( |
126 | | - MarkdownDiagnostic( |
127 | | - kind = MarkdownDiagnosticKind.HighlighterFallback, |
128 | | - message = error.message ?: "TextMate highlighting failed.", |
129 | | - ), |
130 | | - ), |
131 | | - observation = |
132 | | - emptyResult( |
133 | | - request = job.request, |
134 | | - outcome = TextMateParseOutcome.Failed, |
135 | | - queueWaitNanoseconds = job.queueWaitNanoseconds, |
136 | | - schedulingReason = null, |
137 | | - ).observation.copy( |
138 | | - diagnosticKinds = listOf(MarkdownDiagnosticKind.HighlighterFallback), |
139 | | - ), |
140 | | - ) |
141 | | - } |
| 100 | + if (pumping) return |
| 101 | + pumping = true |
| 102 | + scope.launch { |
| 103 | + while (true) { |
| 104 | + val job = |
142 | 105 | owner.withLock { |
143 | | - for (completion in scheduler.finishActive(job.ticketId, raw)) { |
144 | | - waiters.remove(completion.ticketId)?.complete(completion.result) |
| 106 | + scheduler.pickNext().also { next -> if (next == null) pumping = false } |
| 107 | + } ?: break |
| 108 | + val raw = |
| 109 | + try { |
| 110 | + when (val active = ownedDelegate()) { |
| 111 | + is OpenCodeTextMateHighlightingEngine -> |
| 112 | + active.highlightNow(job.request, shouldCancel = job.cancelRequested) |
| 113 | + else -> active.highlight(job.request) |
145 | 114 | } |
| 115 | + } catch (cancelled: CancellationException) { |
| 116 | + throw cancelled |
| 117 | + } catch (error: Throwable) { |
| 118 | + emptyResult( |
| 119 | + request = job.request, |
| 120 | + outcome = TextMateParseOutcome.Failed, |
| 121 | + queueWaitNanoseconds = job.queueWaitNanoseconds, |
| 122 | + schedulingReason = null, |
| 123 | + ).copy( |
| 124 | + diagnostics = |
| 125 | + listOf( |
| 126 | + MarkdownDiagnostic( |
| 127 | + kind = MarkdownDiagnosticKind.HighlighterFallback, |
| 128 | + message = error.message ?: "TextMate highlighting failed.", |
| 129 | + ), |
| 130 | + ), |
| 131 | + observation = |
| 132 | + emptyResult( |
| 133 | + request = job.request, |
| 134 | + outcome = TextMateParseOutcome.Failed, |
| 135 | + queueWaitNanoseconds = job.queueWaitNanoseconds, |
| 136 | + schedulingReason = null, |
| 137 | + ).observation.copy( |
| 138 | + diagnosticKinds = listOf(MarkdownDiagnosticKind.HighlighterFallback), |
| 139 | + ), |
| 140 | + ) |
| 141 | + } |
| 142 | + owner.withLock { |
| 143 | + for (completion in scheduler.finishActive(job.ticketId, raw)) { |
| 144 | + waiters.remove(completion.ticketId)?.complete(completion.result) |
146 | 145 | } |
147 | 146 | } |
| 147 | + // Let a queued cache trim run on the owner dispatcher between jobs. |
| 148 | + yield() |
148 | 149 | } |
| 150 | + } |
149 | 151 | } |
150 | 152 |
|
151 | 153 | private fun ownedDelegate(): TextMateHighlightingEngine = delegate ?: delegateFactory().also { created -> delegate = created } |
|