scoop:scoop.concurrent:0.1.0
src/worker_group.scoop
Selected production source · src/worker_group.scoop
1
package scoop.concurrent3
import scoop.collections.ArrayDeque4
import scoop.platform.concurrent.ConditionVariable5
import scoop.platform.concurrent.Mutex6
import scoop.platform.concurrent.Thread7
import scoop.platform.concurrent.ThreadId8
import scoop.platform.concurrent.currentThreadId10
// Blocking work has its own FIFO and does not participate in coroutine work stealing.11
internal class WorkerGroup private constructor() {12
private val lock = Mutex()13
private val changed = ConditionVariable(lock)14
private val queue = ArrayDeque<() -> Unit>()15
private val workers = ArrayList<WorkerRecord>()16
private val terminated = Channel<Unit>(0L)17
private val terminationListeners = TerminationListeners(lock)18
private var stopping = false19
private var stopped = false20
private var joining = false21
private var joined = false22
private var live = 024
internal companion object {25
internal fun create(threads: Int): WorkerGroup {26
if (threads <= 0)27
throw IllegalArgumentException(Some("worker count must be positive"))28
val group = WorkerGroup()29
group.start(threads)30
return group31
}32
}34
private fun start(threads: Int) {35
var index = 036
try {37
while (index < threads) {38
val record = WorkerRecord()39
lock.withLock {40
workers.add(record)41
live += 142
}43
try {44
val thread = Thread.start { run(record) }45
lock.withLock { record.thread = Some(thread) }46
} catch (error: Throwable) {47
lock.withLock { live -= 1 }48
throw error49
}50
index += 151
}52
} catch (error: Throwable) {53
requestShutdown()54
join()55
throw error56
}57
}59
internal fun ensureOpen() {60
lock.withLock {61
if (stopping)62
throw ExecutorClosedException()63
Unit64
}65
}67
internal fun submit(work: () -> Unit) {68
lock.withLock {69
if (stopping)70
throw ExecutorClosedException()71
queue.addLast(work)72
changed.signal()73
}74
}76
internal val isTerminated: Boolean get() = lock.withLock { stopped }77
internal val isShutdown: Boolean get() = lock.withLock { stopping }79
internal fun onTermination(listener: () -> Unit): Disposable =80
terminationListeners.subscribe(listener)82
internal fun requestShutdown() {83
val notifications =84
lock.withLock {85
stopping = true86
changed.broadcast()87
finishIfEmpty()88
}89
notifications.deliver()90
}92
internal val isCurrentWorker: Boolean get() {93
val current = currentThreadId()94
return lock.withLock {95
for (record in workers) {96
when (record.id) {97
case Some(val id) -> if (id == current)98
return@withLock true99
case None -> {}100
}101
}102
false103
}104
}106
internal fun checkNotWorker() {107
if (isCurrentWorker)108
throw IllegalStateException(Some("worker cannot wait for its own group"))109
}111
internal suspend fun awaitTermination() {112
checkNotWorker()113
terminated.receiveOrNone()114
}116
internal fun join() {117
checkNotWorker()118
val perform =119
lock.withLock {120
if (!stopping)121
throw IllegalStateException(Some("request shutdown before join"))122
while (joining)123
changed.await()124
if (joined)125
return@withLock false126
joining = true127
true128
}129
if (!perform)130
return131
try {132
for (record in workers) {133
val handle = lock.withLock { record.thread }134
when (handle) {135
case Some(val thread) -> {136
thread.join()137
lock.withLock {138
record.thread = None;139
record.id = None140
}141
}142
case None -> {}143
}144
}145
lock.withLock { joined = true }146
} finally {147
lock.withLock {148
joining = false;149
changed.broadcast()150
}151
}152
}154
private fun run(record: WorkerRecord) {155
try {156
lock.withLock { record.id = Some(currentThreadId()) }157
try {158
while (true) {159
val next =160
lock.withLock {161
while (queue.size == 0L && !stopping)162
changed.await()163
queue.removeFirstOrNone()164
}165
when (next) {166
case Some(val work) -> work()167
case None -> return168
}169
}170
} finally {171
val notifications =172
lock.withLock {173
live -= 1;174
finishIfEmpty()175
}176
notifications.deliver()177
}178
} catch (error: Throwable) {179
error.printStackTrace()180
exit(1)181
}182
}184
private fun finishIfEmpty(): WaitNotifications {185
val notifications = WaitNotifications()186
if (stopping && live == 0 && !stopped) {187
stopped = true188
terminationListeners.finishLocked(notifications)189
notifications.add { terminated.close() }190
}191
return notifications192
}193
}195
private class WorkerRecord {196
var thread: Thread? = None197
var id: ThreadId? = None198
}