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 9282119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:17:43+00:00 2026-06-18T18:17:43+00:00

I have a function to disable interrupts, but the problem is that if I

  • 0

I have a function to disable interrupts, but the problem is that if I disable them and I call a function which also disables/enables them, they get re-enabled too early. Is the following logic enough to prevent this?

static volatile int IrqCounter = 0;

void EnableIRQ()
{
    if(IrqCounter > 0)
    {
        IrqCounter--;
    }

    if(IrqCounter == 0)
    {
        __enable_irq();
    }
}

void DisableIRQ()
{
    if(IrqCounter == 0)
    {
        __disable_irq();
    }

    IrqCounter++;
}
  • 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-18T18:17:44+00:00Added an answer on June 18, 2026 at 6:17 pm

    Assuming that you’ve got a system where you can’t change context when interrupts are disabled, then what you’ve got is fine, assuming you keep careful track of when call the enable().

    In the usage you’re describing in the comments below, you plan on using these sections from within an interrupt service routine. Your main use is blocking higher-priority interrupts from running for a certain portion of an ISR.

    Be aware that you’ll have to consider the stack depth of these nested ISRs, as when you enable interrupts before your return from interrupt, you’ll have interrupts enabled in the ISR.

    Regarding other answers: the lack of thread-safety of the enable() (due to the if(IrqCounter > 0)) doesn’t matter, because anytime you’re in the enable() context switches are already disabled due to interrupts being off. (Unless for some reason you have unmatched disable/enable pairs, and in that case you’ve got other issues.)

    The only suggestion I’d have would be to add an ASSERT to the enable instead of the run-time check, as you should never be enabling interrupts that you didn’t disable.

    void EnableIRQ()
    {
      ASSERT(IrqCounter != 0)  //should never be 0, or we'd have an unmatched enable/disable pair
    
      IrqCounter--;  //doesn't matter that this isn't thread safe, as the enable is always called with interrupts disabled.
    
      if(IrqCounter == 0)
      {
          __enable_irq();
      }
    }
    

    I prefer the technique you’ve listed over the save(); disable(); restore(); technique as I don’t like having to keep track of a piece of the OS’ data every time I work with the interrupts. But, you do have to be aware of when you (directly or indirectly) make a call to the enable() from an ISR.

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

Sidebar

Related Questions

This function works fine if I disable errors. But I have to solve them.
I have function getCartItems in cart.js and I want to call that function in
In linux. I want to build an autoclicker that will have an enable/disable function
I have a function that I have implemented as a VBA module which takes
I have function LoadTempMovieList(), and need to load movies from sessionStorage. But it seems
I have function Start() that is fired on ready. When I click on .ExampleClick
I have function some_func_1 which will create an object of type some_type and will
i have function to send mail with attachment to microsoft exchange server. My problem
I have a jQuery function that is causing display errors in IE7. I simply
I have a function when a user double clicks a horizontal list, that element

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.