; vim: set ts=4 sw=4 noexpandtab filetype=scheme: (format (current-error-port) "(current-filename) = ~a\n" (current-filename)) (format (current-error-port) "%load-path = ~a\n" %load-path) (define-module (test-notlocked aab) #:use-module (ice-9 format) #:use-module (system foreign) #:use-module (ice-9 threads) #:use-module (oop goops) ) (define-syntax-rule (verbose-format format-string ...) (verbose-format-with-location (current-source-location) format-string ...)) (define verbose-port (current-error-port)) (define verbose-mutex (make-mutex)) (define (verbose-format-with-location location format-string . format-args) (let ((tod (gettimeofday)) (csl (current-source-location))) (with-mutex verbose-mutex (apply format (append (list verbose-port (string-append "~a.~6,'0d ~6,'0d:~6,'0d:~a ~a:~5,'0d " format-string "\n") (strftime "%Y:%m:%d %H:%M:%S" (localtime (car tod))) (cdr tod) (getpid) (getppid) #f ;(thread-name (current-thread)) (basename (assq-ref location 'filename)) (assq-ref location 'line)) format-args ) )) ) (force-output verbose-port) ) (define exit-status #f) (define (exit-wait) (do () (exit-status) (usleep 500000)) (verbose-format "ending ..." exit-status)) (define (signal-handler signal) (verbose-format "got signal ~a" signal) (set! exit-status #t) (verbose-format "exit-status = ~a" exit-status)) (define (writer id mutex condition-variable) (verbose-format "~a entry" id) (let loop ( (i 0) ) (with-mutex mutex (verbose-format "~a writing ~a" id i) ;(signal-condition-variable condition-variable) (verbose-format "~a wrote ~a" id i) (usleep 20000) ) (usleep 100) (loop (+ 1 i)) ) (verbose-format "~a end" id) ) (define (reader id mutex condition-variable) (verbose-format "~a entry" id) (let loop ( (i 0) ) (with-mutex mutex (verbose-format "~a reading ~a" id i) (wait-condition-variable condition-variable mutex) (usleep 20000) (verbose-format "~a read ~a" id i) ) (usleep 100) (loop (+ 1 i)) ) (verbose-format "~a end" id) ) (define (main) (verbose-format "entry") (verbose-format "entry ~a" (current-source-location)) (sigaction SIGQUIT signal-handler SA_RESTART) (sigaction SIGINT signal-handler SA_RESTART) (sigaction SIGTERM signal-handler SA_RESTART) (let ( (mutex (make-mutex)) (condition-variable (make-condition-variable)) ) (call-with-new-thread (lambda () (writer "writer-000" mutex condition-variable))) (call-with-new-thread (lambda () (reader "reader-000" mutex condition-variable))) (verbose-format "waiting for exit ...") (exit-wait) (verbose-format "done ...") ) (verbose-format "end") (primitive-exit 0) ) (main)