On 3/18/2014 7:47 AM, Alex wrote: > On 03/18/2014 01:26 PM, Dimitry Sibiryakov wrote: >> Back to the question: >> >> 17.03.2014 12:30, Alex Peshkoff wrote: >>>> Besides, storing reference to object already increment its >>>>> usage counter, so nobody can free the object during wrapper's life time. >>> But using something like commit for transaction can nullify next pointer. >> next is a reference counting pointer, it cannot be nullified as long as >> Y-object is >> alive. So, what is the point to use YEntry.next() in Y-object methods? >> > Let me talk about attachment - it's using same approach but sample is > more realistic. > > IAttachment* userHandle points to class YAttachment, containing (among > others) reference counter and reference pointer to IAttachment next. > Let's take into an account that on successful detach next is nullified, > and if other references are present (i.e. YAttachment is not destroyed > due to reference counter > 0) any attempt to use it should raise > 'invalid handle' error. > > Let N (where N > 1) threads to work with this attachment (i.e. reference > counter is N). Thread T1 wants to execute statement - it calls > YAttachment::execute, which checks for non-null next pointer and (at > this time T1 is swapped out, T2 calls detach, next is nullified ...) > invokes next function: next->execute(...). And gets segfault. Absolutely > without mutex this is not solvable, but we want to keep it locked for as > small time as possible. Therefore under locked mutex we copy next to > local variable, addRef() to it, unlock mutex (you see it's locked for a > very small timeslice) and later work with this variable. In the sample > above we will invoke next->execute() and get 'invalid handle' message > from next provider. IMHO it's better than segfault. > > Do you really use a mutex to when walking a structure???? Why not a read/write lock? With a mutex, readers block readers, which is a total waste of cycles, thread stalls, thread switches, etc.
------------------------------------------------------------------------------ Learn Graph Databases - Download FREE O'Reilly Book "Graph Databases" is the definitive new guide to graph databases and their applications. Written by three acclaimed leaders in the field, this first edition is now available. Download your free book today! http://p.sf.net/sfu/13534_NeoTech Firebird-Devel mailing list, web interface at https://lists.sourceforge.net/lists/listinfo/firebird-devel