Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/nio/channels/FileLock.java
38918 views
/*1* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.channels;2627import java.io.IOException;2829/**30* A token representing a lock on a region of a file.31*32* <p> A file-lock object is created each time a lock is acquired on a file via33* one of the {@link FileChannel#lock(long,long,boolean) lock} or {@link34* FileChannel#tryLock(long,long,boolean) tryLock} methods of the35* {@link FileChannel} class, or the {@link36* AsynchronousFileChannel#lock(long,long,boolean,Object,CompletionHandler) lock}37* or {@link AsynchronousFileChannel#tryLock(long,long,boolean) tryLock}38* methods of the {@link AsynchronousFileChannel} class.39*40* <p> A file-lock object is initially valid. It remains valid until the lock41* is released by invoking the {@link #release release} method, by closing the42* channel that was used to acquire it, or by the termination of the Java43* virtual machine, whichever comes first. The validity of a lock may be44* tested by invoking its {@link #isValid isValid} method.45*46* <p> A file lock is either <i>exclusive</i> or <i>shared</i>. A shared lock47* prevents other concurrently-running programs from acquiring an overlapping48* exclusive lock, but does allow them to acquire overlapping shared locks. An49* exclusive lock prevents other programs from acquiring an overlapping lock of50* either type. Once it is released, a lock has no further effect on the locks51* that may be acquired by other programs.52*53* <p> Whether a lock is exclusive or shared may be determined by invoking its54* {@link #isShared isShared} method. Some platforms do not support shared55* locks, in which case a request for a shared lock is automatically converted56* into a request for an exclusive lock.57*58* <p> The locks held on a particular file by a single Java virtual machine do59* not overlap. The {@link #overlaps overlaps} method may be used to test60* whether a candidate lock range overlaps an existing lock.61*62* <p> A file-lock object records the file channel upon whose file the lock is63* held, the type and validity of the lock, and the position and size of the64* locked region. Only the validity of a lock is subject to change over time;65* all other aspects of a lock's state are immutable.66*67* <p> File locks are held on behalf of the entire Java virtual machine.68* They are not suitable for controlling access to a file by multiple69* threads within the same virtual machine.70*71* <p> File-lock objects are safe for use by multiple concurrent threads.72*73*74* <a name="pdep"></a><h2> Platform dependencies </h2>75*76* <p> This file-locking API is intended to map directly to the native locking77* facility of the underlying operating system. Thus the locks held on a file78* should be visible to all programs that have access to the file, regardless79* of the language in which those programs are written.80*81* <p> Whether or not a lock actually prevents another program from accessing82* the content of the locked region is system-dependent and therefore83* unspecified. The native file-locking facilities of some systems are merely84* <i>advisory</i>, meaning that programs must cooperatively observe a known85* locking protocol in order to guarantee data integrity. On other systems86* native file locks are <i>mandatory</i>, meaning that if one program locks a87* region of a file then other programs are actually prevented from accessing88* that region in a way that would violate the lock. On yet other systems,89* whether native file locks are advisory or mandatory is configurable on a90* per-file basis. To ensure consistent and correct behavior across platforms,91* it is strongly recommended that the locks provided by this API be used as if92* they were advisory locks.93*94* <p> On some systems, acquiring a mandatory lock on a region of a file95* prevents that region from being {@link java.nio.channels.FileChannel#map96* <i>mapped into memory</i>}, and vice versa. Programs that combine97* locking and mapping should be prepared for this combination to fail.98*99* <p> On some systems, closing a channel releases all locks held by the Java100* virtual machine on the underlying file regardless of whether the locks were101* acquired via that channel or via another channel open on the same file. It102* is strongly recommended that, within a program, a unique channel be used to103* acquire all locks on any given file.104*105* <p> Some network filesystems permit file locking to be used with106* memory-mapped files only when the locked regions are page-aligned and a107* whole multiple of the underlying hardware's page size. Some network108* filesystems do not implement file locks on regions that extend past a109* certain position, often 2<sup>30</sup> or 2<sup>31</sup>. In general, great110* care should be taken when locking files that reside on network filesystems.111*112*113* @author Mark Reinhold114* @author JSR-51 Expert Group115* @since 1.4116*/117118public abstract class FileLock implements AutoCloseable {119120private final Channel channel;121private final long position;122private final long size;123private final boolean shared;124125/**126* Initializes a new instance of this class.127*128* @param channel129* The file channel upon whose file this lock is held130*131* @param position132* The position within the file at which the locked region starts;133* must be non-negative134*135* @param size136* The size of the locked region; must be non-negative, and the sum137* <tt>position</tt> + <tt>size</tt> must be non-negative138*139* @param shared140* <tt>true</tt> if this lock is shared,141* <tt>false</tt> if it is exclusive142*143* @throws IllegalArgumentException144* If the preconditions on the parameters do not hold145*/146protected FileLock(FileChannel channel,147long position, long size, boolean shared)148{149if (position < 0)150throw new IllegalArgumentException("Negative position");151if (size < 0)152throw new IllegalArgumentException("Negative size");153if (position + size < 0)154throw new IllegalArgumentException("Negative position + size");155this.channel = channel;156this.position = position;157this.size = size;158this.shared = shared;159}160161/**162* Initializes a new instance of this class.163*164* @param channel165* The channel upon whose file this lock is held166*167* @param position168* The position within the file at which the locked region starts;169* must be non-negative170*171* @param size172* The size of the locked region; must be non-negative, and the sum173* <tt>position</tt> + <tt>size</tt> must be non-negative174*175* @param shared176* <tt>true</tt> if this lock is shared,177* <tt>false</tt> if it is exclusive178*179* @throws IllegalArgumentException180* If the preconditions on the parameters do not hold181*182* @since 1.7183*/184protected FileLock(AsynchronousFileChannel channel,185long position, long size, boolean shared)186{187if (position < 0)188throw new IllegalArgumentException("Negative position");189if (size < 0)190throw new IllegalArgumentException("Negative size");191if (position + size < 0)192throw new IllegalArgumentException("Negative position + size");193this.channel = channel;194this.position = position;195this.size = size;196this.shared = shared;197}198199/**200* Returns the file channel upon whose file this lock was acquired.201*202* <p> This method has been superseded by the {@link #acquiredBy acquiredBy}203* method.204*205* @return The file channel, or {@code null} if the file lock was not206* acquired by a file channel.207*/208public final FileChannel channel() {209return (channel instanceof FileChannel) ? (FileChannel)channel : null;210}211212/**213* Returns the channel upon whose file this lock was acquired.214*215* @return The channel upon whose file this lock was acquired.216*217* @since 1.7218*/219public Channel acquiredBy() {220return channel;221}222223/**224* Returns the position within the file of the first byte of the locked225* region.226*227* <p> A locked region need not be contained within, or even overlap, the228* actual underlying file, so the value returned by this method may exceed229* the file's current size. </p>230*231* @return The position232*/233public final long position() {234return position;235}236237/**238* Returns the size of the locked region in bytes.239*240* <p> A locked region need not be contained within, or even overlap, the241* actual underlying file, so the value returned by this method may exceed242* the file's current size. </p>243*244* @return The size of the locked region245*/246public final long size() {247return size;248}249250/**251* Tells whether this lock is shared.252*253* @return <tt>true</tt> if lock is shared,254* <tt>false</tt> if it is exclusive255*/256public final boolean isShared() {257return shared;258}259260/**261* Tells whether or not this lock overlaps the given lock range.262*263* @param position264* The starting position of the lock range265* @param size266* The size of the lock range267*268* @return <tt>true</tt> if, and only if, this lock and the given lock269* range overlap by at least one byte270*/271public final boolean overlaps(long position, long size) {272if (position + size <= this.position)273return false; // That is below this274if (this.position + this.size <= position)275return false; // This is below that276return true;277}278279/**280* Tells whether or not this lock is valid.281*282* <p> A lock object remains valid until it is released or the associated283* file channel is closed, whichever comes first. </p>284*285* @return <tt>true</tt> if, and only if, this lock is valid286*/287public abstract boolean isValid();288289/**290* Releases this lock.291*292* <p> If this lock object is valid then invoking this method releases the293* lock and renders the object invalid. If this lock object is invalid294* then invoking this method has no effect. </p>295*296* @throws ClosedChannelException297* If the channel that was used to acquire this lock298* is no longer open299*300* @throws IOException301* If an I/O error occurs302*/303public abstract void release() throws IOException;304305/**306* This method invokes the {@link #release} method. It was added307* to the class so that it could be used in conjunction with the308* automatic resource management block construct.309*310* @since 1.7311*/312public final void close() throws IOException {313release();314}315316/**317* Returns a string describing the range, type, and validity of this lock.318*319* @return A descriptive string320*/321public final String toString() {322return (this.getClass().getName()323+ "[" + position324+ ":" + size325+ " " + (shared ? "shared" : "exclusive")326+ " " + (isValid() ? "valid" : "invalid")327+ "]");328}329330}331332333