asyncs.test 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. (define-module (test-asyncs)
  19. #:use-module (ice-9 control)
  20. #:use-module (ice-9 q)
  21. #:use-module (ice-9 atomic)
  22. #:use-module (ice-9 threads)
  23. #:use-module (test-suite lib))
  24. (with-test-prefix "interrupts"
  25. (pass-if-equal "self-interruptable v1" 42
  26. (let/ec break
  27. (let lp ((n 0))
  28. (when (= n 10)
  29. (system-async-mark (lambda () (break 42))))
  30. (lp (1+ n)))))
  31. (pass-if-equal "self-interruptable v2" 42
  32. (let/ec break
  33. (begin
  34. (system-async-mark (lambda () (break 42)))
  35. (let lp () (lp))))))
  36. (define (with-sigprof-interrupts hz interrupt proc)
  37. (let ((prev-handler #f)
  38. (period-usecs (inexact->exact (round (/ 1e6 hz)))))
  39. (define (profile-signal-handler _) (interrupt))
  40. (dynamic-wind
  41. (lambda ()
  42. (set! prev-handler (car (sigaction SIGPROF profile-signal-handler)))
  43. (setitimer ITIMER_PROF 0 period-usecs 0 period-usecs))
  44. proc
  45. (lambda ()
  46. (setitimer ITIMER_PROF 0 0 0 0)
  47. (sigaction SIGPROF prev-handler)))))
  48. (when (and (defined? 'setitimer)
  49. (provided? 'ITIMER_PROF))
  50. (pass-if "preemption via sigprof"
  51. ;; Use an atomic box as a compiler barrier.
  52. (let* ((box (make-atomic-box 0))
  53. (preempt-tag (make-prompt-tag))
  54. (runqueue (make-q)))
  55. (define (run-cothreads)
  56. (unless (q-empty? runqueue)
  57. (let ((k (deq! runqueue)))
  58. (call-with-prompt preempt-tag
  59. k
  60. (lambda (k) (enq! runqueue k))))
  61. (run-cothreads)))
  62. (enq! runqueue (lambda ()
  63. (let lp ()
  64. (let ((x (atomic-box-ref box)))
  65. (unless (= x 100)
  66. (when (even? x)
  67. (atomic-box-set! box (1+ x)))
  68. (lp))))))
  69. (enq! runqueue (lambda ()
  70. (let lp ()
  71. (let ((x (atomic-box-ref box)))
  72. (unless (= x 100)
  73. (when (odd? x)
  74. (atomic-box-set! box (1+ x)))
  75. (lp))))))
  76. (with-sigprof-interrupts
  77. 1000 ; Hz
  78. (lambda ()
  79. ;; Could throw an exception if the prompt is
  80. ;; not active (i.e. interrupt happens
  81. ;; outside running a cothread). Ignore in
  82. ;; that case.
  83. (false-if-exception (abort-to-prompt preempt-tag)))
  84. run-cothreads)
  85. (equal? (atomic-box-ref box) 100))))
  86. (when (provided? 'threads)
  87. (pass-if "preemption via external thread"
  88. ;; Use an atomic box as a compiler barrier.
  89. (let* ((box (make-atomic-box 0))
  90. (preempt-tag (make-prompt-tag))
  91. (runqueue (make-q)))
  92. (define (run-cothreads)
  93. (unless (q-empty? runqueue)
  94. (let ((k (deq! runqueue)))
  95. (call-with-prompt preempt-tag
  96. k
  97. (lambda (k) (enq! runqueue k))))
  98. (run-cothreads)))
  99. (enq! runqueue (lambda ()
  100. (let lp ()
  101. (let ((x (atomic-box-ref box)))
  102. (unless (= x 100)
  103. (when (even? x)
  104. (atomic-box-set! box (1+ x)))
  105. (lp))))))
  106. (enq! runqueue (lambda ()
  107. (let lp ()
  108. (let ((x (atomic-box-ref box)))
  109. (unless (= x 100)
  110. (when (odd? x)
  111. (atomic-box-set! box (1+ x)))
  112. (lp))))))
  113. (let* ((main-thread (current-thread))
  114. (preempt-thread (call-with-new-thread
  115. (lambda ()
  116. (let lp ()
  117. (unless (= (atomic-box-ref box) 100)
  118. (usleep 1000)
  119. (system-async-mark
  120. (lambda ()
  121. ;; Could throw an exception if the
  122. ;; prompt is not active
  123. ;; (i.e. interrupt happens outside
  124. ;; running a cothread). Ignore in
  125. ;; that case.
  126. (false-if-exception
  127. (abort-to-prompt preempt-tag)))
  128. main-thread)
  129. (lp)))))))
  130. (run-cothreads)
  131. (join-thread preempt-thread)
  132. (equal? (atomic-box-ref box) 100)))))