Multithreading considerations

Not all types in Nito.Weakness are threadsafe (e.g., WeakReference and WeakCollection). However, ObjectTracker and ObjectId are fully threadsafe.

Lock contention

  • Multiple threads may attempt to retrieve ObjectIds at the same time. This contention is mitigated within the ObjectTracker by defining two tiers of concurrent lookups: the instance's exact type and its hash code. This reduces multithreaded contention in this scenario to a much smaller scope: the target objects must be of the same type and have the same hash code in order to cause contention.
  • One thread may attempt to retrieve an ObjectId and contend with the GC detection thread if it is traversing the same part of the lookup tree.
  • Multiple threads may register actions for the same ObjectId at the same time.

Race conditions

  • One thread may register an action for an ObjectId whose target is being garbage collected. In this case, a race condition "winner" is determined:
    • If the user thread wins, then the target is kept alive until the action is registered. The target may then be garbage collected as normal, and the registered action will eventually be invoked by the GC detection thread.
    • If the garbage collector wins, then the action is not registered, and is invoked immediately.
  • When an AppDomain begins unloading, no more registered actions are invoked. This is a race condition that normally happens when an application exits.

Race conditions (handled by Nito.Weakness)

Last edited Jul 23, 2010 at 7:35 PM by shammah, version 7

Comments

No comments yet.