lucy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Marvin Humphrey (JIRA)" <>
Subject [jira] Updated: (LUCY-92) Lock-free registry for VTables
Date Fri, 01 Jan 2010 22:07:54 GMT


Marvin Humphrey updated LUCY-92:

    Attachment: vtable_registry_hash_to_lfreg.diff

LockFreeRegistry is a minimalist hash table, purpose built for the needs of
the dynamic subclassing system.  Keys can only be inserted once, and cannot be
removed once inserted.  The insertion method, Register(), returns false if an
insertion fails because a key already exists.  At present, the bucket table is
only allocated once, at startup, which means that performance will degrade if
too many dynamic classes are created.  It's possible to fix this, by
introducing a thread-safe rehashing stage (and keeping around all obsolete
bucket tables), but that is left for later.

Collisions are resolved using one singly linked list per bucket.  Entry nodes
are not ordered within the linked list and new nodes may only be appended by
an atomic compare and swap on the last current node's pointer.  If the CAS
operation fails, the inserter must return to the head of the linked list and
traverse it again, looking for duplicate keys.  The loop continues until
either the CAS succeeds and Register() returns true, or a duplicate key is
found and Register() returns false.  

Using this algorithm we can guarantee that duplicate VTables for the same
class can never be registered:

Thread #1                               Thread #2
======================================= ======================================
Failed to fetch VTable for "Foo"        Failed to fetch VTable for "Foo" 
    from the VTable_registry.                from the VTable_registry.
Create VTable for class "Foo".          Create VTable for class "Foo".
Attempt to register new VTable.         Attempt to register new VTable.
Traverse linked list, no dupes found.   Traverse linked list, no dupes found.
Find slot.                              Find slot.
CAS succeeds, Register() returns true.
                                        CAS fails.
                                        Traverse linked list again; this time,
                                            we find a duplicate key, so
                                            Register() returns false.
                                        Attempt to fetch VTable for "Foo" from
                                            the registry once again.  This
                                            time, the attempt succeeds,
                                            retrieving Thread #1's copy.
                                        Discard duplicate, unreferenced "Foo"
                                            VTable, and use the one fetched
                                            from the registry.

When built with Perl bindings, all LockFreeRegistry objects leak, just like
all VTables leak.  This is done because whenever a Perl thread exits, all
destructors get called during "global destruction" phase for that thread -- so
the destructor gets called for shared objects multiple times.  We could
probably get around this problem with a little work, but since the only usages
for LockFreeRegistry are the VTable_registry itself and testing, there's no

> Lock-free registry for VTables
> ------------------------------
>                 Key: LUCY-92
>                 URL:
>             Project: Lucy
>          Issue Type: Improvement
>          Components: Clownfish, Core - Object
>            Reporter: Marvin Humphrey
>            Assignee: Marvin Humphrey
>         Attachments: lock_free_registry.diff, vtable_registry_hash_to_lfreg.diff
> To make Lucy's OO framework thread safe, we must change VTable_registry,
> currently a Lucy::Object::Hash, over to a new class,
> Lucy::Object::LockFreeRegistry. 

This message is automatically generated by JIRA.
You can reply to this email to add a comment to the issue online.

View raw message