aboutsummaryrefslogtreecommitdiff
path: root/Hermes/modules/general.rkt
blob: b33eb8aeaccfca10b63b3eb9033fe069b5f57bd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#lang racket

(provide displayln-safe)
;; Several threads may want to print to stdout, so  lets make things civil
; constant always available
(define stdout (make-semaphore 1))

; prints to stdout with an optional output port
; requires a specified semaphore for the optional output port
(define displayln-safe
  (lambda (a-string [a-semaphore stdout] [a-output-port (current-output-port)])
    (cond [(not (and (eq? a-semaphore stdout) (eq? a-output-port (current-output-port))))
           (semaphore-wait a-semaphore)
           (semaphore-wait stdout)
           (displayln a-string a-output-port)
           (flush-output a-output-port)
           (displayln a-string)
           (semaphore-post stdout)
           (semaphore-post a-semaphore)]
          [else
            (semaphore-wait stdout)
            (displayln a-string)
            (semaphore-post stdout)])))