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

Open
thompsonbry opened this issue Nov 22, 2024 · 2 comments
Open

spin lock #605

thompsonbry opened this issue Nov 22, 2024 · 2 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

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