simpler (?) non-mutex application-lock added by Tolstoy on Mon Jun 22 09:57:34 2020

;;

(import (chicken process signal))
(import (chicken condition))
(import srfi-18)

(define (wait-for-signal on-exit)
  (let* [(wait-forever (lambda _
                         (let loop ()
                           (thread-sleep! 10000)
                           (loop))))
         (t (thread-start! wait-forever))
         (terminate! (lambda _ (thread-terminate! t)))]

    (set-signal-handler! signal/int terminate!)
    (set-signal-handler! signal/hup terminate!)
    (with-exception-handler
     (lambda _ (on-exit))
     (lambda _ (thread-join! t)))))

(define (main)
  (print "Initializing ...")
  ;; Create some resources
  ;;   spawn some services (passing in resources, or state)
  ;;     and then...
  (print "And now we're waiting.")
  (wait-for-signal
   (lambda _
     (print "now exiting, clean up resources, stop threads, whatever"))))

(main)