Skip to content

Commit

Permalink
Merge pull request #206 from Idorobots/fix-preempt-example
Browse files Browse the repository at this point in the history
Made examples/preempt.sprtn work (kinda) in both runtimes.
  • Loading branch information
Idorobots committed Sep 1, 2024
2 parents f6f6fbf + 8534472 commit 982f951
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions examples/preempt.sprtn
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,15 @@
(enqueue-task! t)
(task-pid t)))

(define (yield-delay ms)
;; FIXME Needed to ensure that the value returned by delay-milliseconds is treated correctly by the JS runtime.
(let ((cc (asm (primop-app '&current-continuation))))
(yield cc (delay-milliseconds ms))))

(define (wait-until-ready t)
(let ((ts (current-milliseconds)))
(unless (>= ts (task-rtime t))
(delay-milliseconds 50)
(yield-delay 50)
(wait-until-ready t))))

(define (execute!)
Expand Down Expand Up @@ -118,25 +123,21 @@
)

;; Example

(trampoline
(suspend
(lambda ()
(let ((s (scheduler)))

(define (foo n)
(when (> n 0)
(display "Running foo...\n")
(s.sleep 100)
(foo (- n 1))))

(define (bar n)
(when (> n 0)
(display "Running bar...\n")
(s.sleep 100)
(bar (- n 1))))

(s.spawn-task! (lambda () (foo 100)))
(s.spawn-task! (lambda () (bar 100)))
(display (s.execute!))
(newline)))))
(let ((s (scheduler)))

(define (foo n)
(when (> n 0)
(display "Running foo...\n")
(s.sleep 100)
(foo (- n 1))))

(define (bar n)
(when (> n 0)
(display "Running bar...\n")
(s.sleep 100)
(bar (- n 1))))

(s.spawn-task! (lambda () (foo 100)))
(s.spawn-task! (lambda () (bar 100)))
(display (s.execute!))
(newline))

0 comments on commit 982f951

Please sign in to comment.