From: Javier Sancho Date: Fri, 3 Jul 2015 14:58:14 +0000 (+0200) Subject: Adding REPL cooperative server to the engine loop X-Git-Url: https://git.jsancho.org/?a=commitdiff_plain;h=52af2b21c93a97e9ff5b8a22d0ba5df2cba766ec;p=gacela.git Adding REPL cooperative server to the engine loop * src/engine.scm: Function start-engine has an optional socket parameter for starting a REPL and accessing from an external thread --- diff --git a/src/engine.scm b/src/engine.scm index 7831ff3..dd8a8b2 100644 --- a/src/engine.scm +++ b/src/engine.scm @@ -21,7 +21,9 @@ #:use-module (ice-9 receive) #:use-module (ice-9 threads) #:use-module (srfi srfi-9) - #:use-module (srfi srfi-9 gnu)) + #:use-module (srfi srfi-9 gnu) + #:use-module (system repl server) + #:use-module (system repl coop-server)) ;;; Engine Inner Properties @@ -38,7 +40,6 @@ (make-engine-record entities mutex running-mutex systems) engine? (entities engine-entities set-engine-entities!) - (mutex engine-mutex set-engine-mutex!) (running-mutex engine-running-mutex set-engine-running-mutex!) (systems engine-systems set-engine-systems!)) @@ -52,7 +53,6 @@ (make-engine-record (make-entity-set (new-entity (default-engine-inner-properties))) (make-mutex) - (make-mutex) systems)) (define-syntax define-engine @@ -71,24 +71,32 @@ ;;; Engine execution -(define (start-engine engine) - (cond ((not (engine-running? engine)) - (with-mutex (engine-running-mutex engine) - (let loop () - (let ((t (current-utime)) - (delay 0) - (halt #f)) - (with-mutex (engine-mutex engine) - (for-each - (lambda (s) (eval-system s engine)) - (engine-systems engine)) - (set! delay (- (inexact->exact (* (engine-property engine 'step) 1000000)) - (- (current-utime) t))) - (set! halt (engine-stopping? engine #:clean #t))) - (cond ((not halt) - (cond ((> delay 0) - (usleep delay))) - (loop))))))))) +(define* (start-engine engine #:optional (socket #f)) + (catch + #t + (lambda () + (with-mutex (engine-running-mutex engine) + (let loop ((coop-server (if socket (spawn-coop-repl-server socket) #f))) + (let ((t (current-utime)) + (delay 0) + (halt #f)) + (for-each + (lambda (s) (eval-system s engine)) + (engine-systems engine)) + (set! delay (- (inexact->exact (* (engine-property engine 'step) 1000000)) + (- (current-utime) t))) + (set! halt (engine-stopping? engine #:clean #t)) + (if coop-server + (poll-coop-repl-server coop-server)) + (cond ((not halt) + (cond ((> delay 0) + (usleep delay))) + (loop coop-server)) + (else + (if coop-server + (stop-server-and-clients!)))))))) + (lambda (key args) + #f))) (define (eval-system system engine) (call-with-values