I/O, sleeps, or otherwise causes a thread switch. Presumably such a
procedure has some other way of enforcing the LIFO ordering.
-The @code{outf-console} procedure is provided for debugging purposes.
+The @code{outf-error} procedure is provided for debugging purposes.
It writes one or more argument strings (and @code{write}s any
-non-strings) to the console and flushes, atomically, via a machine
+non-strings) to the Unix ``stderr'' channel, atomically, via a machine
primitive, bypassing the runtime's I/O buffering and thread switching.
-Thus multiple debugging trace messages arrive on the console intact
+Thus trace messages from multiple threads will appear on stderr intact
and uninterrupted.
#| -*-Scheme-*-
-Copyright (C) 2006, 2007, 2008, 2009, 2010 Matthew Birkholz
+Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Matthew Birkholz
This file is part of MIT/GNU Scheme.
(define (call-alien* alien-function args)
(let ((old-top calloutback-stack))
- (if-tracing
- (outf-console ";"(tindent)"=> "alien-function" "args"\n")
+ (%if-tracing
+ (outf-error ";"(tindent)"=> "alien-function" "args"\n")
(set! calloutback-stack (cons (cons* alien-function args) old-top)))
(let ((value (apply (ucode-primitive c-call -1) alien-function args)))
- (if-tracing
- (assert (eq? old-top (cdr calloutback-stack))
- "call-alien: freak stack "calloutback-stack"\n")
+ (%if-tracing
+ (%assert (eq? old-top (cdr calloutback-stack))
+ "call-alien: freak stack "calloutback-stack"\n")
(set! calloutback-stack old-top)
- (outf-console ";"(tindent)"<= "value"\n"))
+ (outf-error ";"(tindent)"<= "value"\n"))
value)))
\f
(error:bad-range-argument id 'apply-callback))
(normalize-aliens! args)
(let ((old-top calloutback-stack))
- (if-tracing
- (outf-console ";"(tindent)"=>> "procedure" "args"\n")
+ (%if-tracing
+ (outf-error ";"(tindent)"=>> "procedure" "args"\n")
(set! calloutback-stack (cons (cons procedure args) old-top)))
(let ((value (apply-callback-proc procedure args)))
- (if-tracing
- (assert (and (pair? calloutback-stack)
- (eq? old-top (cdr calloutback-stack)))
- "callback-handler: freak stack "calloutback-stack"\n")
+ (%if-tracing
+ (%assert (and (pair? calloutback-stack)
+ (eq? old-top (cdr calloutback-stack)))
+ "callback-handler: freak stack "calloutback-stack"\n")
(set! calloutback-stack old-top)
- (outf-console ";"(tindent)"<<= "value"\n"))
+ (outf-error ";"(tindent)"<<= "value"\n"))
value))))
(define (apply-callback-proc procedure args)
(error "Cannot return from a callback more than once.")
(loop)))))))))
-;; For callback debugging...
-(define (outf-console . objects)
- ((ucode-primitive outf-console 1)
+;;; For callback debugging:
+
+(define (outf-error . objects)
+ ((ucode-primitive outf-error 1)
(apply string-append
(map (lambda (o) (if (string? o) o (write-to-string o)))
objects))))
+(define (registered-callback-count)
+ (let* ((vector registered-callbacks)
+ (end (vector-length vector)))
+ (let loop ((i 0)(count 0))
+ (if (fix:< i end)
+ (loop (fix:1+ i)
+ (if (vector-ref vector i)
+ (fix:1+ count)
+ count))
+ (cons count end)))))
+
(define (initialize-callbacks!)
(vector-set! (get-fixed-objects-vector) #x41 callback-handler))
\f
(define calloutback-stack '())
-(define trace? #f)
+(define %trace? #f)
(define (reset-package!)
(reset-alien-functions!)
(reset-malloced-aliens!)
(reset-callbacks!)
(set! %radix (if (fix:fixnum? #x100000000) #x100000000 #x10000))
- (set! trace? #f)
+ (set! %trace? #f)
(set! calloutback-stack '()))
(define (initialize-package!)
(add-gc-daemon! free-malloced-aliens)
unspecific)
-(define-syntax if-tracing
+(define-syntax %if-tracing
(syntax-rules ()
((_ . BODY)
- (if trace? ((lambda () . BODY))))))
+ (if %trace? ((lambda () . BODY))))))
-(define-syntax assert
+(define-syntax %assert
(syntax-rules ()
((_ TEST . MSG)
(if (not TEST) (error "Failed assert:" . MSG)))))
-(define-syntax trace
+(define-syntax %trace
(syntax-rules ()
((_ . MSG)
- (if trace? ((lambda () (outf-console . MSG)))))))
+ (if %trace? ((lambda () (outf-error . MSG)))))))
(define (tindent)
(make-string (* 2 (length calloutback-stack)) #\space))
\ No newline at end of file