servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r674833 - in /servicemix/smx3/trunk/core/servicemix-services/src: main/java/org/apache/servicemix/locks/impl/ test/java/org/apache/servicemix/locks/ test/java/org/apache/servicemix/locks/impl/
Date Tue, 08 Jul 2008 14:11:29 GMT
Author: gnodet
Date: Tue Jul  8 07:11:29 2008
New Revision: 674833

URL: http://svn.apache.org/viewvc?rev=674833&view=rev
Log:
SM-1441: Use a real lock implementation, because the lock() method is heavily used by servicemix-eip

Added:
    servicemix/smx3/trunk/core/servicemix-services/src/test/java/org/apache/servicemix/locks/
    servicemix/smx3/trunk/core/servicemix-services/src/test/java/org/apache/servicemix/locks/impl/
    servicemix/smx3/trunk/core/servicemix-services/src/test/java/org/apache/servicemix/locks/impl/SimpleLockTest.java
Modified:
    servicemix/smx3/trunk/core/servicemix-services/src/main/java/org/apache/servicemix/locks/impl/SimpleLock.java

Modified: servicemix/smx3/trunk/core/servicemix-services/src/main/java/org/apache/servicemix/locks/impl/SimpleLock.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-services/src/main/java/org/apache/servicemix/locks/impl/SimpleLock.java?rev=674833&r1=674832&r2=674833&view=diff
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-services/src/main/java/org/apache/servicemix/locks/impl/SimpleLock.java
(original)
+++ servicemix/smx3/trunk/core/servicemix-services/src/main/java/org/apache/servicemix/locks/impl/SimpleLock.java
Tue Jul  8 07:11:29 2008
@@ -20,60 +20,84 @@
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.locks.Condition;
 import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.AbstractQueuedSynchronizer;
+import java.io.ObjectInputStream;
+import java.io.IOException;
+import java.io.Serializable;
 
 /**
  * @author lhein
  */
-public class SimpleLock implements Lock {
+public class SimpleLock implements Lock, Serializable {
 
-    private AtomicBoolean lock;
+    // Our internal helper class
+    private static class Sync extends AbstractQueuedSynchronizer {
+        // Report whether in locked state
+        protected boolean isHeldExclusively() {
+            return getState() == 1;
+        }
+
+        // Acquire the lock if state is zero
+        public boolean tryAcquire(int acquires) {
+            assert acquires == 1; // Otherwise unused
+            return compareAndSetState(0, 1);
+        }
+
+        // Release the lock by setting state to zero
+        protected boolean tryRelease(int releases) {
+            assert releases == 1; // Otherwise unused
+            if (getState() == 0) {
+                throw new IllegalMonitorStateException();
+            }
+            setState(0);
+            return true;
+        }
+
+        // Provide a Condition
+        Condition newCondition() {
+            return new ConditionObject();
+        }
+
+        // Deserialize properly
+        private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException
{
+            s.defaultReadObject();
+            setState(0); // reset to unlocked state
+        }
+    }
+
+    // The sync object does all the hard work. We just forward to it.
+    private final Sync sync = new Sync();
 
-    /**
-     * default constructor
-     */
-    public SimpleLock() {
-        this.lock = new AtomicBoolean(false);
-    }
-    
-    /* (non-Javadoc)
-     * @see java.util.concurrent.locks.Lock#lock()
-     */
     public void lock() {
-        throw new UnsupportedOperationException();
+        sync.acquire(1);
     }
 
-    /* (non-Javadoc)
-     * @see java.util.concurrent.locks.Lock#lockInterruptibly()
-     */
-    public void lockInterruptibly() throws InterruptedException {
-        throw new UnsupportedOperationException();
+    public boolean tryLock() {
+        return sync.tryAcquire(1);
+    }
+
+    public void unlock() {
+        sync.release(1);
     }
 
-    /* (non-Javadoc)
-     * @see java.util.concurrent.locks.Lock#newCondition()
-     */
     public Condition newCondition() {
-        throw new UnsupportedOperationException();
+        return sync.newCondition();
     }
 
-    /* (non-Javadoc)
-     * @see java.util.concurrent.locks.Lock#tryLock()
-     */
-    public boolean tryLock() {
-        return this.lock.compareAndSet(false, true);
+    public boolean isLocked() {
+        return sync.isHeldExclusively();
     }
 
-    /* (non-Javadoc)
-     * @see java.util.concurrent.locks.Lock#tryLock(long, java.util.concurrent.TimeUnit)
-     */
-    public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
-        throw new UnsupportedOperationException();
+    public boolean hasQueuedThreads() {
+        return sync.hasQueuedThreads();
     }
 
-    /* (non-Javadoc)
-     * @see java.util.concurrent.locks.Lock#unlock()
-     */
-    public void unlock() {
-        this.lock.compareAndSet(true, false);
+    public void lockInterruptibly() throws InterruptedException {
+        sync.acquireInterruptibly(1);
     }
+
+    public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
+        return sync.tryAcquireNanos(1, unit.toNanos(timeout));
+    }
+
 }

Added: servicemix/smx3/trunk/core/servicemix-services/src/test/java/org/apache/servicemix/locks/impl/SimpleLockTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-services/src/test/java/org/apache/servicemix/locks/impl/SimpleLockTest.java?rev=674833&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-services/src/test/java/org/apache/servicemix/locks/impl/SimpleLockTest.java
(added)
+++ servicemix/smx3/trunk/core/servicemix-services/src/test/java/org/apache/servicemix/locks/impl/SimpleLockTest.java
Tue Jul  8 07:11:29 2008
@@ -0,0 +1,35 @@
+/*
+ * 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.servicemix.locks.impl;
+
+import java.util.concurrent.locks.Lock;
+
+import junit.framework.TestCase;
+
+public class SimpleLockTest extends TestCase {
+
+    public void testSimple() {
+        Lock l = new SimpleLock();
+
+        assertTrue(l.tryLock());
+        assertFalse(l.tryLock());
+        l.unlock();
+        l.lock();
+        l.unlock();
+    }
+
+}



Mime
View raw message