Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spin lock #605

Closed
thompsonbry opened this issue Nov 22, 2024 · 14 comments
Closed

spin lock #605

thompsonbry opened this issue Nov 22, 2024 · 14 comments

Comments

@thompsonbry
Copy link

You mentioned an issue with the use of _mm_pause in the spin lock implementation here. We've been using a variant of this spin lock

// spin_lock code is taken from: https://rigtorp.se/spinlock/

| | // modified to handle ARM
| | struct spin_lock_t {
| | std::atomic lock_ = {0};
| |
| | void lock() noexcept {
| | for (;;) {
| | // Optimistically assume the lock is free on the first try
| | if (!lock_.exchange(true, std::memory_order_acquire)) {
| | return;
| | }
| | // Wait for lock to be released without generating cache misses
| | while (lock_.load(std::memory_order_relaxed)) {
| | cpu_acquiesce();
| | }
| | }
| | }
| |
| | bool try_lock() noexcept {
| | // First do a relaxed load to check if lock is free in order to prevent
| | // unnecessary cache misses if someone does while(!try_lock())
| | return !lock_.load(std::memory_order_relaxed) &&
| | !lock_.exchange(true, std::memory_order_acquire);
| | }
| |
| | void unlock() noexcept {
| | lock_.store(false, std::memory_order_release);
| | }
| | }; // spin_lock_t

@laurynas-biveinis
Copy link
Owner

Will you be able to get by with a pure spinlock for locking? Without putting the thread to sleep after some spinning?

@thompsonbry
Copy link
Author

thompsonbry commented Nov 25, 2024 via email

@laurynas-biveinis
Copy link
Owner

The referenced spinlock implementation does not carry over directly to this setting - and the current implementation already addresses the points there. The read lock operation is a pure read. The write lock is a single CAS attempt after a read, which, if fails, does not retry. So in a way this spinlock is already TTAS, although the correspondence is not 1:1. The memory barriers are as weak as possible too.

What needs improving however is the spin loop _mm_pause on very iteration

@thompsonbry
Copy link
Author

thompsonbry commented Dec 7, 2024 via email

@thompsonbry
Copy link
Author

thompsonbry commented Dec 7, 2024 via email

@thompsonbry
Copy link
Author

Ok. What is the problem that we are trying to solve then?

@thompsonbry
Copy link
Author

thompsonbry commented Dec 7, 2024 via email

@laurynas-biveinis
Copy link
Owner

_mm_pause does not put the thread to sleep for the OS scheduler, it compiles to a CPU instruction PAUSE, which apparently enables CPU to save memory traffic and power.

Re. write lock path, what allocation you are referring to? New tree node allocation? If so, that happens before the lock. The allocated nodes are cached thread-locally in case of restarts to avoid repeated allocs-deallocs.

Re. parking threads, with OLC ART it only happens in the read lock path.

I'd imagine the design as _mm_pause for ~5 iterations (maybe even 1-2 busy wait iterations first), then ~10 iterations with random short sleep, repeat

@thompsonbry
Copy link
Author

thompsonbry commented Dec 7, 2024 via email

@thompsonbry
Copy link
Author

thompsonbry commented Dec 7, 2024 via email

@laurynas-biveinis
Copy link
Owner

All algorithms looks like this: reads: read lock (spinning if needed), do things, see if read lock is still valid, if not restart

writes: read lock (spinning if needed), do things, try to upgrade read lock to write lock with a single CAS, if failed, restart.

So both readers and writers could park threads if spinlock is replaced with a lock

@thompsonbry
Copy link
Author

thompsonbry commented Dec 7, 2024 via email

@laurynas-biveinis
Copy link
Owner

All correct

@thompsonbry
Copy link
Author

I am going to close this one for now. There is now a build time option to suppress the use of _mm_pause(). There might be something else to be done here, but it probably warrants its own issue.

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

No branches or pull requests

2 participants