SharedQueue.st
author Claus Gittinger <cg@exept.de>
Sat, 22 Jun 1996 18:52:25 +0200
changeset 396 88bd6136ee67
parent 257 759055770356
child 469 9b4318b56d9d
permissions -rw-r--r--
added #removeLast

"
 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:'Kernel-Processes'
!

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

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.

    [author:]
        Claus Gittinger

    [see also:]
        Semaphore
        Process
"
! !

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

readSemaphore
    "return the semaphore which is signalled when data is available
     for reading."

    ^ dataAvailable

    "Modified: 16.12.1995 / 13:47:11 / cg"
!

removeLast
    "return the last 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 removeLast.
                tally == (contentsArray size - 1) ifTrue:[
                    spaceAvailable signal
                ].
                ok := true
            ]
        ]
    ].
    ^ value

    "Created: 22.6.1996 / 18:50:12 / cg"
!

writeSemaphore
    "return the semaphore which is signalled when the queue has space
     for writing."

    ^ spaceAvailable

    "Modified: 16.12.1995 / 13:47:07 / cg"
! !

!SharedQueue methodsFor:'initialization'!

init:size
    "initialize the receiver for size entries"

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

!SharedQueue  class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic2/SharedQueue.st,v 1.16 1996-06-22 16:52:25 cg Exp $'
! !