SharedQueue.st
author Claus Gittinger <cg@exept.de>
Sat, 11 Nov 1995 16:22:01 +0100
changeset 112 3e18f2cfe430
parent 85 df13b436b54e
child 141 2804943fc6f0
permissions -rw-r--r--
uff - version methods changed to return stings

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

version
    ^ '$Header: /cvs/stx/stx/libbasic2/SharedQueue.st,v 1.11 1995-11-11 15:21:36 cg 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
! !