View Javadoc
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    *     https://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      /**
39       * Gets the {@code Synchronizer} used by this object. An implementation must not return <strong>null</strong>. If no
40       * {@code Synchronizer} has been set so far, a meaningful default {@code Synchronizer} has to be returned.
41       *
42       * @return the {@code Synchronizer} used by this object
43       */
44      Synchronizer getSynchronizer();
45  
46      /**
47       * Locks this object for the specified mode. This call may block until this object is released from other lock
48       * operations. When it returns the caller can access the object in a way compatible to the specified {@code LockMode}.
49       * When done the {@code unlock()} must be called with the same {@code LockMode} argument. In practice, a
50       * <strong>try</strong>-<strong>finally</strong> construct should be used as in the following example:
51       *
52       * <pre>
53       * SynchronizerSupport syncSupport = ...;
54       * syncSupport.lock(LockMode.READ);
55       * try
56       * {
57       *     // read access to syncSupport
58       * }
59       * finally
60       * {
61       *     syncSupport.unlock(LockMode.READ);
62       * }
63       * </pre>
64       *
65       * <em>Note:</em> Always use this method for obtaining a lock rather than accessing the object's {@link Synchronizer}
66       * directly. An implementation may perform additional actions which are not executed when only interacting with the
67       * {@code Synchronizer}.
68       *
69       * @param mode the {@code LockMode}
70       */
71      void lock(LockMode mode);
72  
73      /**
74       * Sets the {@code Synchronizer} to be used by this object. Calling this method and setting an appropriate
75       * {@code Synchronizer} determines whether this object can be accessed in a thread-safe way or not. The argument may be
76       * <strong>null</strong>; in this case an implementation should switch to a default {@code Synchronizer}.
77       *
78       * @param sync the {@code Synchronizer} for this object
79       */
80      void setSynchronizer(Synchronizer sync);
81  
82      /**
83       * Releases a lock of this object that was obtained using the {@link #lock(LockMode)} method. This method must always be
84       * called pair-wise with {@code lock()}. The argument must match to the one passed to the corresponding {@code lock()}
85       * call; otherwise, the behavior of the {@link Synchronizer} is unspecified.
86       *
87       * @param mode the {@code LockMode}
88       */
89      void unlock(LockMode mode);
90  }