From 0f49b8fac5821694a2db6f88b95423e5ad8aa719 Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Wed, 21 Aug 2013 08:15:09 +0200 Subject: [PATCH] Functions to merge systems, using a linear way or using threads * src/system.scm: join-systems threaded-systems * src/test.scm: merge functions testing --- src/system.scm | 30 +++++++++++++++++++++++++++++- src/test.scm | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/system.scm b/src/system.scm index 9df7cdc..3c84548 100644 --- a/src/system.scm +++ b/src/system.scm @@ -180,5 +180,33 @@ (set-entities res e c)))))) +(define (join-systems . systems) + (lambda (entities components) + (let run ((s systems) (e entities) (c components)) + (cond ((null? s) + (values e c)) + (else + (receive (e2 c2) (((car s) e c)) + (run (cdr s) e2 c2))))))) + + +(define (threaded-systems . systems) + (lambda (entities components) + (let run-wait ((thd + (map + (lambda (s) + (call-with-new-thread + (lambda () (s entities components)))) + systems)) + (e entities) (c components)) + (cond ((null? thd) + (values e c)) + (else + (receive (e2 c2) ((join-thread (car thd)) e c) + (run-wait (cdr thd) e2 c2))))))) + + (export find-entities-by-components - make-system) + make-system + join-systems + threaded-systems) diff --git a/src/test.scm b/src/test.scm index 021afef..45bdef5 100644 --- a/src/test.scm +++ b/src/test.scm @@ -86,3 +86,37 @@ )) (export test2) + + +(define (test3) + (let ((entities '()) + (components '()) + (s1 (make-system '(l) + (lambda (e) + (map + (lambda (e1) + `(,(car e1) . ((l . (cons 1 (cdr e1))))) + e))))) + (s2 (make-system '(l) + (lambda (e) + (map + (lambda (e1) + `(,(car e1) . ((l . (cons 2 (cdr e1)))))) + e))))) + (receive (e c) (set-entities `((#f . ((l . ()))) (#f . ((l . ())))) entities components) + ((join-systems s1 s2) e c)))) + +(export test3) + + +(define (test4) + (let ((f1 (lambda (e c) (sleep 3) (lambda (e2 c2) (values (+ 1 e2) c2)))) + (f2 (lambda (e c) (sleep 4) (lambda (e2 c2) (values e2 (+ 10 c2)))))) + (let ((t (current-time))) + (receive (e c) ((join-systems f1 f2) 2 2) (format #t "~a~%~a~%" e c)) + (display (- (current-time) t)) (newline) (newline)) + (let ((t (current-time))) + (receive (e c) ((threaded-systems f1 f2) 2 2) (format #t "~a~%~a~%" e c)) + (display (- (current-time) t)) (newline) (newline)))) + +(export test4) -- 2.39.5