You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by vo...@apache.org on 2016/03/09 10:27:57 UTC

[42/50] [abbrv] ignite git commit: IGNITE-2766: Now StripedCompositeReadWriteLock normally will not use thread-locals when inside IgniteThread.

IGNITE-2766: Now StripedCompositeReadWriteLock normally  will not use thread-locals when inside IgniteThread.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/95f0c846
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/95f0c846
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/95f0c846

Branch: refs/heads/ignite-1786
Commit: 95f0c84679889ecabc0e572c25fe7f1515762b29
Parents: ee817dd
Author: vozerov-gridgain <vo...@gridgain.com>
Authored: Fri Mar 4 12:35:22 2016 +0300
Committer: vozerov-gridgain <vo...@gridgain.com>
Committed: Fri Mar 4 12:35:22 2016 +0300

----------------------------------------------------------------------
 .../util/StripedCompositeReadWriteLock.java     | 14 ++++++--
 .../org/apache/ignite/thread/IgniteThread.java  | 34 ++++++++++++++++++--
 .../ignite/thread/IgniteThreadFactory.java      |  7 +++-
 3 files changed, 49 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/95f0c846/modules/core/src/main/java/org/apache/ignite/internal/util/StripedCompositeReadWriteLock.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/StripedCompositeReadWriteLock.java b/modules/core/src/main/java/org/apache/ignite/internal/util/StripedCompositeReadWriteLock.java
index e28a5f8..96445d8 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/util/StripedCompositeReadWriteLock.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/util/StripedCompositeReadWriteLock.java
@@ -17,6 +17,7 @@
 
 package org.apache.ignite.internal.util;
 
+import org.apache.ignite.thread.IgniteThread;
 import org.jetbrains.annotations.NotNull;
 
 import java.util.concurrent.TimeUnit;
@@ -65,9 +66,18 @@ public class StripedCompositeReadWriteLock implements ReadWriteLock {
 
     /** {@inheritDoc} */
     @NotNull @Override public Lock readLock() {
-        int idx = IDX.get() % locks.length;
+        int idx;
 
-        return locks[idx].readLock();
+        if (Thread.currentThread() instanceof IgniteThread) {
+            idx = ((IgniteThread)Thread.currentThread()).groupIndex();
+
+            if (idx == IgniteThread.GRP_IDX_UNASSIGNED)
+                idx = IDX.get();
+        }
+        else
+            idx = IDX.get();
+
+        return locks[idx % locks.length].readLock();
     }
 
     /** {@inheritDoc} */

http://git-wip-us.apache.org/repos/asf/ignite/blob/95f0c846/modules/core/src/main/java/org/apache/ignite/thread/IgniteThread.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/thread/IgniteThread.java b/modules/core/src/main/java/org/apache/ignite/thread/IgniteThread.java
index a9f1fbb..03ed589 100644
--- a/modules/core/src/main/java/org/apache/ignite/thread/IgniteThread.java
+++ b/modules/core/src/main/java/org/apache/ignite/thread/IgniteThread.java
@@ -33,6 +33,9 @@ import org.apache.ignite.internal.util.worker.GridWorker;
  * <b>Note</b>: this class is intended for internal use only.
  */
 public class IgniteThread extends Thread {
+    /** Index for unassigned thread. */
+    public static final int GRP_IDX_UNASSIGNED = -1;
+
     /** Default thread's group. */
     private static final ThreadGroup DFLT_GRP = new ThreadGroup("ignite");
 
@@ -42,13 +45,16 @@ public class IgniteThread extends Thread {
     /** The name of the grid this thread belongs to. */
     protected final String gridName;
 
+    /** Group index. */
+    private final int grpIdx;
+
     /**
      * Creates thread with given worker.
      *
      * @param worker Runnable to create thread with.
      */
     public IgniteThread(GridWorker worker) {
-        this(DFLT_GRP, worker.gridName(), worker.name(), worker);
+        this(DFLT_GRP, worker.gridName(), worker.name(), worker, GRP_IDX_UNASSIGNED);
     }
 
     /**
@@ -59,7 +65,19 @@ public class IgniteThread extends Thread {
      * @param r Runnable to execute.
      */
     public IgniteThread(String gridName, String threadName, Runnable r) {
-        this(DFLT_GRP, gridName, threadName, r);
+        this(gridName, threadName, r, GRP_IDX_UNASSIGNED);
+    }
+
+    /**
+     * Creates grid thread with given name for a given grid.
+     *
+     * @param gridName Name of grid this thread is created for.
+     * @param threadName Name of thread.
+     * @param r Runnable to execute.
+     * @param grpIdx Index within a group.
+     */
+    public IgniteThread(String gridName, String threadName, Runnable r, int grpIdx) {
+        this(DFLT_GRP, gridName, threadName, r, grpIdx);
     }
 
     /**
@@ -70,11 +88,13 @@ public class IgniteThread extends Thread {
      * @param gridName Name of grid this thread is created for.
      * @param threadName Name of thread.
      * @param r Runnable to execute.
+     * @param grpIdx Thread index within a group.
      */
-    public IgniteThread(ThreadGroup grp, String gridName, String threadName, Runnable r) {
+    public IgniteThread(ThreadGroup grp, String gridName, String threadName, Runnable r, int grpIdx) {
         super(grp, r, createName(cntr.incrementAndGet(), threadName, gridName));
 
         this.gridName = gridName;
+        this.grpIdx = grpIdx;
     }
 
     /**
@@ -86,6 +106,7 @@ public class IgniteThread extends Thread {
         super(threadGrp, threadName);
 
         this.gridName = gridName;
+        this.grpIdx = GRP_IDX_UNASSIGNED;
     }
 
     /**
@@ -98,6 +119,13 @@ public class IgniteThread extends Thread {
     }
 
     /**
+     * @return Group index.
+     */
+    public int groupIndex() {
+        return grpIdx;
+    }
+
+    /**
      * Creates new thread name.
      *
      * @param num Thread number.

http://git-wip-us.apache.org/repos/asf/ignite/blob/95f0c846/modules/core/src/main/java/org/apache/ignite/thread/IgniteThreadFactory.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/thread/IgniteThreadFactory.java b/modules/core/src/main/java/org/apache/ignite/thread/IgniteThreadFactory.java
index cd0a9cc..55557dd 100644
--- a/modules/core/src/main/java/org/apache/ignite/thread/IgniteThreadFactory.java
+++ b/modules/core/src/main/java/org/apache/ignite/thread/IgniteThreadFactory.java
@@ -18,6 +18,8 @@
 package org.apache.ignite.thread;
 
 import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.atomic.AtomicInteger;
+
 import org.jetbrains.annotations.NotNull;
 
 /**
@@ -31,6 +33,9 @@ public class IgniteThreadFactory implements ThreadFactory {
     /** Thread name. */
     private final String threadName;
 
+    /** Index generator for threads. */
+    private final AtomicInteger idxGen = new AtomicInteger();
+
     /**
      * Constructs new thread factory for given grid. All threads will belong
      * to the same default thread group.
@@ -55,6 +60,6 @@ public class IgniteThreadFactory implements ThreadFactory {
 
     /** {@inheritDoc} */
     @Override public Thread newThread(@NotNull Runnable r) {
-        return new IgniteThread(gridName, threadName, r);
+        return new IgniteThread(gridName, threadName, r, idxGen.incrementAndGet());
     }
 }
\ No newline at end of file