Author: trustin
Date: Mon Nov  5 02:19:56 2007
New Revision: 591948

URL: http://svn.apache.org/viewvc?rev=591948&view=rev
Log:
* Added SynchronizedQueue
* Changed AbstractIoSession.writeRequestQueue to use a synchronize 
CircularQueue, which performs better in most cases.

Added:
    mina/trunk/core/src/main/java/org/apache/mina/util/SynchronizedQueue.java   
(with props)
Modified:
    mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoSession.java

Modified: 
mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoSession.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoSession.java?rev=591948&r1=591947&r2=591948&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoSession.java 
(original)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/AbstractIoSession.java 
Mon Nov  5 02:19:56 2007
@@ -26,11 +26,13 @@
 import java.nio.channels.FileChannel;
 import java.util.Queue;
 import java.util.Set;
-import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
 
+import org.apache.mina.util.CircularQueue;
+import org.apache.mina.util.SynchronizedQueue;
+
 
 /**
  * Base implementation of [EMAIL PROTECTED] IoSession}.
@@ -61,12 +63,13 @@
     private IoSessionAttributeMap attributes;
 
     private final Queue<WriteRequest> writeRequestQueue =
-        new ConcurrentLinkedQueue<WriteRequest>() {
-            private static final long serialVersionUID = -3899506857975733565L;
+        new SynchronizedQueue<WriteRequest>(new 
CircularQueue<WriteRequest>(512)) {
+
+            private static final long serialVersionUID = 6579730560333933524L;
 
             // Discard close request offered by closeOnFlush() silently.
             @Override
-            public WriteRequest peek() {
+            public synchronized WriteRequest peek() {
                 WriteRequest answer = super.peek();
                 if (answer == CLOSE_REQUEST) {
                     AbstractIoSession.this.close();
@@ -77,7 +80,7 @@
             }
 
             @Override
-            public WriteRequest poll() {
+            public synchronized WriteRequest poll() {
                 WriteRequest answer = super.poll();
                 if (answer == CLOSE_REQUEST) {
                     AbstractIoSession.this.close();

Added: mina/trunk/core/src/main/java/org/apache/mina/util/SynchronizedQueue.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/util/SynchronizedQueue.java?rev=591948&view=auto
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/util/SynchronizedQueue.java 
(added)
+++ mina/trunk/core/src/main/java/org/apache/mina/util/SynchronizedQueue.java 
Mon Nov  5 02:19:56 2007
@@ -0,0 +1,130 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.mina.util;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Queue;
+
+/**
+ * A decorator that makes the specified [EMAIL PROTECTED] Queue} thread-safe.
+ * Like any other synchronizing wrappers, iteration is not thread-safe.
+ * 
+ * @author The Apache Directory Project ([EMAIL PROTECTED])
+ * @version $Rev$, $Date$
+ */
+public class SynchronizedQueue<E> implements Queue<E>, Serializable {
+    
+    private static final long serialVersionUID = -1439242290701194806L;
+    
+    private final Queue<E> q;
+
+    public SynchronizedQueue(Queue<E> q) {
+        this.q = q;
+    }
+    
+    public synchronized boolean add(E e) {
+        return q.add(e);
+    }
+
+    public synchronized E element() {
+        return q.element();
+    }
+
+    public synchronized boolean offer(E e) {
+        return q.offer(e);
+    }
+
+    public synchronized E peek() {
+        return q.peek();
+    }
+
+    public synchronized E poll() {
+        return q.poll();
+    }
+
+    public synchronized E remove() {
+        return q.remove();
+    }
+
+    public synchronized boolean addAll(Collection<? extends E> c) {
+        return q.addAll(c);
+    }
+
+    public synchronized void clear() {
+        q.clear();
+    }
+
+    public synchronized boolean contains(Object o) {
+        return q.contains(o);
+    }
+
+    public synchronized boolean containsAll(Collection<?> c) {
+        return q.containsAll(c);
+    }
+
+    public synchronized boolean isEmpty() {
+        return q.isEmpty();
+    }
+
+    public synchronized Iterator<E> iterator() {
+        return q.iterator();
+    }
+
+    public synchronized boolean remove(Object o) {
+        return q.remove(o);
+    }
+
+    public synchronized boolean removeAll(Collection<?> c) {
+        return q.removeAll(c);
+    }
+
+    public synchronized boolean retainAll(Collection<?> c) {
+        return q.retainAll(c);
+    }
+
+    public synchronized int size() {
+        return q.size();
+    }
+
+    public synchronized Object[] toArray() {
+        return q.toArray();
+    }
+
+    public synchronized <T> T[] toArray(T[] a) {
+        return q.toArray(a);
+    }
+
+    @Override
+    public synchronized boolean equals(Object obj) {
+        return q.equals(obj);
+    }
+
+    @Override
+    public synchronized int hashCode() {
+        return q.hashCode();
+    }
+
+    @Override
+    public synchronized String toString() {
+        return q.toString();
+    }
+}
\ No newline at end of file

Propchange: 
mina/trunk/core/src/main/java/org/apache/mina/util/SynchronizedQueue.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
mina/trunk/core/src/main/java/org/apache/mina/util/SynchronizedQueue.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date


Reply via email to