001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.locks.impl;
018    
019    import java.io.IOException;
020    import java.io.ObjectInputStream;
021    import java.io.Serializable;
022    import java.util.concurrent.TimeUnit;
023    import java.util.concurrent.locks.AbstractQueuedSynchronizer;
024    import java.util.concurrent.locks.Condition;
025    import java.util.concurrent.locks.Lock;
026    
027    /**
028     * @author lhein
029     */
030    @Deprecated
031    public class SimpleLock implements Lock, Serializable {
032    
033        // Our internal helper class
034        private static class Sync extends AbstractQueuedSynchronizer {
035            // Report whether in locked state
036            protected boolean isHeldExclusively() {
037                return getState() == 1;
038            }
039    
040            // Acquire the lock if state is zero
041            public boolean tryAcquire(int acquires) {
042                assert acquires == 1; // Otherwise unused
043                return compareAndSetState(0, 1);
044            }
045    
046            // Release the lock by setting state to zero
047            protected boolean tryRelease(int releases) {
048                assert releases == 1; // Otherwise unused
049                if (getState() == 0) {
050                    throw new IllegalMonitorStateException();
051                }
052                setState(0);
053                return true;
054            }
055    
056            // Provide a Condition
057            Condition newCondition() {
058                return new ConditionObject();
059            }
060    
061            // Deserialize properly
062            private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
063                s.defaultReadObject();
064                setState(0); // reset to unlocked state
065            }
066        }
067    
068        // The sync object does all the hard work. We just forward to it.
069        private final Sync sync = new Sync();
070    
071        public void lock() {
072            sync.acquire(1);
073        }
074    
075        public boolean tryLock() {
076            return sync.tryAcquire(1);
077        }
078    
079        public void unlock() {
080            sync.release(1);
081        }
082    
083        public Condition newCondition() {
084            return sync.newCondition();
085        }
086    
087        public boolean isLocked() {
088            return sync.isHeldExclusively();
089        }
090    
091        public boolean hasQueuedThreads() {
092            return sync.hasQueuedThreads();
093        }
094    
095        public void lockInterruptibly() throws InterruptedException {
096            sync.acquireInterruptibly(1);
097        }
098    
099        public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
100            return sync.tryAcquireNanos(1, unit.toNanos(timeout));
101        }
102    
103    }