public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Oleg Nesterov <oleg@tv-sign.ru>
To: Ingo Molnar <mingo@elte.hu>
Cc: lkml <linux-kernel@vger.kernel.org>,
	Linus Torvalds <torvalds@osdl.org>, Andrew Morton <akpm@osdl.org>,
	Arjan van de Ven <arjanv@infradead.org>,
	Nicolas Pitre <nico@cam.org>,
	Jes Sorensen <jes@trained-monkey.org>,
	Zwane Mwaikambo <zwane@arm.linux.org.uk>,
	David Howells <dhowells@redhat.com>,
	Alan Cox <alan@lxorguk.ukuu.org.uk>,
	Benjamin LaHaise <bcrl@kvack.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Christoph Hellwig <hch@infradead.org>, Andi Kleen <ak@suse.de>,
	Russell King <rmk+lkml@arm.linux.org.uk>
Subject: Re: [patch 5/9] mutex subsystem, core
Date: Thu, 22 Dec 2005 16:38:55 +0300	[thread overview]
Message-ID: <43AAAC6F.17CC646@tv-sign.ru> (raw)
In-Reply-To: 20051222114233.GF18878@elte.hu

Ingo Molnar wrote:
>
> +__mutex_lock_common(struct mutex *lock, struct mutex_waiter *waiter,
> +		    struct thread_info *ti,
> +		    unsigned long task_state __IP_DECL__)
> +{
> +	struct task_struct *task = ti->task;
> +	unsigned int old_val;
> +
> +	/*
> +	 * Lets try to take the lock again - this is needed even if
> +	 * we get here for the first time (shortly after failing to
> +	 * acquire the lock), to make sure that we get a wakeup once
> +	 * it's unlocked. Later on this is the operation that gives
> +	 * us the lock. If there are other waiters we need to xchg it
> +	 * to -1, so that when we release the lock, we properly wake
> +	 * up the other waiters:
> +	 */
> +	old_val = atomic_xchg(&lock->count, -1);
> +
> +	if (unlikely(old_val == 1)) {
> +		/*
> +		 * Got the lock - rejoice! But there's one small
> +		 * detail to fix up: above we have set the lock to -1,
> +		 * unconditionally. But what if there are no waiters?
> +		 * While it would work with -1 too, 0 is a better value
> +		 * in that case, because we wont hit the slowpath when
> +		 * we release the lock. We can simply use atomic_set()
> +		 * for this, because we are the owners of the lock now,
> +		 * and are still holding the wait_lock:
> +		 */
> +		if (likely(list_empty(&lock->wait_list)))
> +			atomic_set(&lock->count, 0);

This is a minor issue, but still I think it makes sense to optimize
for uncontended case:

	old_val = atomic_xchg(&lock->count, 0); // no sleepers

	if (old_val == 1) {
		// sleepers ?
		if (!list_empty(&lock->wait_list))
			// need to wakeup them
			atomic_set(&lock->count, -1);
		...
	}

Oleg.

  parent reply	other threads:[~2005-12-22 12:23 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-22 11:42 [patch 5/9] mutex subsystem, core Ingo Molnar
2005-12-22 11:57 ` Christoph Hellwig
2005-12-22 12:52   ` Ingo Molnar
2005-12-22 13:38 ` Oleg Nesterov [this message]
2005-12-22 14:46   ` Ingo Molnar
2005-12-22 16:12     ` Oleg Nesterov

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=43AAAC6F.17CC646@tv-sign.ru \
    --to=oleg@tv-sign.ru \
    --cc=ak@suse.de \
    --cc=akpm@osdl.org \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=arjanv@infradead.org \
    --cc=bcrl@kvack.org \
    --cc=dhowells@redhat.com \
    --cc=hch@infradead.org \
    --cc=jes@trained-monkey.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=nico@cam.org \
    --cc=rmk+lkml@arm.linux.org.uk \
    --cc=rostedt@goodmis.org \
    --cc=torvalds@osdl.org \
    --cc=zwane@arm.linux.org.uk \
    /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