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