1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.configuration2.sync; 18 19 /** 20 * <p> 21 * Definition of an interface for objects that can be associated with a {@link Synchronizer}. 22 * </p> 23 * <p> 24 * This interface defines methods for querying and setting the {@code Synchronizer}. In addition, it is possible to lock 25 * the object for a certain operation. This is useful if some complex operations are to be performed on the 26 * {@code SynchronizerSupport} object in an atomic way. 27 * </p> 28 * <p> 29 * Note that the actual effect of these methods depends on the concrete {@code Synchronizer} implementation in use! If 30 * only a dummy {@code Synchronizer} is involved (which is appropriate if objects are only accessed by a single thread), 31 * locking an object does not really prohibit concurrent access. 32 * </p> 33 * 34 * @since 2.0 35 */ 36 public interface SynchronizerSupport { 37 /** 38 * Gets the {@code Synchronizer} used by this object. An implementation must not return <b>null</b>. If no 39 * {@code Synchronizer} has been set so far, a meaningful default {@code Synchronizer} has to be returned. 40 * 41 * @return the {@code Synchronizer} used by this object 42 */ 43 Synchronizer getSynchronizer(); 44 45 /** 46 * Locks this object for the specified mode. This call may block until this object is released from other lock 47 * operations. When it returns the caller can access the object in a way compatible to the specified {@code LockMode}. 48 * When done the {@code unlock()} must be called with the same {@code LockMode} argument. In practice, a 49 * <b>try</b>-<b>finally</b> construct should be used as in the following example: 50 * 51 * <pre> 52 * SynchronizerSupport syncSupport = ...; 53 * syncSupport.lock(LockMode.READ); 54 * try 55 * { 56 * // read access to syncSupport 57 * } 58 * finally 59 * { 60 * syncSupport.unlock(LockMode.READ); 61 * } 62 * </pre> 63 * 64 * <em>Note:</em> Always use this method for obtaining a lock rather than accessing the object's {@link Synchronizer} 65 * directly. An implementation may perform additional actions which are not executed when only interacting with the 66 * {@code Synchronizer}. 67 * 68 * @param mode the {@code LockMode} 69 */ 70 void lock(LockMode mode); 71 72 /** 73 * Sets the {@code Synchronizer} to be used by this object. Calling this method and setting an appropriate 74 * {@code Synchronizer} determines whether this object can be accessed in a thread-safe way or not. The argument may be 75 * <b>null</b>; in this case an implementation should switch to a default {@code Synchronizer}. 76 * 77 * @param sync the {@code Synchronizer} for this object 78 */ 79 void setSynchronizer(Synchronizer sync); 80 81 /** 82 * Releases a lock of this object that was obtained using the {@link #lock(LockMode)} method. This method must always be 83 * called pair-wise with {@code lock()}. The argument must match to the one passed to the corresponding {@code lock()} 84 * call; otherwise, the behavior of the {@link Synchronizer} is unspecified. 85 * 86 * @param mode the {@code LockMode} 87 */ 88 void unlock(LockMode mode); 89 }