Re: Compiler Support for Ensuring that a Statement is Atomic

Discussion in 'C Programming' started by Mr. C, Jan 20, 2010.

  1. Mr. C

    Mr. C Guest

    On Fri, 15 Jan 2010 19:52:21 -0500, "Datesfat Chicks"
    <> wrote:

    > DI();
    > sempahore++;
    > EI();


    Nobody has mentioned the problem with using the DI/EI pair in code
    segments where interrupts can some times be enabled and other times be
    disabled. The code above will always enable interrupts, even if they
    were not enabled before the DI was executed.
    Mr. C, Jan 20, 2010
    #1
    1. Advertising

  2. Datesfat Chicks wrote:
    > This problem is normally very easy to address. It is normal to examine
    > the interrupt mask and make the decision about whether to enable
    > interrupts again based on whether they were enabled originally.


    Easy, yes. But potentially quite wrong, too. You can't implement a
    semaphore using a sequence of operations (store current interrupt mask,
    then disable interrupt) that itself needs to be protected by one to
    avoid untimely outside interference. That's an exercise in pulling
    yourself out of the pond by yanking on your own hair.

    > im = imask(); //Built-in function supported by the compiler.
    >
    > DI();


    And you'll be SOL if the interrupt mask gets changed by an interrupt
    happening in between those two instructions.
    Hans-Bernhard Bröker, Jan 20, 2010
    #2
    1. Advertising

  3. Mr. C

    Nobody Guest

    On Wed, 20 Jan 2010 09:39:39 -0500, Mr.C wrote:

    >> DI();
    >> sempahore++;
    >> EI();

    >
    > Nobody has mentioned the problem with using the DI/EI pair in code
    > segments where interrupts can some times be enabled and other times be
    > disabled. The code above will always enable interrupts, even if they
    > were not enabled before the DI was executed.


    It's not a problem if you only use DI/EI around very small sections of
    code so that you know that DI/EI will never nest.

    For larger sections of code, you should probably be using mutexes,
    semaphores, RCU, or whatever (which may need to use DI/EI in their
    implementation).
    Nobody, Jan 21, 2010
    #3
  4. On Jan 20, 11:46 pm, "Datesfat Chicks" <>
    wrote:
    > > On Fri, 15 Jan 2010 19:52:21 -0500, "Datesfat Chicks"
    > > <> wrote:
    > >>   DI();
    > >>   sempahore++;
    > >>   EI();

    > > The code above will always enable interrupts, even if they
    > > were not enabled before the DI was executed.


    I almost posted something like the following, but was busy stamping
    out flames in another ng. :)

    > This problem is normally very easy to address.  It is normal to examine the
    > interrupt mask and make the decision about whether to enable interrupts
    > again based on whether they were enabled originally.
    >
    > im = imask(); //Built-in function supported by the compiler.
    > DI();
    > semaphore++;
    > if (!im)
    >    EI();


    Nothing to add, except a True Anecdote(tm). Development of a peculiar
    system(*) was getting behind schedule due to a very intermittent
    bug. I came across the responsible programmer sitting next to a logic
    analyzer reading a comic book or something. "Can't do anything until
    the analyzer triggers again" was his answer. "Problem occurs only
    once every several hours."

    I showed him how to research by writing (the equivalent of)
    assert(imask() == ENABLED);
    before the DI(); and thus make the system "fail" several times a
    second instead of once every several hours. I'm sure he then
    figured out the fix himself.

    (* -- IIRC, it was a processor emulating a disk controller,
    connected to a disk controller emulating a processor, or
    some such. :)

    James Dow Allen
    James Dow Allen, Jan 21, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David
    Replies:
    0
    Views:
    316
    David
    May 2, 2004
  2. Simon
    Replies:
    3
    Views:
    823
    Simon Harvey
    May 26, 2004
  3. Peter Hardy

    Ensuring users have provided valid html

    Peter Hardy, Dec 29, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    322
    Peter Hardy
    Dec 29, 2004
  4. Boudewijn Dijkstra

    Re: Compiler Support for Ensuring that a Statement is Atomic

    Boudewijn Dijkstra, Jan 18, 2010, in forum: C Programming
    Replies:
    2
    Views:
    292
    Boudewijn Dijkstra
    Jan 19, 2010
  5. Charles Oliver Nutter

    [ANN] atomic 0.0.1 - An atomic reference for Ruby

    Charles Oliver Nutter, Jun 8, 2010, in forum: Ruby
    Replies:
    5
    Views:
    229
    Robert Dober
    Jun 8, 2010
Loading...

Share This Page