Reentrant Function

Multi tool use
Multi tool use












0















Hello in the article from https://www.embedded.com/design/operating-systems/4008268/2/Back-to-the-Basics--Practical-Embedded-Coding-Tips-Part-1, there is mention on how to make a function reentrant.



long i; 
void do_something(void){
disable_interrupts();
i+=0x1234;
enable_interrupts();
}


Autor tells: "This solution does not work. If do_something() is a generic routine, perhaps called from many places, and is invoked with interrupts disabled, it returns after turning them back on. The machine's context is changed, probably in a very dangerous manner."



I do not understand exactly how changed the machine's context is dangerous? Could somebody give some example where this could lead to harmful consequences to clarify it?










share|improve this question



























    0















    Hello in the article from https://www.embedded.com/design/operating-systems/4008268/2/Back-to-the-Basics--Practical-Embedded-Coding-Tips-Part-1, there is mention on how to make a function reentrant.



    long i; 
    void do_something(void){
    disable_interrupts();
    i+=0x1234;
    enable_interrupts();
    }


    Autor tells: "This solution does not work. If do_something() is a generic routine, perhaps called from many places, and is invoked with interrupts disabled, it returns after turning them back on. The machine's context is changed, probably in a very dangerous manner."



    I do not understand exactly how changed the machine's context is dangerous? Could somebody give some example where this could lead to harmful consequences to clarify it?










    share|improve this question

























      0












      0








      0








      Hello in the article from https://www.embedded.com/design/operating-systems/4008268/2/Back-to-the-Basics--Practical-Embedded-Coding-Tips-Part-1, there is mention on how to make a function reentrant.



      long i; 
      void do_something(void){
      disable_interrupts();
      i+=0x1234;
      enable_interrupts();
      }


      Autor tells: "This solution does not work. If do_something() is a generic routine, perhaps called from many places, and is invoked with interrupts disabled, it returns after turning them back on. The machine's context is changed, probably in a very dangerous manner."



      I do not understand exactly how changed the machine's context is dangerous? Could somebody give some example where this could lead to harmful consequences to clarify it?










      share|improve this question














      Hello in the article from https://www.embedded.com/design/operating-systems/4008268/2/Back-to-the-Basics--Practical-Embedded-Coding-Tips-Part-1, there is mention on how to make a function reentrant.



      long i; 
      void do_something(void){
      disable_interrupts();
      i+=0x1234;
      enable_interrupts();
      }


      Autor tells: "This solution does not work. If do_something() is a generic routine, perhaps called from many places, and is invoked with interrupts disabled, it returns after turning them back on. The machine's context is changed, probably in a very dangerous manner."



      I do not understand exactly how changed the machine's context is dangerous? Could somebody give some example where this could lead to harmful consequences to clarify it?







      interrupt reentrancy disable






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 30 '18 at 12:22









      SimpleThingsSimpleThings

      156




      156
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Note that do_something() can be called both from places where interrupts are enabled, and from places where interrupts are already disabled. Enabling interrupts on the second case goes against the expectations of the caller in a vary dangerous way.



          What you really need is to save the previous state of interrupts while disabling them, and restore it afterwards.



          So, a better version would be:



          long i; 
          void do_something(void){
          irq_state_t prev_int_state = disable_interrupts_save();
          i+=0x1234;
          restore_interrupts(prev_int_state);
          }





          share|improve this answer
























          • So, in other words, if I understood you correctly there could be a scenario where a caller task that calls function do_something disabled interrupts prior to the call of a function, and once a function returns, an unexpected interrupt (caller thinking he already disabled interrupts) and might come and interrupt caller task? So in irq_state_t prev_int_state I would have to save whole Status register/register where interrupt flags are stored? Or additional action is required?

            – SimpleThings
            Dec 30 '18 at 14:13













          • @SimpleThings: yes, correct.

            – ninjalj
            Dec 31 '18 at 11:21











          • Thanks a lot for your help!

            – SimpleThings
            Dec 31 '18 at 13:13











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53977548%2freentrant-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Note that do_something() can be called both from places where interrupts are enabled, and from places where interrupts are already disabled. Enabling interrupts on the second case goes against the expectations of the caller in a vary dangerous way.



          What you really need is to save the previous state of interrupts while disabling them, and restore it afterwards.



          So, a better version would be:



          long i; 
          void do_something(void){
          irq_state_t prev_int_state = disable_interrupts_save();
          i+=0x1234;
          restore_interrupts(prev_int_state);
          }





          share|improve this answer
























          • So, in other words, if I understood you correctly there could be a scenario where a caller task that calls function do_something disabled interrupts prior to the call of a function, and once a function returns, an unexpected interrupt (caller thinking he already disabled interrupts) and might come and interrupt caller task? So in irq_state_t prev_int_state I would have to save whole Status register/register where interrupt flags are stored? Or additional action is required?

            – SimpleThings
            Dec 30 '18 at 14:13













          • @SimpleThings: yes, correct.

            – ninjalj
            Dec 31 '18 at 11:21











          • Thanks a lot for your help!

            – SimpleThings
            Dec 31 '18 at 13:13
















          0














          Note that do_something() can be called both from places where interrupts are enabled, and from places where interrupts are already disabled. Enabling interrupts on the second case goes against the expectations of the caller in a vary dangerous way.



          What you really need is to save the previous state of interrupts while disabling them, and restore it afterwards.



          So, a better version would be:



          long i; 
          void do_something(void){
          irq_state_t prev_int_state = disable_interrupts_save();
          i+=0x1234;
          restore_interrupts(prev_int_state);
          }





          share|improve this answer
























          • So, in other words, if I understood you correctly there could be a scenario where a caller task that calls function do_something disabled interrupts prior to the call of a function, and once a function returns, an unexpected interrupt (caller thinking he already disabled interrupts) and might come and interrupt caller task? So in irq_state_t prev_int_state I would have to save whole Status register/register where interrupt flags are stored? Or additional action is required?

            – SimpleThings
            Dec 30 '18 at 14:13













          • @SimpleThings: yes, correct.

            – ninjalj
            Dec 31 '18 at 11:21











          • Thanks a lot for your help!

            – SimpleThings
            Dec 31 '18 at 13:13














          0












          0








          0







          Note that do_something() can be called both from places where interrupts are enabled, and from places where interrupts are already disabled. Enabling interrupts on the second case goes against the expectations of the caller in a vary dangerous way.



          What you really need is to save the previous state of interrupts while disabling them, and restore it afterwards.



          So, a better version would be:



          long i; 
          void do_something(void){
          irq_state_t prev_int_state = disable_interrupts_save();
          i+=0x1234;
          restore_interrupts(prev_int_state);
          }





          share|improve this answer













          Note that do_something() can be called both from places where interrupts are enabled, and from places where interrupts are already disabled. Enabling interrupts on the second case goes against the expectations of the caller in a vary dangerous way.



          What you really need is to save the previous state of interrupts while disabling them, and restore it afterwards.



          So, a better version would be:



          long i; 
          void do_something(void){
          irq_state_t prev_int_state = disable_interrupts_save();
          i+=0x1234;
          restore_interrupts(prev_int_state);
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 30 '18 at 12:47









          ninjaljninjalj

          35.1k682123




          35.1k682123













          • So, in other words, if I understood you correctly there could be a scenario where a caller task that calls function do_something disabled interrupts prior to the call of a function, and once a function returns, an unexpected interrupt (caller thinking he already disabled interrupts) and might come and interrupt caller task? So in irq_state_t prev_int_state I would have to save whole Status register/register where interrupt flags are stored? Or additional action is required?

            – SimpleThings
            Dec 30 '18 at 14:13













          • @SimpleThings: yes, correct.

            – ninjalj
            Dec 31 '18 at 11:21











          • Thanks a lot for your help!

            – SimpleThings
            Dec 31 '18 at 13:13



















          • So, in other words, if I understood you correctly there could be a scenario where a caller task that calls function do_something disabled interrupts prior to the call of a function, and once a function returns, an unexpected interrupt (caller thinking he already disabled interrupts) and might come and interrupt caller task? So in irq_state_t prev_int_state I would have to save whole Status register/register where interrupt flags are stored? Or additional action is required?

            – SimpleThings
            Dec 30 '18 at 14:13













          • @SimpleThings: yes, correct.

            – ninjalj
            Dec 31 '18 at 11:21











          • Thanks a lot for your help!

            – SimpleThings
            Dec 31 '18 at 13:13

















          So, in other words, if I understood you correctly there could be a scenario where a caller task that calls function do_something disabled interrupts prior to the call of a function, and once a function returns, an unexpected interrupt (caller thinking he already disabled interrupts) and might come and interrupt caller task? So in irq_state_t prev_int_state I would have to save whole Status register/register where interrupt flags are stored? Or additional action is required?

          – SimpleThings
          Dec 30 '18 at 14:13







          So, in other words, if I understood you correctly there could be a scenario where a caller task that calls function do_something disabled interrupts prior to the call of a function, and once a function returns, an unexpected interrupt (caller thinking he already disabled interrupts) and might come and interrupt caller task? So in irq_state_t prev_int_state I would have to save whole Status register/register where interrupt flags are stored? Or additional action is required?

          – SimpleThings
          Dec 30 '18 at 14:13















          @SimpleThings: yes, correct.

          – ninjalj
          Dec 31 '18 at 11:21





          @SimpleThings: yes, correct.

          – ninjalj
          Dec 31 '18 at 11:21













          Thanks a lot for your help!

          – SimpleThings
          Dec 31 '18 at 13:13





          Thanks a lot for your help!

          – SimpleThings
          Dec 31 '18 at 13:13


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53977548%2freentrant-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          gOSrokfx19P
          jZYX9aoNKOVE3bY15 u,ri 8c7 JQ3HIQyO5cIq,Af6ig3we

          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas