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