lua-users home
lua-l archive

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


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.

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.