    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 */
    017package org.apache.commons.configuration2.sync;
    018
    019import java.util.concurrent.locks.ReadWriteLock;
    020import java.util.concurrent.locks.ReentrantReadWriteLock;
    021
    022/**
    023 * <p>
    024 * A special implementation of {@code Synchronizer} based on the JDK's
    025 * {@code ReentrantReadWriteLock} class.
    026 * </p>
    027 * <p>
    028 * This class manages a {@code ReadWriteLock} object internally. The methods of
    029 * the {@code Synchronizer} interface are delegated to this lock. So this class
    030 * behaves in the same way as documented for {@code ReentrantReadWriteLock}.
    031 * </p>
    032 * <p>
    033 * Using this {@code Synchronizer} implementation is appropriate to make
    034 * configuration objects thread-safe. This means that multiple threads can read
    035 * configuration data in parallel; if one thread wants to update the
    036 * configuration, this happens with an exclusive lock.
    037 * </p>
    038 *
    039 * @version $Id: ReadWriteSynchronizer.java 1842194 2018-09-27 22:24:23Z ggregory $
    040 * @since 2.0
    041 */
    042public class ReadWriteSynchronizer implements Synchronizer
    043{
    044    /** The lock object used by this Synchronizer. */
    045    private final ReadWriteLock lock;
    046
    047    /**
    048     * Creates a new instance of {@code ReadWriteSynchronizer} and initializes
    049     * it with the given lock object. This constructor can be used to pass a
    050     * lock object which has been configured externally. If the lock object is
    051     * <b>null</b>, a default lock object is created.
    052     *
    053     * @param l the lock object to be used (can be <b>null</b>)
    054     */
    055    public ReadWriteSynchronizer(final ReadWriteLock l)
    056    {
    057        lock = (l != null) ? l : createDefaultLock();
    058    }
    059
    060    /**
    061     * Creates a new instance of {@code ReadWriteSynchronizer} and initializes
    062     * it with a lock object of type {@code ReentrantReadWriteLock}.
    063     */
    064    public ReadWriteSynchronizer()
    065    {
    066        this(null);
    067    }
    068
    069    @Override
    070    public void beginRead()
    071    {
    072        lock.readLock().lock();
    073    }
    074
    075    @Override
    076    public void endRead()
    077    {
    078        lock.readLock().unlock();
    079    }
    080
    081    @Override
    082    public void beginWrite()
    083    {
    084        lock.writeLock().lock();
    085    }
    086
    087    @Override
    088    public void endWrite()
    089    {
    090        lock.writeLock().unlock();
    091    }
    092
    093    /**
    094     * Returns a new default lock object which is used if no lock is passed to
    095     * the constructor.
    096     *
    097     * @return the new default lock object
    098     */
    099    private static ReadWriteLock createDefaultLock()
    100    {
    101        return new ReentrantReadWriteLock();
    102    }
    103}