From: Matthew Wilcox <matthew@wil.cx>
To: Nicolas Pitre <nico@cam.org>, Linus Torvalds <torvalds@osdl.org>,
David Howells <dhowells@redhat.com>,
Steven Rostedt <rostedt@goodmis.org>,
linux-arch@vger.kernel.org, lkml <linux-kernel@vger.kernel.org>,
mingo@redhat.com, Andrew Morton <akpm@osdl.org>
Subject: Re: [PATCH 1/12]: MUTEX: Implement mutexes
Date: Mon, 19 Dec 2005 06:54:53 -0700 [thread overview]
Message-ID: <20051219135453.GQ2361@parisc-linux.org> (raw)
In-Reply-To: <20051219092743.GA9609@flint.arm.linux.org.uk>
On Mon, Dec 19, 2005 at 09:27:44AM +0000, Russell King wrote:
> > mov r0, #1
> > swp r1, r0, [%0]
> > cmp r1, #0
> > bne __contention
> That's over-simplified, and is the easy bit. Now work out how you handle
> the unlock operation.
>
> You don't know whether the lock is contended or not in the unlock path,
> so you always have to do the "wake up" thing. (You can't rely on the
> value of the lock since another thread may well be between this swp
> instruction and entering the __contention function. Hence you can't
> use the value of the lock to determine whether there's anyone sleeping
> on it.)
Here's a slightly less efficient way to determine if anyone else has
swp'd behind your back (apologies if I get my ARM assembly wrong, it's
been a few years):
swp r1, r13, [%0] # load r1 from addr and store r13 there
cmp r1, #0 # is r1 0?
streq r13, [%0, #4] # if it is, store a copy of r13 at the
# next address
blne __lock_contention
I'm assuming that r13 (the stack pointer) will be different for each
task, and (with this being a mutex), we won't try to double-acquire it.
Unlock is then:
mov r0, #0 # put 0 in r0
swp r1, r0, [%0] # release the lock
ldr r0, [%0, #4] # load the copy
cmp r0, r1 # did it change?
blne __unlock_contention
In __unlock_contention, thread1 would try to re-acquire the mutex (at
[%0]), and if it does, wake up the first waiter. Obviously it's unfair
as thread3 could come along and grab the mutex, but that's not a huge
deal.
Note that we're not checking the value of r13 at the unlock site as lock
and unlock can be done at different stack levels. We're checking to see
if the value last written to the lock was the one by the successful
acquirer.
next prev parent reply other threads:[~2005-12-19 13:54 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-12-16 23:13 [PATCH 0/12]: MUTEX: Introduce mutex implementation David Howells
2005-12-16 23:13 ` [PATCH 1/12]: MUTEX: Implement mutexes David Howells
2005-12-17 3:58 ` Steven Rostedt
2005-12-17 7:35 ` Linus Torvalds
2005-12-17 19:21 ` David Howells
2005-12-17 20:11 ` Linus Torvalds
2005-12-17 21:44 ` Russell King
2005-12-18 1:29 ` Nicolas Pitre
2005-12-18 2:34 ` Linus Torvalds
2005-12-18 4:07 ` Nicolas Pitre
2005-12-18 4:18 ` Steven Rostedt
2005-12-18 6:30 ` Linus Torvalds
2005-12-18 9:26 ` Russell King
2005-12-18 18:42 ` Linus Torvalds
2005-12-18 19:41 ` James Bottomley
2005-12-18 19:54 ` Linus Torvalds
2005-12-19 1:48 ` Nicolas Pitre
2005-12-19 9:27 ` Russell King
2005-12-19 13:54 ` Matthew Wilcox [this message]
2005-12-19 15:49 ` Nicolas Pitre
2005-12-19 15:45 ` Nicolas Pitre
2005-12-18 17:29 ` Nicolas Pitre
2005-12-18 13:38 ` Alan Cox
2005-12-18 17:21 ` Nicolas Pitre
2005-12-17 7:55 ` Nick Piggin
2005-12-17 12:36 ` Steven Rostedt
2005-12-16 23:13 ` [PATCH 2/12]: MUTEX: Provide SWAP-based mutex for FRV David Howells
2005-12-16 23:13 ` [PATCH 3/12]: MUTEX: Rename DECLARE_MUTEX for arch/ dir David Howells
2005-12-16 23:13 ` [PATCH 8/12]: MUTEX: Rename DECLARE_MUTEX for kernel/ dir David Howells
2005-12-16 23:13 ` [PATCH 6/12]: MUTEX: Rename DECLARE_MUTEX for fs/ dir David Howells
2005-12-16 23:13 ` [PATCH 10/12]: MUTEX: Rename DECLARE_MUTEX for sound/ dir David Howells
2005-12-16 23:13 ` [PATCH 7/12]: MUTEX: Rename DECLARE_MUTEX for include/asm-*/ dirs David Howells
2005-12-16 23:13 ` [PATCH 4/12]: MUTEX: Rename DECLARE_MUTEX for drivers/ dir, A thru M David Howells
2005-12-16 23:13 ` [PATCH 11/12]: MUTEX: Rename DECLARE_MUTEX for miscellaneous directories David Howells
2005-12-16 23:13 ` [PATCH 5/12]: MUTEX: Rename DECLARE_MUTEX for drivers/ dir, N thru Z David Howells
2005-12-16 23:13 ` [PATCH 9/12]: MUTEX: Rename DECLARE_MUTEX for net/ dir David Howells
2005-12-16 23:13 ` [PATCH 12/12]: MUTEX: Provide synchronisation primitive testing module David Howells
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=20051219135453.GQ2361@parisc-linux.org \
--to=matthew@wil.cx \
--cc=akpm@osdl.org \
--cc=dhowells@redhat.com \
--cc=linux-arch@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=nico@cam.org \
--cc=rostedt@goodmis.org \
--cc=torvalds@osdl.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