Re: Thread safe circular buffer.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Mar 07, 2007 at 03:18:08PM +0200, Tzahi Fadida wrote:
> Hi,
> I am reading ldd3 and i am trying to understand how a circular buffer without
> locking works.
> If there is one writer and multiple readers. The writer has a pointer to head.
> the readers each have a pointer of their own to tail. Assumming the head
> can't circle on the tail in time, everything is peachy.
> However, in ldd3 the readers are sharing the tail pointer, how can this be
> alright? (See the code in ldd3->interrupt handling->Implementing a handler)
> At the very least you should somehow atomically grab the tail into a local var
> and also atomically increase the tail position all in one go so you won't get
> a race condition.
> Either that, or i did not understand the code.
> 10x.

You may want to take a look at include/linux/kfifo.h and kernel/kfifo.c.  This
appears to be a generic circular buffer implementation CMIIW.  It would
probably be better to use this (presumably) well established code than to
write your own.

Also, the comments in the kfifo.c file seem to imply that if there is only one
concurrent reader and one concurrent writer no extra locking is needed.  This,
to me, implies that if there are multiple readers and/or writers you will need
more locking.  The kfifo.h file includes functions to do locking for you if
you need it.

	Hope this helps,
		--Ethan

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux