Refactored atomic updates of max and min into utility class Atomics.

This commit is contained in:
Simone Bordet 2012-06-08 09:59:36 +02:00
parent f94f9a066a
commit 7f39b8b7a4
4 changed files with 93 additions and 54 deletions

View File

@ -64,6 +64,7 @@ import org.eclipse.jetty.spdy.frames.SynStreamFrame;
import org.eclipse.jetty.spdy.frames.WindowUpdateFrame;
import org.eclipse.jetty.spdy.generator.Generator;
import org.eclipse.jetty.spdy.parser.Parser;
import org.eclipse.jetty.util.Atomics;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@ -831,16 +832,7 @@ public class StandardSession implements ISession, Parser.Listener, Handler<Stand
{
int streamId = stream.getId();
if (stream.isClosed() && streamId % 2 != streamIds.get() % 2)
{
// Non-blocking atomic update
int oldValue = lastStreamId.get();
while (streamId > oldValue)
{
if (lastStreamId.compareAndSet(oldValue,streamId))
break;
oldValue = lastStreamId.get();
}
}
Atomics.updateMax(lastStreamId, streamId);
}
@Override

View File

@ -0,0 +1,68 @@
// ========================================================================
// Copyright (c) 2012 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.util;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
public class Atomics
{
private Atomics()
{
}
public static void updateMin(AtomicLong currentMin, long newValue)
{
long oldValue = currentMin.get();
while (newValue < oldValue)
{
if (currentMin.compareAndSet(oldValue, newValue))
break;
oldValue = currentMin.get();
}
}
public static void updateMax(AtomicLong currentMax, long newValue)
{
long oldValue = currentMax.get();
while (newValue > oldValue)
{
if (currentMax.compareAndSet(oldValue, newValue))
break;
oldValue = currentMax.get();
}
}
public static void updateMin(AtomicInteger currentMin, int newValue)
{
int oldValue = currentMin.get();
while (newValue < oldValue)
{
if (currentMin.compareAndSet(oldValue, newValue))
break;
oldValue = currentMin.get();
}
}
public static void updateMax(AtomicInteger currentMax, int newValue)
{
int oldValue = currentMax.get();
while (newValue > oldValue)
{
if (currentMax.compareAndSet(oldValue, newValue))
break;
oldValue = currentMax.get();
}
}
}

View File

@ -15,6 +15,8 @@ package org.eclipse.jetty.util.statistic;
import java.util.concurrent.atomic.AtomicLong;
import org.eclipse.jetty.util.Atomics;
/* ------------------------------------------------------------ */
/** Statistics on a counter value.
@ -53,13 +55,7 @@ public class CounterStatistic
long value=_curr.addAndGet(delta);
if (delta > 0)
_total.addAndGet(delta);
long oldValue = _max.get();
while (value > oldValue)
{
if (_max.compareAndSet(oldValue, value))
break;
oldValue = _max.get();
}
Atomics.updateMax(_max,value);
}
/* ------------------------------------------------------------ */
@ -113,9 +109,4 @@ public class CounterStatistic
{
return _total.get();
}
/* ------------------------------------------------------------ */
protected void upxdateMax(long value)
{
}
}

View File

@ -15,6 +15,8 @@ package org.eclipse.jetty.util.statistic;
import java.util.concurrent.atomic.AtomicLong;
import org.eclipse.jetty.util.Atomics;
/* ------------------------------------------------------------ */
/**
@ -61,17 +63,9 @@ public class SampleStatistic
_totalVariance100.addAndGet(delta10*delta10);
}
long oldMax = _max.get();
while (sample > oldMax)
{
if (_max.compareAndSet(oldMax, sample))
break;
oldMax = _max.get();
Atomics.updateMax(_max, sample);
}
}
/* ------------------------------------------------------------ */
/**
* @return the max value
*/
@ -80,25 +74,21 @@ public class SampleStatistic
return _max.get();
}
/* ------------------------------------------------------------ */
public long getTotal()
{
return _total.get();
}
/* ------------------------------------------------------------ */
public long getCount()
{
return _count.get();
}
/* ------------------------------------------------------------ */
public double getMean()
{
return (double)_total.get()/_count.get();
}
/* ------------------------------------------------------------ */
public double getVariance()
{
final long variance100 = _totalVariance100.get();
@ -107,10 +97,8 @@ public class SampleStatistic
return count>1?((double)variance100)/100.0/(count-1):0.0;
}
/* ------------------------------------------------------------ */
public double getStdDev()
{
return Math.sqrt(getVariance());
}
}