forked from gambit/gambit
/
_thread.scm
3275 lines (2800 loc) · 109 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-09-27 23:18:40 feeley>
;;; Copyright (c) 1994-2008 by Marc Feeley, All Rights Reserved.
;;;============================================================================
(##include "header.scm")
;;(##include "sys.scm")
;;;============================================================================
;;; Implementation of exceptions.
(implement-library-type-deadlock-exception)
(implement-library-type-abandoned-mutex-exception)
(implement-library-type-scheduler-exception)
(implement-library-type-noncontinuable-exception)
(implement-library-type-initialized-thread-exception)
(define-prim (##raise-initialized-thread-exception proc . args)
(##extract-procedure-and-arguments
proc
args
#f
#f
#f
(lambda (procedure arguments dummy1 dummy2 dummy3)
(macro-raise
(macro-make-initialized-thread-exception procedure arguments)))))
(implement-library-type-uninitialized-thread-exception)
(define-prim (##raise-uninitialized-thread-exception proc . args)
(##extract-procedure-and-arguments
proc
args
#f
#f
#f
(lambda (procedure arguments dummy1 dummy2 dummy3)
(macro-raise
(macro-make-uninitialized-thread-exception procedure arguments)))))
(implement-library-type-inactive-thread-exception)
(define-prim (##raise-inactive-thread-exception proc . args)
(##extract-procedure-and-arguments
proc
args
#f
#f
#f
(lambda (procedure arguments dummy1 dummy2 dummy3)
(macro-raise
(macro-make-inactive-thread-exception procedure arguments)))))
(implement-library-type-started-thread-exception)
(define-prim (##raise-started-thread-exception proc . args)
(##extract-procedure-and-arguments
proc
args
#f
#f
#f
(lambda (procedure arguments dummy1 dummy2 dummy3)
(macro-raise
(macro-make-started-thread-exception procedure arguments)))))
(implement-library-type-terminated-thread-exception)
(define-prim (##raise-terminated-thread-exception proc . args)
(##extract-procedure-and-arguments
proc
args
#f
#f
#f
(lambda (procedure arguments dummy1 dummy2 dummy3)
(macro-raise
(macro-make-terminated-thread-exception procedure arguments)))))
(implement-library-type-uncaught-exception)
(define-prim (##raise-uncaught-exception reason proc . args)
(##extract-procedure-and-arguments
proc
args
reason
#f
#f
(lambda (procedure arguments reason dummy1 dummy2)
(macro-raise
(macro-make-uncaught-exception procedure arguments reason)))))
(implement-library-type-join-timeout-exception)
(define-prim (##raise-join-timeout-exception proc . args)
(##extract-procedure-and-arguments
proc
args
#f
#f
#f
(lambda (procedure arguments dummy1 dummy2 dummy3)
(macro-raise
(macro-make-join-timeout-exception procedure arguments)))))
(implement-library-type-mailbox-receive-timeout-exception)
(define-prim (##raise-mailbox-receive-timeout-exception proc . args)
(##extract-procedure-and-arguments
proc
args
#f
#f
#f
(lambda (procedure arguments dummy1 dummy2 dummy3)
(macro-raise
(macro-make-mailbox-receive-timeout-exception procedure arguments)))))
;;;----------------------------------------------------------------------------
;;; Define type checking procedures.
(implement-check-type-continuation)
(implement-check-type-time)
(implement-check-type-absrel-time)
(implement-check-type-absrel-time-or-false)
(implement-check-type-thread)
(implement-check-type-mutex)
(implement-check-type-condvar)
(implement-check-type-tgroup)
;;;----------------------------------------------------------------------------
(implement-type-thread)
(implement-type-mutex)
(implement-type-condvar)
(implement-type-tgroup)
(implement-library-type-thread-state-uninitialized)
(implement-library-type-thread-state-initialized)
(implement-library-type-thread-state-normally-terminated)
(implement-library-type-thread-state-abnormally-terminated)
(implement-library-type-thread-state-active)
(##declare (not interrupts-enabled));;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;============================================================================
;;; File: "thread.scm"
;;; Copyright (c) 2003 by Marc Feeley, All Rights Reserved.
;;; Version 1.3 (august 14, 2000)
(##define-macro (macro-if-gambit-3.0 yes no)
no)
(##define-macro (macro-not-yet-implemented)
`(##void));;;;;;;;;;;*************
;;;----------------------------------------------------------------------------
;;; Implementation of dynamic environments.
(define ##parameter-counter 0)
(define-prim (##make-parameter
init
#!optional
(f (macro-absent-obj)))
(let ((filter
(if (##eq? f (macro-absent-obj))
(lambda (x) x)
f)))
(macro-check-procedure filter 2 (make-parameter init f)
(let* ((val
(filter init))
(new-count
(##fixnum.+ ##parameter-counter 1)))
;; Note: it is unimportant if the increment of
;; ##parameter-counter is not atomic; it simply means a
;; possible close repetition of the same hash code
(set! ##parameter-counter new-count)
(let ((descr
(macro-make-parameter-descr
val
(##partial-bit-reverse new-count)
filter)))
(letrec ((param
(lambda (#!optional (new-val (macro-absent-obj)))
(if (##eq? new-val (macro-absent-obj))
(##dynamic-ref param)
(##dynamic-set!
param
((macro-parameter-descr-filter descr) new-val))))))
param))))))
(define-prim (make-parameter init #!optional (f (macro-absent-obj)))
(macro-force-vars (f)
(##make-parameter init f)))
(define ##current-exception-handler
(##make-parameter
(lambda (exc)
(##thread-end-with-uncaught-exception! exc))
(lambda (val)
(macro-check-procedure val 1 (##current-exception-handler val)
val))))
(define current-exception-handler
##current-exception-handler)
(define ##current-readtable
(##make-parameter
##main-readtable
(lambda (val)
(macro-check-readtable val 1 (##current-readtable val)
val))))
(define current-readtable
##current-readtable)
(##open-all-predefined)
(define ##current-input-port
(##make-parameter
##stdin-port
(lambda (val)
(macro-check-input-port val 1 (##current-input-port val)
val))))
(define current-input-port
##current-input-port)
(define ##current-output-port
(##make-parameter
##stdout-port
(lambda (val)
(macro-check-output-port val 1 (##current-output-port val)
val))))
(define current-output-port
##current-output-port)
(define ##current-error-port
(##make-parameter
##stderr-port
(lambda (val)
(macro-check-output-port val 1 (##current-error-port val)
val))))
(define current-error-port
##current-error-port)
(define-prim (##current-directory-filter val)
(if (##eq? val (macro-absent-obj))
(let ((current-dir
(##os-path-normalize-directory #f)))
(if (##fixnum? current-dir)
(##exit-with-err-code current-dir)
current-dir))
(macro-check-string val 1 (##current-directory val)
(let ((normalized-dir
(##os-path-normalize-directory (##path-expand val))))
(if (##fixnum? normalized-dir)
(##raise-os-exception #f normalized-dir ##current-directory val)
normalized-dir)))))
(define ##current-directory
(##make-parameter
(macro-absent-obj)
##current-directory-filter))
(define current-directory
##current-directory)
(define-prim (##parameter? obj)
(##declare (not interrupts-enabled))
(and (##procedure? obj)
(##closure? obj)
(##eq? (##closure-code obj)
(##closure-code ##current-exception-handler))))
(##define-macro (macro-parameter-descr param)
`(##closure-ref ,param 1))
(define-prim (##parameterize param val thunk)
(##declare (not interrupts-enabled))
(macro-check-procedure param 1 (##parameterize param val thunk)
(macro-check-procedure thunk 3 (##parameterize param val thunk)
(if (##parameter? param)
(##dynamic-let
param
((macro-parameter-descr-filter (macro-parameter-descr param)) val)
thunk)
(let ((save (param)))
(##dynamic-wind
(lambda () ;; before
(param val))
thunk
(lambda () ;; after
(param save))))))))
(define-prim (##dynamic-ref param)
(##declare (not interrupts-enabled))
(cond ((##eq? param ##current-exception-handler)
(macro-current-exception-handler))
((##eq? param ##current-input-port)
(macro-current-input-port))
((##eq? param ##current-output-port)
(macro-current-output-port))
(else
(let* ((current-thread
(macro-current-thread))
(c1
(macro-thread-denv-cache1 current-thread)))
(if (##eq? param (##car c1))
(##cdr c1)
(let ((c2 (macro-thread-denv-cache2 current-thread)))
(if (##eq? param (##car c2))
(begin
(macro-thread-denv-cache2-set! current-thread c1)
(macro-thread-denv-cache1-set! current-thread c2)
(##cdr c2))
(let ((c3 (macro-thread-denv-cache3 current-thread)))
(if (##eq? param (##car c3))
(begin
(macro-thread-denv-cache3-set! current-thread c2)
(macro-thread-denv-cache2-set! current-thread c1)
(macro-thread-denv-cache1-set! current-thread c3)
(##cdr c3))
(let* ((denv
(macro-thread-denv current-thread))
(x
(##env-lookup (macro-denv-local denv) param)))
(if x
(begin
(macro-thread-denv-cache3-set!
current-thread
(macro-thread-denv-cache2 current-thread))
(macro-thread-denv-cache2-set!
current-thread
(macro-thread-denv-cache1 current-thread))
(macro-thread-denv-cache1-set!
current-thread
x)
(##cdr x))
(macro-parameter-descr-value
(macro-parameter-descr param)))))))))))))
(define-prim (##dynamic-set! param val)
(##declare (not interrupts-enabled))
(cond ((##eq? param ##current-exception-handler)
(macro-current-exception-handler-set! val)
(##void))
((##eq? param ##current-input-port)
(macro-current-input-port-set! val)
(##void))
((##eq? param ##current-output-port)
(macro-current-output-port-set! val)
(##void))
(else
(let* ((current-thread
(macro-current-thread))
(c1
(macro-thread-denv-cache1 current-thread)))
(if (##eq? param (##car c1))
(begin
(##set-cdr! c1 val)
(##void))
(let ((c2 (macro-thread-denv-cache2 current-thread)))
(if (##eq? param (##car c2))
(begin
(macro-thread-denv-cache2-set! current-thread c1)
(macro-thread-denv-cache1-set! current-thread c2)
(##set-cdr! c2 val)
(##void))
(let ((c3 (macro-thread-denv-cache3 current-thread)))
(if (##eq? param (##car c3))
(begin
(macro-thread-denv-cache3-set! current-thread c2)
(macro-thread-denv-cache2-set! current-thread c1)
(macro-thread-denv-cache1-set! current-thread c3)
(##set-cdr! c3 val)
(##void))
(let* ((denv
(macro-thread-denv current-thread))
(x
(##env-lookup (macro-denv-local denv) param)))
(if x
(begin
(macro-thread-denv-cache3-set!
current-thread
(macro-thread-denv-cache2 current-thread))
(macro-thread-denv-cache2-set!
current-thread
(macro-thread-denv-cache1 current-thread))
(macro-thread-denv-cache1-set!
current-thread
x)
(##set-cdr! x val)
(##void))
(begin
(macro-parameter-descr-value-set!
(macro-parameter-descr param)
val)
(##void)))))))))))))
(define-prim (##dynamic-let param val thunk)
(##declare (not interrupts-enabled))
(cond ((##eq? param ##current-exception-handler)
(macro-dynamic-bind exception-handler
val
thunk))
((##eq? param ##current-input-port)
(macro-dynamic-bind input-port
val
thunk))
((##eq? param ##current-output-port)
(macro-dynamic-bind output-port
val
thunk))
(else
(let* ((param-val
(##cons param val))
(denv
(macro-thread-denv (macro-current-thread)))
(new-local-denv
(##env-insert (macro-denv-local denv) param-val)))
(##dynamic-env-bind
(macro-make-denv
new-local-denv
(macro-denv-dynwind denv)
(macro-denv-interrupt-mask denv)
(macro-denv-debugging-settings denv)
(macro-denv-exception-handler denv)
(macro-denv-input-port denv)
(macro-denv-output-port denv)
(macro-denv-repl-context denv))
thunk)))))
(define-prim (##dynamic-env->list denv)
(##cons (macro-denv-exception-handler denv)
(##cons (macro-denv-input-port denv)
(##cons (macro-denv-output-port denv)
(##env-flatten (macro-denv-local denv)
'())))))
(define-prim (##env-insert env param-val)
(let* ((param
(##car param-val))
(hash-param
(macro-parameter-descr-hash
(macro-parameter-descr param))))
(define (insert env)
(if (##null? env)
(macro-make-env
param-val
'()
'())
(let* ((x
(macro-env-param-val env))
(param-x
(##car x))
(hash-param-x
(macro-parameter-descr-hash
(macro-parameter-descr param-x))))
(cond ((##fixnum.< hash-param hash-param-x)
(macro-make-env
x
(insert (macro-env-left env))
(macro-env-right env)))
((or (##fixnum.< hash-param-x hash-param)
(##not (##eq? param-x param)))
(macro-make-env
x
(macro-env-left env)
(insert (macro-env-right env))))
(else
(macro-make-env
param-val
(macro-env-left env)
(macro-env-right env)))))))
(insert env)))
(define-prim (##env-insert! env param val)
(##declare (not interrupts-enabled))
(let ((hash-param
(macro-parameter-descr-hash
(macro-parameter-descr param))))
(define (insert! env)
(let* ((x
(macro-env-param-val env))
(param-x
(##car x))
(hash-param-x
(macro-parameter-descr-hash
(macro-parameter-descr param-x))))
(cond ((##fixnum.< hash-param hash-param-x)
(let ((branch (macro-env-left env)))
(if (##null? branch)
(let ((y (##cons param val)))
(macro-env-left-set! env (macro-make-env y '() '()))
(##void))
(insert! branch))))
((or (##fixnum.< hash-param-x hash-param)
(##not (##eq? param-x param)))
(let ((branch (macro-env-right env)))
(if (##null? branch)
(let ((y (##cons param val)))
(macro-env-right-set! env (macro-make-env y '() '()))
(##void))
(insert! branch))))
(else
(##set-cdr! x val)
(##void)))))
(insert! env)))
(define-prim (##env-lookup env param)
(##declare (not interrupts-enabled))
(let ((hash-param
(macro-parameter-descr-hash
(macro-parameter-descr param))))
(define (lookup env)
(if (##null? env)
#f
(let* ((x
(macro-env-param-val env))
(param-x
(##car x))
(hash-param-x
(macro-parameter-descr-hash
(macro-parameter-descr param-x))))
(cond ((##fixnum.< hash-param hash-param-x)
(lookup (macro-env-left env)))
((or (##fixnum.< hash-param-x hash-param)
(##not (##eq? param-x param)))
(lookup (macro-env-right env)))
(else
x)))))
(lookup env)))
(define-prim (##env-flatten env tail)
(define (flatten env tail)
(if (##null? env)
tail
(##cons (macro-env-param-val env)
(flatten (macro-env-left env)
(flatten (macro-env-right env)
tail)))))
(flatten env tail))
;;;----------------------------------------------------------------------------
;;; Implementation of time objects.
(define-prim (##absrel-timeout->timeout absrel-timeout)
(cond ((##not absrel-timeout)
#t)
((macro-time? absrel-timeout)
(macro-update-current-time!)
(let ((current-time
(macro-current-time
(macro-thread-floats (macro-run-queue))))
(point
(macro-time-point absrel-timeout)))
(and (##flonum.< current-time point)
point)))
((and (##fixnum? absrel-timeout)
(##not (##fixnum.< 0 absrel-timeout)))
#f)
(else
(let ((flonum-absrel-timeout
(macro-real->inexact absrel-timeout)))
(and (##flonum.positive? flonum-absrel-timeout)
(begin
(macro-update-current-time!)
(let ((current-time
(macro-current-time
(macro-thread-floats (macro-run-queue)))))
(##flonum.+ current-time
flonum-absrel-timeout))))))))
(define-prim (##timeout->time absrel-timeout)
(cond ((##not absrel-timeout)
(macro-make-time (macro-inexact-+inf) #f #f #f))
((macro-time? absrel-timeout)
absrel-timeout)
((##real? absrel-timeout)
(let ((flonum-absrel-timeout
(macro-real->inexact absrel-timeout)))
(macro-update-current-time!)
(let ((current-time
(macro-current-time
(macro-thread-floats (macro-run-queue)))))
(macro-make-time
(##flonum.+ current-time flonum-absrel-timeout)
#f
#f
#f))))
(else
(##fail-check-absrel-time-or-false
1
timeout->time
absrel-timeout))))
;;;----------------------------------------------------------------------------
;;; Implementation of blocked thread queues.
(define-rbtree
macro-btq-init!
macro-thread->btq
##btq-insert!
##btq-remove!
##btq-reposition!
macro-btq-singleton?
macro-btq-color
macro-btq-color-set!
macro-btq-parent
macro-btq-parent-set!
macro-btq-left
macro-btq-left-set!
macro-btq-right
macro-btq-right-set!
macro-thread-higher-prio?
macro-btq-leftmost
macro-btq-leftmost-set!
#f
#f
)
(define-prim (##btq-abandon! btq)
(##declare (not interrupts-enabled))
(macro-btq-deq-remove! btq)
(let ((leftmost (macro-btq-leftmost btq)))
(if (##eq? leftmost btq)
(macro-btq-unlink! btq 'abandoned)
(if (macro-mutex? btq)
(##mutex-signal-no-reschedule! btq leftmost #t)
(begin
(let ((owner (macro-btq-owner btq)))
(if (macro-thread? owner)
(##thread-effective-priority-downgrade! owner)))
(macro-btq-unlink! btq 'abandoned))))))
;;; Implementation of timeout queues.
(define-rbtree
macro-toq-init!
macro-thread->toq
##toq-insert!
##toq-remove!
##toq-reposition!
macro-toq-singleton?
macro-toq-color
macro-toq-color-set!
macro-toq-parent
macro-toq-parent-set!
macro-toq-left
macro-toq-left-set!
macro-toq-right
macro-toq-right-set!
macro-thread-sooner-or-simultaneous-and-higher-prio?
macro-toq-leftmost
macro-toq-leftmost-set!
#f
#f
)
;;;----------------------------------------------------------------------------
;;; Implementation of threads.
(define-prim (##run-queue)
(##declare (not interrupts-enabled))
(macro-run-queue))
(define-prim (##current-thread)
(##declare (not interrupts-enabled))
(macro-current-thread))
(define-prim (##make-thread thunk name tgroup)
(##declare (not interrupts-enabled))
(macro-make-thread thunk name tgroup))
(define-prim (##thread-start! thread)
(##declare (not interrupts-enabled))
(macro-thread-result-set! thread ##thread-start-action!)
(##btq-insert! (macro-run-queue) thread)
(macro-thread-reschedule-if-needed!)
thread)
(define-prim (##thread-base-priority-set! thread base-priority)
(##declare (not interrupts-enabled))
(let ((floats (macro-thread-floats thread)))
;; save old boosted priority for ##thread-boosted-priority-changed!
(macro-temp-set!
(macro-thread-floats (macro-run-queue))
(macro-boosted-priority floats))
(if (##flonum.= (macro-base-priority floats)
(macro-boosted-priority floats))
(begin (set! pcase3 (##fixnum.+ pcase3 1));;;;;;;;;;;;;;;;;;
(macro-boosted-priority-set!
floats
base-priority)
)
(begin (set! pcase4 (##fixnum.+ pcase4 1));;;;;;;;;;;;;;;;;;
(macro-boosted-priority-set!
floats
(##flonum.+ base-priority
(macro-priority-boost floats))))
)
(macro-base-priority-set! floats base-priority)
(##thread-boosted-priority-changed! thread)
;; the change of priority may have made a higher priority
;; thread runnable, check for this
(macro-thread-reschedule-if-needed!)))
(define-prim (##thread-quantum-set! thread quantum)
(##declare (not interrupts-enabled))
(macro-thread-quantum-set! thread quantum)
;; check if the current thread's quantum is now over
(if (and (##eq? thread (macro-current-thread))
(##not (##flonum.< (macro-thread-quantum-used thread) quantum)))
(##thread-yield!)
(##void)))
(define-prim (##thread-priority-boost-set! thread priority-boost)
(##declare (not interrupts-enabled))
(let ((floats (macro-thread-floats thread)))
(macro-priority-boost-set! floats priority-boost)
(if (##flonum.= (macro-base-priority floats)
(macro-boosted-priority floats))
(begin (set! pcase5 (##fixnum.+ pcase5 1));;;;;;;;;;;;;;;;;;
(##void)
)
(begin
(set! pcase6 (##fixnum.+ pcase6 1));;;;;;;;;;;;;;;;;;
;; save old boosted priority for ##thread-boosted-priority-changed!
(macro-temp-set!
(macro-thread-floats (macro-run-queue))
(macro-boosted-priority floats))
(macro-boosted-priority-set!
floats
(##flonum.+ (macro-base-priority floats)
priority-boost))
(##thread-boosted-priority-changed! thread)
;; the change of priority may have made a higher priority
;; thread runnable, check for this
(macro-thread-reschedule-if-needed!)))))
(define-prim (##thread-boosted-priority-changed! thread)
(##declare (not interrupts-enabled))
(let ((floats (macro-thread-floats thread)))
(cond ((##flonum.<
(macro-effective-priority floats)
(macro-boosted-priority floats))
(set! pcase7 (##fixnum.+ pcase7 1));;;;;;;;;;;;;;;;;;
(macro-effective-priority-set!
floats
(macro-boosted-priority floats))
(##thread-effective-priority-changed! thread #t))
((##flonum.=
(macro-effective-priority floats)
(macro-temp (macro-thread-floats (macro-run-queue))))
(set! pcase8 (##fixnum.+ pcase8 1));;;;;;;;;;;;;;;;;;
(##thread-effective-priority-downgrade! thread))
(else (set! pcase9 (##fixnum.+ pcase9 1)));;;;;;;;;;;;;;;;;;
)))
(define-prim (##thread-effective-priority-changed! thread effective-priority-increased?)
(##declare (not interrupts-enabled))
(if (macro-toq-parent thread)
(##toq-reposition! thread)) ;; reposition thread in the toq it is in
(if (macro-btq-parent thread)
(begin
(##btq-reposition! thread) ;; reposition thread in the btq it is in
;; make sure the owner of the blocked thread queue
;; (i.e. mutex, condvar, etc) inherits the thread's effective
;; priority
(let ((owner (macro-btq-owner (macro-thread->btq thread))))
(if (macro-thread? owner)
(if effective-priority-increased?
(begin (set! pcase10 (##fixnum.+ pcase10 1));;;;;;;;;;;;;;;;;;
(macro-thread-inherit-priority! owner thread)
)
(begin (set! pcase11 (##fixnum.+ pcase11 1));;;;;;;;;;;;;;;;;;
(##thread-effective-priority-downgrade! owner)))))))
)
;; (##thread-effective-priority-downgrade! thread) is called to
;; recompute the effective priority of a thread. It is only called in
;; situations where the new effective priority is no more than the old
;; one.
(define-prim (##thread-effective-priority-downgrade! thread)
(##declare (not interrupts-enabled))
(let ((floats (macro-thread-floats thread)))
;; save old effective priority for later
(macro-temp-set!
(macro-thread-floats (macro-run-queue))
(macro-effective-priority floats))
;; compute the maximum of the boosted priority and the
;; effective priority of all the threads in blocked thread
;; queues (i.e. mutexes, condvars, etc) owned by the thread
(macro-effective-priority-set!
floats
(macro-boosted-priority floats))
(let loop ((btq (macro-btq-deq-next thread)))
(if (##not (##eq? btq thread))
(let ((leftmost (macro-btq-leftmost btq)))
(if (##not (##eq? leftmost btq))
(let ((leftmost-floats (macro-thread-floats leftmost)))
(if (##flonum.< (macro-effective-priority floats)
(macro-effective-priority leftmost-floats))
(macro-effective-priority-set!
floats
(macro-effective-priority leftmost-floats)))))
(loop (macro-btq-deq-next btq)))))
;;;;;;;;;;;;(set! pcase14 (##cons (##cons (macro-temp (macro-thread-floats (macro-run-queue))) (macro-effective-priority floats))pcase14));;;;;;;;;;;;;;;;;;;;;;;
(if (##not (##flonum.=
(macro-temp (macro-thread-floats (macro-run-queue)))
(macro-effective-priority floats)))
(begin (set! pcase12 (##fixnum.+ pcase12 1));;;;;;;;;;;;;;;;;;
(##thread-effective-priority-changed! thread #f)
)
(set! pcase13 (##fixnum.+ pcase13 1));;;;;;;;;;;;;;;;;;
)))
(define-prim (##thread-btq-insert! btq thread)
(##declare (not interrupts-enabled))
(##btq-insert! btq thread)
(let ((owner (macro-btq-owner btq)))
(if (macro-thread? owner)
(macro-thread-inherit-priority! owner thread))))
(define-prim (##thread-btq-remove! thread)
(##declare (not interrupts-enabled))
(let ((owner (macro-btq-owner (macro-thread->btq thread))))
(##btq-remove! thread)
(if (macro-thread? owner)
(if (##flonum.= (macro-thread-effective-priority thread)
(macro-thread-effective-priority owner))
(##thread-effective-priority-downgrade! owner)))))
(define-prim (##thread-toq-remove! thread)
(##declare (not interrupts-enabled))
(##toq-remove! thread))
(define-prim (##thread-check-timeouts!)
(##declare (not interrupts-enabled))
(macro-update-current-time!)
(let ((run-queue (macro-run-queue)))
(let loop ()
(let ((leftmost (macro-toq-leftmost run-queue)))
(if (and (##not (##eq? leftmost run-queue))
(##not (macro-thread-sooner? run-queue leftmost)))
(begin
(macro-thread-result-set! leftmost ##thread-timeout-action!)
(macro-thread-btq-remove-if-in-btq! leftmost)
(##thread-toq-remove! leftmost)
(##btq-insert! run-queue leftmost)
(loop)))))))
(define-prim (##thread-check-devices! timeout)
(##declare (not interrupts-enabled))
(let* ((run-queue (macro-run-queue))
(code (##os-condvar-select! run-queue timeout)))
(let loop ((condvar (macro-btq-deq-next run-queue)))
(if (##eq? condvar run-queue)
code
(let ((next (macro-btq-deq-next condvar)))
(if (##fixnum.odd? (macro-btq-owner condvar))
(##device-condvar-broadcast-no-reschedule! condvar))
(loop next))))))
(define-prim (##thread-heartbeat!)
(##declare (not interrupts-enabled))
(let ((code (##thread-check-devices! #f))) ;; poll devices
(##thread-check-timeouts!)
(if (and (##fixnum.< code 0)
(##not (##fixnum.= code ##err-code-EINTR)))
;; there was an error that cannot be handled, so force the
;; primordial thread to wakeup and raise a "scheduler
;; error" exception
(##thread-report-scheduler-error! code))
(let* ((run-queue
(macro-run-queue))
(current-thread
(macro-current-thread))
(run-queue-floats
(macro-thread-floats run-queue))
(current-thread-floats
(macro-thread-floats current-thread))
(quantum-used
(##flonum.+ (macro-quantum-used current-thread-floats)
(macro-heartbeat-interval run-queue-floats))))
(macro-quantum-used-set! current-thread-floats quantum-used)
(if (##flonum.< quantum-used
(macro-quantum current-thread-floats))
(macro-thread-reschedule-if-needed!)
(##thread-yield!)))))
(define-prim (##thread-yield!)
(##declare (not interrupts-enabled))
(let* ((current-thread
(macro-current-thread))
(run-queue
(macro-run-queue)))
(if (##eq? (macro-btq-singleton? run-queue) current-thread)
(begin
;; fast case where only one thread is runnable
(macro-thread-unboost-and-clear-quantum-used! current-thread)
(##void))
(macro-thread-save!
(lambda (current-thread)
(##btq-remove! current-thread)
(macro-thread-unboost-and-clear-quantum-used! current-thread)
(macro-thread-result-set! current-thread ##thread-void-action!)
(##btq-insert! (macro-run-queue) current-thread)
(##thread-schedule!))))))
(define-prim (##thread-reschedule!)
(##declare (not interrupts-enabled))