Logo
Distributed Genetic Programming Framework
print print

File org.jfree.threads.ReaderWriterLock.java

Here you can find all the information about the file org.jfree.threads.ReaderWriterLock.java. You may explore it here or download it onto your local disk.
/* ========================================================================
 * JCommon : a free general purpose class library for the Java(tm) platform
 * ========================================================================
 *
 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
 * 
 * Project Info:  http://www.jfree.org/jcommon/index.html
 *
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation; either version 2.1 of the License, or 
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
 * USA.  
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 * 
 * ---------------------
 * ReaderWriterLock.java
 * ---------------------
 *
 * $Id: ReaderWriterLock.java,v 1.3 2005/10/18 13:18:34 mungady Exp $
 *
 * Changes
 * -------
 * 29-Jan-2003 : Added standard header (DG);
 *
 */


package org.jfree.threads;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * A reader-writer lock from "Java Threads" by Scott Oak and Henry Wong.
 *
 * @author Scott Oak and Henry Wong
 */

public class ReaderWriterLock {

    /**
     * A node for the waiting list.
     *
     * @author Scott Oak and Henry Wong
     */

    private static class ReaderWriterNode {

        /** A reader. */
        protected static final int READER = 0;

        /** A writer. */
        protected static final int WRITER = 1;

        /** The thread. */
        protected Thread t;

        /** The state. */
        protected int state;

        /** The number of acquires.*/
        protected int nAcquires;

        /**
         * Creates a new node.
         *
         * @param t  the thread.
         * @param state  the state.
         */

        private ReaderWriterNode(final Thread t, final int state) {
            this.t = t;
            this.state = state;
            this.nAcquires = 0;
        }

    }

    /** The waiting threads. */
    private ArrayList waiters;

    /**
     * Default constructor.
     */

    public ReaderWriterLock() {
        this.waiters = new ArrayList();
    }

    /**
     * Grab the read lock.
     */

    public synchronized void lockRead() {
        final ReaderWriterNode node;
        final Thread me = Thread.currentThread();
        final int index = getIndex(me);
        if (index == -1) {
            node = new ReaderWriterNode(me, ReaderWriterNode.READER);
            this.waiters.add(node);
        }
        else {
            node = (ReaderWriterNode) this.waiters.get(index);
        }
        while (getIndex(me) > firstWriter()) {
            try {
                wait();
            }
            catch (Exception e) {
                System.err.println("ReaderWriterLock.lockRead(): exception.");
                System.err.print(e.getMessage());
            }
        }
        node.nAcquires++;
    }

    /**
     * Grab the write lock.
     */

    public synchronized void lockWrite() {
        final ReaderWriterNode node;
        final Thread me = Thread.currentThread();
        final int index = getIndex(me);
        if (index == -1) {
            node = new ReaderWriterNode(me, ReaderWriterNode.WRITER);
            this.waiters.add(node);
        }
        else {
            node = (ReaderWriterNode) this.waiters.get(index);
            if (node.state == ReaderWriterNode.READER) {
                throw new IllegalArgumentException("Upgrade lock");
            }
            node.state = ReaderWriterNode.WRITER;
        }
        while (getIndex(me) != 0) {
            try {
                wait();
            }
            catch (Exception e) {
                System.err.println("ReaderWriterLock.lockWrite(): exception.");
                System.err.print(e.getMessage());
            }
        }
        node.nAcquires++;
    }

    /**
     * Unlock.
     */

    public synchronized void unlock() {

        final ReaderWriterNode node;
        final Thread me = Thread.currentThread();
        final int index = getIndex(me);
        if (index > firstWriter()) {
            throw new IllegalArgumentException("Lock not held");
        }
        node = (ReaderWriterNode) this.waiters.get(index);
        node.nAcquires--;
        if (node.nAcquires == 0) {
            this.waiters.remove(index);
        }
        notifyAll();
    }

    /**
     * Returns the index of the first waiting writer.
     *
     * @return The index.
     */

    private int firstWriter() {
        final Iterator e = this.waiters.iterator();
        int index = 0;
        while (e.hasNext()) {
            final ReaderWriterNode node = (ReaderWriterNode) e.next();
            if (node.state == ReaderWriterNode.WRITER) {
                return index;
            }
            index += 1;
        }
        return Integer.MAX_VALUE;
    }

    /**
     * Returns the index of a thread.
     *
     * @param t  the thread.
     *
     * @return The index.
     */

    private int getIndex(final Thread t) {
        final Iterator e = this.waiters.iterator();
        int index = 0;
        while (e.hasNext()) {
            final ReaderWriterNode node = (ReaderWriterNode) e.next();
            if (node.t == t) {
                return index;
            }
            index += 1;
        }
        return -1;
    }

}

File Information:

file name:ReaderWriterLock.java
package:org.jfree.threads
qualified name:org.jfree.threads.ReaderWriterLock.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/jfree/threads/ReaderWriterLock.java
size:5.938 KB (6081 B)
uploaded: 2015-07-22 04:11:09 GMT+0000
last update: 2006-09-26 10:30:34 GMT+0000
last access: 2018-01-22 23:53:29 GMT+0000

statistics online since 2006-01-02.   RSS Feed
Contact us by sending an email to tweise@gmx.de to receive further information, to report errors, or to join our project.
All content on this site (http://dgpf.sourceforge.net/) is LGPL-licensed.
http://dgpf.sourceforge.net/scripts/source/source.php last modified at 2015-07-22 04:10:53 GMT+0000 served at 2018-01-22 23:53:29 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo