Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8439737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:05:01+00:00 2026-06-10T08:05:01+00:00

While looking at the source code of System.ServiceModel.Channels.BufferManager, I noticed this method: void TuneQuotas()

  • 0

While looking at the source code of System.ServiceModel.Channels.BufferManager, I noticed this method:

void TuneQuotas()
{
    if (areQuotasBeingTuned)
        return;

    bool lockHeld = false;
    try
    {
        try { }
        finally
        {
            lockHeld = Monitor.TryEnter(tuningLock);
        }

        // Don't bother if another thread already has the lock
        if (!lockHeld || areQuotasBeingTuned)
            return;
        areQuotasBeingTuned = true;
    }
    finally
    {
        if (lockHeld)
        {
            Monitor.Exit(tuningLock);
        }
    }
    //
    // DO WORK... (code removed for brevity)
    //
    areQuotasBeingTuned = false;
}

Obviously, they want only one thread to run TuneQuotas(), and other threads to not wait if it is already being run by another thread. I should note that the code removed was not try protected.

I’m trying to understand the advantages of this method above over just doing this:

void TuneQuotas()
{
    if(!Monitor.TryEnter(tuningLock)) return;
    //
    // DO WORK...
    //
    Monitor.Exit(tuningLock);
}

Any ideas why they might have bothered with all that? I suspect the way they use the finally blocks is to guard against a thread abort scenario, but I still don’t see the point because, even with all this code, TuneQuotas() would be locked for good if that one thread doesn’t make it all the way to the end to set areQuotasBeingTunes=false, for one reason or another. So is there something cool about this pattern that I’m missing?

EDIT:
As a side note, it seems the method exists in .NET 4.0, which I confirmed using this code running on framework 4 (although I cannot confirm that the content of the method hasn’t changed from what I found on the web):

var buffMgr = BufferManager.CreateBufferManager(1, 1);
var pooledBuffMgrType = buffMgr.GetType()
    .GetProperty("InternalBufferManager")
    .GetValue(buffMgr, null)
    .GetType();

Debug.WriteLine(pooledBuffMgrType.Module.FullyQualifiedName);
foreach (var methodInfo in pooledBuffMgrType
    .GetMethods(BindingFlags.Instance | BindingFlags.NonPublic))
{
    Debug.WriteLine(methodInfo.Name);
}

which outputs:

C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.DurableInstancing\v4.0_4.0.0.0__3    1bf3856ad364e35\System.Runtime.DurableInstancing.dll
ChangeQuota
DecreaseQuota
FindMostExcessivePool
FindMostStarvedPool
FindPool
IncreaseQuota
TuneQuotas
Finalize
MemberwiseClone
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:05:03+00:00Added an answer on June 10, 2026 at 8:05 am

    I’ll add some comments:

    void TuneQuotas()
    {
        if (areQuotasBeingTuned)
            return; //fast-path, does not require locking
    
        bool lockHeld = false;
        try
        {
            try { }
            finally
            {
                //finally-blocks cannot be aborted by Thread.Abort
                //The thread could be aborted after getting the lock and before setting lockHeld
                lockHeld = Monitor.TryEnter(tuningLock);
            }
    
            // Don't bother if another thread already has the lock
            if (!lockHeld || areQuotasBeingTuned)
                return; //areQuotasBeingTuned could have switched to true in the mean-time
            areQuotasBeingTuned = true; //prevent others from needlessly trying to lock (trigger fast-path)
        }
        finally //ensure the lock being released
        {
            if (lockHeld)
            {
                Monitor.Exit(tuningLock);
            }
        }
        //
        // DO WORK... (code removed for brevity)
        //
        //this might be a bug. There should be a call to Thread.MemoryBarrier,
        //or areQuotasBeingTuned should be volatile
        //if not, the write might never reach other processor cores
        //maybe this doesn't matter for x86
        areQuotasBeingTuned = false;
    }
    

    The simple version you gave does not protect against some problems. At the very least it is not exception-safe (lock won’t be released). Interestingly, the “sophisticated” version, doesn’t either.

    This method has been removed from .NET 4.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While looking through the Selenium source code I noticed the following in the PageFactory:
While looking through some old code I came across this gem: MyObject o =
While looking at some open source code to learn more about J2EE, I came
After stumbling upon the following class while looking at magento source code: Mage_Core_Block_Template_Facade, i
while looking at Shrinkr 's source code (we all review other project's source code
As the title suggests I am looking for a open source ERP package. While
while looking at some code I stumbled onto: throw /*-->*/new std::exception (//... and I
While looking up the answer to this question: Why is an out parameter not
While looking at some conceptual questions in C,I came across this question in a
I discovered this quite by accident while looking for a file with a number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.