Proper maintenance and care of multi-threading locks¶
The following strategies are used to ensure that the code is dead-lock free (generally by addressing the 4th Coffman condition: circular wait).
- structure code such that only one lock will need to be acquired at a time
- always acquire shared locks in the same order, as given by the table below
- avoid constructs that expect to need unrestricted recursion
Below are all of the locks that exist in the system and the mechanisms for using them that avoid the potential for deadlocks (no Ostrich algorithm allowed here):
The following are definitely leaf locks (level 1), and must not try to acquire any other lock:
Note that this lock is acquired implicitly by
JL_UNLOCK. use the
_NOGCvariants to avoid that for level 1 locks.
While holding this lock, the code must not do any allocation or hit any safepoints. Note that there are safepoints when doing allocation, enabling / disabling GC, entering / restoring exception frames, and taking / releasing locks.
flisp itself is already threadsafe, this lock only protects the
The following is a leaf lock (level 2), and only acquires level 1 locks (safepoint) internally:
The following is a level 3 lock, which can only acquire level 1 or level 2 locks internally:
The following is a level 4 lock, which can only recurse to acquire level 1, 2, or 3 locks:
No Julia code may be called while holding a lock above this point.
The following is a level 6 lock, which can only recurse to acquire locks at lower levels:
The following is an almost root lock (level end-1), meaning only the root look may be held when trying to acquire it:
this one is perhaps one of the most tricky ones, since type-inference can be invoked from many points
currently the lock is merged with the codegen lock, since they call each other recursively
The following is the root lock, meaning no other lock shall be held when trying to acquire it:
this should be held while attempting a top-level action (such as making a new type or defining a new method): trying to obtain this lock inside a staged function will cause a deadlock condition!
additionally, it’s unclear if any code can safely run in parallel with an arbitrary toplevel expression, so it may require all threads to get to a safepoint first
The following locks are broken:
doesn’t exist right now
fix: create it