mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: john moser <bluefoxicy@linux.net>
To: linux-kernel@vger.kernel.org
Subject: spin_lock() and smp/multicall logic
Date: Thu, 8 Jan 2004 16:35:22 -0800 (PST)	[thread overview]
Message-ID: <20040109003522.F1411E4B9@sitemail.everyone.net> (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/

             reply	other threads:[~2004-01-09  0:35 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-09  0:35 john moser [this message]
2004-01-09  4:50 ` Stephen Hemminger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040109003522.F1411E4B9@sitemail.everyone.net \
    --to=bluefoxicy@linux.net \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®