SafeTypes2
|
The garbage collector synchronization mechanism for multi-threaded applications is entirely re-written based on more rigorous and formal design and reasoning.
The ultimate goal is to ensure garbage collection won't interfere with application use of SafeTypes2 objects. The extended goals are as follow:
To this end, we need to first track the number of threads holding the shared "reader" lock, hence "thread" lock, thus the thr_count
variable. Secondly, since multiple threads can enter GC, we need a counter for that as well, thus the gc_waiting
variable. Thirdly, since some threads requesting GC aren't necessarily the one(s) holding the thread lock, we need a third variable to keep a count, thus we need another gc_pending
variable.
Because threads lock are shared (i.e. not exclusive), they only have to atomically change the thr_count
counter; on the other hand, GC lock must block the calling threads until all other locks are released - this includes:
To this end, the GC synchronization mechanism have several states:
Synchronization Object State Variables:
stateguard
- the current state of the synchronization object.thr_count
- records the number of threads holding the "reader" lock.gc_waiting
- the number of threads blocked waiting for GC operation.gc_pending
- the number of threads holding the threads lock that're blocked waiting for GC operation.threc
- a thread-local variable initialized to 0, introduced to support lock recursion for the threads lock.For ease of presentation, it is assumed that during the execution of the following procedures, no other threads will access the state variables unless during the window of waiting, which will be explicitly indicated.
BEGIN - To Aqcuire a Threads Lock:
stateguard
becomes the 'initial state'.threc
is 0, increment thr_count
.threc
. ENDBEGIN - To Release a Threads Lock:
stateguard
becomes 'initial state' or 'GC entry state'.threc
.threc
is 0, decrement thr_count
. ENDBEGIN - To Acquire a GC Lock:
stateguard
becomes 'initial state' or 'GC entry state'.stateguard
to 'GC entry state'.gc_waiting
.threc
is greater than 0, then this thread is holding a threads lock, so increment gc_pending
.gc_pending
reaches thr_count
.stateguard
is 'GC operation state', then {return}.stateguard
to 'GC operation state'. ENDBEGIN - To Release a GC Lock:
stateguard
becomes 'GC operation state' or 'GC finish state'.stateguard
to 'GC finish state'.gc_waiting
.gc_waiting
reaches 0.threc
is greater than 0, then this thread is holding a threads lock, so decrement gc_pending
. (Note that this step must come after waiting for gc_waiting
to reach 0, to ensure the wait for gc_pending
to reach thr_count
in GC lock acquisition don't fail spuriously.)stateguard
to 'initial state'. END