| 1 |
;;;; asyncs.test -*- mode: scheme; coding: utf-8; -*- |
| 2 |
;;;; |
| 3 |
;;;; Copyright (C) 2016, 2017 Free Software Foundation, Inc. |
| 4 |
;;;; |
| 5 |
;;;; This library is free software; you can redistribute it and/or |
| 6 |
;;;; modify it under the terms of the GNU Lesser General Public |
| 7 |
;;;; License as published by the Free Software Foundation; either |
| 8 |
;;;; version 3 of the License, or (at your option) any later version. |
| 9 |
;;;; |
| 10 |
;;;; This library is distributed in the hope that it will be useful, |
| 11 |
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
;;;; Lesser General Public License for more details. |
| 14 |
;;;; |
| 15 |
;;;; You should have received a copy of the GNU Lesser General Public |
| 16 |
;;;; License along with this library; if not, write to the Free Software |
| 17 |
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
|
| 19 |
(define-module (test-asyncs) |
| 20 |
#:use-module (ice-9 control) |
| 21 |
#:use-module (ice-9 q) |
| 22 |
#:use-module (ice-9 atomic) |
| 23 |
#:use-module (ice-9 threads) |
| 24 |
#:use-module (test-suite lib)) |
| 25 |
|
| 26 |
|
| 27 |
(with-test-prefix "interrupts" |
| 28 |
(pass-if-equal "self-interruptable v1" 42 |
| 29 |
(let/ec break |
| 30 |
(let lp ((n 0)) |
| 31 |
(when (= n 10) |
| 32 |
(system-async-mark (lambda () (break 42)))) |
| 33 |
(lp (1+ n))))) |
| 34 |
|
| 35 |
(pass-if-equal "self-interruptable v2" 42 |
| 36 |
(let/ec break |
| 37 |
(begin |
| 38 |
(system-async-mark (lambda () (break 42))) |
| 39 |
(let lp () (lp)))))) |
| 40 |
|
| 41 |
(define (with-sigprof-interrupts hz interrupt proc) |
| 42 |
(let ((prev-handler #f) |
| 43 |
(period-usecs (inexact->exact (round (/ 1e6 hz))))) |
| 44 |
(define (profile-signal-handler _) (interrupt)) |
| 45 |
(dynamic-wind |
| 46 |
(lambda () |
| 47 |
(set! prev-handler (car (sigaction SIGPROF profile-signal-handler))) |
| 48 |
(setitimer ITIMER_PROF 0 period-usecs 0 period-usecs)) |
| 49 |
proc |
| 50 |
(lambda () |
| 51 |
(setitimer ITIMER_PROF 0 0 0 0) |
| 52 |
(sigaction SIGPROF prev-handler))))) |
| 53 |
|
| 54 |
(when (and (defined? 'setitimer) |
| 55 |
(provided? 'ITIMER_PROF)) |
| 56 |
(pass-if "preemption via sigprof" |
| 57 |
;; Use an atomic box as a compiler barrier. |
| 58 |
(let* ((box (make-atomic-box 0)) |
| 59 |
(preempt-tag (make-prompt-tag)) |
| 60 |
(runqueue (make-q))) |
| 61 |
(define (run-cothreads) |
| 62 |
(unless (q-empty? runqueue) |
| 63 |
(let ((k (deq! runqueue))) |
| 64 |
(call-with-prompt preempt-tag |
| 65 |
k |
| 66 |
(lambda (k) (enq! runqueue k)))) |
| 67 |
(run-cothreads))) |
| 68 |
(enq! runqueue (lambda () |
| 69 |
(let lp () |
| 70 |
(let ((x (atomic-box-ref box))) |
| 71 |
(unless (= x 100) |
| 72 |
(when (even? x) |
| 73 |
(atomic-box-set! box (1+ x))) |
| 74 |
(lp)))))) |
| 75 |
(enq! runqueue (lambda () |
| 76 |
(let lp () |
| 77 |
(let ((x (atomic-box-ref box))) |
| 78 |
(unless (= x 100) |
| 79 |
(when (odd? x) |
| 80 |
(atomic-box-set! box (1+ x))) |
| 81 |
(lp)))))) |
| 82 |
(with-sigprof-interrupts |
| 83 |
1000 ; Hz |
| 84 |
(lambda () |
| 85 |
;; Interrupt could fire outside running a cothread, or |
| 86 |
;; recursively within an async; ignore in that case. |
| 87 |
(when (suspendable-continuation? preempt-tag) |
| 88 |
(abort-to-prompt preempt-tag))) |
| 89 |
run-cothreads) |
| 90 |
(equal? (atomic-box-ref box) 100)))) |
| 91 |
|
| 92 |
(when (provided? 'threads) |
| 93 |
(pass-if "preemption via external thread" |
| 94 |
;; Use an atomic box as a compiler barrier. |
| 95 |
(let* ((box (make-atomic-box 0)) |
| 96 |
(preempt-tag (make-prompt-tag)) |
| 97 |
(runqueue (make-q))) |
| 98 |
(define (run-cothreads) |
| 99 |
(unless (q-empty? runqueue) |
| 100 |
(let ((k (deq! runqueue))) |
| 101 |
(call-with-prompt preempt-tag |
| 102 |
k |
| 103 |
(lambda (k) (enq! runqueue k)))) |
| 104 |
(run-cothreads))) |
| 105 |
(enq! runqueue (lambda () |
| 106 |
(let lp () |
| 107 |
(let ((x (atomic-box-ref box))) |
| 108 |
(unless (= x 100) |
| 109 |
(when (even? x) |
| 110 |
(atomic-box-set! box (1+ x))) |
| 111 |
(lp)))))) |
| 112 |
(enq! runqueue (lambda () |
| 113 |
(let lp () |
| 114 |
(let ((x (atomic-box-ref box))) |
| 115 |
(unless (= x 100) |
| 116 |
(when (odd? x) |
| 117 |
(atomic-box-set! box (1+ x))) |
| 118 |
(lp)))))) |
| 119 |
(let* ((main-thread (current-thread)) |
| 120 |
(preempt-thread |
| 121 |
(call-with-new-thread |
| 122 |
(lambda () |
| 123 |
(let lp () |
| 124 |
(unless (= (atomic-box-ref box) 100) |
| 125 |
(usleep 1000) |
| 126 |
(system-async-mark |
| 127 |
(lambda () |
| 128 |
;; Interrupt could fire outside running a |
| 129 |
;; cothread, or recursively within an async; |
| 130 |
;; ignore in that case. |
| 131 |
(when (suspendable-continuation? preempt-tag) |
| 132 |
(abort-to-prompt preempt-tag))) |
| 133 |
main-thread) |
| 134 |
(lp))))))) |
| 135 |
(run-cothreads) |
| 136 |
(join-thread preempt-thread) |
| 137 |
(equal? (atomic-box-ref box) 100))))) |