SharedQueue.st
author claus
Fri, 05 Aug 1994 03:02:31 +0200
changeset 34 8913d4dad799
parent 30 f34b335ac2d7
child 68 6650e0d50a1a
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 1993 by Claus Gittinger
              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.
"

Queue subclass:#SharedQueue
         instanceVariableNames:'accessProtect dataAvailable spaceAvailable'
         classVariableNames:''
         poolDictionaries:''
         category:'Collections-Ordered'!

SharedQueue comment:'
COPYRIGHT (c) 1993 by Claus Gittinger
              All Rights Reserved

$Header: /cvs/stx/stx/libbasic2/SharedQueue.st,v 1.7 1994-08-05 01:02:31 claus Exp $
'!

!SharedQueue class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1993 by Claus Gittinger
              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.
"
!

version
"
$Header: /cvs/stx/stx/libbasic2/SharedQueue.st,v 1.7 1994-08-05 01:02:31 claus Exp $
"
!

documentation
"
    SharedQueues provide a safe mechanism for processes to communicate.
    They are basically Queues, with added secure access to the internals,
    allowing use from multiple processes (i.e. the access methods use
    critical regions to protect against confusion due to a process
    switch within a modification).

    Also, sharedQueues can be used for synchronization, since a reading
    process will be blocked when attempting to read an empty queue, while
    a writer will be blocked when attempting to write into a full queue.
    For nonBlocking read, use #isEmpty; for nonBlocking write, use #isFull.

    See samples in doc/coding.
"
! !

!SharedQueue methodsFor:'initialization'!

init:size
    "initialize the receiver for size entries"

    super init:size.
    accessProtect := Semaphore forMutualExclusion.
    dataAvailable := Semaphore new.
    spaceAvailable := Semaphore new
! !

!SharedQueue methodsFor:'accessing'!

next
    "return the next value in the queue; if it its empty, wait 'til
     something is put into the receiver.
     When the datum has been removed, signal space-availability to
     writers"

    |value ok|

    ok := false.
    [ok] whileFalse:[
        [tally == 0] whileTrue:[
            dataAvailable wait
        ].
        accessProtect critical:[
            "
             this check is needed, since another process may
             have read the value in the meantime ...
            "
            tally == 0 ifFalse:[
                value := super next.
                tally == (contentsArray size - 1) ifTrue:[
                    spaceAvailable signal
                ].
                ok := true
            ]
        ]
    ].
    ^ value
!

nextPut:anObject
    "enter anObject into the queue; wait for available space, if
     the queue is full. After the put, signal availablity of a datum
     to readers."

    |ok|

    ok := false.
    [ok] whileFalse:[
        [tally == contentsArray size] whileTrue:[
            spaceAvailable wait
        ].
        accessProtect critical:[
            tally == contentsArray size ifFalse:[
                super nextPut:anObject.
                tally == 1 ifTrue:[
                    dataAvailable signal
                ].
                ok := true
            ]
        ]
    ].
    ^ anObject
! !