forked from mighty-gerbils/gerbil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.ss
532 lines (456 loc) · 14.4 KB
/
event.ss
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
;;; -*- Gerbil -*-
;;; (C) vyzo
;;; event-driven programming with conditions
(import :gerbil/gambit
:std/error
:std/sugar
:std/misc/shuffle)
(export wait select sync
wait-io! &wait-io!
wrap-evt handle-evt choice-evt
never-evt always-evt
sync-object?
make-event event? event-e event-e-set!
! !*)
;;; Low level event programming primitives:
;;; - (wait selector (timeout #f))
;;; waits until the selector is signaled
;;; returns the selector if ready, #f if the timeout is reached
;;; - (select [selector ...] (timeout #f))
;;; waits until at least one selector is signaled
;;; returns a ready selector, #f if timeout is reached
;;;
;;; Condition Selectors:
;;; - threads
;;; - locked mutex - condvar pairs
;;; - naked i/o condvars
;;;
;;; High level event programming primitives:
;;; - (sync evt ...)
;;; waits until at least one synchronizable object evt is ready
;;; returns a ready evt
;;;
;;; Events are implemented on top of condition selectors
;;; See further below for details and macros
(def (wait sel (timeo #f))
(if (maybe-timeout? timeo)
(let (wait-e (selector-wait-e sel))
(and (wait-e sel timeo) sel))
(raise-bad-argument wait "timeout or #f" timeo)))
(def (select sels (timeo #f))
(match sels
([]
(raise-bad-argument select "selection set: empty"))
([sel]
(wait sel timeo))
(else
(if (maybe-timeout? timeo)
(do-select sels timeo)
(raise-bad-argument select "timeout or #f" timeo)))))
;; specialized variant for waiting on an io condvar
(def (wait-io! iocv (timeo #f))
(unless (io-condition-variable? iocv)
(raise-bad-argument wait-io! "IO condition variable" iocv))
(unless (maybe-timeout? timeo)
(raise-bad-argument wait-io! "timeout or #f" timeo))
(&wait-io! iocv timeo))
;; unchecked variant of wait-io!
(def (&wait-io! iocv (timeo #f))
(selector-wait-io-condvar iocv timeo))
;;; select implementation details
(defstruct selection (e mx cv)
final: #t)
(defstruct selector (sel wait abort)
final: #t)
(def (do-select sels timeo)
(let* ((xsels (map wrap-selector sels))
(mx (make-mutex 'select))
(cv (make-condition-variable 'select))
(selection (make-selection #f mx cv))
(threads (map (lambda (xsel) (spawn/name 'select select1 selection xsel))
xsels))
(abs-timeo (if timeo (timeout->abs-timeout timeo) absent-obj))
(finalize!
(lambda ()
(for-each thread-terminate! threads)
(for-each (lambda (xsel) ((selector-abort xsel) (selector-sel xsel)))
xsels))))
(let lp ()
(mutex-lock! mx)
(cond
((selection-e selection) =>
(lambda (xsel)
(mutex-unlock! mx)
(finalize!)
(selector-sel xsel)))
((mutex-unlock! mx cv abs-timeo)
(lp))
(else
(finalize!)
#f)))))
(def (select1 selection xsel)
(when ((selector-wait xsel) (selector-sel xsel) #f)
(mutex-lock! (selection-mx selection))
(unless (selection-e selection)
(set! (selection-e selection) xsel)
(condition-variable-signal! (selection-cv selection)))
(mutex-unlock! (selection-mx selection))))
(defrules dispatch-selector ()
((_ sel do-thread do-condvar do-io-condvar)
(cond
((thread? sel)
do-thread)
((io-condition-variable? sel)
do-io-condvar)
((and (pair? sel)
(mutex? (car sel))
(condition-variable? (cdr sel)))
(if (eq? (current-thread) (macro-mutex-btq-owner (car sel)))
do-condvar
(BUG 'sync "Illegal selector; mutex must be owned by current thread" sel)))
(else
(BUG 'sync "Illegal selector" sel)))))
(def (wrap-selector sel)
(dispatch-selector sel
(make-selector sel selector-wait-thread void)
(make-selector sel selector-wait-condvar selector-abort-condvar)
(make-selector sel selector-wait-io-condvar void)))
(def (selector-wait-e sel)
(dispatch-selector sel
selector-wait-thread
selector-wait-condvar
selector-wait-io-condvar))
(def (selector-wait-thread sel timeo)
(try
(thread-join! sel (or timeo absent-obj))
#t
(catch (uncaught-exception? e) #t)
(catch (terminated-thread-exception? e) #t)
(catch (join-timeout-exception? e) #f)))
(def (selector-wait-condvar sel timeo)
(with ([mx . cv] sel)
(mutex-unlock! mx cv (or timeo absent-obj))))
(def (selector-abort-condvar sel)
(with ([mx . cv] sel)
(when (eq? (current-thread) (macro-mutex-btq-owner mx))
(mutex-unlock! mx))))
(def (selector-wait-io-condvar sel timeo)
(##wait-for-io! sel (if timeo (timeout->abs-timeout->seconds timeo) #t)))
(def (maybe-timeout? obj)
(or (not obj)
(real? obj)
(time? obj)))
(def (timeout->abs-timeout timeo)
(if (time? timeo) timeo
(seconds->time (+ (##current-time-point) timeo))))
(def (timeout->abs-timeout->seconds timeo)
(if (time? timeo)
(time->seconds timeo)
(+ (##current-time-point) timeo)))
(def (io-condition-variable? obj)
(and (condition-variable? obj)
(##foreign? (macro-condvar-name obj))))
;; _gambit#
(extern namespace: #f
macro-condvar-name
macro-mutex-btq-owner)
;;; An event object is an object wrapping some arbitrary state with 3 methods:
;;; - (poll evt)
;;; This method is called at the begining of synchronization with sync
;;; and should return a synchronizer, which can be:
;;; - #t if the event is ready
;;; - #f if the event will not be ready at this synchronization
;;; - a time object by which the event will be ready
;;; - a condition selector to wait for the event
;;;
;;; - (ready evt) => value
;;; Called when the synchronizer returned by poll is ready and the event
;;; has been selected; the returned value is the result of the synchronization
;;;
;;; - (abort evt)
;;; Called if the synchronization aborts prior to calling poll in the event.
;;;
(defstruct event (e poll ready abort)
constructor: :init!
final: #t)
(defstruct event-handler (e K)
final: #t)
(defstruct event-set (e)
final: #t)
(defmethod {:init! event}
(lambda (self e poll ready (abort void))
(struct-instance-init! self e poll ready abort)))
(def (sync-object? obj)
(or (event? obj)
(event-handler? obj)
(event-set? obj)))
(def never-evt
(make-event #f false void))
(def always-evt
(make-event #f true values))
(def (wrap-evt obj)
(cond
((sync-object? obj) obj)
((sync-selector? obj)
(make-event obj event-e event-e event-abort-sync-selector))
((or (time? obj) (real? obj))
(make-event (timeout->abs-timeout obj) event-e false))
((input-port? obj)
(input-port-evt obj))
(else
(wrap-evt {:event obj}))))
(def (handle-evt obj K)
(let (evt (wrap-evt obj))
(if (event-set? evt)
(make-event-set (map (cut make-event-handler <> K) (event-set-e evt)))
(make-event-handler evt K))))
(def (choice-evt . args)
(let lp ((rest args) (evts []))
(match rest
([obj . rest]
(let (evt (wrap-evt obj))
(if (event-set? evt)
(if (null? evts) ; consing optimization
(lp rest (event-set-e evt))
(lp rest (foldl cons evts (event-set-e evt))))
(lp rest (cons evt evts)))))
(else
(make-event-set evts)))))
;;; sync
(def (sync . args)
(def mutexes
(make-hash-table-eq))
(def (loop rest sels sel-evts timeo timeo-evt)
(match rest
([evt . rest]
(if (or (event? evt) (event-handler? evt))
(poll evt rest sels sel-evts timeo timeo-evt)
(loop (foldl cons rest (shuffle (event-set-e evt)))
sels sel-evts timeo timeo-evt)))
(else
(cond
((not (null? sels))
(let (rz (select sels timeo))
(if rz
(dispatch-selector rz sels sel-evts)
(dispatch-evt timeo-evt))))
(timeo
(thread-sleep! timeo)
(dispatch-evt timeo-evt))
(else
(thread-sleep! +inf.0))))))
(def (poll evt rest sels sel-evts timeo timeo-evt)
(let (rz (poll-evt evt))
(cond
((not rz)
(loop rest sels sel-evts timeo timeo-evt))
((true? rz)
(abort-selectors! rest sels)
(dispatch-evt evt))
((time? rz)
(if timeo
(if (< (time->seconds rz)
(time->seconds timeo))
(loop rest sels sel-evts rz evt)
(loop rest sels sel-evts timeo timeo-evt))
(loop rest sels sel-evts rz evt)))
((sync-selector? rz)
(check-mutex! rz rest sels)
(loop rest (cons rz sels) (cons evt sel-evts) timeo timeo-evt))
(else
(abort! rest sels (cut error "Bad synchronizer" evt rz))))))
(def (poll-evt evt)
(let lp ((evt evt))
(if (event? evt)
((event-poll evt) evt)
(lp (event-handler-e evt)))))
(def (dispatch-evt evt)
(let recur ((evt evt))
(if (event? evt)
((event-ready evt) evt)
(call/values
(lambda () (recur (event-handler-e evt)))
(event-handler-K evt)))))
(def (dispatch-selector rz sels sel-evts)
(let lp ((rest-sels sels)
(rest-evts sel-evts))
(match rest-sels
([sel . rest-sels]
(match rest-evts
([evt . rest-evts]
(if (eq? sel rz)
(dispatch-evt evt)
(lp rest-sels rest-evts))))))))
(def (check-mutex! rz rest sels)
(when (pair? rz)
(let (mx (car rz))
(if (hash-get mutexes mx)
(abort! rest sels (cut error "Duplicate mutex synchronizer" rz))
(hash-put! mutexes mx #t)))))
(def (abort! rest sels E)
(abort-selectors! rest sels)
(E))
(def (abort-selectors! rest sels)
(for-each abort-selector! sels)
(for-each abort-evt! rest))
(def (abort-selector! sel)
(when (pair? sel)
(mutex-unlock! (car sel))))
(def (abort-evt! evt)
(cond
((event? evt)
((event-abort evt) evt))
((event-handler? evt)
(abort-evt! (event-handler-e evt)))
(else
(for-each abort-evt! (event-set-e evt)))))
(loop (shuffle (map wrap-evt args))
[] [] #f #f))
;;; sync macros
(defrules ! ()
((_ . clause)
(sync (!-clause clause))))
(defrules !* ()
((_ clause ...)
(sync (!-clause clause) ...)))
(defrules !-clause (=>)
((_ (evt => K))
(handle-evt evt K))
((_ (evt body rest ...))
(handle-evt evt (lambda (_) body rest ...))))
;;; sync implementation details
(def (sync-selector? obj)
(or (thread? obj)
(and (pair? obj)
(mutex? (car obj))
(condition-variable? (cdr obj)))
(io-condition-variable? obj)))
(def (event-abort-sync-selector evt)
(let (sel (event-e evt))
(when (pair? sel)
(mutex-unlock! (car sel)))))
(def (input-port-evt port)
(cond
((macro-u8vector-port? port)
(make-u8vector-port-evt port))
((macro-string-port? port)
(make-string-port-evt port))
((macro-vector-port? port)
(make-vector-port-evt port))
((macro-device-port? port)
(make-device-port-evt port))
((macro-tcp-server-port? port)
(make-tcp-server-port-evt port))
((macro-raw-device-port? port)
(make-raw-device-port-evt port))
(else
(BUG 'sync "can't wrap event around unknown port type" port))))
(def (make-u8vector-port-evt port)
(def (rbuf-check port)
(##fx< (macro-byte-port-rlo port)
(macro-byte-port-rhi port)))
(def (rbuf-fill port)
(macro-byte-port-rbuf-fill port))
(def (condvar-e port)
(macro-u8vector-port-rcondvar port))
(def (poll evt)
(user-port-poll (event-e evt) rbuf-check rbuf-fill condvar-e))
(make-event port poll event-e))
(def (make-string-port-evt port)
(def (rbuf-check port)
(##fx< (macro-character-port-rlo port)
(macro-character-port-rhi port)))
(def (rbuf-fill port)
(macro-character-port-rbuf-fill port))
(def (condvar-e port)
(macro-string-port-rcondvar port))
(def (poll evt)
(user-port-poll (event-e evt) rbuf-check rbuf-fill condvar-e))
(make-event port poll event-e))
(def (make-vector-port-evt port)
(def (rbuf-check port)
(##fx< (macro-vector-port-rlo port)
(macro-vector-port-rhi port)))
(def (rbuf-fill port)
(macro-vector-port-rbuf-fill port))
(def (condvar-e port)
(macro-vector-port-rcondvar port))
(def (poll evt)
(user-port-poll (event-e evt) rbuf-check rbuf-fill condvar-e))
(make-event port poll event-e))
(def (user-port-poll port rbuf-check rbuf-fill condvar-e)
(def (fill! port)
(input-port-rbuf-fill! port (rbuf-fill port)))
(let* ((mx (macro-port-mutex port)))
(mutex-lock! mx)
(cond
((rbuf-check port)
(mutex-unlock! mx)
#t)
((fill! port)
(mutex-unlock! mx)
#t)
(else
(cons mx (condvar-e port))))))
(def (input-port-rbuf-fill! port fill)
(let lp ()
(let (r (fill port 1 #f))
(cond
((eq? r ##err-code-EINTR) ; interrupted
(lp))
((eq? r ##err-code-EAGAIN) #f) ; would block
(else #t)))))
(def (make-device-port-evt port)
(def (fill! port)
(input-port-rbuf-fill! port (macro-byte-port-rbuf-fill port)))
(def (poll evt)
(let* ((port (event-e evt))
(mx (macro-port-mutex port)))
(cond
((or (##fx< (macro-byte-port-rlo port)
(macro-byte-port-rhi port))
(##fx< (macro-character-port-rlo port)
(macro-character-port-rhi port)))
(mutex-unlock! mx)
#t)
((fill! port)
(mutex-unlock! mx)
#t)
(else
(mutex-unlock! mx)
(macro-device-port-rdevice-condvar port)))))
(make-event port poll event-e))
(def (make-tcp-server-port-evt port)
(def (poll evt)
(let (port (event-e evt))
(macro-tcp-server-port-rdevice-condvar port)))
(make-event port poll event-e))
(def (make-raw-device-port-evt port)
(def (poll evt)
(let (port (event-e evt))
(macro-raw-device-port-rdevice-condvar port)))
(make-event port poll event-e))
;; _gambit#
(extern namespace: #f
macro-u8vector-port?
macro-u8vector-port-rcondvar
macro-string-port?
macro-string-port-rcondvar
macro-vector-port?
macro-vector-port-rbuf-fill
macro-vector-port-rcondvar
macro-device-port?
macro-device-port-rdevice-condvar
macro-tcp-server-port?
macro-tcp-server-port-rdevice-condvar
macro-raw-device-port?
macro-raw-device-port-rdevice-condvar
macro-port-mutex
macro-byte-port-rlo
macro-byte-port-rhi
macro-byte-port-rbuf-fill
macro-character-port-rlo
macro-character-port-rhi
macro-character-port-rbuf-fill
macro-vector-port-rlo
macro-vector-port-rhi)