diff options
author | Nathan Harmata <nharmata@google.com> | 2015-03-07 02:21:20 +0000 |
---|---|---|
committer | Damien Martin-Guillerez <dmarting@google.com> | 2015-03-10 13:58:41 +0000 |
commit | 9b370024faa05f44d13dee1a42b35f7907afa991 (patch) | |
tree | 32ebac1b14c55bb9afe497438238c7ffe47539eb /src/main/java/com/google/devtools/build/lib/concurrent/KeyedLocker.java | |
parent | 539f7ad83cba45b2286f7366ab3bd34cc435fe5a (diff) |
Introduce KeyedLocker, a nice concurrency abstraction for managing lots of mutexes, and RefCountedMultisetKeyedLocker, an efficient implementation of this abstraction.
--
MOS_MIGRATED_REVID=88000985
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/concurrent/KeyedLocker.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/concurrent/KeyedLocker.java | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/concurrent/KeyedLocker.java b/src/main/java/com/google/devtools/build/lib/concurrent/KeyedLocker.java new file mode 100644 index 0000000000..8228f5b236 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/concurrent/KeyedLocker.java @@ -0,0 +1,49 @@ +// Copyright 2015 Google Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.lib.concurrent; + +import javax.annotation.concurrent.ThreadSafe; + +/** A keyed store of locks. */ +@ThreadSafe +public interface KeyedLocker<K> { + /** Used to yield access to the implicit lock granted by {@link #lock}. */ + @ThreadSafe + interface AutoUnlocker extends AutoCloseable { + /** + * If this was returned by {@code lock(k)}, yields exclusive access to {@code k}. + * + * <p>This method should be called at most once, and may only be called by the same thread that + * acquired the {@link AutoUnlocker} via {@link #lock}. Implementations are free to do anything + * if this is violated. + */ + @Override + void close(); + } + + /** + * Blocks the current thread until it has exclusive access to do things with {@code k} and + * returns a {@link AutoUnlocker} instance for yielding the implicit lock. The intended usage + * is: + * + * <pre> + * {@code + * try (AutoUnlocker unlocker = locker.lock(k)) { + * // Your code here. + * } + * } + * </pre> + */ + AutoUnlocker lock(K key); +}
\ No newline at end of file |