BackgroundQueueProcessingJob.st
author Claus Gittinger <cg@exept.de>
Sat, 02 May 2020 21:40:13 +0200
changeset 5476 7355a4b11cb6
parent 4502 108026668d4b
permissions -rw-r--r--
#FEATURE by cg class: Socket class added: #newTCPclientToHost:port:domain:domainOrder:withTimeout: changed: #newTCPclientToHost:port:domain:withTimeout:

"
 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' }"

"{ NameSpace: Smalltalk }"

AbstractBackgroundJob subclass:#BackgroundQueueProcessingJob
	instanceVariableNames:'queue queueAccessLock queueProcessedSema'
	classVariableNames:''
	poolDictionaries:''
	category:'System-Support'
!

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

documentation
"
    A BackgroundQueueProcessingJob is a specialized form
    of a BackgroundJob for background processing of a queue.
    Each item in the queue (added by sending #add: item) 
    is processed (using the instance variable job).

    Implementation notes:
    The thread is running only if there is at least one item
    to process. When the queue is empty, the thread teminates.
    It is started again when a new item is added to the queue.

    [author:]
        Jan Vrany <jan.vrany@fit.cvut.cz>

    [instance variables:]
        queue       <OrderedCollection>     the queue of items to be procesed
        queueAccessLock<RecursionLock>      a lock used to synchronize
                                            access to the queue
        queueProcessedSema<Semaphore>       a semaphore signaled when the queue is
                                            processed (and therefore empty).
    [class variables:]

    [see also:]
        BackgroundJob

"
!

examples
"
    | job text |
    job :=  BackgroundQueueProcessingJob named: 'example job' on:[:text|
                Delay waitForSeconds: 3.
                Transcript showCR:'One guy said: ', text
            ].
    job add:'Hello world'. 
    Delay waitForSeconds: 5.
    job add:'Ahoj Svete!!'.
    job restart.
    Delay waitForSeconds: 1.
    job add:'Haya, looks like proper queue, you should see all greetings'.

"
! !


!BackgroundQueueProcessingJob methodsFor:'adding & removing'!

add: object

    ^self add: object at: nil

    "Created: / 28-04-2011 / 20:40:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (format): / 03-08-2011 / 16:43:01 / cg"
    "Modified: / 07-09-2011 / 12:35:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

add: object at: index
    queueAccessLock critical:[
        (queue includes: object) ifFalse:[
            index notNil ifTrue:[
                queue add: object at: index
            ] ifFalse:[
                queue add: object
            ]
        ].
        self start
    ].

    "Modified (format): / 03-08-2011 / 16:43:01 / cg"
    "Created: / 07-09-2011 / 12:34:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!BackgroundQueueProcessingJob methodsFor:'initialization'!

initialize
    "Invoked when a new instance is created."

    super initialize.
    queue := OrderedCollection new.
    queueAccessLock := RecursionLock name:'BackgroundQueueProcessingJob lock'.
    queueProcessedSema := Semaphore name:'BackgroundQueueProcessingJob sema'.

    "Modified: / 06-12-2011 / 15:50:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 20-02-2017 / 20:46:30 / stefan"
    "Modified: / 09-08-2017 / 11:52:07 / cg"
! !

!BackgroundQueueProcessingJob methodsFor:'private'!

setupThread: t priority: p
    "Sets up worker thread"

    super setupThread: t priority: p.
    t addExitAction: [ queueProcessedSema signalForAll ].

    "Created: / 20-01-2012 / 16:40:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!BackgroundQueueProcessingJob methodsFor:'processing'!

process
    [
        |hasMore item |

        hasMore := false.
        queueAccessLock critical:[
            queue notEmpty ifTrue:[
                hasMore := true.
                item := queue removeFirst
            ]
        ].
        hasMore ifFalse:[^ self].
        self processItem: item.
        item := nil.
    ] loop.

    "Created: / 28-04-2011 / 20:36:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (format): / 03-08-2011 / 16:43:21 / cg"
    "Modified: / 20-01-2012 / 16:39:45 / 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 methodsFor:'queries'!

numberOfItemsInQueue
    "Returns number of items waiting to be processed. 
     May not be accurate as there is no synchronization
     (intentionally, for performance reasons)"
    
    ^ queue size

    "Created: / 06-12-2011 / 16:01:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!BackgroundQueueProcessingJob methodsFor:'start & stop'!

stopAndRemoveAll
    "Terminates queue processing and remove all pending
     items in a queue"

    self stop.
    queue := OrderedCollection new.

    "Created: / 09-04-2012 / 17:18:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 22-02-2017 / 15:09:41 / stefan"
! !

!BackgroundQueueProcessingJob methodsFor:'utilities'!

waitUntilProcessed
    "Blocks the receiver until all items from the queue are processed.
    NOTE, that it may block forever if another thread is filling queue
    fast enough."

    | shouldWait |        
    queueAccessLock critical:[
        shouldWait := queue notEmpty.
    ].
    shouldWait ifTrue:[
        self start.
        queueProcessedSema wait.
    ]

    "
    queueProcessedSema signalForAll    
    "

    "Created: / 06-12-2011 / 15:58:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!BackgroundQueueProcessingJob class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
!

version_SVN
    ^ '$Id$'
! !