rt/emul/compact/src/main/java/java/util/concurrent/locks/ReadWriteLock.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 19 Mar 2016 10:46:31 +0100
branchjdk7-b147
changeset 1890 212417b74b72
permissions -rw-r--r--
Bringing in all concurrent package from JDK7-b147
jaroslav@1890
     1
/*
jaroslav@1890
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1890
     3
 *
jaroslav@1890
     4
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1890
     5
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1890
     6
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1890
     7
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1890
     8
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1890
     9
 *
jaroslav@1890
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1890
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1890
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1890
    13
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1890
    14
 * accompanied this code).
jaroslav@1890
    15
 *
jaroslav@1890
    16
 * You should have received a copy of the GNU General Public License version
jaroslav@1890
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1890
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1890
    19
 *
jaroslav@1890
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1890
    21
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1890
    22
 * questions.
jaroslav@1890
    23
 */
jaroslav@1890
    24
jaroslav@1890
    25
/*
jaroslav@1890
    26
 * This file is available under and governed by the GNU General Public
jaroslav@1890
    27
 * License version 2 only, as published by the Free Software Foundation.
jaroslav@1890
    28
 * However, the following notice accompanied the original version of this
jaroslav@1890
    29
 * file:
jaroslav@1890
    30
 *
jaroslav@1890
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
jaroslav@1890
    32
 * Expert Group and released to the public domain, as explained at
jaroslav@1890
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
jaroslav@1890
    34
 */
jaroslav@1890
    35
jaroslav@1890
    36
package java.util.concurrent.locks;
jaroslav@1890
    37
jaroslav@1890
    38
