forked from gambit/gambit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_thread#.scm
1648 lines (1309 loc) · 52.3 KB
/
_thread#.scm
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;============================================================================
;;; File: "_thread#.scm", Time-stamp: <2008-11-22 09:00:30 feeley>
;;; Copyright (c) 1994-2008 by Marc Feeley, All Rights Reserved.
;;;============================================================================
;;; Representation of exceptions.
(define-library-type-of-exception deadlock-exception
id: 54294cd7-1c33-40e1-940e-7400e1126a5a
constructor: #f
opaque:
)
(define-library-type-of-exception abandoned-mutex-exception
id: e0e435ae-0097-47c9-8d4a-9d761979522c
constructor: #f
opaque:
)
(define-library-type-of-exception scheduler-exception
id: 0d164889-74b4-48ca-b291-f5ec9e0499fe
constructor: #f
opaque:
unprintable:
read-only:
reason
)
(define-library-type-of-exception noncontinuable-exception
id: 1bcc14ff-4be5-4573-a250-729b773bdd50
constructor: #f
opaque:
unprintable:
read-only:
reason
)
(define-library-type-of-exception initialized-thread-exception
id: e38351db-bef7-4c30-b610-b9b271e99ec3
constructor: #f
opaque:
unprintable:
read-only:
procedure
arguments
)
(define-library-type-of-exception uninitialized-thread-exception
id: 71831161-39c1-4a10-bb79-04342e1981c3
constructor: #f
opaque:
unprintable:
read-only:
procedure
arguments
)
(define-library-type-of-exception inactive-thread-exception
id: 339af4ff-3d44-4bec-a90b-d981fd13834d
constructor: #f
opaque:
unprintable:
read-only:
procedure
arguments
)
(define-library-type-of-exception started-thread-exception
id: ed07bce3-b882-4737-ac5e-3035b7783b8a
constructor: #f
opaque:
unprintable:
read-only:
procedure
arguments
)
(define-library-type-of-exception terminated-thread-exception
id: 85f41657-8a51-4690-abef-d76dc37f4465
constructor: #f
opaque:
unprintable:
read-only:
procedure
arguments
)
(define-library-type-of-exception uncaught-exception
id: 7022e42c-4ecb-4476-be40-3ca2d45903a7
constructor: #f
opaque:
unprintable:
read-only:
procedure
arguments
reason
)
(define-library-type-of-exception join-timeout-exception
id: 7af7ca4a-ecca-445f-a270-de9d45639feb
constructor: #f
opaque:
unprintable:
read-only:
procedure
arguments
)
(define-library-type-of-exception mailbox-receive-timeout-exception
id: 5f13e8c4-2c68-4eb5-b24d-249a9356c918
constructor: #f
opaque:
unprintable:
read-only:
procedure
arguments
)
(define-library-type-of-exception rpc-remote-error-exception
id: 6469e5eb-3117-4c29-89df-c348479dac93
constructor: #f
opaque:
unprintable:
read-only:
procedure
arguments
message
)
;;;----------------------------------------------------------------------------
;;; Define type checking macros.
(define-check-type continuation 'continuation
##continuation?)
(define-check-type time (macro-type-time)
macro-time?)
(define-check-type absrel-time 'absrel-time
macro-absrel-time?)
(define-check-type absrel-time-or-false 'absrel-time-or-false
macro-absrel-time-or-false?)
(define-check-type thread 'thread
macro-thread?)
(define-check-type mutex (macro-type-mutex)
macro-mutex?)
(define-check-type condvar (macro-type-condvar)
macro-condvar?)
(define-check-type tgroup (macro-type-tgroup)
macro-tgroup?)
(##define-macro (macro-initialized-thread? thread)
`(macro-thread-cont ,thread))
(##define-macro (macro-started-thread-given-initialized? thread)
`(or (##not (##procedure? (macro-thread-exception? ,thread)))
(macro-thread-result ,thread)))
(##define-macro (macro-terminated-thread-given-initialized? thread)
`(##not (macro-thread-end-condvar ,thread)))
(##define-macro (macro-check-initialized-thread thread form expr)
`(if (##not (macro-initialized-thread? ,thread))
(##raise-uninitialized-thread-exception ,@form)
,expr))
(##define-macro (macro-check-not-initialized-thread thread form expr)
`(if (macro-initialized-thread? ,thread)
(##raise-initialized-thread-exception ,@form)
,expr))
(##define-macro (macro-check-not-started-thread-given-initialized thread form expr)
`(if (let ((thread ,thread))
(or (macro-terminated-thread-given-initialized? thread)
(macro-started-thread-given-initialized? thread)))
(##raise-started-thread-exception ,@form)
,expr))
;;;----------------------------------------------------------------------------
;;; Priority queue generator macro.
;; The red-black tree implementation used here is inspired from the
;; code in the MIT-Scheme runtime (file "rbtree.scm"). That code is
;; based on the algorithms presented in the book "Introduction to
;; Algorithms" by Cormen, Leiserson, and Rivest.
;;
;; The main differences with the MIT-Scheme code are:
;;
;; 1) Nil pointers are replaced by a special sentinel that is also
;; the cell that contains a pointer (in the "left child" field)
;; to the root of the red-black tree. The "right child" field of
;; the sentinel is never accessed. The sentinel is black.
;;
;; 2) The color field contains #f when the node is red and a
;; reference to the sentinel when the node is black. It is thus
;; possible to find the sentinel from any node in constant time
;; (if the node is black extract the color field, otherwise
;; extract the color field of the parent, which must be black).
;;
;; 3) One field of the sentinel always points to the leftmost node of
;; the red-black tree. This allows constant time access to the
;; "minimum" node, which is a frequent operation of priority queues.
;;
;; 4) Several cases are handled specially (see the code for details).
;;
;; 5) Macros are used to generate code specialized for each case of
;; symmetrical operations (e.g. left and right rotation).
;;
;; 6) Nodes are assumed to be preallocated. Inserting and deleting a
;; node from a red-black tree does not perform any heap
;; allocation. Moreover, all algorithms consume a constant amount
;; of stack space.
(##define-macro (define-rbtree
rbtree-init!
node->rbtree
insert!
remove!
reposition!
singleton?
color
color-set!
parent
parent-set!
left
left-set!
right
right-set!
before?
leftmost
leftmost-set!
rightmost
rightmost-set!)
(define (black rbtree)
rbtree)
(define (black? rbtree)
`(lambda (node)
(,color node)))
(define (blacken! rbtree)
`(lambda (node)
(,color-set! node ,(black rbtree))))
(define (red)
#f)
(define (red?)
`(lambda (node)
(##not (,color node))))
(define (reden!)
`(lambda (node)
(,color-set! node ,(red))))
(define (copy-color!)
`(lambda (node1 node2)
(,color-set! node1 (,color node2))))
(define (exchange-color!)
`(lambda (node1 node2)
(let ((color-node1 (,color node1)))
(,color-set! node1 (,color node2))
(,color-set! node2 color-node1))))
(define (update-parent!)
`(lambda (parent-node old-node new-node)
(if (##eq? old-node (,left parent-node))
(,left-set! parent-node new-node)
(,right-set! parent-node new-node))))
(define (rotate! side1 side1-set! side2 side2-set!)
`(lambda (node)
(let ((side2-node (,side2 node)))
(let ((side1-side2-node (,side1 side2-node)))
(,side2-set! node side1-side2-node)
(,parent-set! side1-side2-node node))
(let ((parent-node (,parent node)))
(,side1-set! side2-node node)
(,parent-set! node side2-node)
(,parent-set! side2-node parent-node)
(,(update-parent!) parent-node node side2-node)))))
(define (rotate-left!)
(rotate! left left-set! right right-set!))
(define (rotate-right!)
(rotate! right right-set! left left-set!))
(define (neighbor side other-side)
`(lambda (node rbtree)
(let ((side-node (,side node)))
(if (##eq? side-node rbtree)
(let ((parent-node (,parent node)))
(if (or (##eq? parent-node rbtree)
(##eq? node (,side parent-node)))
rbtree
parent-node))
(let loop ((x side-node))
(let ((other-side-x (,other-side x)))
(if (##eq? other-side-x rbtree)
x
(loop other-side-x))))))))
`(begin
(##define-macro (,rbtree-init! rbtree)
`(let ((rbtree ,rbtree))
(##declare (not interrupts-enabled))
,',@(if leftmost
`((,leftmost-set! rbtree rbtree))
`())
(,',(blacken! 'rbtree) rbtree)
(,',parent-set! rbtree rbtree)
(,',left-set! rbtree rbtree)))
(##define-macro (,node->rbtree node)
`(let ((node ,node))
(##declare (not interrupts-enabled))
(or (,',color node)
(let ((parent-node (,',parent node)))
(and parent-node ;; make sure node is in a rbtree
(,',color parent-node))))))
(define-prim (,insert! rbtree node)
(##declare (not interrupts-enabled))
(define (fixup!)
(let loop ((x node))
(let ((parent-x (,parent x)))
(if (,(red?) parent-x)
(let ((parent-parent-x (,parent parent-x)))
(##define-macro (body
side1
rotate-side1!
side2
rotate-side2!)
`(let ((side2-parent-parent-x (,side2 parent-parent-x)))
(if (,',(red?) side2-parent-parent-x)
(begin
(,',(blacken! 'rbtree) parent-x)
(,',(blacken! 'rbtree) side2-parent-parent-x)
(,',(reden!) parent-parent-x)
(loop parent-parent-x))
(let ((y
(if (##eq? x (,side2 parent-x))
(begin
(,rotate-side1! parent-x)
(,',parent parent-x))
(,',parent x))))
(,',(blacken! 'rbtree) y)
(let ((parent-y (,',parent y)))
(,',(reden!) parent-y)
(,rotate-side2! parent-y))))))
(if (##eq? parent-x (,left parent-parent-x))
(body ,left
,(rotate-left!)
,right
,(rotate-right!))
(body ,right
,(rotate-right!)
,left
,(rotate-left!)))))))
(,(blacken! 'rbtree) (,left rbtree))
(##void))
(##define-macro (insert-below! x)
`(let ((x ,x))
(if (,',before? node x)
(insert-left! (,',left x) x)
(insert-right! (,',right x) x))))
(define (insert-left! left-x x)
(if (##eq? left-x rbtree)
(begin
(,left-set! x node)
(,parent-set! node x)
;; check if leftmost must be updated
,@(if leftmost
`((if (##eq? x (,leftmost rbtree))
(,leftmost-set! rbtree node)))
`())
(fixup!))
(insert-below! left-x)))
(define (insert-right! right-x x)
(if (##eq? right-x rbtree)
(begin
(,right-set! x node)
(,parent-set! node x)
;; check if rightmost must be updated
,@(if rightmost
`((if (##eq? x (,rightmost rbtree))
(,rightmost-set! rbtree node)))
`())
(fixup!))
(insert-below! right-x)))
(,(reden!) node)
(,left-set! node rbtree)
(,right-set! node rbtree)
(insert-left! (,left rbtree) rbtree)
(,parent-set! rbtree rbtree))
(define-prim (,remove! node)
(##declare (not interrupts-enabled))
(let ((rbtree (,node->rbtree node)))
(define (fixup! parent-node node)
(##define-macro (body side1 rotate-side1! side2 rotate-side2!)
`(let ((x
(let ((side2-parent-node (,side2 parent-node)))
(if (,',(red?) side2-parent-node)
(begin
(,',(blacken! 'rbtree) side2-parent-node)
(,',(reden!) parent-node)
(,rotate-side1! parent-node)
(,side2 parent-node))
side2-parent-node))))
(define (common-case y)
(,',(copy-color!) y parent-node)
(,',(blacken! 'rbtree) parent-node)
(,',(blacken! 'rbtree) (,side2 y))
(,rotate-side1! parent-node)
(,',(blacken! 'rbtree) (,',left rbtree)))
(if (,',(red?) (,side2 x))
(common-case x)
(let ((side1-x (,side1 x)))
(if (,',(black? 'rbtree) side1-x)
(begin
(,',(reden!) x)
(fixup! (,',parent parent-node) parent-node))
(begin
(,',(blacken! 'rbtree) side1-x)
(,',(reden!) x)
(,rotate-side2! x)
(common-case (,side2 parent-node))))))))
(cond ((or (##eq? parent-node rbtree) (,(red?) node))
(,(blacken! 'rbtree) node))
((##eq? node (,left parent-node))
(body ,left
,(rotate-left!)
,right
,(rotate-right!)))
(else
(body ,right
,(rotate-right!)
,left
,(rotate-left!)))))
(let ((parent-node (,parent node))
(left-node (,left node))
(right-node (,right node)))
(,parent-set! node #f) ;; to avoid leaks
(,left-set! node #f)
(,right-set! node #f)
(cond ((##eq? left-node rbtree)
;; check if leftmost must be updated
,@(if leftmost
`((if (##eq? node (,leftmost rbtree))
(,leftmost-set!
rbtree
(if (##eq? right-node rbtree)
parent-node
right-node))))
`())
(,parent-set! right-node parent-node)
(,(update-parent!) parent-node node right-node)
(if (,(black? 'rbtree) node)
(begin
(,(reden!) node) ;; to avoid leaks
(fixup! parent-node right-node))))
((##eq? right-node rbtree)
;; check if rightmost must be updated
,@(if rightmost
`((if (##eq? node (,rightmost rbtree))
(,rightmost-set!
rbtree
left-node)))
`())
(,parent-set! left-node parent-node)
(,(update-parent!) parent-node node left-node)
;; At this point we know that the node is black.
;; This is because the right child is nil and the
;; left child is red (if the left child was black
;; the tree would not be balanced)
(,(reden!) node) ;; to avoid leaks
(fixup! parent-node left-node))
(else
(let loop ((x right-node) (parent-x node))
(let ((left-x (,left x)))
(if (##eq? left-x rbtree)
(begin
(,(exchange-color!) x node)
(,parent-set! left-node x)
(,left-set! x left-node)
(,parent-set! x parent-node)
(,(update-parent!) parent-node node x)
(if (##eq? x right-node)
(if (,(black? 'rbtree) node)
(begin
(,(reden!) node) ;; to avoid leaks
(fixup! x (,right x))))
(let ((right-x (,right x)))
(,parent-set! right-x parent-x)
(,left-set! parent-x right-x)
(,parent-set! right-node x)
(,right-set! x right-node)
(if (,(black? 'rbtree) node)
(begin
(,(reden!) node) ;; to avoid leaks
(fixup! parent-x right-x))))))
(loop left-x x)))))))
(,parent-set! rbtree rbtree)))
(##define-macro (,singleton? rbtree)
`(let ((rbtree ,rbtree))
(##declare (not interrupts-enabled))
(let ((root (,',left rbtree)))
(and (##not (##eq? root rbtree))
(##eq? (,',left root) rbtree)
(##eq? (,',right root) rbtree)
root))))
(define-prim (,reposition! node)
(##declare (not interrupts-enabled))
(let* ((rbtree
(,node->rbtree node))
(predecessor-node
(,(neighbor left right) node rbtree))
(successor-node
(,(neighbor right left) node rbtree)))
(if (or (and (##not (##eq? predecessor-node rbtree))
(,before? node predecessor-node))
(and (##not (##eq? successor-node rbtree))
(,before? successor-node node)))
(begin
(,remove! node)
(,insert! rbtree node)))))))
;;;----------------------------------------------------------------------------
;;; Representation of dynamic environments.
;; The dynamic environment contains the set of dynamically bound
;; "parameters" (such as the current input port and current exception
;; handler) that is attached to a continuation, and thereby to a
;; thread.
;; The dynamic environment stores the bindings of normal parameters in
;; a binary tree ordered using an integer identifier attached to the
;; parameter. Some frequently used parameters (such as the current
;; input port and exception handler) are stored separately.
(##define-macro (macro-make-denv local dynwind im ds eh ip op rc)
`(##vector ,local ,dynwind ,im ,ds ,eh ,ip ,op ,rc))
(##define-macro (macro-denv-local d) `(macro-slot 0 ,d))
(##define-macro (macro-denv-local-set! d x) `(macro-slot 0 ,d ,x))
(##define-macro (macro-denv-dynwind d) `(macro-slot 1 ,d))
(##define-macro (macro-denv-dynwind-set! d x) `(macro-slot 1 ,d ,x))
(##define-macro (macro-denv-interrupt-mask d) `(macro-slot 2 ,d))
(##define-macro (macro-denv-interrupt-mask-set! d x) `(macro-slot 2 ,d ,x))
(##define-macro (macro-denv-debugging-settings d) `(macro-slot 3 ,d))
(##define-macro (macro-denv-debugging-settings-set! d x)`(macro-slot 3 ,d ,x))
(##define-macro (macro-denv-exception-handler d) `(macro-slot 4 ,d))
(##define-macro (macro-denv-exception-handler-set! d x) `(macro-slot 4 ,d ,x))
(##define-macro (macro-denv-input-port d) `(macro-slot 5 ,d))
(##define-macro (macro-denv-input-port-set! d x) `(macro-slot 5 ,d ,x))
(##define-macro (macro-denv-output-port d) `(macro-slot 6 ,d))
(##define-macro (macro-denv-output-port-set! d x) `(macro-slot 6 ,d ,x))
(##define-macro (macro-denv-repl-context d) `(macro-slot 7 ,d))
(##define-macro (macro-denv-repl-context-set! d x) `(macro-slot 7 ,d ,x))
;;; Environment tree nodes.
(##define-macro (macro-make-env pv l r) `(##vector ,pv ,l ,r))
(##define-macro (macro-env-param-val e) `(macro-slot 0 ,e))
(##define-macro (macro-env-param-val-set! e x) `(macro-slot 0 ,e ,x))
(##define-macro (macro-env-left e) `(macro-slot 1 ,e))
(##define-macro (macro-env-left-set! e x) `(macro-slot 1 ,e ,x))
(##define-macro (macro-env-right e) `(macro-slot 2 ,e))
(##define-macro (macro-env-right-set! e x) `(macro-slot 2 ,e ,x))
;;; Dynamic-wind frames.
(##define-macro (macro-make-dynwind level before after cont)
`(##vector ,level ,before ,after ,cont))
(##define-macro (macro-dynwind-level d) `(macro-slot 0 ,d))
(##define-macro (macro-dynwind-level-set! d x) `(macro-slot 0 ,d ,x))
(##define-macro (macro-dynwind-before d) `(macro-slot 1 ,d))
(##define-macro (macro-dynwind-before-set! d x) `(macro-slot 1 ,d ,x))
(##define-macro (macro-dynwind-after d) `(macro-slot 2 ,d))
(##define-macro (macro-dynwind-after-set! d x) `(macro-slot 2 ,d ,x))
(##define-macro (macro-dynwind-cont d) `(macro-slot 3 ,d))
(##define-macro (macro-dynwind-cont-set! d x) `(macro-slot 3 ,d ,x))
;;; Parameter descriptors.
(##define-macro (macro-make-parameter-descr value hash filter)
`(##vector ,value ,hash ,filter))
(##define-macro (macro-parameter-descr-value p) `(macro-slot 0 ,p))
(##define-macro (macro-parameter-descr-value-set! p x) `(macro-slot 0 ,p ,x))
(##define-macro (macro-parameter-descr-hash p) `(macro-slot 1 ,p))
(##define-macro (macro-parameter-descr-hash-set! p x) `(macro-slot 1 ,p ,x))
(##define-macro (macro-parameter-descr-filter p) `(macro-slot 2 ,p))
(##define-macro (macro-parameter-descr-filter-set! p x) `(macro-slot 2 ,p ,x))
;;; Binding of special dynamic variables.
(##define-macro (macro-dynamic-bind var val thunk)
`(let ((val ,val) (thunk ,thunk))
(##declare (not interrupts-enabled))
(let ((current-denv (macro-thread-denv (macro-current-thread))))
(##dynamic-env-bind
(macro-make-denv
(macro-denv-local current-denv)
,(if (eq? var 'dynwind)
`val
`(macro-denv-dynwind current-denv))
,(if (eq? var 'interrupt-mask)
`val
`(macro-denv-interrupt-mask current-denv))
,(if (eq? var 'debugging-settings)
`val
`(macro-denv-debugging-settings current-denv))
,(if (eq? var 'exception-handler)
`(##cons ##current-exception-handler val)
`(macro-denv-exception-handler current-denv))
,(if (eq? var 'input-port)
`(##cons ##current-input-port val)
`(macro-denv-input-port current-denv))
,(if (eq? var 'output-port)
`(##cons ##current-output-port val)
`(macro-denv-output-port current-denv))
,(if (eq? var 'repl-context)
`(##cons #f val)
`(macro-denv-repl-context current-denv)))
thunk))))
(##define-macro (macro-current-interrupt-mask)
`(macro-denv-interrupt-mask (macro-thread-denv (macro-current-thread))))
(##define-macro (macro-debugging-settings-port)
`(macro-denv-debugging-settings (macro-thread-denv (macro-current-thread))))
(##define-macro (macro-current-exception-handler)
`(##cdr
(macro-denv-exception-handler (macro-thread-denv (macro-current-thread)))))
(##define-macro (macro-current-exception-handler-set! val)
`(##set-cdr!
(macro-denv-exception-handler (macro-thread-denv (macro-current-thread)))
,val))
(##define-macro (macro-current-input-port)
`(##cdr
(macro-denv-input-port (macro-thread-denv (macro-current-thread)))))
(##define-macro (macro-current-input-port-set! val)
`(##set-cdr!
(macro-denv-input-port (macro-thread-denv (macro-current-thread)))
,val))
(##define-macro (macro-current-output-port)
`(##cdr
(macro-denv-output-port (macro-thread-denv (macro-current-thread)))))
(##define-macro (macro-current-output-port-set! val)
`(##set-cdr!
(macro-denv-output-port (macro-thread-denv (macro-current-thread)))
,val))
(##define-macro (macro-current-repl-context)
`(##cdr
(macro-denv-repl-context (macro-thread-denv (macro-current-thread)))))
(##define-macro (macro-current-repl-context-set! val)
`(##set-cdr!
(macro-denv-repl-context (macro-thread-denv (macro-current-thread)))
,val))
;;; Exception raising.
(##define-macro (macro-raise obj)
`(let ((obj ,obj))
(##declare (not safe)) ;; avoid procedure check on the call to the handler
((macro-current-exception-handler) obj)))
(##define-macro (macro-abort obj)
`(let ((obj ,obj))
(##declare (not safe)) ;; avoid procedure check on the call to the handler
((macro-current-exception-handler) obj)
(##abort (macro-make-noncontinuable-exception obj))))
;;;----------------------------------------------------------------------------
;;; Representation of time objects.
(define-type time
id: 9700b02a-724f-4888-8da8-9b0501836d8e
type-exhibitor: macro-type-time
constructor: macro-make-time
implementer: implement-type-time
opaque:
macros:
prefix: macro-
unprintable:
point
;; the following fields are for eventual compatibility with srfi-19
type
second
nanosecond
)
(##define-macro (macro-absrel-time? obj)
`(let ((obj ,obj))
(or (##real? obj)
(macro-time? obj))))
(##define-macro (macro-absrel-time-or-false? obj)
`(let ((obj ,obj))
(or (##not obj)
(macro-absrel-time? obj))))
;;;----------------------------------------------------------------------------
;;; Root thread settings.
(##define-macro (macro-thread-root-base-priority)
(exact->inexact 0))
(##define-macro (macro-thread-root-quantum)
(exact->inexact 2/100))
(##define-macro (macro-thread-root-priority-boost)
(exact->inexact (expt 10 -6)))
(##define-macro (macro-default-heartbeat-interval)
(exact->inexact 1/100))
;;;----------------------------------------------------------------------------
;;; Representation of thread groups, threads, mutexes and condition variables.
;; A thread group contains a double-ended-queue of the threads that are
;; in the group and a double-ended-queue of the thread groups that are
;; in the group. A thread contains a double-ended-queue of the mutexes
;; that are owned by the thread. Conversely, a mutex contains a queue
;; of the threads blocked on the mutex. A condition variable also
;; contains a queue of the threads blocked on the condition variable.
;;; Representation of blocked thread queues.
(##define-macro (macro-btq-deq-next node) `(macro-slot 1 ,node))
(##define-macro (macro-btq-deq-next-set! node x) `(macro-slot 1 ,node ,x))
(##define-macro (macro-btq-deq-prev node) `(macro-slot 2 ,node))
(##define-macro (macro-btq-deq-prev-set! node x) `(macro-slot 2 ,node ,x))
(##define-macro (macro-btq-color node) `(macro-slot 3 ,node))
(##define-macro (macro-btq-color-set! node x) `(macro-slot 3 ,node ,x))
(##define-macro (macro-btq-parent node) `(macro-slot 4 ,node))
(##define-macro (macro-btq-parent-set! node x) `(macro-slot 4 ,node ,x))
(##define-macro (macro-btq-left node) `(macro-slot 5 ,node))
(##define-macro (macro-btq-left-set! node x) `(macro-slot 5 ,node ,x))
(##define-macro (macro-btq-right node) `(macro-slot 6 ,node))
(##define-macro (macro-btq-right-set! node x) `(macro-slot 6 ,node ,x))
(##define-macro (macro-btq-leftmost node) `(macro-slot 6 ,node))
(##define-macro (macro-btq-leftmost-set! node x) `(macro-slot 6 ,node ,x))
(##define-macro (macro-btq-owner node) `(macro-slot 7 ,node))
(##define-macro (macro-btq-owner-set! node x) `(macro-slot 7 ,node ,x))
(##define-macro (macro-btq-deq-init! deq)
`(let ((deq ,deq))
(##declare (not interrupts-enabled))
(macro-btq-deq-next-set! deq deq)
(macro-btq-deq-prev-set! deq deq)))
(##define-macro (macro-btq-deq-remove! item)
`(let ((item ,item))
(##declare (not interrupts-enabled))
(let ((item-next (macro-btq-deq-next item))
(item-prev (macro-btq-deq-prev item)))
(macro-btq-deq-next-set! item-prev item-next)
(macro-btq-deq-prev-set! item-next item-prev))))
(##define-macro (macro-btq-deq-insert! deq item)
`(let ((deq ,deq) (item ,item))
(##declare (not interrupts-enabled))
;; add item to tail of deq
(let ((deq-last (macro-btq-deq-prev deq)))
(macro-btq-deq-next-set! deq-last item)
(macro-btq-deq-prev-set! deq item)
(macro-btq-deq-next-set! item deq)
(macro-btq-deq-prev-set! item deq-last))))
(##define-macro (macro-btq-link! mutex thread)
`(let ((mutex ,mutex) (thread ,thread))
(##declare (not interrupts-enabled))
(macro-btq-deq-insert! thread mutex)
(macro-btq-owner-set! mutex thread)))
(##define-macro (macro-btq-unlink! mutex state)
`(let ((mutex ,mutex) (state ,state))
(##declare (not interrupts-enabled))
(macro-btq-deq-init! mutex)
(macro-btq-owner-set! mutex state)))
;;; Representation of timeout queues.
(##define-macro (macro-toq-color node) `(macro-slot 8 ,node))
(##define-macro (macro-toq-color-set! node x) `(macro-slot 8 ,node ,x))
(##define-macro (macro-toq-parent node) `(macro-slot 9 ,node))
(##define-macro (macro-toq-parent-set! node x) `(macro-slot 9 ,node ,x))
(##define-macro (macro-toq-left node) `(macro-slot 10 ,node))
(##define-macro (macro-toq-left-set! node x) `(macro-slot 10 ,node ,x))
(##define-macro (macro-toq-right node) `(macro-slot 11 ,node))
(##define-macro (macro-toq-right-set! node x) `(macro-slot 11 ,node ,x))
(##define-macro (macro-toq-leftmost node) `(macro-slot 11 ,node))
(##define-macro (macro-toq-leftmost-set! node x) `(macro-slot 11 ,node ,x))
;;; Representation of threads.
;; The state of a thread is determined by the content of the
;; "end-condvar", "exception?" and "result" fields:
;;
;; thread state "end-condvar" "exception?" "result"
;; not yet started condvar thunk #f
;; started, never run condvar thunk ##thread-start-action!
;; started, has run condvar #f action procedure/#f/#t
;; terminated with result #f #f result object
;; terminated with exception #f #t exception object
(define-type thread
id: d05e0aa7-e235-441d-aa41-c1ac02065460
extender: macro-define-type-of-thread
type-exhibitor: macro-type-thread
constructor: macro-construct-thread
implementer: implement-type-thread
opaque:
macros:
prefix: macro-
unprintable:
(btq-deq-next init: #f) ;; blocked thread queues owned by thread
(btq-deq-prev init: #f)
(btq-color init: #f) ;; to keep thread in a blocked thread queue
(btq-parent init: #f)
(btq-left init: #f)
(btq-leftmost init: #f)
(tgroup init: #f) ;; thread-group this thread belongs to
(toq-color init: #f) ;; to keep thread in a timeout queue
(toq-parent init: #f)
(toq-left init: #f)
(toq-leftmost init: #f)
(threads-deq-next init: #f) ;; threads in this thread group
(threads-deq-prev init: #f)
(floats init: #f)
(name init: #f)
(end-condvar init: #f)
(exception? init: #f)
(result init: #f)
(cont init: #f)
(denv init: #f)
(denv-cache1 init: #f)
(denv-cache2 init: #f)
(denv-cache3 init: #f)
(repl-channel init: #f)
(mailbox init: #f)
(specific init: #f)
)
;;; Access to floating point fields.
(##define-macro (macro-timeout f) `(##f64vector-ref ,f 0))
(##define-macro (macro-timeout-set! f x) `(##f64vector-set! ,f 0 ,x))
(##define-macro (macro-base-priority f) `(##f64vector-ref ,f 1))
(##define-macro (macro-base-priority-set! f x) `(##f64vector-set! ,f 1 ,x))
(##define-macro (macro-quantum f) `(##f64vector-ref ,f 2))
(##define-macro (macro-quantum-set! f x) `(##f64vector-set! ,f 2 ,x))
(##define-macro (macro-quantum-used f) `(##f64vector-ref ,f 3))
(##define-macro (macro-quantum-used-set! f x) `(##f64vector-set! ,f 3 ,x))
(##define-macro (macro-priority-boost f) `(##f64vector-ref ,f 4))
(##define-macro (macro-priority-boost-set! f x) `(##f64vector-set! ,f 4 ,x))
(##define-macro (macro-boosted-priority f) `(##f64vector-ref ,f 5))
(##define-macro (macro-boosted-priority-set! f x) `(##f64vector-set! ,f 5 ,x))
(##define-macro (macro-effective-priority f) `(##f64vector-ref ,f 6))
(##define-macro (macro-effective-priority-set! f x)`(##f64vector-set! ,f 6 ,x))
(##define-macro (macro-thread-timeout t)
`(macro-timeout (macro-thread-floats ,t)))
(##define-macro (macro-thread-timeout-set! t x)
`(macro-timeout-set! (macro-thread-floats ,t) ,x))
(##define-macro (macro-thread-base-priority t)
`(macro-base-priority (macro-thread-floats ,t)))
(##define-macro (macro-thread-base-priority-set! t x)
`(macro-base-priority-set! (macro-thread-floats ,t) ,x))
(##define-macro (macro-thread-quantum t)
`(macro-quantum (macro-thread-floats ,t)))
(##define-macro (macro-thread-quantum-set! t x)