Join GitHub today
doc: define how finalizers interact with memory model #9442
Finalizers introduce concurrency, but the synchronization between goroutines accessing the object and the finalizer are not defined. The proposed wording is along the lines of "SetFinalizer happens before execution of the finalizer starts". This makes the following program correctly synchronized:
I know there is a certain amount of confusion at present about SetFinalizer, but I'm not sure your proposal is strong enough. I think that this program is race free:
In words, if I have a pointer, then the pointer can not be finalized while I'm in the process of using it to assign a value. That is stronger than merely saying that SetFinalizer happens before the execution of the finalizer.
One possibility would be "given a pointer p, any read or write of *p happens before p's finalizer is run." But that is too strong, as it prohibits the compiler from reordering any operation across the use of a pointer.
I feel like what we want here is an inverse relationship. Given a pointer p, there is no possible ordering such that the finalizer happens-before any read or write of *p. But I don't know if that makes any sense.
Indeed. Also if you have
FWIW Java makes exactly the guarantee I described.
It makes sense, but it states effectively what I proposed :)