Regarding mmap synchronization.
dhylands at gmail.com
Sun Sep 18 14:49:29 EDT 2011
On Sun, Sep 18, 2011 at 11:02 AM, mindentropy <mindentropy at gmail.com> wrote:
> I have mmaped a circular queue buffer created in the kernel. Now I want to
> mmap the read and write pointers in the queue but I am not sure how to
> synchronize the access of the pointers between the kernel and userspace(while
> checking sizes for overflow and underflow). How should I go about doing this?
The way I normally deal with this is to use 2 indicies, a get index
and a put index. One of the indicies if only ever written by kernel
space, and the other is only ever written by user space.
Normally, I would arrange the get and put pointers to be in uncached
memory. If they're in cached memory, I would ensure that they're on
different cache lines.
You make the circular buffer be a power of 2 in size, and you
determine the number of items in the queue by subtracting the get
index from the put index. To retrieve the items from the queue, you
apply a mask.
Lets say you're using 32-bit indicies and you have a max of 512 items
in the queue. You would mask the index with ~0x1FF in order to
determine the real index value.
If the items in the circular buffer are in cached memory, then I
normally try to make each item be an exact multiple of the cache line
size. I find using uncached memory is generally better for this type
of thing (the accesses are slower, but may be faster after accounting
for the cache management).
If you want to communicate in both directions, then you create a
separate queue for each direction.
Shuswap, BC, Canada
More information about the Kernelnewbies