lua-users home
lua-l archive

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



> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of Peng Zhicheng
> Sent: dinsdag 8 april 2014 2:34
> To: lua-l@lists.lua.org
> Subject: Re: Algorithm help: passing strings between threads with minimal
> copying
> 
> On 04/06/2014 08:38 PM, Ross Bencina wrote:
> > Hi Rena,
> >
> >> I wondered if anyone else had any feedback on this.
> >
> > I like using atomic reference counting for this kind of thing.
> >
> > Assuming that the string buffers are immutable, here's an idea:
> >
> > Arrange for your allocator to allocate these string buffers specially. Lua
> state A will still think that they are normal memory blocks, but under the
> hood you will have made room for an extra reference count that's modified
> using atomic increment/decrement.

How would you distinguish a string allocation from some other allocation?

> >
> > State A gets a 1 count to the buffer when it allocates the block. That
> will be decremented when the GC frees the memory.
> >
> > When you send the string to State B, increment the count. When you've
> finished with the buffer in state B, decrement the count. When the count
> drops to zero (in whichever thread), free the buffer.
> >
> > Make sure you use an appropriate library/api for doing the atomic
> inc/decs.
> >
> > Ross.
> >
> I like this solution. Although the allocation is complicated (not that much
> I suppose),
> you keep the Lua side natual and clean.
> 
> I actually had thought of using ref count as well, but with something
> wrapped in userdata;
> not a good solution of course.