linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* spin_lock() and smp/multicall logic
@ 2004-01-09  0:35 john moser
  2004-01-09  4:50 ` Stephen Hemminger
  0 siblings, 1 reply; 2+ messages in thread
From: john moser @ 2004-01-09  0:35 UTC (permalink / raw)
  To: linux-kernel

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/

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: spin_lock() and smp/multicall logic
  2004-01-09  0:35 spin_lock() and smp/multicall logic john moser
@ 2004-01-09  4:50 ` Stephen Hemminger
  0 siblings, 0 replies; 2+ messages in thread
From: Stephen Hemminger @ 2004-01-09  4:50 UTC (permalink / raw)
  To: john moser; +Cc: linux-kernel

john moser wrote:

> 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

> 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"





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-01-09  4:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-01-09  0:35 spin_lock() and smp/multicall logic john moser
2004-01-09  4:50 ` Stephen Hemminger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).