ARM cortex: mutex using bitmap

Given that on an ARM Cortex M3 I can:

  • atomically reads one bit
  • atomically set one bit
  • atomically clear one bit

How can I combine them for a set of mutex-style operations:

try lock
take lock
release lock

It try_lockeither seems to take_lockrequire two operations that would not be atomic.

Do I need more control for this? Disable global interrupts do this, but it seems like there should be a more surgical approach.

+5
source share
4 answers

rwl_TryLock() , ( , ). :

int rwl_TryLock(volatile uint32_t *lock, int who){

    Var_SetBit_BB((uint32_t)lock, who);
    if(*lock == (1<<who)){ // check that we have exclusive access
        // got the lock!
        return 1;
    } 

    // do not have the lock
    Var_ResetBit_BB((uint32_t)lock, who); // clear the lock flag
    return 0;
}

, (.. , who == 1, , ), .

, / Cortex M3 ( NVIC). , , ( , )?

+4

- . . Load Exclusive Store Exclusive /. , , , , , .

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0439b/CHDDIGAC.html

, -...

+2

:

" ARM Cortex-M3 - ARM microcontroller . - -- - . ? - . "" . , , , 1 -. ( -) , , . , , (, ). "( )

() :

/*
 * Frees a lock.
 *
 * @note lock must point to a fully aligned 32 bit integer.
 * (atomically set to 0)
 *
 * @returns 1 if successfull
 */
int rwl_FreeLock(volatile uint32_t *lock){
    *lock = 0;
    return 1; // always successful
}

/*
 * Attempts to acquire a lock
 * @param who is the client taking the lock
 * @lock pointer to the mutex (uint32_t value in memory)
 * @note lock must point to a fully aligned 32 bit integer.
 * (atomically set to 1 only if set to 0)
 */
int rwl_TryLock(volatile uint32_t *lock, int who){
    // initial check of lock
    if(*lock == 0){
        Var_SetBit_BB((uint32_t)lock, who);
        if(*lock == (1<<who)){ // check that we still have exclusive access
            // got the lock!
            return 1;
        } else {
                    // do not have the lock
            Var_ResetBit_BB((uint32_t)lock, who); // clear the lock flag
            return 0;
        }
    }
}

Var_Set_BB/Var_Reset_BB: / , -. ()

!!!

+1

-banding ARM; load-exclusive/store-conditional . - , . , (, , , ).

0

All Articles