Added new API to BackgroundJob: allow to pass down a block to evaluate as background job
authorJan Vrany <jan.vrany@fit.cvut.cz>
Sat, 21 Feb 2015 23:42:34 +0100
changeset 3505 148aeaa05f75
parent 3504 98cfb12e9c3d
child 3506 bf90531e8a26
Added new API to BackgroundJob: allow to pass down a block to evaluate as background job ...in addition to evaluate preconfigured one. See #start: and #restart:
BackgroundJob.st
--- a/BackgroundJob.st	Sat Feb 21 12:13:35 2015 +0100
+++ b/BackgroundJob.st	Sat Feb 21 23:42:34 2015 +0100
@@ -48,17 +48,74 @@
 "
 ! !
 
+!BackgroundJob methodsFor:'processing'!
+
+process
+    "Actually perform the job. This method is called from the background worker thread"               
+
+    self process:job
+
+    "Created: / 21-02-2015 / 10:14:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+process: aBlock
+    aBlock value
+
+    "Created: / 21-02-2015 / 10:19:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!BackgroundJob methodsFor:'start & stop'!
+
+restart: aBlock
+    "Restart the job, evaluationg aBlock instead of pre-configured job"
+
+    running ifTrue:[
+        self stop.
+    ].
+    self start: aBlock
+
+    "Created: / 21-02-2015 / 10:16:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+start: aBlock
+    "Start the job, evaluating aBlock instead of pre-configured `job`."
+
+    self start: aBlock withPriority: priority
+
+    "Created: / 21-02-2015 / 10:17:34 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+start: block withPriority: prio
+    | t |
+
+    ((t := thread) isNil or:[t isDead]) ifTrue:[
+        thread := [
+            [
+                running := true.
+                self process: block.
+            ] ensure: [
+                running := false.
+                thread := nil
+            ]
+        ] newProcess.
+        self setupThread: thread priority: prio.
+        thread resume.
+    ]
+
+    "Created: / 21-02-2015 / 10:18:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
 !BackgroundJob class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/BackgroundJob.st,v 1.14 2015-02-21 10:03:13 vrany Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/BackgroundJob.st,v 1.15 2015-02-21 22:42:34 vrany Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic2/BackgroundJob.st,v 1.14 2015-02-21 10:03:13 vrany Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/BackgroundJob.st,v 1.15 2015-02-21 22:42:34 vrany Exp $'
 !
 
 version_SVN
-    ^ '$Id: BackgroundJob.st,v 1.14 2015-02-21 10:03:13 vrany Exp $'
+    ^ '$Id: BackgroundJob.st,v 1.15 2015-02-21 22:42:34 vrany Exp $'
 ! !