rt/emul/compact/src/main/java/java/util/concurrent/locks/Lock.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
import java.util.concurrent.TimeUnit;
jaroslav@1890
    38
jaroslav@1890
    39
/**
jaroslav@1890
    40
 * {@code Lock} implementations provide more extensive locking
jaroslav@1890
    41
 * operations than can be obtained using {@code synchronized} methods
jaroslav@1890
    42
 * and statements.  They allow more flexible structuring, may have
jaroslav@1890
    43
 * quite different properties, and may support multiple associated
jaroslav@1890
    44
 * {@link Condition} objects.
jaroslav@1890
    45
 *
jaroslav@1890
    46
 * <p>A lock is a tool for controlling access to a shared resource by
jaroslav@1890
    47
 * multiple threads. Commonly, a lock provides exclusive access to a
jaroslav@1890
    48
 * shared resource: only one thread at a time can acquire the lock and
jaroslav@1890
    49
 * all access to the shared resource requires that the lock be
jaroslav@1890
    50
 * acquired first. However, some locks may allow concurrent access to
jaroslav@1890
    51
 * a shared resource, such as the read lock of a {@link ReadWriteLock}.
jaroslav@1890
    52
 *
jaroslav@1890
    53
 * <p>The use of {@code synchronized} methods or statements provides
jaroslav@1890
    54
 * access to the implicit monitor lock associated with every object, but
jaroslav@1890
    55
 * forces all lock acquisition and release to occur in a block-structured way:
jaroslav@1890
    56
 * when multiple locks are acquired they must be released in the opposite
jaroslav@1890
    57
 * order, and all locks must be released in the same lexical scope in which
jaroslav@1890
    58
 * they were acquired.
jaroslav@1890
    59
 *
jaroslav@1890
    60
 * <p>While the scoping mechanism for {@code synchronized} methods
jaroslav@1890
    61
 * and statements makes it much easier to program with monitor locks,
jaroslav@1890
    62
 * and helps avoid many common programming errors involving locks,
jaroslav@1890
    63
 * there are occasions where you need to work with locks in a more
jaroslav@1890
    64
 * flexible way. For example, some algorithms for traversing
jaroslav@1890
    65
 * concurrently accessed data structures require the use of
jaroslav@1890
    66
 * &quot;hand-over-hand&quot; or &quot;chain locking&quot;: you
jaroslav@1890
    67
 * acquire the lock of node A, then node B, then release A and acquire
jaroslav@1890
    68
 * C, then release B and acquire D and so on.  Implementations of the
jaroslav@1890
    69
 * {@code Lock} interface enable the use of such techniques by
jaroslav@1890
    70
 * allowing a lock to be acquired and released in different scopes,
jaroslav@1890
    71
 * and allowing multiple locks to be acquired and released in any
jaroslav@1890
    72
 * order.
jaroslav@1890
    73
 *
jaroslav@1890
    74
 * <p>With this increased flexibility comes additional
jaroslav@1890
    75
 * responsibility. The absence of block-structured locking removes the
jaroslav@1890
    76
 * automatic release of locks that occurs with {@code synchronized}
jaroslav@1890
    77
 * methods and statements. In most cases, the following idiom
jaroslav@1890
    78
 * should be used:
jaroslav@1890
    79
 *
jaroslav@1890
    80
 * <pre><tt>     Lock l = ...;
jaroslav@1890
    81
 *     l.lock();
jaroslav@1890
    82
 *     try {
jaroslav@1890
    83
 *         // access the resource protected by this lock
jaroslav@1890
    84
 *     } finally {
jaroslav@1890
    85
 *         l.unlock();
jaroslav@1890
    86
 *     }
jaroslav@1890
    87
 * </tt></pre>
jaroslav@1890
    88
 *
jaroslav@1890
    89
 * When locking and unlocking occur in different scopes, care must be
jaroslav@1890
    90
 * taken to ensure that all code that is executed while the lock is
jaroslav@1890
    91
 * held is protected by try-finally or try-catch to ensure that the
jaroslav@1890
    92
 * lock is released when necessary.
jaroslav@1890
    93
 *
jaroslav@1890
    94
 * <p>{@code Lock} implementations provide additional functionality
jaroslav@1890
    95
 * over the use of {@code synchronized} methods and statements by
jaroslav@1890
    96
 * providing a non-blocking attempt to acquire a lock ({@link
jaroslav@1890
    97
 * #tryLock()}), an attempt to acquire the lock that can be
jaroslav@1890
    98
 * interrupted ({@link #lockInterruptibly}, and an attempt to acquire
jaroslav@1890
    99
 * the lock that can timeout ({@link #tryLock(long, TimeUnit)}).
jaroslav@1890
   100
 *
jaroslav@1890
   101
 * <p>A {@code Lock} class can also provide behavior and semantics
jaroslav@1890
   102
 * that is quite different from that of the implicit monitor lock,
jaroslav@1890
   103
 * such as guaranteed ordering, non-reentrant usage, or deadlock
jaroslav@1890
   104
 * detection. If an implementation provides such specialized semantics
jaroslav@1890
   105
 * then the implementation must document those semantics.
jaroslav@1890
   106
 *
jaroslav@1890
   107
 * <p>Note that {@code Lock} instances are just normal objects and can
jaroslav@1890
   108
 * themselves be used as the target in a {@code synchronized} statement.
jaroslav@1890
   109
 * Acquiring the
jaroslav@1890
   110
 * monitor lock of a {@code Lock} instance has no specified relationship
jaroslav@1890
   111
 * with invoking any of the {@link #lock} methods of that instance.
jaroslav@1890
   112
 * It is recommended that to avoid confusion you never use {@code Lock}
jaroslav@1890
   113
 * instances in this way, except within their own implementation.
jaroslav@1890
   114
 *
jaroslav@1890
   115
 * <p>Except where noted, passing a {@code null} value for any
jaroslav@1890
   116
 * parameter will result in a {@link NullPointerException} being
jaroslav@1890
   117
 * thrown.
jaroslav@1890
   118
 *
jaroslav@1890
   119
 * <h3>Memory Synchronization</h3>
jaroslav@1890
   120
 *
jaroslav@1890
   121
 * <p>All {@code Lock} implementations <em>must</em> enforce the same
jaroslav@1890
   122
 * memory synchronization semantics as provided by the built-in monitor
jaroslav@1890
   123
 * lock, as described in section 17.4 of
jaroslav@1890
   124
 * <cite>The Java&trade; Language Specification</cite>:
jaroslav@1890
   125
 * <ul>
jaroslav@1890
   126
 * <li>A successful {@code lock} operation has the same memory
jaroslav@1890
   127
 * synchronization effects as a successful <em>Lock</em> action.
jaroslav@1890
   128
 * <li>A successful {@code unlock} operation has the same
jaroslav@1890
   129
 * memory synchronization effects as a successful <em>Unlock</em> action.
jaroslav@1890
   130
 * </ul>
jaroslav@1890
   131
 *
jaroslav@1890
   132
 * Unsuccessful locking and unlocking operations, and reentrant
jaroslav@1890
   133
 * locking/unlocking operations, do not require any memory
jaroslav@1890
   134
 * synchronization effects.
jaroslav@1890
   135
 *
jaroslav@1890
   136
 * <h3>Implementation Considerations</h3>
jaroslav@1890
   137
 *
jaroslav@1890
   138
 * <p> The three forms of lock acquisition (interruptible,
jaroslav@1890
   139
 * non-interruptible, and timed) may differ in their performance
jaroslav@1890
   140
 * characteristics, ordering guarantees, or other implementation
jaroslav@1890
   141
 * qualities.  Further, the ability to interrupt the <em>ongoing</em>
jaroslav@1890
   142
 * acquisition of a lock may not be available in a given {@code Lock}
jaroslav@1890
   143
 * class.  Consequently, an implementation is not required to define
jaroslav@1890
   144
 * exactly the same guarantees or semantics for all three forms of
jaroslav@1890
   145
 * lock acquisition, nor is it required to support interruption of an
jaroslav@1890
   146
 * ongoing lock acquisition.  An implementation is required to clearly
jaroslav@1890
   147
 * document the semantics and guarantees provided by each of the
jaroslav@1890
   148
 * locking methods. It must also obey the interruption semantics as
jaroslav@1890
   149
 * defined in this interface, to the extent that interruption of lock
jaroslav@1890
   150
 * acquisition is supported: which is either totally, or only on
jaroslav@1890
   151
 * method entry.
jaroslav@1890
   152
 *
jaroslav@1890
   153
 * <p>As interruption generally implies cancellation, and checks for
jaroslav@1890
   154
 * interruption are often infrequent, an implementation can favor responding
jaroslav@1890
   155
 * to an interrupt over normal method return. This is true even if it can be
jaroslav@1890
   156
 * shown that the interrupt occurred after another action may have unblocked
jaroslav@1890
   157
 * the thread. An implementation should document this behavior.
jaroslav@1890
   158
 *
jaroslav@1890
   159
 * @see ReentrantLock
jaroslav@1890
   160
 * @see Condition
jaroslav@1890
   161
 * @see ReadWriteLock
jaroslav@1890
   162
 *
jaroslav@1890
   163
 * @since 1.5
jaroslav@1890
   164
 * @author Doug Lea
jaroslav@1890
   165
 */
