spin_lock() and smp/multicall logic

From: john moser
Date: Thu Jan 08 2004 - 19:38:05 EST


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/
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/