Scoop API

scoop:scoop.concurrent:0.1.0

src/worker_group.scoop

Selected production source · src/worker_group.scoop

1package scoop.concurrent
3import scoop.collections.ArrayDeque
4import scoop.platform.concurrent.ConditionVariable
5import scoop.platform.concurrent.Mutex
6import scoop.platform.concurrent.Thread
7import scoop.platform.concurrent.ThreadId
8import scoop.platform.concurrent.currentThreadId
10// Blocking work has its own FIFO and does not participate in coroutine work stealing.
11internal 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 = false
19 private var stopped = false
20 private var joining = false
21 private var joined = false
22 private var live = 0
24 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 group
31 }
32 }
34 private fun start(threads: Int) {
35 var index = 0
36 try {
37 while (index < threads) {
38 val record = WorkerRecord()
39 lock.withLock {
40 workers.add(record)
41 live += 1
42 }
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 error
49 }
50 index += 1
51 }
52 } catch (error: Throwable) {
53 requestShutdown()
54 join()
55 throw error
56 }
57 }
59 internal fun ensureOpen() {
60 lock.withLock {
61 if (stopping)
62 throw ExecutorClosedException()
63 Unit
64 }
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 = true
86 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 true
99 case None -> {}
100 }
101 }
102 false
103 }
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 false
126 joining = true
127 true
128 }
129 if (!perform)
130 return
131 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 = None
140 }
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 -> return
168 }
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 = true
188 terminationListeners.finishLocked(notifications)
189 notifications.add { terminated.close() }
190 }
191 return notifications
192 }
195private class WorkerRecord {
196 var thread: Thread? = None
197 var id: ThreadId? = None