On Tue, Sep 1, 2015 at 2:14 PM, Giovanni Piero Deretta
On 1 Sep 2015 10:35 am, "Andrey Semashev"
wrote: [...] I know there's probably no way to ensure that the function meets these requirements, and it is possible to get an infinite loop in runtime, so maybe always using a CAS loop would be a safer approach, even on an architecture with LL/SC instructions.
The implementation could fall back on the CAS loop after the first failed LL/SC. You might want to do it even in the case if contention to reduce the window the cachelines must be held. It is not optimal in case that the reservation always fail, but is always correct and the performance can be corrected with profiling and some sort of CAS hint.
Thanks for the suggestion. Yes, that's probably the best way to implement it. I can provide two functions in the API - one that always implements a CAS loop right away and the other that might try injecting modify function between LL and SC and do the CAS loop if that fails. On a CAS-only architecture the two functions would be equivalent.