-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert free_at_safepoint from a vector to a linked list and eliminate its associated mutex #1649
base: main
Are you sure you want to change the base?
Conversation
They were split in two places in moar.h - now they are in one place. Include the file at the earlier point in moar.h - this effectively moves the definition of various macros ahead of most headers, which is needed to implement the next commit.
Re-order `MVM_6model_set_debug_name` to move code before or after the critical section where `mutex_free_at_safepoint` is locked. In particular, this moves `MVM_string_utf8_encode_C_string` outside of the critical section, which is arguably a fix for a subtle bug, as that function has code paths that could throw an exception.
`MVM_6model_set_debug_name` changes the pointer stored at `STABLE(type)->debug_name`. Instead of doing the pointer in a critical section while holding a mutex, we use an atomic Compare And Swap to exchange the pointer, and explicitly handle the two possibilities (swap successful, swap failed).
My subsequent changes assume that the pointer generated by the expression
`STABLE(type)` can't change (outside of the GC) - is it correct that
`st` is never reassigned? You can view, comment on, or merge this pull
Unfortunately it isn't. Rebless changes the STABLE:
https://github.com/MoarVM/MoarVM/blob/master/src/6model/reprs/P6opaque.c#L1358
|
Hmm, interesting. I think (stress think) that actually that means that the existing code in
and sure, it is holding the mutex Whereas my final version with this:
I think can't. Sure, it can't know which @jnthn - have we uncovered a race condition here? |
At least, whatever stable we read at this point, it will still point at
allocated memory and writing to debug_name is safe.
Considering how rebless and setdebugtypename are used, I'd be highly surprised
if we ever ran into an issue there. setdebugtypename is usually done right
after allocating a type object, i.e. no other thread will have seen it at this
point.
|
If we have C11 atomics we can use a single `atomic_exchange_explicit` in `MVM_6model_set_debug_name` to update STABLE(type)->debug_name. `libatomic_ops` doesn't have any equivalent to this, hence there we still have to use 2 atomic operations to implement the swap.
00b1233
to
07037c9
Compare
src/atomics.h
free_at_safepoint
from a vector to a linked listmutex_free_at_safepoint
by usingMVM_casptr
atomic_exchange_explicit
instead ofMVM_casptr
The commit that converts
free_at_safepoint
to a linked list is code @MasterDuke17 was working on as part of investigating eliminating the FSAMy subsequent changes assume that the pointer generated by the expression
STABLE(type)
can't change (outside of the GC) - is it correct thatst
is never reassigned?