Skip to contentSkip to conversation

spin_lock() and smp/multicall logic

DISCUSSION john moser 2 participants 2 messages Jan 9, 2004

lore
Share & tools
lore

john moser

author
#0 lore raw

As always, CC all replies back to me.

I'm looking at include/linux/spinlock.h and I'm incredibly confused on something.

The logic I've come up with to make a function that is 100% impossible to execute twice at once, even if it gets called on 2 processors in perfect parallel, is:

int run_me_once_at_once() {
static int skip_out = 0; /*0 the first time*/
int retval;
start_check:
if (skip_out)

    return -EBUSY; /*busy*/

skip_out++;
if (skip_out != 1) { /*okay, somehow we must be in perfec parallel*/
retval = -EBUSY;/*so undo OUR skip_out++ and return the busy signal*/ goto out;
}

retval = 0; /*success!*/
out:
skip_out--;
return retval;
}

In basic terms, if there's an infinite number of processes executing this in perfect parallel, then in the worst case every one of them will realize this and return an -EBUSY error. If one is far enough behind all of the others that they all get to their respective skip_out-- calls before it gets its relavent check, then that ONE will execute.

Now, if you want to wait until the function is free instead of returning an erorr, then you will need to decriment your skip_out and goto start_check, then wait. Here would be my logic for that:

int run_me_once_at_once() {
static int skip_out = 0; /*0 the first time*/
int retval;
int rwait = 1; /*hold-down timer count*/
start_check:
do { /*hold-down timer*/
} while(--rwait);

  while (skip_out) { /*busy, just idle for a while.*/
  }

skip_out++; /*okay, try now*/
if (skip_out != 1) { /*okay, somehow we must be in perfec parallel*/

    skip_out--; /*so undo OUR skip_out++*/
    rwait = get_random_int(); /*random hold-down timer to break parallelism*/
    goto start_check; /*and go wait and try again*/

}

retval = 0; /*success!*/
out:
skip_out--;
return retval;
}

I don't understand anything about SMP or spin_lock(), but for some applications at least, you can protect a function from parallel calls like this.

SO the two questions I'd like to address here are:

1) What is the purpose of spin_lock()
2) Am I the first person to come up with this method of non-parallel execution guarentee?

just wondering.

_____________________________________________________________
Linux.Net -->Open Source to everyone
Powered by Linare Corporation
http://www.linare.com/

#1 lore raw
Quoted 3 lines · john moser

As always, CC all replies back to me.

I'm looking at include/linux/spinlock.h and I'm incredibly confused on something.

Buggy implementation of run_once deleted

Quoted 5 lines

SO the two questions I'd like to address here are:

1) What is the purpose of spin_lock()
2) Am I the first person to come up with this method of non-parallel execution guarentee?

You invented an unsafe version of monitor using spin-locks. Go read a on SMP operating systems.

"UNIX(R) Systems for Modern Architectures: Symmetric Multiprocessing and Caching for Kernel Programmers"