Skip to content
Snippets Groups Projects
Commit 945c4ce9 authored by Olivier Dion's avatar Olivier Dion
Browse files

test/agent/jami/signal: Add syntax rules for signal handlers

Small helpers for signal handling.

`with-signal-handler` is useful for scoping signal handler and
`with-signal-handler-sync` for signal synchronization.

Change-Id: Idc7696fb273003d526f3a4658e7fb5623c2c5827
parent 84c7c9de
No related branches found
No related tags found
No related merge requests found
......@@ -23,6 +23,7 @@ MODULES = \
examples/active-agent.scm \
examples/passive-agent.scm \
jami/logger.scm \
jami/signal.scm \
scenarios/peer-monitor/scenario.scm
GOBJECTS = $(MODULES:%=%.go)
......
(define-module (jami signal)
#:use-module (ice-9 atomic)
#:use-module (ice-9 format)
#:use-module (ice-9 threads)
#:use-module (jami logger)
#:use-module (jami signal bindings)
#:re-export (on-signal)
#:export-syntax (with-signal
with-signal-sync)
#:export (log-signal))
;;; Install for signal SIG an HANDLER and execute bodies. HANDLER is removed
;;; for SIG at the exit of the dynamic extent.
(define-syntax-rule (with-signal sig handler body body* ...)
(let ([continue? (make-atomic-box #t)])
(dynamic-wind
(const #f)
(lambda ()
(on-signal
sig
(lambda args
(let ([continue? (atomic-box-ref continue?)])
(when continue? (apply handler args))
continue?)))
body
body* ...)
(lambda () (atomic-box-set! continue? #f)))))
(define* (wait-cnd cnd mtx #:optional timeout)
(if timeout
(wait-condition-variable cnd mtx (+ (current-time) timeout))
(wait-condition-variable cnd mtx)))
;;; Synchronize with Jami's signal. Install signal handler for SIG and wait for
;;; either PRED to return #t or that TIMEOUT seconds has expired, after TRIGGERS
;;; are been executed.
(define-syntax with-signal-sync
(syntax-rules ()
[(_ sig pred) (with-signal-sync sig pred #f #f)]
[(_ sig pred timeout) (with-signal-sync sig pred timeout #f)]
[(_ sig pred timeout triggers ...)
(let ([mtx (make-recursive-mutex)]
[cnd (make-condition-variable)])
(with-mutex mtx
(on-signal sig (lambda args
(let ([sync? (apply pred args)])
(when sync?
(with-mutex mtx
(signal-condition-variable cnd)))
(not sync?))))
(begin triggers ...)
(wait-cnd cnd mtx timeout)))]))
;;; Install signal handler for SIG that print the arguments of the signal.
(define (log-signal sig)
(on-signal sig (lambda args
(info "Signal ~a:~{ ~a~}" sig args)
#t)))
......@@ -40,7 +40,7 @@ install_scheme_primitives()
load_module("jami call", install_call_primitives);
load_module("jami conversation", install_conversation_primitives);
load_module("jami logger bindings", install_logger_primitives);
load_module("jami signal", install_signal_primitives);
load_module("jami signal bindings", install_signal_primitives);
}
/*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment