The HyperNews Linux KHG Discussion Pages

## using cli/sti() and save_flags/restore_flags()

  Forum: The Linux Kernel Hackers' Guide
  Date: Wed, 30 Jul 1997 21:43:57 GMT
  From: george <georgel@cs.ucla.edu>

snippets of kernel code look like the following
      save_flags(flags);
      cli();
      ...
      restore_flags(flags);
does restore_flags() somehow magically call sti()? the answer: yes. why/how? remember that interrupt enable is _also_ a flag. thus restoring it would take care of everything. be careful _not_ to simply call sti(). sti() would enable interrupts, even if they were disabled to start with. in this case, you have just messed things up, as the example below illustrates
    foo(){
      cli();
      ...
      sti();
    }
    bar(){
      cli();
      foo();  /* must not mess with cli() setting */
      this_must_have_ints_off();
      sti();
      foo();
    }
this was in response to a query i had about their use and there wasn't anything in the khg about this. hence, i am adding this knowledge so others may know.