void TuneQuotas()
{
if (this.areQuotasBeingTuned)
{
return;
}
bool lockHeld = false;
try
{
Monitor.TryEnter(this.tuningLock, ref lockHeld);
// Don't bother if another thread already has the lock
if (!lockHeld || this.areQuotasBeingTuned)
{
return;
}
this.areQuotasBeingTuned = true;
}
finally
{
if (lockHeld)
{
Monitor.Exit(this.tuningLock);
}
}
// find the "poorest" pool
int starvedIndex = FindMostStarvedPool();
if (starvedIndex >= 0)
{
BufferPool starvedBufferPool = this.bufferPools[starvedIndex];
if (this.remainingMemory < starvedBufferPool.BufferSize)
{
// find the "richest" pool
int excessiveIndex = FindMostExcessivePool();
if (excessiveIndex >= 0)
{
// steal from the richest
DecreaseQuota(ref this.bufferPools[excessiveIndex]);
}
}
if (this.remainingMemory >= starvedBufferPool.BufferSize)
{
// give to the poorest
IncreaseQuota(ref this.bufferPools[starvedIndex]);
}
}
// reset statistics
for (int i = 0; i < this.bufferPools.Length; i++)
{
BufferPool bufferPool = this.bufferPools[i];
bufferPool.Misses = 0;
}
this.totalMisses = 0;
this.areQuotasBeingTuned = false;
}