cstamas commented on a change in pull request #131: URL: https://github.com/apache/maven-resolver/pull/131#discussion_r761470897
########## File path: maven-resolver-named-locks/src/main/java/org/eclipse/aether/named/support/FileLockNamedLock.java ########## @@ -0,0 +1,287 @@ +package org.eclipse.aether.named.support; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.channels.FileChannel; +import java.nio.channels.FileLock; +import java.nio.channels.OverlappingFileLockException; +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.locks.ReentrantLock; + +import static org.eclipse.aether.named.support.Retry.retry; + +/** + * Named lock that uses {@link FileLock}. An instance of this class is about ONE LOCK (one file) and is possibly used + * by multiple threads. Each thread (if properly coded re boxing) will try to obtain either shared or exclusive lock. + * As file locks are JVM-scoped (so one JVM can obtain same file lock only once), the threads share file lock and + * synchronize according to it. Still, as file lock obtain operation does not block (or in other word, the method + * that does block cannot be controlled for how long it blocks), we are "simulate" thread blocking using + * {@link Retry} utility. + * This implementation performs coordination on thread (JVM-local) level but also on process level, as long as other + * parties are using this same "advisory" locking mechanism. + * + * @since TBD + */ +public final class FileLockNamedLock + extends NamedLockSupport +{ + private static final long RETRY_SLEEP_MILLIS = 100L; + + private static final long LOCK_POSITION = 0L; + + private static final long LOCK_SIZE = 1L; + + /** + * Thread -> steps stack (true = shared, false = exclusive) + */ + private final HashMap<Thread, Deque<Boolean>> threadSteps; + + /** + * The {@link FileChannel} this instance is about. + */ + private final FileChannel fileChannel; + + /** + * The reference of {@link FileLock}, if obtained. + */ + private final AtomicReference<FileLock> fileLockRef; Review comment: Literally is same effect as `volatile`, but I come from Modula2/Oberon universe, not C, so i dislike `volatile` keyword. Makes it (at every spot of access) clear what is it, unlike `volatile` (that makes it clear only at definition, but not on access). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
