On Thu, May 6, 2021 at 4:16 AM Vitaly Isaev <vitalyisa...@gmail.com> wrote: > > In a well-known book "The Art of Multiprocessor Programming" by Herlihy, > Shavit some of lock-free and wait-free algorithms utilize Java's template > AtomicMarkableReference<T> type. It allows to perform single atomic CAS > operation on the pair consisting of T reference and boolean mark. > > There is no similar type in C/C++/Go stdlib, but at least in C++ it's > possible to model it using bit stealing approach (see C++ example). On x86_64 > arch only 48 bits of 64 bits are actually used, so one can store arbitrary > data in the remaining 16 bits, and work with the whole pointer and the data > atomically. > > As far as I understand, there are two requirements to implement this approach: > > Pointers must be aligned. > Pointer's low bits must be clear (if you want to store something like bool in > this area, it must not be already occupied). > > But some stackowerflow users have questioned whether these remaining bits are > really free in Go. Perhaps Go runtime already uses these area for GC or some > other background routines? > > So is it possible to use pointer bit stealing technique in Go? Are there any > working examples?
The Go language spec does not in any way guarantee that this will work. If it happens to work today, it may break in the future. So I definitely recommend against it. That said, I believe it will work today, and in fact the Go runtime uses a somewhat similar technique internally (by the way, in the bit stealing approach you need more than a boolean mark, or you will get ABA problems). It's OK for the Go runtime to do this, because if we break the required guarantees in the future we will fix the Go runtime at the same time. The relevant code is https://golang.org/src/runtime/lfstack.go https://golang.org/src/runtime/lfstack_32bit.go https://golang.org/src/runtime/lfstack_64bit.go Again, outside of the runtime code like this is not supported and may well break in the future. Ian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcWEfHagBqn-6KZKC75DrNM9GOkGwapD1yn9MUUZh_X7TA%40mail.gmail.com.