-
Notifications
You must be signed in to change notification settings - Fork 5
/
reppy-channels.lisp
475 lines (405 loc) · 16.2 KB
/
reppy-channels.lisp
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
;; reppy-channels.lisp -- Channels a'la Reppy's CML
;;
;; DM/HMSC 01/00
;; -------------------------------------------------------------
(in-package "REPPY-CHANNELS")
;; -------------------------------------------------------
;; Reppy CML Style Channels and Events with Combinators
(defun spawn (fn &rest args)
(apply 'mpcompat:process-run-function (gensym) nil fn args))
;; -----------------------------------------------------------------------
(defclass channel ()
((valid :accessor channel-valid
:initform t)
(readers :accessor channel-pending-readers
:initform nil)
(writers :accessor channel-pending-writers
:initform nil)
))
(defun make-channel ()
(make-instance 'channel))
;; -----------------------------------------------------------------------
(defclass comm-cell ()
;; ref cell (NIL = not yet sync'd, >= 0 sync'd)
((performed :accessor comm-performed :initarg :performed)
;; the sync mailbox
(mbox :accessor comm-mbox :initarg :mbox)
;; the comm data value
(data :accessor comm-data :initarg :data)
;; the comm event number for this comm
(evnum :accessor comm-evnum :initarg :evnum)
;; a ref cell holding the number of valid channels waited on
(nrvalid :accessor comm-nrvalid :initarg :nrvalid)
))
;; -----------------------------------------------------------------------
(defclass behaviors ()
;; function to poll for comm (t if data avail)
((poll :accessor bev-poll :initarg :poll)
;; function to suspend on channel
(suspend :accessor bev-suspend :initarg :suspend)
;; function to return result of comm
(result :accessor bev-result :initarg :result)
))
;; -----------------------------------------------------------------------
(defclass event ()
())
;; -----------------------------------------------------------------------
(defclass comm-event (event)
((behavior :accessor comm-behavior :initarg :behavior)
))
;; -----------------------------------------------------------------------
(defclass ref-cell ()
((value :accessor ref-cell-value :initarg :value)
;; provides a level of indirection
))
(defun make-ref (val)
(make-instance 'ref-cell :value val))
(defclass bev-cell ()
((bev :accessor bev-cell-bev :initarg :bev)
(aborts :accessor bev-cell-abort-ids :initarg :abort-ids)))
(defclass abort-cell ()
((id :accessor abort-cell-id :initarg :id)
(fn :accessor abort-cell-fn :initarg :fn)
(args :accessor abort-cell-args :initarg :args)))
(defmethod already-syncd-p ((cell comm-cell))
(ref-cell-value (comm-performed cell)))
;; -----------------------------------------------------------------------
(defmethod sendEvt ((ch channel) data)
(make-instance 'comm-event
:behavior
(lambda (performed mbox evnum nrvalid)
(let ((wcomm (make-instance
'comm-cell
:performed performed
:mbox mbox
:data data
:evnum evnum
:nrvalid nrvalid)))
(make-instance
'behaviors
:poll (lambda ()
(if (channel-valid ch)
(prog ()
again
(let ((rcomm (pop (channel-pending-readers ch))))
(if rcomm
(if (already-syncd-p rcomm)
(go again)
(block not-syncd
(setf
(comm-data rcomm) (comm-data wcomm)
(ref-cell-value performed) evnum
(ref-cell-value (comm-performed rcomm))
(comm-evnum rcomm))
(mpcompat:mailbox-send (comm-mbox rcomm) t)
(return t))))
))))
:suspend (lambda ()
(if (channel-valid ch)
(setf (channel-pending-writers ch)
(um:conc1 (cleanup-queue (channel-pending-writers ch))
wcomm))
(decf (ref-cell-value nrvalid))))
:result (constantly nil)
)))))
(defvar *no-data* #(:no-data)) ;; unique under eq
(defmethod recvEvt ((ch channel))
(make-instance 'comm-event
:behavior
(lambda (performed mbox evnum nrvalid)
(let ((rcomm (make-instance
'comm-cell
:performed performed
:mbox mbox
:data *no-data*
:evnum evnum
:nrvalid nrvalid)))
(make-instance
'behaviors
:poll (lambda ()
(if (channel-valid ch)
(prog ()
again
(let ((wcomm (pop (channel-pending-writers ch))))
(if wcomm
(if (already-syncd-p wcomm)
(go again)
(block not-syncd
(setf (comm-data rcomm) (comm-data wcomm)
(ref-cell-value performed) evnum
(ref-cell-value (comm-performed wcomm))
(comm-evnum wcomm))
(mpcompat:mailbox-send (comm-mbox wcomm) t)
(return t))))
))))
:suspend (lambda ()
(if (channel-valid ch)
(setf (channel-pending-readers ch)
(um:conc1 (cleanup-queue (channel-pending-readers ch))
rcomm))
(decf (ref-cell-value nrvalid))))
:result (lambda ()
(let ((ans (comm-data rcomm)))
(if (eq ans *no-data*)
(error "Event.receive")
ans)))
)))))
(defun alwaysEvt (data)
(make-instance 'comm-event
:behavior
(lambda (performed mbox evnum nrvalid)
(declare (ignore mbox nrvalid))
(make-instance
'behaviors
:poll (lambda ()
(setf (ref-cell-value performed) evnum)
t)
:suspend (constantly nil)
:result (constantly data))
)))
(defvar neverEvt
(make-instance 'comm-event
:behavior
(lambda (performed mbox evnum nrvalid)
(declare (ignore performed mbox evnum nrvalid))
(make-instance 'behaviors
:poll (constantly nil)
:suspend (constantly nil)
:result (lambda ()
(error "neverEvt.result"))) )))
(defun cleanup-queue (q)
;; remove all communication opportunities already synchronized
(delete-if 'already-syncd-p q))
;; -----------------------------------------------------
;; Reppy Combinators
;;
(defclass choice-event (event)
((choices :accessor choice-list :initarg :choices)))
(defun choose (&rest lst)
(make-instance 'choice-event
:choices lst))
(defclass guarded-event (event)
((fn :accessor guard-fn :initarg :fn)
(args :accessor guard-args :initarg :args)))
(defun guard (fn &rest args)
(make-instance 'guarded-event
:fn fn
:args args))
(defclass wrapped-abort-event (event)
((evt :accessor wrapped-event :initarg :event)
(fn :accessor abort-fn :initarg :fn)
(args :accessor abort-args :initarg :args)))
(defmethod wrap-abort ((ev event) fn &rest args)
(make-instance 'wrapped-abort-event
:event ev
:fn fn
:args args))
(defmethod wrap ((ev comm-event) fn &rest args)
(let ((sav (comm-behavior ev)))
(setf (comm-behavior ev)
(lambda (performed mbox evnum nrvalid)
(let ((bev (funcall sav performed mbox evnum nrvalid)))
(make-instance 'behaviors
:poll (bev-poll bev)
:suspend (bev-suspend bev)
:result (lambda ()
(apply fn
(funcall
(bev-result bev))
args))
))))
ev))
(defmethod wrap ((ev choice-event) fn &rest args)
(choose (mapcar (lambda (ev)
(apply 'wrap ev fn args))
(choice-list ev))))
(defmethod wrap ((ev wrapped-abort-event) fn &rest args)
(apply 'wrap-abort
(apply 'wrap (wrapped-event ev) fn args)
(abort-fn ev)
(abort-args ev)))
(defmethod wrap ((ev guarded-event) fn &rest args)
(guard (lambda ()
(apply 'wrap
(apply (guard-fn ev) (guard-args ev))
fn args))
))
(defun scramble-array (v)
(let ((len (length v)))
(if (zerop len)
(error "Event.choose"))
(do ((ix (1- len) (1- ix)))
((zerop ix))
(let ((jx (random (1+ ix)))
(tmp (aref v ix)))
(setf (aref v ix) (aref v jx)
(aref v jx) tmp)))
v))
(defmethod flatten-event (abort-list accum accum-abort (ev comm-event))
(values (cons (make-instance 'bev-cell
:bev (comm-behavior ev)
:abort-ids abort-list)
accum)
accum-abort))
(defvar *id-counter* 0)
(defun gen-id ()
(incf *id-counter*))
(defmethod flatten-event (abort-list accum accum-abort (ev wrapped-abort-event))
(let ((id (gen-id)))
(flatten-event (cons id abort-list) accum
(cons (make-instance 'abort-cell
:id id
:fn (abort-fn ev)
:args (abort-args ev))
accum-abort)
(wrapped-event ev))))
(defmethod flatten-event (abort-list accum accum-abort (ev guarded-event))
(flatten-event abort-list accum accum-abort
(apply (guard-fn ev) (guard-args ev))))
(defmethod flatten-event (abort-list accum accum-abort (ev choice-event))
(labels
((flatten-list (accum accum-abort lst)
(if (endp lst)
(values accum accum-abort)
(multiple-value-bind (accumx accum-abortx)
(flatten-event abort-list accum accum-abort
(car lst))
(flatten-list accumx accum-abortx (cdr lst)))
)))
(flatten-list accum accum-abort (choice-list ev))))
(defun do-aborts (abort-env)
(dolist (cell abort-env)
(apply (abort-cell-fn cell) (abort-cell-args cell))))
(defun do-selective-aborts (abort-env genev performed)
(let ((nabort-ids (bev-cell-abort-ids (aref genev (ref-cell-value performed)))))
(do-aborts (delete-if (lambda (cell)
(member (abort-cell-id cell) nabort-ids))
abort-env))
))
(defvar *masterlock* (mpcompat:make-lock :name "Reppy Master Lock"))
(defun prep-bevs (genev)
(let* ((performed (make-ref nil))
(mbox (mpcompat:make-mailbox :size 1))
(len (length genev))
(nrvalid (make-ref len))
(bev (make-array len)))
(dotimes (ix len)
(setf (aref bev ix)
(funcall (bev-cell-bev (aref genev ix))
performed mbox ix nrvalid)))
(values performed mbox bev nrvalid)))
(defun channel-ready (bev)
(funcall (bev-poll bev)))
(defun suspend-bev (bev)
(funcall (bev-suspend bev)))
(defun get-result (bevs performed)
(funcall (bev-result (aref bevs (ref-cell-value performed)))))
(defvar *die* #(:die)) ;; unique value
(defun sync-wait (mbox)
(when (eq *die* (mpcompat:mailbox-read mbox))
(mpcompat:process-kill (mpcompat:current-process))))
(defun basic-sync (abort-env genev)
(multiple-value-bind (performed mbox bev nrvalid)
(prep-bevs genev)
(let ((needs-wait nil))
(mpcompat:with-lock (*masterlock*)
(unless (some 'channel-ready bev)
;; suspend until a communication takes place
(map nil 'suspend-bev bev)
(when (zerop (ref-cell-value nrvalid))
(mpcompat:mailbox-send mbox *die*))
(setf needs-wait t)))
(when needs-wait
(sync-wait mbox)))
(let ((ans (get-result bev performed)))
(do-selective-aborts abort-env genev performed)
ans)))
(defun basic-poll (abort-env genev)
(multiple-value-bind (performed mbox bev nrvalid)
(prep-bevs genev)
(declare (ignore mbox nrvalid))
(let ((ready nil))
(mpcompat:with-lock (*masterlock*)
(setf ready (some 'channel-ready bev)))
(if ready
(progn
(do-selective-aborts abort-env genev performed)
(values (get-result bev performed) t))
(progn
(setf (ref-cell-value performed) t) ;; mark all as sync'd
(do-aborts abort-env)
nil))
)))
(defmethod poll ((ev event))
(multiple-value-bind (evl abort-env)
(flatten-event nil nil nil ev)
(basic-poll abort-env (scramble-array (coerce evl 'vector)))
))
(defmethod sync ((ev event))
(multiple-value-bind (evl abort-env)
(flatten-event nil nil nil ev)
(basic-sync abort-env (scramble-array (coerce evl 'vector)))
))
(defun select (&rest evl)
(sync (apply 'choose evl)))
(defmethod send ((ch channel) item)
(sync (sendEvt ch item)))
(defmethod recv ((ch channel))
(sync (recvEvt ch)))
(defun sendNack (nackCh)
(let* ((evt (sendEvt nackCh t))
(performed (make-ref nil))
(mbox (mpcompat:make-mailbox))
(nrvalid (make-ref 1))
(beh (funcall (comm-behavior evt)
performed mbox 0 nrvalid)))
(mpcompat:with-lock (*masterlock*)
(unless (funcall (bev-poll beh))
(funcall (bev-suspend beh))))
))
(defmethod discard-channel ((ch channel))
(mpcompat:with-lock (*masterlock*)
(setf (channel-valid ch) nil)
(labels
((poll (q)
(dolist (comm q)
(unless (already-syncd-p comm)
(if (zerop (decf (ref-cell-value
(comm-nrvalid comm))))
(mpcompat:mailbox-send (comm-mbox comm) *die*)))
)))
(poll (channel-pending-readers ch))
(poll (channel-pending-writers ch)))
))
(defun with-nack (fn)
(guard (lambda ()
(let ((nackCh (make-channel)))
(wrap-abort
(wrap (funcall fn (recvEvt nackCh))
(lambda (x)
(discard-channel nackCh)
x))
(lambda ()
(sendNack nackCh)))
))))
#|
(defun tst ()
(let ((ch (make-channel))
(ans (make-channel)))
(spawn (lambda ()
(sync (choose (wrap (recvEvt ch) (lambda (x) (send ans (list 1 x))))
(wrap (recvEvt ch) (lambda (y) (send ans (list 2 y))))))))
(send ch 15)
(recv ans)))
(defun tst ()
(let ((ch1 (make-channel))
(ch2 (make-channel))
(ans (make-channel)))
(spawn (lambda ()
(send ans (list 1 (sync (choose (recvEvt ch1) (recvEvt ch2)))))))
(spawn (lambda ()
(send ans (list 2 (sync (choose (recvEvt ch2) (recvEvt ch1)))))))
(spawn (lambda () (sync (sendEvt ch1 15))))
(spawn (lambda () (sync (sendEvt ch2 16))))
(recv ans)))
|#