public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: Stefan Richter <stefanr@s5r6.in-berlin.de>
Cc: dhowells@redhat.com, torvalds@osdl.org,
	akpm@linux-foundation.org, sgruszka@redhat.com,
	davem@davemloft.net, linux-kernel@vger.kernel.org,
	"Paul E. McKenney" <paulmck@linux.vnet.ibm.com>,
	Randy Dunlap <rdunlap@xenotime.net>
Subject: Re: [PATCH] Document Linux's circular buffering capabilities
Date: Fri, 12 Mar 2010 23:41:08 +0000	[thread overview]
Message-ID: <18180.1268437268@redhat.com> (raw)
In-Reply-To: <4B9A6033.8010705@s5r6.in-berlin.de>

Stefan Richter <stefanr@s5r6.in-berlin.de> wrote:

> "...do this" could be misunderstood as "use a modulus instruction",
> although the heading says that this section is about 2^n sized buffers.
> How about reversing the leading paragraph?
> 
>     Calculating the occupied or free space of a circular buffer involves
>     a somewhat slow modulus operation.  But if the buffer size is an
>     exact power of 2, a quick bitwise AND can be used instead.
> 
>     There is a set of macros which do the latter, that can be made use
>     of by [...]

How about:

	Calculation of the occupancy or the remaining capacity of an
	arbitrarily sized circular buffer would normally be a slow operation,
	requiring the use of a modulus (divide) instruction.  However, if the
	buffer is of a power-of-2 size, then a much quicker bitwise-AND
	instruction can be used instead.

	Linux provides a set of macros for handling power-of-2 circular
	buffers.  These can be made use of by:
	...

> > +Note the use of ACCESS_ONCE() in both algorithms to read the opposition index.
> > +This prevents the compiler from discarding and reloading its cached value -
> > +which some compilers will do, even after an implied compiler barrier.
> 
> I don't understand why ACCESS_ONCE is needed here.  The CIRC_SPACE and
> CIRC_CNT macros do not look at head and tail more than once.

In this example they don't, but say someone wants to read several elements
from the buffer, they might end up accessing their copy of head several times.

If you only access it once anyway, ACCESS_ONCE() shouldn't hurt.

David

  reply	other threads:[~2010-03-12 23:41 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-11 17:20 [PATCH] Document Linux's circular buffering capabilities David Howells
2010-03-11 18:10 ` Randy Dunlap
2010-03-11 19:20   ` David Howells
2010-03-12 15:39 ` Stefan Richter
2010-03-12 23:41   ` David Howells [this message]
2010-03-13  9:26     ` Stefan Richter
2010-03-15 12:36       ` David Howells
2010-03-15 13:43         ` Stefan Richter
2010-03-15 13:55           ` David Howells
2010-03-15 14:16             ` Stefan Richter
2010-03-19 17:53           ` Mark Rustad

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=18180.1268437268@redhat.com \
    --to=dhowells@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=davem@davemloft.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=rdunlap@xenotime.net \
    --cc=sgruszka@redhat.com \
    --cc=stefanr@s5r6.in-berlin.de \
    --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