Skip to content

add spinlock with exponential backoff#3

Open
AneesPatel wants to merge 1 commit into
mainfrom
feat/backoff-spinlock
Open

add spinlock with exponential backoff#3
AneesPatel wants to merge 1 commit into
mainfrom
feat/backoff-spinlock

Conversation

@AneesPatel

Copy link
Copy Markdown
Owner

What

Variant of the existing SpinLock that adds exponential backoff. Same TTAS structure but when a CAS fails, it waits an increasing number of pause cycles before retrying.

Why

Under heavy contention (8+ threads), the basic spinlock turns into a cache line stampede. Every thread hammers the same lock word with acquire RMWs and trashes each other's L1 caches. Backing off spreads out the retry attempts and gives the current lock holder time to actually finish.

When NOT to use this

At low contention the plain SpinLock is faster - the extra delay adds up when the lock is usually free. This one pays off when you have many threads and longer critical sections.

Notes

  • Backoff range: 1 to 1024 pause cycles, doubles on each failed attempt, resets on success.
  • The spin_for() loop uses CPU_PAUSE() so it still hints the CPU properly.
  • Test uses 8 threads (vs the SpinLock test's 4) to exercise the high-contention path.

Files

  • include/BackoffSpinLock.h
  • tests/test_backoffspinlock.cpp
  • CMakeLists.txt

The basic TTAS spinlock was hammering the cache under 8+ threads.
Exponential backoff reduces coherence traffic at high contention.
MIN=1 MAX=1024 pause cycles felt right in testing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant