You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by mi...@apache.org on 2020/07/10 07:33:04 UTC

[maven-resolver] 01/01: [MRESOLVER-123] Provide a global locking sync context by default

This is an automated email from the ASF dual-hosted git repository.

michaelo pushed a commit to branch MRESOLVER-123
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git

commit 869f2926ae6e0415bd1edab0c504e2920fa1c744
Author: Michael Osipov <mi...@apache.org>
AuthorDate: Mon Jun 29 21:17:35 2020 +0200

    [MRESOLVER-123] Provide a global locking sync context by default
---
 .../internal/impl/DefaultSyncContextFactory.java   | 40 +++++++++++++++++++---
 1 file changed, 35 insertions(+), 5 deletions(-)

diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultSyncContextFactory.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultSyncContextFactory.java
index 69fdbc6..6c45a08 100644
--- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultSyncContextFactory.java
+++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultSyncContextFactory.java
@@ -8,9 +8,9 @@ package org.eclipse.aether.internal.impl;
  * 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
@@ -20,6 +20,8 @@ package org.eclipse.aether.internal.impl;
  */
 
 import java.util.Collection;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import javax.inject.Named;
 
@@ -28,31 +30,59 @@ import org.eclipse.aether.SyncContext;
 import org.eclipse.aether.artifact.Artifact;
 import org.eclipse.aether.impl.SyncContextFactory;
 import org.eclipse.aether.metadata.Metadata;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.inject.Singleton;
 
 /**
- * A factory to create synchronization contexts. This default implementation actually does not provide any real
- * synchronization but merely completes the repository system.
+ * A factory to create synchronization contexts. This default implementation uses fair global locking
+ * based on {@link ReentrantReadWriteLock}. Explicit artifacts and metadata passed are ignored.
  */
 @Named
+@Singleton
 public class DefaultSyncContextFactory
     implements SyncContextFactory
 {
+    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock( true );
 
     public SyncContext newInstance( RepositorySystemSession session, boolean shared )
     {
-        return new DefaultSyncContext();
+        return new DefaultSyncContext( shared ? lock.readLock() : lock.writeLock(), shared );
     }
 
     static class DefaultSyncContext
         implements SyncContext
     {
+        private static final Logger LOGGER = LoggerFactory.getLogger( DefaultSyncContext.class );
+
+        private final Lock lock;
+        private final boolean shared;
+        private int lockHoldCount;
+
+        DefaultSyncContext( Lock lock, boolean shared )
+        {
+            this.lock = lock;
+            this.shared = shared;
+        }
 
         public void acquire( Collection<? extends Artifact> artifact, Collection<? extends Metadata> metadata )
         {
+            LOGGER.debug( "Acquiring global {} lock (currently held: {})",
+                          shared ? "read" : "write", lockHoldCount );
+            lock.lock();
+            lockHoldCount++;
         }
 
         public void close()
         {
+            while ( lockHoldCount != 0 )
+            {
+                LOGGER.debug( "Releasing global {} lock (currently held: {})",
+                              shared ? "read" : "write", lockHoldCount );
+                lock.unlock();
+                lockHoldCount--;
+            }
         }
 
     }