Best sync method/primitive for array of reference counters
Posted: Sat Aug 06, 2022 2:23 pm
Hello,
I'm working on a program that will utilize a sort-of garbage collection mechanism, tracked by a single array of reference counters that can be read and/or modified by multiple threads (and/or processes via shared memory). I've been researching thread/process synchronization methods to try to figure out the quickest way to atomically update the reference counters - the only operations would be to increment an element's counter (one of the counters in the array that is), or decrement it, but after decrementing, a check is made to see if the count is 0, in which case a routine is run to free the element of that reference counter. That latter part is what I'm getting hung on up - the check needs to be atomic as well to ensure multiple threads don't end up double-freeing (or leaking) the object. But the usual methods (POSIX mutexes and semaphores) seem to involve a lot of overhead, so I've also been trying to learn about transactional memory, atomic types, and even assembly methods (lock cmpxchng, though I haven't figured out how that could work). I can't help but think it could be done in userspace with minimal overhead. Would anyone have any advice?
Thank you!
I'm working on a program that will utilize a sort-of garbage collection mechanism, tracked by a single array of reference counters that can be read and/or modified by multiple threads (and/or processes via shared memory). I've been researching thread/process synchronization methods to try to figure out the quickest way to atomically update the reference counters - the only operations would be to increment an element's counter (one of the counters in the array that is), or decrement it, but after decrementing, a check is made to see if the count is 0, in which case a routine is run to free the element of that reference counter. That latter part is what I'm getting hung on up - the check needs to be atomic as well to ensure multiple threads don't end up double-freeing (or leaking) the object. But the usual methods (POSIX mutexes and semaphores) seem to involve a lot of overhead, so I've also been trying to learn about transactional memory, atomic types, and even assembly methods (lock cmpxchng, though I haven't figured out how that could work). I can't help but think it could be done in userspace with minimal overhead. Would anyone have any advice?
Thank you!