low contention counter

This commit is contained in:
Alexey Kudravtsev
2011-07-19 16:50:50 +04:00
parent 827a9bbde2
commit 3a992bdaf0
2 changed files with 53 additions and 5 deletions
@@ -21,12 +21,11 @@ import com.intellij.openapi.editor.ex.DocumentEx;
import com.intellij.openapi.editor.ex.RangeMarkerEx;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.UserDataHolderBase;
import com.intellij.util.DistributedCounter;
import com.intellij.util.Processor;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.atomic.AtomicLong;
public class RangeMarkerImpl extends UserDataHolderBase implements RangeMarkerEx, MutableInterval {
private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.editor.impl.RangeMarkerImpl");
@@ -34,8 +33,7 @@ public class RangeMarkerImpl extends UserDataHolderBase implements RangeMarkerEx
protected RangeMarkerTree.RMNode myNode;
private final long myId;
//private static long counter;
private static final AtomicLong counter = new AtomicLong();
private static final DistributedCounter counter = new DistributedCounter();
protected RangeMarkerImpl(@NotNull DocumentEx document, int start, int end, boolean register) {
this(document, start, end, register, false, false);
@@ -52,7 +50,7 @@ public class RangeMarkerImpl extends UserDataHolderBase implements RangeMarkerEx
}
myDocument = document;
myId = counter.getAndIncrement();
myId = counter.next();
if (register) {
registerInTree(start, end, greedyToLeft, greedyToRight, 0);
}
@@ -0,0 +1,50 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed 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.
*/
package com.intellij.util;
import java.util.concurrent.atomic.AtomicLong;
/**
* Low-contention counter.
* Repeated calls to {@link #next()} return increasing numbers unique across all calling threads.
*/
public class DistributedCounter {
private static final int CHUNK_SIZE = 1000;
private final AtomicLong nextChunkStart = new AtomicLong();
// must not ne static
private final ThreadLocal<NextPair> localCounter = new ThreadLocal<NextPair>();
private static class NextPair {
long nextId;
final long limit;
private NextPair(long nextId, long limit) {
this.nextId = nextId;
this.limit = limit;
}
}
public long next() {
NextPair nextPair = localCounter.get();
if (nextPair == null || nextPair.nextId == nextPair.limit) {
long start = nextChunkStart.getAndAdd(CHUNK_SIZE);
nextPair = new NextPair(start, start + CHUNK_SIZE);
localCounter.set(nextPair);
}
long result = nextPair.nextId;
nextPair.nextId++;
return result;
}
}