BackgroundQueueProcessingJob.st
author Claus Gittinger <cg@exept.de>
Fri, 01 Jul 2011 15:21:58 +0200
changeset 2573 7c39e920ef05
child 2575 b86b3329f23c
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 }"

BackgroundJob subclass:#BackgroundQueueProcessingJob
	instanceVariableNames:'queue queueAccessLock'
	classVariableNames:''
	poolDictionaries:''
	category:'Interface-CodeView'
!

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

!BackgroundQueueProcessingJob class methodsFor:'instance creation'!

new
    "return an initialized instance"

    ^ self basicNew initialize.
! !

!BackgroundQueueProcessingJob methodsFor:'adding & removing'!

add: object

    "includes: is not synchronized, but should not harm"

    queueAccessLock critical:[
        (queue includes: object)
            ifFalse:[queue add: object].
        self start].

    "Created: / 28-04-2011 / 20:40:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 18-05-2011 / 23:30:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!BackgroundQueueProcessingJob methodsFor:'initialization'!

initialize
    "Invoked when a new instance is created."


    queue := OrderedCollection new.
    queueAccessLock := RecursionLock new.

    "Modified: / 18-05-2011 / 23:25:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!BackgroundQueueProcessingJob methodsFor:'processing'!

process

    | item |

    [
	item := nil.	 
        queueAccessLock critical:
            [queue isEmpty ifFalse:[item := queue removeFirst]].        
	item isNil ifFalse:
	    [self processItem: item].
	item notNil.
    ] whileTrue

    "Created: / 28-04-2011 / 20:36:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 18-05-2011 / 23:30:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

processItem: item

    ^job value: item

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

!BackgroundQueueProcessingJob class methodsFor:'documentation'!

version_CVS
    ^ '$Header: /cvs/stx/stx/libbasic2/BackgroundQueueProcessingJob.st,v 1.1 2011-07-01 13:21:58 cg Exp $'
!

version_SVN
    ^ '§Id§'
! !