Path: blob/master/src/java.base/share/classes/sun/nio/fs/AbstractWatchService.java
41159 views
/*1* Copyright (c) 2008, 2011, 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 sun.nio.fs;2627import java.nio.file.*;28import java.util.concurrent.*;29import java.io.IOException;3031/**32* Base implementation class for watch services.33*/3435abstract class AbstractWatchService implements WatchService {3637// signaled keys waiting to be dequeued38private final LinkedBlockingDeque<WatchKey> pendingKeys =39new LinkedBlockingDeque<WatchKey>();4041// special key to indicate that watch service is closed42private final WatchKey CLOSE_KEY =43new AbstractWatchKey(null, null) {44@Override45public boolean isValid() {46return true;47}4849@Override50public void cancel() {51}52};5354// used when closing watch service55private volatile boolean closed;56private final Object closeLock = new Object();5758protected AbstractWatchService() {59}6061/**62* Register the given object with this watch service63*/64abstract WatchKey register(Path path,65WatchEvent.Kind<?>[] events,66WatchEvent.Modifier... modifers)67throws IOException;6869// used by AbstractWatchKey to enqueue key70final void enqueueKey(WatchKey key) {71pendingKeys.offer(key);72}7374/**75* Throws ClosedWatchServiceException if watch service is closed76*/77private void checkOpen() {78if (closed)79throw new ClosedWatchServiceException();80}8182/**83* Checks the key isn't the special CLOSE_KEY used to unblock threads when84* the watch service is closed.85*/86private void checkKey(WatchKey key) {87if (key == CLOSE_KEY) {88// re-queue in case there are other threads blocked in take/poll89enqueueKey(key);90}91checkOpen();92}9394@Override95public final WatchKey poll() {96checkOpen();97WatchKey key = pendingKeys.poll();98checkKey(key);99return key;100}101102@Override103public final WatchKey poll(long timeout, TimeUnit unit)104throws InterruptedException105{106checkOpen();107WatchKey key = pendingKeys.poll(timeout, unit);108checkKey(key);109return key;110}111112@Override113public final WatchKey take()114throws InterruptedException115{116checkOpen();117WatchKey key = pendingKeys.take();118checkKey(key);119return key;120}121122/**123* Tells whether or not this watch service is open.124*/125final boolean isOpen() {126return !closed;127}128129/**130* Retrieves the object upon which the close method synchronizes.131*/132final Object closeLock() {133return closeLock;134}135136/**137* Closes this watch service. This method is invoked by the close138* method to perform the actual work of closing the watch service.139*/140abstract void implClose() throws IOException;141142@Override143public final void close()144throws IOException145{146synchronized (closeLock) {147// nothing to do if already closed148if (closed)149return;150closed = true;151152implClose();153154// clear pending keys and queue special key to ensure that any155// threads blocked in take/poll wakeup156pendingKeys.clear();157pendingKeys.offer(CLOSE_KEY);158}159}160}161162163