login
Header Space

 
 

Re: down_spin() implementation

Score:
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Matthew Wilcox <matthew@...>
Cc: Luck, Tony <tony.luck@...>, Stephen Rothwell <sfr@...>, <linux-arch@...>, <linux-mm@...>, <linux-kernel@...>
Date: Thursday, March 27, 2008 - 8:01 pm

On Friday 28 March 2008 01:15, Matthew Wilcox wrote:

Uhm, how do you use this exactly? All other holders of this
semaphore must have preempt disabled and not sleep, right? (and
so you need a new down() that disables preempt too)

So the only difference between this and a spinlock I guess is
that waiters can sleep rather than spin on contention (except
this down_spin guy, which sleeps).

Oh, I see from the context of Tony's message... so this can *only*
be used when preempt is off, and *only* against other down_spin
lockers.

Bad idea to be hack this into the semaphore code, IMO. It would
take how many lines to implement it properly?


struct {
  atomic_t cur;
  int max;
} ss_t;

void spin_init(ss_t *ss, int max)
{
	&ss->cur = ATOMIC_INIT(0);
	&ss->max = max;
}

void spin_take(ss_t *ss)
{
  preempt_disable();
  while (unlikely(!atomic_add_unless(&ss->cur, 1, &ss->max))) {
    while (atomic_read(&ss->cur) == ss->max)
      cpu_relax();
  }
}

void spin_put(ss_t *ss)
{
  smp_mb();
  atomic_dec(&ss->cur);
  preempt_enable();
}

About the same number as down_spin(). And it is much harder to
misuse. So LOC isn't such a great argument for this kind of thing.

My 2c

--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
What if a TLB flush needed to sleep?, Luck, Tony, (Tue Mar 25, 4:49 pm)
Re: What if a TLB flush needed to sleep?, Christoph Lameter, (Wed Mar 26, 3:25 pm)
Re: What if a TLB flush needed to sleep?, Thomas Gleixner, (Wed Mar 26, 4:29 pm)
Re: What if a TLB flush needed to sleep?, Christoph Lameter, (Wed Mar 26, 9:19 pm)
Re: What if a TLB flush needed to sleep?, Peter Zijlstra, (Thu Mar 27, 9:20 am)
Re: What if a TLB flush needed to sleep?, Christoph Lameter, (Thu Mar 27, 2:44 pm)
Re: What if a TLB flush needed to sleep?, Peter Zijlstra, (Fri Mar 28, 5:59 am)
Re: What if a TLB flush needed to sleep?, Matthew Wilcox, (Wed Mar 26, 8:32 am)
RE: What if a TLB flush needed to sleep?, Luck, Tony, (Wed Mar 26, 4:29 pm)
down_spin() implementation, Matthew Wilcox, (Thu Mar 27, 10:15 am)
Re: down_spin() implementation, Jens Axboe, (Fri Mar 28, 8:51 am)
Re: down_spin() implementation, Matthew Wilcox, (Fri Mar 28, 9:17 am)
Re: down_spin() implementation, Jens Axboe, (Fri Mar 28, 9:24 am)
Re: down_spin() implementation, Nick Piggin, (Thu Mar 27, 8:01 pm)
Re: down_spin() implementation, Matthew Wilcox, (Fri Mar 28, 8:45 am)
Re: down_spin() implementation, Nick Piggin, (Fri Mar 28, 9:04 pm)
RE: down_spin() implementation, Luck, Tony, (Fri Mar 28, 5:16 pm)
Re: down_spin() implementation, Arnd Bergmann, (Fri Mar 28, 7:48 pm)
Re: down_spin() implementation, Stephen Rothwell, (Fri Mar 28, 12:51 am)
Re: down_spin() implementation, Nick Piggin, (Fri Mar 28, 1:03 am)
Re: down_spin() implementation, Matthew Wilcox, (Fri Mar 28, 8:46 am)
Re: What if a TLB flush needed to sleep?, Jens Axboe, (Thu Mar 27, 4:09 am)
Re: What if a TLB flush needed to sleep?, Alan Cox, (Tue Mar 25, 5:47 pm)
speck-geostationary