jaroslav@1890
   166
public interface Lock {
jaroslav@1890
   167
jaroslav@1890
   168
    /**
jaroslav@1890
   169
     * Acquires the lock.
jaroslav@1890
   170
     *
jaroslav@1890
   171
     * <p>If the lock is not available then the current thread becomes
jaroslav@1890
   172
     * disabled for thread scheduling purposes and lies dormant until the
jaroslav@1890
   173
     * lock has been acquired.
jaroslav@1890
   174
     *
jaroslav@1890
   175
     * <p><b>Implementation Considerations</b>
jaroslav@1890
   176
     *
jaroslav@1890
   177
     * <p>A {@code Lock} implementation may be able to detect erroneous use
jaroslav@1890
   178
     * of the lock, such as an invocation that would cause deadlock, and
jaroslav@1890
   179
     * may throw an (unchecked) exception in such circumstances.  The
jaroslav@1890
   180
     * circumstances and the exception type must be documented by that
jaroslav@1890
   181
     * {@code Lock} implementation.
jaroslav@1890
   182
     */
jaroslav@1890
   183
    void lock();
jaroslav@1890
   184
jaroslav@1890
   185
    /**
jaroslav@1890
   186
     * Acquires the lock unless the current thread is
jaroslav@1890
   187
     * {@linkplain Thread#interrupt interrupted}.
jaroslav@1890
   188
     *
jaroslav@1890
   189
     * <p>Acquires the lock if it is available and returns immediately.
jaroslav@1890
   190
     *
jaroslav@1890
   191
     * <p>If the lock is not available then the current thread becomes
jaroslav@1890
   192
     * disabled for thread scheduling purposes and lies dormant until
jaroslav@1890
   193
     * one of two things happens:
jaroslav@1890
   194
     *
jaroslav@1890
   195
     * <ul>
jaroslav@1890
   196
     * <li>The lock is acquired by the current thread; or
jaroslav@1890
   197
     * <li>Some other thread {@linkplain Thread#interrupt interrupts} the
jaroslav@1890
   198
     * current thread, and interruption of lock acquisition is supported.
jaroslav@1890
   199
     * </ul>
jaroslav@1890
   200
     *
jaroslav@1890
   201
     * <p>If the current thread:
jaroslav@1890
   202
     * <ul>
jaroslav@1890
   203
     * <li>has its interrupted status set on entry to this method; or
jaroslav@1890
   204
     * <li>is {@linkplain Thread#interrupt interrupted} while acquiring the
jaroslav@1890
   205
     * lock, and interruption of lock acquisition is supported,
jaroslav@1890
   206
     * </ul>
jaroslav@1890
   207
     * then {@link InterruptedException} is thrown and the current thread's
jaroslav@1890
   208
     * interrupted status is cleared.
jaroslav@1890
   209
     *
jaroslav@1890
   210
     * <p><b>Implementation Considerations</b>
jaroslav@1890
   211
     *
jaroslav@1890
   212
     * <p>The ability to interrupt a lock acquisition in some
jaroslav@1890
   213
     * implementations may not be possible, and if possible may be an
jaroslav@1890
   214
     * expensive operation.  The programmer should be aware that this
jaroslav@1890
   215
     * may be the case. An implementation should document when this is
jaroslav@1890
   216
     * the case.
jaroslav@1890
   217
     *
jaroslav@1890
   218
     * <p>An implementation can favor responding to an interrupt over
jaroslav@1890
   219
     * normal method return.
jaroslav@1890
   220
     *
jaroslav@1890
   221
     * <p>A {@code Lock} implementation may be able to detect
jaroslav@1890
   222
     * erroneous use of the lock, such as an invocation that would
jaroslav@1890
   223
     * cause deadlock, and may throw an (unchecked) exception in such
jaroslav@1890
   224
     * circumstances.  The circumstances and the exception type must
jaroslav@1890
   225
     * be documented by that {@code Lock} implementation.
jaroslav@1890
   226
     *
jaroslav@1890
   227
     * @throws InterruptedException if the current thread is
jaroslav@1890
   228
     *         interrupted while acquiring the lock (and interruption
jaroslav@1890
   229
     *         of lock acquisition is supported).
jaroslav@1890
   230
     */
jaroslav@1890
   231
    void lockInterruptibly() throws InterruptedException;
jaroslav@1890
   232
jaroslav@1890
   233
    /**
jaroslav@1890
   234
     * Acquires the lock only if it is free at the time of invocation.
jaroslav@1890
   235
     *
jaroslav@1890
   236
     * <p>Acquires the lock if it is available and returns immediately
jaroslav@1890
   237
     * with the value {@code true}.
jaroslav@1890
   238
     * If the lock is not available then this method will return
jaroslav@1890
   239
     * immediately with the value {@code false}.
jaroslav@1890
   240
     *
jaroslav@1890
   241
     * <p>A typical usage idiom for this method would be:
jaroslav@1890
   242
     * <pre>
jaroslav@1890
   243
     *      Lock lock = ...;
jaroslav@1890
   244
     *      if (lock.tryLock()) {
jaroslav@1890
   245
     *          try {
jaroslav@1890
   246
     *              // manipulate protected state
jaroslav@1890
   247
     *          } finally {
jaroslav@1890
   248
     *              lock.unlock();
jaroslav@1890
   249
     *          }
jaroslav@1890
   250
     *      } else {
jaroslav@1890
   251
     *          // perform alternative actions
jaroslav@1890
   252
     *      }
jaroslav@1890
   253
     * </pre>
jaroslav@1890
   254
     * This usage ensures that the lock is unlocked if it was acquired, and
jaroslav@1890
   255
     * doesn't try to unlock if the lock was not acquired.
jaroslav@1890
   256
     *
jaroslav@1890
   257
     * @return {@code true} if the lock was acquired and
jaroslav@1890
   258
     *         {@code false} otherwise
jaroslav@1890
   259
     */
jaroslav@1890
   260
    boolean tryLock();
jaroslav@1890
   261
jaroslav@1890
   262
    /**
jaroslav@1890
   263
     * Acquires the lock if it is free within the given waiting time and the
jaroslav@1890
   264
     * current thread has not been {@linkplain Thread#interrupt interrupted}.
jaroslav@1890
   265
     *
jaroslav@1890
   266
     * <p>If the lock is available this method returns immediately
jaroslav@1890
   267
     * with the value {@code true}.
jaroslav@1890
   268
     * If the lock is not available then
jaroslav@1890
   269
     * the current thread becomes disabled for thread scheduling
jaroslav@1890
   270
     * purposes and lies dormant until one of three things happens:
jaroslav@1890
   271
     * <ul>
jaroslav@1890
   272
     * <li>The lock is acquired by the current thread; or
jaroslav@1890
   273
     * <li>Some other thread {@linkplain Thread#interrupt interrupts} the
jaroslav@1890
   274
     * current thread, and interruption of lock acquisition is supported; or
jaroslav@1890
   275
     * <li>The specified waiting time elapses
jaroslav@1890
   276
     * </ul>
jaroslav@1890
   277
     *
jaroslav@1890
   278
     * <p>If the lock is acquired then the value {@code true} is returned.
jaroslav@1890
   279
     *
jaroslav@1890
   280
     * <p>If the current thread:
jaroslav@1890
   281
     * <ul>
jaroslav@1890
   282
     * <li>has its interrupted status set on entry to this method; or
jaroslav@1890
   283
     * <li>is {@linkplain Thread#interrupt interrupted} while acquiring
jaroslav@1890
   284
     * the lock, and interruption of lock acquisition is supported,
jaroslav@1890
   285
     * </ul>
jaroslav@1890
   286
     * then {@link InterruptedException} is thrown and the current thread's
jaroslav@1890
   287
     * interrupted status is cleared.
jaroslav@1890
   288
     *
jaroslav@1890
   289
     * <p>If the specified waiting time elapses then the value {@code false}
jaroslav@1890
   290
     * is returned.
jaroslav@1890
   291
     * If the time is
jaroslav@1890
   292
     * less than or equal to zero, the method will not wait at all.
jaroslav@1890
   293
     *
jaroslav@1890
   294
     * <p><b>Implementation Considerations</b>
jaroslav@1890
   295
     *
jaroslav@1890
   296
     * <p>The ability to interrupt a lock acquisition in some implementations
jaroslav@1890
   297
     * may not be possible, and if possible may
jaroslav@1890
   298
     * be an expensive operation.
jaroslav@1890
   299
     * The programmer should be aware that this may be the case. An
jaroslav@1890
   300
     * implementation should document when this is the case.
jaroslav@1890
   301
     *
jaroslav@1890
   302
     * <p>An implementation can favor responding to an interrupt over normal
jaroslav@1890
   303
     * method return, or reporting a timeout.
jaroslav@1890
   304
     *
jaroslav@1890
   305
     * <p>A {@code Lock} implementation may be able to detect
jaroslav@1890
   306
     * erroneous use of the lock, such as an invocation that would cause
jaroslav@1890
   307
     * deadlock, and may throw an (unchecked) exception in such circumstances.
jaroslav@1890
   308
     * The circumstances and the exception type must be documented by that
jaroslav@1890
   309
     * {@code Lock} implementation.
jaroslav@1890
   310
     *
jaroslav@1890
   311
     * @param time the maximum time to wait for the lock
jaroslav@1890
   312
     * @param unit the time unit of the {@code time} argument
jaroslav@1890
   313
     * @return {@code true} if the lock was acquired and {@code false}
jaroslav@1890
   314
     *         if the waiting time elapsed before the lock was acquired
jaroslav@1890
   315
     *
jaroslav@1890
   316
     * @throws InterruptedException if the current thread is interrupted
jaroslav@1890
   317
     *         while acquiring the lock (and interruption of lock
jaroslav@1890
   318
     *         acquisition is supported)
jaroslav@1890
   319
     */
jaroslav@1890
   320
    boolean tryLock(long time, TimeUnit unit) throws InterruptedException;
jaroslav@1890
   321
jaroslav@1890
   322
    /**
jaroslav@1890
   323
     * Releases the lock.
jaroslav@1890
   324
     *
jaroslav@1890
   325
     * <p><b>Implementation Considerations</b>
jaroslav@1890
   326
     *
jaroslav@1890
   327
     * <p>A {@code Lock} implementation will usually impose
jaroslav@1890
   328
     * restrictions on which thread can release a lock (typically only the
jaroslav@1890
   329
     * holder of the lock can release it) and may throw
jaroslav@1890
   330
     * an (unchecked) exception if the restriction is violated.
jaroslav@1890
   331
     * Any restrictions and the exception
jaroslav@1890
   332
     * type must be documented by that {@code Lock} implementation.
jaroslav@1890
   333
     */
jaroslav@1890
   334
    void unlock();
jaroslav@1890
   335
jaroslav@1890
   336
    /**
jaroslav@1890
   337
     * Returns a new {@link Condition} instance that is bound to this
jaroslav@1890
   338
     * {@code Lock} instance.
jaroslav@1890
   339
     *
jaroslav@1890
   340
     * <p>Before waiting on the condition the lock must be held by the
jaroslav@1890
   341
     * current thread.
jaroslav@1890
   342
     * A call to {@link Condition#await()} will atomically release the lock
jaroslav@1890
   343
     * before waiting and re-acquire the lock before the wait returns.
jaroslav@1890
   344
     *
jaroslav@1890
   345
     * <p><b>Implementation Considerations</b>
jaroslav@1890
   346
     *
jaroslav@1890
   347
     * <p>The exact operation of the {@link Condition} instance depends on
jaroslav@1890
   348
     * the {@code Lock} implementation and must be documented by that
jaroslav@1890
   349
     * implementation.
jaroslav@1890
   350
     *
jaroslav@1890
   351
     * @return A new {@link Condition} instance for this {@code Lock} instance
jaroslav@1890
   352
     * @throws UnsupportedOperationException if this {@code Lock}
jaroslav@1890
   353
     *         implementation does not support conditions
jaroslav@1890
   354
     */
jaroslav@1890
   355
    Condition newCondition();
jaroslav@1890
   356
}