001    /* ====================================================================
002     * The Apache Software License, Version 1.1
003     *
004     * Copyright (c) 2003 The Apache Software Foundation.  All rights
005     * reserved.
006     *
007     * Redistribution and use in source and binary forms, with or without
008     * modification, are permitted provided that the following conditions
009     * are met:
010     *
011     * 1. Redistributions of source code must retain the above copyright
012     *    notice, this list of conditions and the following disclaimer.
013     *
014     * 2. Redistributions in binary form must reproduce the above copyright
015     *    notice, this list of conditions and the following disclaimer in
016     *    the documentation and/or other materials provided with the
017     *    distribution.
018     *
019     * 3. The end-user documentation included with the redistribution,
020     *    if any, must include the following acknowledgment:
021     *       "This product includes software developed by the
022     *        Apache Software Foundation (http://www.apache.org/)."
023     *    Alternately, this acknowledgment may appear in the software itself,
024     *    if and wherever such third-party acknowledgments normally appear.
025     *
026     * 4. The names "The Jakarta Project", "Commons", and "Apache Software
027     *    Foundation" must not be used to endorse or promote products derived
028     *    from this software without prior written permission. For written
029     *    permission, please contact apache@apache.org.
030     *
031     * 5. Products derived from this software may not be called "Apache",
032     *    nor may "Apache" appear in their name, without prior written
033     *    permission of the Apache Software Foundation.
034     *
035     * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036     * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038     * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039     * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042     * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044     * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046     * SUCH DAMAGE.
047     * ====================================================================
048     *
049     * This software consists of voluntary contributions made by many
050     * individuals on behalf of the Apache Software Foundation.  For more
051     * information on the Apache Software Foundation, please see
052     * <http://www.apache.org/>.
053     *
054     */
055    package org.jpu.patterns.common;
056    
057    public class ReentrantLock implements IReentrantLock {
058    
059        /**
060         * Default constructor.
061         */
062        public ReentrantLock() {
063        }
064    
065        /**
066         * Acquires the lock.
067         */
068        public void lock() {
069            Thread caller = Thread.currentThread();
070            synchronized(this) {
071                if (caller == _owner) {
072                    _count++;
073                } else {
074                    try {
075                        while (_owner != null) {
076                            this.wait();
077                        }
078                        _owner = caller;
079                        _count = 1;
080                    } catch (InterruptedException exception) {
081                        return;
082                    }
083                }
084            }
085        }
086    
087        /**
088         * Acquires the lock only if it not held by another thread.
089         *
090         * @return <code>true</code> if the lock was free and was acquired by the
091         *         current thread, or the lock was already held by the current
092         *         thread; <code>false</code> otherwise.
093         */
094        public boolean tryLock() {
095            synchronized(this) {
096                if (_owner == null) {
097                    lock();
098                    return true;
099                } else {
100                    return false;
101                }
102            }
103        }
104    
105        /**
106         * Attempts to release this lock. The lock is actually released if at
107         * least as many {@link #unlock} as {@link #lock} have been performed
108         * on this {@link ReentrantLock} by the current thread.
109         *
110         * throws IllegalMonitorStateExeception if the current thread does not hold
111         *        this lock.
112         */
113        public void unlock() {
114            synchronized (this) {
115                if (Thread.currentThread() == _owner) {
116                    if (--_count == 0) {
117                        _owner = null;
118                        this.notify();
119                    }
120                } else {
121                    throw new IllegalMonitorStateException(
122                        "Current thread does not hold this lock");
123                }
124            }
125        }
126    
127        public String toString() {
128            return "ReentrantLock(_owner=" + _owner + ", _count=" + _count + ")";
129        }
130    
131        /**
132         * Returns the thread owner of this {@link ReentrantLock}.
133         *
134         * @return the owner of this lock.
135         */
136        public Thread getOwner() {
137            return _owner;
138        }
139    
140        /**
141         * Holds the owner of this lock.
142         */
143        private Thread _owner;
144    
145        /**
146         * Holds the number of time this lock has been acquired by its owner.
147         */
148        private long _count;
149    }
150