BackgroundJob.st
author Claus Gittinger <cg@exept.de>
Fri, 01 Jul 2011 15:22:08 +0200
changeset 2574 0c9345fafac7
child 2576 7f5b86f90249
permissions -rw-r--r--
initial checkin

"
 COPYRIGHT (c) 2006 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libtool' }"

"{ NameSpace: Tools }"

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

!BackgroundJob class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2006 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
! !

!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>"
!

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.
! !

!BackgroundJob methodsFor:'processing'!

process

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

    job value

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

!BackgroundJob methodsFor:'start & stop'!

restart

    self stop; start

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

start

    | t |
    ((t := thread) isNil or:[t isDead]) ifTrue:
        [thread := 
            [
                [ self process ] 
                    ensure:
                        [thread := nil]
            ] newProcess.
        thread priority: Processor userBackgroundPriority.
        thread name: name.
        thread resume.
    ]

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

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>"
! !

!BackgroundJob class methodsFor:'documentation'!

version_CVS
    ^ '$Header: /cvs/stx/stx/libbasic2/BackgroundJob.st,v 1.1 2011-07-01 13:22:08 cg Exp $'
!

version_SVN
    ^ '§Id§'
! !