Files
2026-08-28 00:17:52 -04:00

2.6 KiB

title, type, source, source_url, author, handle, post_id, date, archived, draft, tags, description, in_reply_to
title type source source_url author handle post_id date archived draft tags description in_reply_to
This is how you write a good x64 spinlock: Use two stage construct. archive twitter https://x.com/SebAaltonen/status/1125064645637738496 Sebastian Aaltonen SebAaltonen 1125064645637738496 2019-05-05 2026-08-27 false
archive
twitter
SebAaltonen
This is how you write a good x64 spinlock: Use two stage construct.

Source

Thread

1/ @SebAaltonen ^1125064645637738496

This is how you write a good x64 spinlock: Use two stage construct. Outer test is read. Multiple cores reading same cache line = no contention. Inner = exchange instead of CAS. Pause instruction in the loop body to save power and/or give all cycles to other logical core (SMT).

Branches: archive/threads/SebAaltonen/2019-05-05-this-is-how-you-write-a-good-x64-spinlock-use-two/2019-05-05-SebAaltonen-dont-use-spinlock-for-waits-use-spinlock-only-to, archive/threads/SebAaltonen/2019-05-05-this-is-how-you-write-a-good-x64-spinlock-use-two/2019-05-05-romzr-got-a-link-to-the-slide-deck, archive/threads/SebAaltonen/2019-05-05-this-is-how-you-write-a-good-x64-spinlock-use-two/2019-05-05-pATjako-i-would-love-to-see-a-comparison-to-arm64-from, archive/threads/SebAaltonen/2019-05-05-this-is-how-you-write-a-good-x64-spinlock-use-two/2019-05-05-ArvidGerstmann-interesting-ive-been-using-xchg-over-cmpxchg-for, archive/threads/SebAaltonen/2019-05-05-this-is-how-you-write-a-good-x64-spinlock-use-two/2019-05-06-morfar-its-a-shame-there-is-no-mm-pause-in-the-bad-case, archive/threads/SebAaltonen/2019-05-05-this-is-how-you-write-a-good-x64-spinlock-use-two/2019-05-06-matiasgoldberg-i-have-one-suggestion-1st-stage-you-measure-how, archive/threads/SebAaltonen/2019-05-05-this-is-how-you-write-a-good-x64-spinlock-use-two/2019-05-06-0xF390-you-can-find-many-implementations-here-including, archive/threads/SebAaltonen/2019-05-05-this-is-how-you-write-a-good-x64-spinlock-use-two/2019-05-06-0xF390-tatas-read-only-inner-loop-is-a-much-better

2/ @SebAaltonen ^1125622264819605504

Important clarification: Using basic load works only on x86/64. Better use atomic load. It will result in the exactly same compiled code on x64 and work also on ARM64. Also gives compiler more info to avoid unwanted optimizations.

3/ @SebAaltonen ^1125622526250508289

For more info watch this: https://x.com/sebaaltonen/status/1125084734365872129?s=21