-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbthreads.ts
350 lines (318 loc) · 9.11 KB
/
bthreads.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
* A TypeScript implementation of behavioral threads (b-threads) with async operation support,
* using Effection for structured concurrency.
*/
import {
Operation,
Task,
Channel,
Result,
spawn,
createChannel,
call,
} from "npm:effection"
/**
* The state of an executable operation in a thread
*/
export type Exec<Event> =
| { state: "none" }
| { state: "pending"; op: () => Operation<Event> }
| { state: "running"; task: Task<void> }
| { state: "done"; result: Result<Event> }
/**
* Thread synchronization specification at a sync point
*/
export interface Sync<Event> {
/** Events to request */
post: Event[]
/** Predicate for events to wait for */
wait: (event: Event) => boolean
/** Predicate for events to block */
halt: (event: Event) => boolean
/** Optional async operation to execute */
exec: Exec<Event>
}
/**
* A behavioral thread instance
*/
export interface Thread<Event> {
name: string
sync: Sync<Event>
proc: Generator<Sync<Event>, void, Event>
prio: number
}
/**
* Configuration for creating a sync point
*/
export interface SyncOptions<Event> {
/** Events to request */
post?: Event[]
/** Predicate for events to wait for */
wait?: (event: Event) => boolean
/** Predicate for events to block */
halt?: (event: Event) => boolean
/** Async operation to execute */
exec?: () => Operation<Event>
}
/**
* Creates a new sync point specification
*/
export function makeSyncSpec<Event>({
post = [],
wait = () => false,
halt = () => false,
exec,
}: Partial<SyncOptions<Event>>): Sync<Event> {
return {
post,
wait,
halt,
exec: exec ? { state: "pending", op: exec } : { state: "none" },
}
}
/**
* Configuration for creating a behavioral thread
*/
export interface ThreadOptions<Event> {
/** Thread name for debugging */
name: string
/** Thread priority (higher runs first) */
prio?: number
/** Thread behavior generator */
behavior: () => Generator<Sync<Event>, void, Event>
}
/**
* Creates a new behavioral thread
*/
function makeThread<Event>({
name,
prio = 1,
behavior,
}: ThreadOptions<Event>): Thread<Event> {
const proc = behavior()
const { done, value: sync } = proc.next()
return done ? createEmptyThread(name) : { name, prio, proc, sync }
}
/**
* Creates an empty thread (useful for completion)
*/
function createEmptyThread<Event>(name: string): Thread<Event> {
return {
name,
proc: (function* () {})(),
sync: makeSyncSpec({}),
prio: 0,
}
}
/**
* Starts a thread's pending operation if it has one.
* The operation runs until either:
* 1. It completes naturally, in which case its result becomes a post
* 2. The thread receives an event it's waiting for, which halts the operation
*/
function* startThreadOperationIfNecessary<Event>(
thread: Thread<Event>,
completionChannel: Channel<void, void>
) {
if (thread.sync.exec.state === "pending") {
const operation = thread.sync.exec.op
const operationTask = yield* spawn(function* () {
try {
const x = yield* operation()
markExecutionAsDone(x)
} catch (e: unknown) {
console.error("Thread operation error", thread.name, e)
markExecutionAsFailed(e)
} finally {
yield* completionChannel.send()
}
})
markOperationAsRunning(operationTask)
}
function markOperationAsRunning(operationTask: Task<void>) {
thread.sync.exec = { state: "running", task: operationTask }
}
function markExecutionAsFailed(e: unknown) {
thread.sync.exec = {
state: "done",
result: { ok: false, error: e as Error },
}
}
function markExecutionAsDone(x: Event) {
thread.sync.exec = {
state: "done",
result: { ok: true, value: x },
}
}
}
/**
* Core scheduling function that:
* 1. Processes completed operations, turning their results into posts
* 2. Selects the highest priority non-blocked requested event
* 3. Advances threads that posted or were waiting for the selected event
*/
function* schedule<Event>(
threads: Set<Thread<Event>>,
notify: Channel<void, void>
): Operation<boolean> {
let didWork = false
// Handle completed operations
for (const thread of threads) {
if (thread.sync.exec.state === "done") {
const { result } = thread.sync.exec
if (result.ok) {
thread.sync.exec = { state: "none" }
thread.sync.post = [result.value]
} else {
const { done, value: sync } = thread.proc.throw(result.error)
if (done) {
threads.delete(thread)
} else {
thread.sync = sync
yield* startThreadOperationIfNecessary(thread, notify)
}
}
didWork = true
}
}
// Select next event
const selectedEvent = [...threads]
.sort((a, b) => b.prio - a.prio)
.flatMap((x) => x.sync.post)
.find(
(x) =>
![...threads].some((y) => {
const halted = y.sync.halt(x)
if (halted) {
console.debug(x, "halted by", y.name)
}
return halted
})
)
if (selectedEvent) {
console.debug(`Selected event`, selectedEvent)
// Advance threads affected by selected event
for (const thread of threads) {
const { post, wait, exec } = thread.sync
if (post.includes(selectedEvent) || wait(selectedEvent)) {
if (exec.state === "running") {
yield* exec.task.halt()
thread.sync.exec = { state: "none" }
}
const { done, value } = thread.proc.next(selectedEvent)
if (done) {
threads.delete(thread)
} else {
thread.sync = value
yield* startThreadOperationIfNecessary(thread, notify)
}
}
}
didWork = true
} else {
console.debug("No event selected")
}
return didWork
}
/**
* Creates and runs a system of behavioral threads.
*
* The system operation:
* 1. Accepts a body that can spawn threads using the provided thread factory
* 2. Maintains a set of active threads and their synchronization states
* 3. Coordinates threads through a turn-based event selection process where:
* - Threads declare events they request, wait for, or block
* - The system selects a non-blocked requested event
* - Affected threads advance and may start new operations
* 4. Supports async operations that can be interrupted by events
*
*/
export function* behavioralThreadSystem<Event, V = void>(
body: (
addBehavioralThread: {
(
name: string,
behavior: () => Generator<Sync<Event>, void, Event>
): Operation<void>
},
sync: typeof makeSyncSpec<Event>
) => Operation<V>
): Operation<V> {
console.debug("Starting behavioral thread system")
// This channel is used to notify the scheduler that a new thread has been created
const heyThereIsANewPendingThread = createChannel<void>()
// Set of threads that will be started
const pendingThreads = new Set<Thread<Event>>()
// Run the body with thread factory
const result = yield* body(function* (
name: string,
behavior: () => Generator<Sync<Event>, void, Event>
) {
console.debug(`Creating new thread: ${name}`)
const thread = makeThread({ name, behavior })
yield* startThreadOperationIfNecessary(
thread,
heyThereIsANewPendingThread
)
pendingThreads.add(thread)
yield* heyThereIsANewPendingThread.send()
},
makeSyncSpec)
let activeThreads = new Set<Thread<Event>>()
// Main scheduling loop
try {
yield* call(function* () {
console.debug("Starting scheduler")
for (;;) {
// Incorporate any new threads
if (pendingThreads.size > 0) {
console.debug(
`Adding ${pendingThreads.size} new threads to active set`
)
activeThreads = new Set([...activeThreads, ...pendingThreads])
pendingThreads.clear()
}
const pendingThreadNotifications = yield* heyThereIsANewPendingThread
// Process until no more work to do
for (;;) {
console.debug(
`Processing schedule iteration with ${activeThreads.size} active threads`
)
if (
false ===
(yield* schedule(activeThreads, heyThereIsANewPendingThread))
) {
console.debug("No more work to do in current schedule iteration")
break
}
}
if (pendingThreads.size === 0) {
console.debug("No more active or pending threads")
break
}
console.log(
Deno.inspect(
{ activeThreads, pendingThreads },
{ depth: 10, colors: true }
)
)
console.debug("Checking for notifications")
// Check if we're done
const notificationsResult = yield* pendingThreadNotifications.next()
if (notificationsResult.done) {
console.debug("Notification channel closed")
break
} else {
console.debug("Notification received")
}
}
console.debug("Scheduler complete, sending completion notification")
})
} catch (e) {
console.error("Scheduler error", e)
} finally {
console.debug("Cleaning up scheduler")
}
yield* heyThereIsANewPendingThread.close()
return result
}