BackgroundJob.st
author Claus Gittinger <cg@exept.de>
Wed, 03 Aug 2011 16:43:28 +0200
changeset 2588 8773788944dd
parent 2587 e149cfcded61
child 2590 89d5257edfcf
permissions -rw-r--r--
x

"
 COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague
	      All Rights Reserved

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the 'Software'), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"
"{ Package: 'stx:libbasic2' }"

Object subclass:#BackgroundJob
	instanceVariableNames:'name job priority thread running'
	classVariableNames:''
	poolDictionaries:''
	category:'Interface-CodeView'
!

!BackgroundJob class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague
	      All Rights Reserved

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the 'Software'), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"
! !

!BackgroundJob class methodsFor:'instance creation'!

named: name

    ^self new name: name

    "Created: / 28-04-2011 / 20:26:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

named: name on: block

    ^self new name: name; job: block

    "Created: / 28-04-2011 / 20:27:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

new
    "return an initialized instance"

    ^ self basicNew initialize.
!

on: block

    ^self new job: block

    "Created: / 28-04-2011 / 20:30:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!BackgroundJob methodsFor:'accessing'!

job
    ^ job
!

job:aBlockOrMessageSend
    job := aBlockOrMessageSend.
!

name
    ^ name ? '<unnamed background job>'

    "Modified: / 28-04-2011 / 20:29:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

name:aString
    name := aString.
!

priority
    ^ priority
!

priority:anInteger
    "Set the priority of a job thread"

    priority := anInteger.

    "Modified (comment): / 29-07-2011 / 10:54:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (format): / 03-08-2011 / 16:42:45 / cg"
! !

!BackgroundJob methodsFor:'initialization'!

initialize
    priority := Processor userBackgroundPriority.
    running := false.

    "/ super initialize.   -- commented since inherited method does nothing

    "Modified: / 29-07-2011 / 10:52:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (format): / 03-08-2011 / 16:42:51 / cg"
! !

!BackgroundJob methodsFor:'processing'!

process
    "Do the job. May be overriden by subclasses to
     do some more complex operations"

    running := true.
    job value

    "Created: / 28-04-2011 / 20:23:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (format): / 03-08-2011 / 16:42:42 / cg"
! !

!BackgroundJob methodsFor:'queries'!

running
    "Return true if the job is actually running, i.e., if it
     is actually computing a value, contrary to #scheduled, that
     returns true even if computation actually did not start
     (i.e., thread is created but was not scheduled so far)"

    ^running

    "Created: / 29-07-2011 / 10:23:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (format): / 03-08-2011 / 16:42:35 / cg"
!

scheduled
    "Return true, if the job has been already started"

    ^thread notNil

    "Created: / 29-07-2011 / 10:47:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (format): / 03-08-2011 / 16:42:32 / cg"
! !

!BackgroundJob methodsFor:'start & stop'!

restart
    running ifTrue:[
        self stop.
    ].
    self start

    "Created: / 28-04-2011 / 20:31:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (format): / 03-08-2011 / 16:42:39 / cg"
!

start

    self startWithPriority: priority

    "Created: / 28-04-2011 / 20:23:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

startWithPriority: prio
    | t |

    ((t := thread) isNil or:[t isDead]) ifTrue:[
        thread := [
            [
                self process
            ] ensure: [
                running := false.
                thread := nil
            ]
        ] newProcess.
        thread priority: prio.
        thread name: name.
        thread resume.
    ]

    "Created: / 29-07-2011 / 11:04:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (format): / 03-08-2011 / 16:42:19 / cg"
!

stop
    | t |

    (t := thread) ifNil:[^self].
    thread := nil.
    t terminate.
    "/ raise its prio to make it terminate quickly
    t priority:(Processor userSchedulingPriority + 1)

    "Created: / 28-04-2011 / 20:31:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (format): / 03-08-2011 / 16:41:53 / cg"
! !

!BackgroundJob class methodsFor:'documentation'!

version_CVS
    ^ '$Header: /cvs/stx/stx/libbasic2/BackgroundJob.st,v 1.5 2011-08-03 14:43:28 cg Exp $'
!

version_SVN
    ^ '§Id§'
! !