Atomic spinlock

This is a discussion on Atomic spinlock within the C Programming forums, part of the General Programming Boards category; What is the best way to implement this atomically? I have access to a macro that is atomic (ATOMIC_BLOCK). Code: ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    399

    Atomic spinlock

    What is the best way to implement this atomically? I have access to a macro that is atomic (ATOMIC_BLOCK).

    Code:
    while (busy)
        ;
    
    ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
    {
    /* Do something. */
    }
    My main concern is that the value of busy gets changed by an interrupt after I have jumped out of the while loop but before I have entered the atomic block. I guess I could test it again within the atomic block, but I'm not sure if I like that solution or not.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Without any information about the system it is hard to say.
    Is the value of the variable busy available to all processes?
    Set busy after the loop so others can't use the resource that busy controls.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    It can be changed by interrupt routines at any time that interrupts are enabled (and they are enabled here unless you're in an atomic block).

    busy controls access to other resources that are shared.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    So ISRs have higher priority unless the program is in the atomic block; one option is to loop forever and if (busy == 0) <enter atomic block>, as in
    Code:
    while (1)
        if (!busy) {
            <begin atomic block>
            . . .
            <end atomic block>
            <exit loop>
        }
    Last edited by itCbitC; 03-20-2010 at 03:07 PM.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    But busy could still change after the if test but before you have entered the atomic block.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by Memloop View Post
    But busy could still change after the if test but before you have entered the atomic block.
    Yep! you're right and I realized that soon as I hit <submit>.
    So loop forever and see if busy is clear or set, as in
    Code:
    while (1) {
        <begin atomic block>
            if (!busy) 
                <do something>
        <end atomic block>
    }

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    Constantly disabling interrupts isn't too friendly.

    Assuming reads and writes to "busy" are sequentially consistent and safe with respect to interrupts (or threads) on this platform:
    Code:
    while (!done)
    {
        while (busy)
            ;
    
        ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
        {
            if (!busy)
            {
                /* Do something. */
                done = true;
            }
        }
    }
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Atomic compare and swap.
    By Maz in forum C Programming
    Replies: 3
    Last Post: 04-07-2009, 02:47 PM
  2. Atomic operations
    By MarkZWEERS in forum C++ Programming
    Replies: 3
    Last Post: 02-07-2009, 04:21 PM
  3. Atomic integers & memcpy()
    By rasta_freak in forum C Programming
    Replies: 11
    Last Post: 08-05-2008, 12:08 PM
  4. atomic operation of reference assignment?
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-27-2008, 08:14 AM
  5. Atomic Operations
    By Elysia in forum Windows Programming
    Replies: 27
    Last Post: 03-27-2008, 02:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21