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 * An interface controlling synchronization of configuration instances.
022 * </p>
023 * <p>
024 * Each {@code Configuration} object derived from
025 * {@link org.apache.commons.configuration2.AbstractConfiguration AbstractConfiguration}
026 * has an associated {@code Synchronizer} object. Before an operation on the
027 * configuration is performed (e.g. a property read or an update), the
028 * {@code Synchronizer} is invoked. Depending on the concrete implementation of
029 * the {@code Synchronizer} used, the configuration can be made thread-safe.
030 * </p>
031 * <p>
032 * Whether a configuration has to be thread-safe or not is a matter of a
033 * concrete use case. For instance, an application that just reads some
034 * configuration settings on startup does need a thread-safe configuration
035 * implementation. A configuration in contrast which is shared between multiple
036 * components and updated concurrently should better be thread-safe. In order to
037 * satisfy both kinds of use cases, the support for thread-safety has been
038 * extracted out of the configuration implementation and refactored into this
039 * {@code Synchronizer} interface. By assigning different {@code Synchronizer}
040 * implementations to a configuration instance, the instance's support for
041 * concurrent access can be adapted to the concrete use case.
042 * </p>
043 * <p>
044 * The methods defined by this interface are similar to a <em>read-write
045 * lock</em>. The {@code Synchronizer} is notified when read or write operations
046 * start and end. A concrete implementation can then apply a specific policy to
047 * decide when threads need to block or when access to the configuration for the
048 * desired operation is granted.
049 * </p>
050 *
051 * @since 2.0
052 */
053public interface Synchronizer
054{
055    /**
056     * Notifies this {@code Synchronizer} that the current thread is going to
057     * start a read operation on the managed configuration. This call can block
058     * if a concrete implementation decides that the thread has to wait until a
059     * specific condition is fulfilled.
060     */
061    void beginRead();
062
063    /**
064     * Notifies this {@code Synchronizer} that the current thread has finished
065     * its read operation. This may cause other waiting threads to be granted
066     * access to the managed configuration.
067     */
068    void endRead();
069
070    /**
071     * Notifies this {@code Synchronizer} that the current thread is going to
072     * start a write operation on the managed configuration. This call may
073     * block. For instance, a concrete implementation may suspend the thread
074     * until all read operations currently active are finished,
075     */
076    void beginWrite();
077
078    /**
079     * Notifies this {@code Synchronizer} that the current thread has finished
080     * its write operation. This may cause other waiting threads to be granted
081     * access to the managed configuration.
082     */
083    void endWrite();
084}