You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by bl...@apache.org on 2002/05/29 19:44:26 UTC
cvs commit: jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/event AbstractQueue.java DefaultQueue.java FixedSizeQueue.java Source.java
bloritsch 02/05/29 10:44:25
Modified: event/src/java/org/apache/excalibur/event AbstractQueue.java
DefaultQueue.java FixedSizeQueue.java Source.java
Log:
applied patch from Greg Steuck (greg-apache-bugs@nest.cx)
Revision Changes Path
1.8 +2 -0 jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/event/AbstractQueue.java
Index: AbstractQueue.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/event/AbstractQueue.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- AbstractQueue.java 2 May 2002 16:26:48 -0000 1.7
+++ AbstractQueue.java 29 May 2002 17:44:25 -0000 1.8
@@ -15,6 +15,8 @@
*/
public abstract class AbstractQueue implements Queue
{
+ // this object is immutable, so it can be safely shared
+ protected final static QueueElement[] EMPTY_ARRAY = new QueueElement[ 0 ];
protected long m_timeout = 0;
/**
1.13 +29 -26 jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/event/DefaultQueue.java
Index: DefaultQueue.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/event/DefaultQueue.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- DefaultQueue.java 2 May 2002 16:26:48 -0000 1.12
+++ DefaultQueue.java 29 May 2002 17:44:25 -0000 1.13
@@ -176,14 +176,7 @@
public QueueElement[] dequeue( final int numElements )
{
- int arraySize = numElements;
-
- if( size() < numElements )
- {
- arraySize = size();
- }
-
- QueueElement[] elements = null;
+ QueueElement[] elements = EMPTY_ARRAY;
try
{
@@ -191,17 +184,9 @@
{
try
{
- if( size() < numElements )
- {
- arraySize = size();
- }
-
- elements = new QueueElement[ arraySize ];
-
- for( int i = 0; i < arraySize; i++ )
- {
- elements[ i ] = (QueueElement)m_elements.remove();
- }
+ elements = retrieveElements( m_elements,
+ Math.min( size(),
+ numElements ) );
}
finally
{
@@ -218,7 +203,7 @@
public QueueElement[] dequeueAll()
{
- QueueElement[] elements = null;
+ QueueElement[] elements = EMPTY_ARRAY;
try
{
@@ -226,12 +211,7 @@
{
try
{
- elements = new QueueElement[ size() ];
-
- for( int i = 0; i < elements.length; i++ )
- {
- elements[ i ] = (QueueElement)m_elements.remove();
- }
+ elements = retrieveElements( m_elements, size() );
}
finally
{
@@ -241,6 +221,29 @@
}
catch( InterruptedException ie )
{
+ }
+
+ return elements;
+ }
+
+ /**
+ * Removes the given number of elements from the given <code>buf</code>
+ * and returns them in an array. Trusts the caller to pass in a buffer
+ * full of <code>QueueElement</code>s and with at least <code>count</code>
+ * elements available.
+ * <p>
+ * @param buf to remove elements from, the caller is responsible
+ * for synchronizing access
+ * @param count number of elements to remove/return
+ * @return requested number of elements
+ */
+ private static QueueElement[] retrieveElements( Buffer buf, int count )
+ {
+ QueueElement[] elements = new QueueElement[ count ];
+
+ for( int i = 0; i < count; i++ )
+ {
+ elements[ i ] = (QueueElement) buf.remove();
}
return elements;
1.9 +102 -76 jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/event/FixedSizeQueue.java
Index: FixedSizeQueue.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/event/FixedSizeQueue.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- FixedSizeQueue.java 2 May 2002 16:26:48 -0000 1.8
+++ FixedSizeQueue.java 29 May 2002 17:44:25 -0000 1.9
@@ -59,21 +59,23 @@
try
{
m_mutex.acquire();
+ try
+ {
+ if( elements.length + m_reserve + size() > maxSize() )
+ {
+ throw new SinkFullException( "Not enough room to enqueue these elements." );
+ }
- if( elements.length + m_reserve + size() > maxSize() )
+ enqueue = new FixedSizePreparedEnqueue( this, elements );
+ }
+ finally
{
- throw new SinkFullException( "Not enough room to enqueue these elements." );
+ m_mutex.release();
}
-
- enqueue = new FixedSizePreparedEnqueue( this, elements );
}
catch( InterruptedException ie )
{
}
- finally
- {
- m_mutex.release();
- }
return enqueue;
}
@@ -85,22 +87,24 @@
try
{
m_mutex.acquire();
+ try
+ {
+ if( 1 + m_reserve + size() > maxSize() )
+ {
+ return false;
+ }
- if( 1 + m_reserve + size() > maxSize() )
+ addElement( element );
+ success = true;
+ }
+ finally
{
- return false;
+ m_mutex.release();
}
-
- addElement( element );
- success = true;
}
catch( InterruptedException ie )
{
}
- finally
- {
- m_mutex.release();
- }
return success;
}
@@ -113,23 +117,26 @@
try
{
m_mutex.acquire();
- if( elements.length + m_reserve + size() > maxSize() )
+ try
{
- throw new SinkFullException( "Not enough room to enqueue these elements." );
- }
+ if( elements.length + m_reserve + size() > maxSize() )
+ {
+ throw new SinkFullException( "Not enough room to enqueue these elements." );
+ }
- for( int i = 0; i < len; i++ )
+ for( int i = 0; i < len; i++ )
+ {
+ addElement( elements[ i ] );
+ }
+ }
+ finally
{
- addElement( elements[ i ] );
+ m_mutex.release();
}
}
catch( InterruptedException ie )
{
}
- finally
- {
- m_mutex.release();
- }
}
public void enqueue( final QueueElement element )
@@ -138,56 +145,47 @@
try
{
m_mutex.acquire();
- if( 1 + m_reserve + size() > maxSize() )
+ try
{
- throw new SinkFullException( "Not enough room to enqueue these elements." );
- }
+ if( 1 + m_reserve + size() > maxSize() )
+ {
+ throw new SinkFullException( "Not enough room to enqueue these elements." );
+ }
- addElement( element );
+ addElement( element );
+ }
+ finally
+ {
+ m_mutex.release();
+ }
}
catch( InterruptedException ie )
{
}
- finally
- {
- m_mutex.release();
- }
}
public QueueElement[] dequeue( final int numElements )
{
- int arraySize = numElements;
-
- if( size() < numElements )
- {
- arraySize = size();
- }
-
- QueueElement[] elements = null;
+ QueueElement[] elements = EMPTY_ARRAY;
try
{
- m_mutex.attempt( m_timeout );
-
- if( size() < numElements )
- {
- arraySize = size();
- }
-
- elements = new QueueElement[ arraySize ];
-
- for( int i = 0; i < arraySize; i++ )
+ if( m_mutex.attempt( m_timeout ) )
{
- elements[ i ] = removeElement();
+ try
+ {
+ elements = retrieveElements( Math.min( size(),
+ numElements ) );
+ }
+ finally
+ {
+ m_mutex.release();
+ }
}
}
catch( InterruptedException ie )
{
}
- finally
- {
- m_mutex.release();
- }
return elements;
}
@@ -221,28 +219,52 @@
return element;
}
- public QueueElement[] dequeueAll()
+ /**
+ * Removes exactly <code>count</code> elements from the underlying
+ * element store and returns them as an array of QueueElements.
+ * The caller is responsible for synchronizing access to the
+ * element store and passing the correct value for
+ * <code>count</code>.
+ * <p>
+ * The method can be further optimized by using System.arraycopy
+ * if it is found to underperform.
+ *
+ * @param count number of elements to return
+ * @return requested number of elements
+ */
+ private final QueueElement[] retrieveElements( int count )
{
- QueueElement[] elements = null;
+ QueueElement[] elements = new QueueElement[ count ];
- try
+ for( int i = 0; i < count; i++ )
{
- m_mutex.attempt( m_timeout );
+ elements[ i ] = removeElement();
+ }
- elements = new QueueElement[ size() ];
+ return elements;
+ }
+
+ public QueueElement[] dequeueAll()
+ {
+ QueueElement[] elements = EMPTY_ARRAY;
- for( int i = 0; i < elements.length; i++ )
+ try
+ {
+ if( m_mutex.attempt( m_timeout ) )
{
- elements[ i ] = removeElement();
+ try
+ {
+ elements = retrieveElements( size() );
+ }
+ finally
+ {
+ m_mutex.release();
+ }
}
}
catch( InterruptedException ie )
{
}
- finally
- {
- m_mutex.release();
- }
return elements;
}
@@ -253,19 +275,23 @@
try
{
- m_mutex.attempt( m_timeout );
-
- if( size() > 0 )
+ if( m_mutex.attempt( m_timeout ) )
{
- element = removeElement();
+ try
+ {
+ if( size() > 0 )
+ {
+ element = removeElement();
+ }
+ }
+ finally
+ {
+ m_mutex.release();
+ }
}
}
catch( InterruptedException ie )
{
- }
- finally
- {
- m_mutex.release();
}
return element;
1.9 +10 -7 jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/event/Source.java
Index: Source.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/event/src/java/org/apache/excalibur/event/Source.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- Source.java 2 May 2002 16:26:48 -0000 1.8
+++ Source.java 29 May 2002 17:44:25 -0000 1.9
@@ -27,26 +27,29 @@
void setTimeout( long millis );
/**
- * Dequeues the next element, or returns <code>null</code> if there is
- * nothing left on the queue.
+ * Dequeues the next element, or <code>null</code> if there is
+ * nothing left on the queue or in case of a timeout while
+ * attempting to obtain the mutex
*
* @return the next <code>QueueElement</code> on the queue
*/
QueueElement dequeue();
/**
- * Dequeues all available elements, or returns <code>null</code> if there is
- * nothing left on the queue.
+ * Dequeues all available elements. Returns a zero-sized array in
+ * case of a timeout while attempting to obtain the mutex or if
+ * there is nothing left on the queue.
*
* @return all pending <code>QueueElement</code>s on the queue
*/
QueueElement[] dequeueAll();
/**
- * Dequeues at most <code>num</code> available elements, or returns
- * <code>null</code> if there is nothing left on the queue.
+ * Dequeues at most <code>num</code> available elements. Returns a
+ * zero-sized array in case of a timeout while attempting to
+ * obtain the mutex or if there is nothing left on the queue.
*
- * @return At most <code>num</code> <code>QueueElement</code>s on the queue
+ * @return At most <code>num</code> <code>QueueElement</code>s from the queue
*/
QueueElement[] dequeue( int num );
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>