public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: bwatson@kahuna.cag.cpqcorp.net
Cc: marcelo@conectiva.com.br, linux-kernel@vger.kernel.org,
	dhowells@redhat.com, hch@caldera.de
Subject: Re: [PATCH] 2.4.18-pre9, trylock for read/write semaphores
Date: Wed, 13 Feb 2002 08:19:43 +0000	[thread overview]
Message-ID: <26130.1013588383@warthog.cambridge.redhat.com> (raw)
In-Reply-To: Message from bwatson@kahuna.cag.cpqcorp.net  of "Tue, 12 Feb 2002 14:45:00 PST." <200202122257.g1CMv9W05368@kahuna.cag.cpqcorp.net>


Hi Brian,

Having examined your code again, I've got some comments on it:

I think the following would be more elegant:

	static inline int __down_read_trylock(struct rw_semaphore *sem)
	{
		signed long old, new;

		do {
			old = (volatile signed long)sem->count;
			if (old < RWSEM_UNLOCKED_VALUE)
				return 0;
			new = old + RWSEM_ACTIVE_READ_BIAS;
		} while (cmpxchg(&sem->count, old, new) != old);

		return 1;
	}

I'm also not sure that the cast has any effect in the following excerpt from
the above:

	old = (volatile signed long)sem->count;

What you may actually want is:

	static inline int __down_read_trylock(struct rw_semaphore *sem)
	{
		volatile signed long *pcount;
		signed long old, new;

		pcount = &sem->count;
		do {
			old = *pcount;
			if (old < RWSEM_UNLOCKED_VALUE)
				return 0;
			new = old + RWSEM_ACTIVE_READ_BIAS;
		} while (cmpxchg(pcount, old, new) != old);

		return 1;
	}

Or maybe even:

	static inline int __down_read_trylock(struct rw_semaphore *sem)
	{
		__s32 result, tmp;
		__asm__ __volatile__(
			"# beginning __down_read_trylock\n\t"
			"  movl      %0,%1\n\t"
			"1:\n\t"
			"  movl	     %1,%2\n\t"
			"  addl      %3,%2\n\t"
			"  jle	     2f\n\t"
	LOCK_PREFIX	"  cmpxchgl  %2,%0\n\t"
			"  jnz	     1b\n\t"
			"# ending __down_read_trylock\n\t"
			: "+m"(sem->count), "=&a"(result), "+&r"(tmp)
			: "i"(RWSEM_ACTIVE_READ_BIAS)
			: "memory", "cc");
		return result>=0 ? 1 : 0;
	}

Using this inline assembly has three advantages over mixing lots of C into it:

 (1) If the sign/zero flags are set as the result of calculating the new
     value, then the old value was negative, and so the trylock has failed.

 (2) CMPXCHG sets the zero flag on success and clears it on failure (something
     that C can't test with the current inline asm setup).

 (3) CMPXCHG fetches the current value of sem->count into EAX if it fails to
     swap (so we don't need to get it again - hence why "1:" is where it is).

Point (3) could be incorporated into the C version. It may also be desirable
to include a rep;nop in whichever version is used, but I don't think it's
going to spin enough for that.


David

  parent reply	other threads:[~2002-02-13  8:20 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-02-12 22:45 [PATCH] 2.4.18-pre9, trylock for read/write semaphores bwatson
2002-02-12 23:29 ` Alan Cox
2002-02-13  1:47   ` Brian J. Watson
2002-02-13  7:47   ` David Howells
2002-02-13  8:19 ` David Howells [this message]
2002-02-14  0:13   ` Brian J. Watson
  -- strict thread matches above, loose matches on Subject: below --
2002-02-21 18:04 Kendrick M. Smith
2002-02-21 21:54 ` Brian J. Watson
2002-02-21 23:02   ` Kendrick M. Smith
2002-02-21 23:26     ` Brian J. Watson

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=26130.1013588383@warthog.cambridge.redhat.com \
    --to=dhowells@redhat.com \
    --cc=bwatson@kahuna.cag.cpqcorp.net \
    --cc=hch@caldera.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marcelo@conectiva.com.br \
    /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