diff src/scheme/task.scm @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/scheme/task.scm	Tue Jan 17 17:50:20 2012 +0000
@@ -0,0 +1,88 @@
+;;; task related utilities
+(import "samer.tools.*")
+
+
+(define task (macro body `(SilkTask. (lambda () ,@body))))
+
+;;; convert a CompoundTask to a Scheme list
+(define (tasks->list tasks)
+	(define L ())
+	(define (list-it it)
+		(if (.more it) (begin
+			(set! L (append L (list (.next it))))
+		(list-it it))))
+	(list-it (.iterator tasks))
+	L
+)
+
+;;; create and access main task list
+
+(define _thread null)
+(define _tasklist_stack ())
+
+(define (start) (exec _thread "start"))
+(define (stop)  (exec _thread "stop"))
+(define (thread) _thread)
+
+;;; creates a task list/thread if none exists, returns a task list
+(define (tasks)
+	(if (null? _tasklist_stack)
+		(let ((t (CompoundTask.)))
+			(set! _thread (RThread. t))
+			(push-tasklist t)
+			t
+		)
+		(first _tasklist_stack)
+	)
+)
+
+(define (push-tasklist t)
+  (set! _tasklist_stack (cons t _tasklist_stack)))
+
+(define (pop-tasklist)
+  (let ((t (first _tasklist_stack)))
+    (set! _tasklist_stack (rest _tasklist_stack))
+	 t))
+
+(define with-tasks (macro (tasks . body)
+	`(begin
+		(push-tasklist ,tasks)
+		(tryCatch
+			(let ((a (begin . ,body))) (pop-tasklist) a)
+			(lambda (ex) (pop-tasklist) (throw ex))))))
+
+; add task to current task list
+(define (addtask t) (.addTask (tasks) t) t)
+(define (removetask t) (.removeTask (tasks) t) t)
+(define-method (add (t Task)) (.addTask (tasks) t) t)
+(define-method (addafter (a Task) (t Task)) (.addTaskAfter (tasks) t a) t)
+(define-method (remove (t Task)) (.removeTask (tasks) t) t)
+
+; add multiple tasks to current task list
+(define (addtasks . t)
+	(for-each (lambda (t) (.addTask (tasks) t)) t)
+	(tasks) )
+
+(define-method (pr-tasks) (print (tasks->list (tasks))))
+(define-method (view-tasks) (view-tasks (tasks)))
+(define-method (view-tasks tasks) (view-list (tasks->list tasks) "Tasks"))
+(define-method (task-ref (n Integer)) (list-ref (tasks->list (tasks)) n))
+(define-method (task-ref tasks (n Integer)) (list-ref (tasks->list tasks) n))
+
+(define (switch task) (SwitchTask. task))
+
+;;; Create a subrate task and a UI for the subrate factor
+;;; !! need to dispose of VInteger when task is disposed.
+(define (sub n task)
+	(define vn (VInteger. "subfactor" n))
+	(define subt (SubrateTask. n task))
+	(on-change vn (.setFactor subt (.value$ vn)))
+	(.value$ vn n) (.changed vn)
+	subt
+)
+
+(define (seq . tasks)
+	(define c (CompoundTask.))
+	(for-each (lambda (t) (.addTask c t)) tasks)
+	c)
+