    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 {@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     * Gets the {@code Synchronizer} used by this object. An implementation must not return <b>null</b>. If no
    039     * {@code Synchronizer} has been set so far, a meaningful default {@code Synchronizer} has to be returned.
    040     *
    041     * @return the {@code Synchronizer} used by this object
    042     */
    043    Synchronizer getSynchronizer();
    044
    045    /**
    046     * Sets the {@code Synchronizer} to be used by this object. Calling this method and setting an appropriate
    047     * {@code Synchronizer} determines whether this object can be accessed in a thread-safe way or not. The argument may be
    048     * <b>null</b>; in this case an implementation should switch to a default {@code Synchronizer}.
    049     *
    050     * @param sync the {@code Synchronizer} for this object
    051     */
    052    void setSynchronizer(Synchronizer sync);
    053
    054    /**
    055     * Locks this object for the specified mode. This call may block until this object is released from other lock
    056     * operations. When it returns the caller can access the object in a way compatible to the specified {@code LockMode}.
    057     * When done the {@code unlock()} must be called with the same {@code LockMode} argument. In practice, a
    058     * <b>try</b>-<b>finally</b> construct should be used as in the following example:
    059     *
    060     * <pre>
    061     * SynchronizerSupport syncSupport = ...;
    062     * syncSupport.lock(LockMode.READ);
    063     * try
    064     * {
    065     *     // read access to syncSupport
    066     * }
    067     * finally
    068     * {
    069     *     syncSupport.unlock(LockMode.READ);
    070     * }
    071     * </pre>
    072     *
    073     * <em>Note:</em> Always use this method for obtaining a lock rather than accessing the object's {@link Synchronizer}
    074     * directly. An implementation may perform additional actions which are not executed when only interacting with the
    075     * {@code Synchronizer}.
    076     *
    077     * @param mode the {@code LockMode}
    078     */
    079    void lock(LockMode mode);
    080
    081    /**
    082     * Releases a lock of this object that was obtained using the {@link #lock(LockMode)} method. This method must always be
    083     * called pair-wise with {@code lock()}. The argument must match to the one passed to the corresponding {@code lock()}
    084     * call; otherwise, the behavior of the {@link Synchronizer} is unspecified.
    085     *
    086     * @param mode the {@code LockMode}
    087     */
    088    void unlock(LockMode mode);
    089}