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