Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a Mutex Lock in C

Tags:

c

assembly

mutex

I'm trying to make a really simple spinlock mutex in C and for some reason I'm getting cases where two threads are getting the lock at the same time, which shouldn't be possible. It's running on a multiprocessor system which may be why there's a problem. Any ideas why it's not working?

void mutexLock(mutex_t *mutexlock, pid_t owner)
{
int failure = 1;
while(mutexlock->mx_state == 0 || failure || mutexlock->mx_owner != owner)
{
    failure = 1;
    if (mutexlock->mx_state == 0)
    {
        asm(
        "movl    $0x01,%%eax\n\t"      // move 1 to eax
        "xchg    %%eax,%0\n\t"         // try to set the lock bit
        "mov     %%eax,%1\n\t"         // export our result to a test var
        :"=r"(mutexlock->mx_state),"=r"(failure)
        :"r"(mutexlock->mx_state)
        :"%eax"
        );
    }
    if (failure == 0)
    {
        mutexlock->mx_owner = owner; //test to see if we got the lock bit
    }
    } 
}
like image 527
Adam Avatar asked Apr 21 '10 01:04

Adam


1 Answers

Well for a start you're testing an uninitialised variable (failure) the first time the while() condition is executed.

Your actual problem is that you're telling gcc to use a register for mx_state - which clearly won't work for a spinlock. Try:

    asm volatile (
    "movl    $0x01,%%eax\n\t"      // move 1 to eax
    "xchg    %%eax,%0\n\t"         // try to set the lock bit
    "mov     %%eax,%1\n\t"         // export our result to a test var
    :"=m"(mutexlock->mx_state),"=r"(failure)
    :"m"(mutexlock->mx_state)
    :"%eax"
    );

Note that asm volatile is also important here, to ensure that it doesn't get hoisted out of your while loop.

like image 61
caf Avatar answered Sep 27 '22 23:09

caf