/**
jaroslav@1890
    39
 * A <tt>ReadWriteLock</tt> maintains a pair of associated {@link
jaroslav@1890
    40
 * Lock locks}, one for read-only operations and one for writing.
jaroslav@1890
    41
 * The {@link #readLock read lock} may be held simultaneously by
jaroslav@1890
    42
 * multiple reader threads, so long as there are no writers.  The
jaroslav@1890
    43
 * {@link #writeLock write lock} is exclusive.
jaroslav@1890
    44
 *
jaroslav@1890
    45
 * <p>All <tt>ReadWriteLock</tt> implementations must guarantee that
jaroslav@1890
    46
 * the memory synchronization effects of <tt>writeLock</tt> operations
jaroslav@1890
    47
 * (as specified in the {@link Lock} interface) also hold with respect
jaroslav@1890
    48
 * to the associated <tt>readLock</tt>. That is, a thread successfully
jaroslav@1890
    49
 * acquiring the read lock will see all updates made upon previous
jaroslav@1890
    50
 * release of the write lock.
jaroslav@1890
    51
 *
jaroslav@1890
    52
 * <p>A read-write lock allows for a greater level of concurrency in
jaroslav@1890
    53
 * accessing shared data than that permitted by a mutual exclusion lock.
jaroslav@1890
    54
 * It exploits the fact that while only a single thread at a time (a
jaroslav@1890
    55
 * <em>writer</em> thread) can modify the shared data, in many cases any
jaroslav@1890
    56
 * number of threads can concurrently read the data (hence <em>reader</em>
jaroslav@1890
    57
 * threads).
jaroslav@1890
    58
 * In theory, the increase in concurrency permitted by the use of a read-write
jaroslav@1890
    59
 * lock will lead to performance improvements over the use of a mutual
jaroslav@1890
    60
 * exclusion lock. In practice this increase in concurrency will only be fully
jaroslav@1890
    61
 * realized on a multi-processor, and then only if the access patterns for
jaroslav@1890
    62
 * the shared data are suitable.
jaroslav@1890
    63
 *
jaroslav@1890
    64
 * <p>Whether or not a read-write lock will improve performance over the use
jaroslav@1890
    65
 * of a mutual exclusion lock depends on the frequency that the data is
jaroslav@1890
    66
 * read compared to being modified, the duration of the read and write
jaroslav@1890
    67
 * operations, and the contention for the data - that is, the number of
jaroslav@1890
    68
 * threads that will try to read or write the data at the same time.
jaroslav@1890
    69
 * For example, a collection that is initially populated with data and
jaroslav@1890
    70
 * thereafter infrequently modified, while being frequently searched
jaroslav@1890
    71
 * (such as a directory of some kind) is an ideal candidate for the use of
jaroslav@1890
    72
 * a read-write lock. However, if updates become frequent then the data
jaroslav@1890
    73
 * spends most of its time being exclusively locked and there is little, if any
jaroslav@1890
    74
 * increase in concurrency. Further, if the read operations are too short
jaroslav@1890
    75
 * the overhead of the read-write lock implementation (which is inherently
jaroslav@1890
    76
 * more complex than a mutual exclusion lock) can dominate the execution
jaroslav@1890
    77
 * cost, particularly as many read-write lock implementations still serialize
jaroslav@1890
    78
 * all threads through a small section of code. Ultimately, only profiling
jaroslav@1890
    79
 * and measurement will establish whether the use of a read-write lock is
jaroslav@1890
    80
 * suitable for your application.
jaroslav@1890
    81
 *
jaroslav@1890
    82
 *
jaroslav@1890
    83
 * <p>Although the basic operation of a read-write lock is straight-forward,
jaroslav@1890
    84
 * there are many policy decisions that an implementation must make, which
jaroslav@1890
    85
 * may affect the effectiveness of the read-write lock in a given application.
jaroslav@1890
    86
 * Examples of these policies include:
jaroslav@1890
    87
 * <ul>
jaroslav@1890
    88
 * <li>Determining whether to grant the read lock or the write lock, when
jaroslav@1890
    89
 * both readers and writers are waiting, at the time that a writer releases
jaroslav@1890
    90
 * the write lock. Writer preference is common, as writes are expected to be
jaroslav@1890
    91
 * short and infrequent. Reader preference is less common as it can lead to
jaroslav@1890
    92
 * lengthy delays for a write if the readers are frequent and long-lived as
jaroslav@1890
    93
 * expected. Fair, or &quot;in-order&quot; implementations are also possible.
jaroslav@1890
    94
 *
jaroslav@1890
    95
 * <li>Determining whether readers that request the read lock while a
jaroslav@1890
    96
 * reader is active and a writer is waiting, are granted the read lock.
jaroslav@1890
    97
 * Preference to the reader can delay the writer indefinitely, while
jaroslav@1890
    98
 * preference to the writer can reduce the potential for concurrency.
jaroslav@1890
    99
 *
jaroslav@1890
   100
 * <li>Determining whether the locks are reentrant: can a thread with the
jaroslav@1890
   101
 * write lock reacquire it? Can it acquire a read lock while holding the
jaroslav@1890
   102
 * write lock? Is the read lock itself reentrant?
jaroslav@1890
   103
 *
jaroslav@1890
   104
 * <li>Can the write lock be downgraded to a read lock without allowing
jaroslav@1890
   105
 * an intervening writer? Can a read lock be upgraded to a write lock,
jaroslav@1890
   106
 * in preference to other waiting readers or writers?
jaroslav@1890
   107
 *
jaroslav@1890
   108
 * </ul>
jaroslav@1890
   109
 * You should consider all of these things when evaluating the suitability
jaroslav@1890
   110
 * of a given implementation for your application.
jaroslav@1890
   111
 *
jaroslav@1890
   112
 * @see ReentrantReadWriteLock
jaroslav@1890
   113
 * @see Lock
jaroslav@1890
   114
 * @see ReentrantLock
jaroslav@1890
   115
 *
jaroslav@1890
   116
 * @since 1.5
jaroslav@1890
   117
 * @author Doug Lea
jaroslav@1890
   118
 */
jaroslav@1890
   119
public interface ReadWriteLock {
jaroslav@1890
   120
    /**
jaroslav@1890
   121
     * Returns the lock used for reading.
jaroslav@1890
   122
     *
jaroslav@1890
   123
     * @return the lock used for reading.
jaroslav@1890
   124
     */
jaroslav@1890
   125
    Lock readLock();
jaroslav@1890
   126
jaroslav@1890
   127
    /**
jaroslav@1890
   128
     * Returns the lock used for writing.
jaroslav@1890
   129
     *
jaroslav@1890
   130
     * @return the lock used for writing.
jaroslav@1890
   131
     */
jaroslav@1890
   132
    Lock writeLock();
jaroslav@1890
   133
}