    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
    019/**
    020 * <p>
    021 * Definition of an interface for objects that can be associated with a
    022 * {@link Synchronizer}.
    023 * </p>
    024 * <p>
    025 * This interface defines methods for querying and setting the
    026 * {@code Synchronizer}. In addition, it is possible to lock the object for a
    027 * certain operation. This is useful if some complex operations are to be
    028 * performed on the {@code SynchronizerSupport} object in an atomic way.
    029 * </p>
    030 * <p>
    031 * Note that the actual effect of these methods depends on the concrete
    032 * {@code Synchronizer} implementation in use! If only a dummy
    033 * {@code Synchronizer} is involved (which is appropriate if objects are only
    034 * accessed by a single thread), locking an object does not really prohibit
    035 * concurrent access.
    036 * </p>
    037 *
    038 * @version $Id: SynchronizerSupport.java 1624601 2014-09-12 18:04:36Z oheger $
    039 * @since 2.0
    040 */
    041public interface SynchronizerSupport
    042{
    043    /**
    044     * Returns the {@code Synchronizer} used by this object. An implementation
    045     * must not return <b>null</b>. If no {@code Synchronizer} has been set so
    046     * far, a meaningful default {@code Synchronizer} has to be returned.
    047     *
    048     * @return the {@code Synchronizer} used by this object
    049     */
    050    Synchronizer getSynchronizer();
    051
    052    /**
    053     * Sets the {@code Synchronizer} to be used by this object. Calling this
    054     * method and setting an appropriate {@code Synchronizer} determines whether
    055     * this object can be accessed in a thread-safe way or not. The argument may
    056     * be <b>null</b>; in this case an implementation should switch to a default
    057     * {@code Synchronizer}.
    058     *
    059     * @param sync the {@code Synchronizer} for this object
    060     */
    061    void setSynchronizer(Synchronizer sync);
    062
    063    /**
    064     * Locks this object for the specified mode. This call may block until this
    065     * object is released from other lock operations. When it returns the caller
    066     * can access the object in a way compatible to the specified
    067     * {@code LockMode}. When done the {@code unlock()} must be called with the
    068     * same {@code LockMode} argument. In practice, a <b>try</b>-<b>finally</b>
    069     * construct should be used as in the following example:
    070     *
    071     * <pre>
    072     * SynchronizerSupport syncSupport = ...;
    073     * syncSupport.lock(LockMode.READ);
    074     * try
    075     * {
    076     *     // read access to syncSupport
    077     * }
    078     * finally
    079     * {
    080     *     syncSupport.unlock(LockMode.READ);
    081     * }
    082     * </pre>
    083     *
    084     * <em>Note:</em> Always use this method for obtaining a lock rather than
    085     * accessing the object's {@link Synchronizer} directly. An implementation
    086     * may perform additional actions which are not executed when only
    087     * interacting with the {@code Synchronizer}.
    088     *
    089     * @param mode the {@code LockMode}
    090     */
    091    void lock(LockMode mode);
    092
    093    /**
    094     * Releases a lock of this object that was obtained using the
    095     * {@link #lock(LockMode)} method. This method must always be called
    096     * pair-wise with {@code lock()}. The argument must match to the one passed
    097     * to the corresponding {@code lock()} call; otherwise, the behavior of the
    098     * {@link Synchronizer} is unspecified.
    099     *
    100     * @param mode the {@code LockMode}
    101     */
    102    void unlock(LockMode mode);
    103}