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

fix data race in BM_ThreadYieldSpinLockThrashing #1099

Conversation

esigo
Copy link
Member

@esigo esigo commented Nov 25, 2021

Fixes #1098 (issue)

Changes

Please provide a brief description of the changes here.
The lock function, should return only and only if it actually acquired the lock. This was not the case if the locking was not successful after a limit amount of tries.
see here for more info the issue.
For significant contributions please make sure you have completed the following items:

  • CHANGELOG.md updated for non-trivial changes
  • Unit tests have been added
  • Changes in public API reviewed

@esigo esigo requested a review from a team November 25, 2021 18:07
@codecov
Copy link

codecov bot commented Nov 25, 2021

Codecov Report

Merging #1099 (b0900d0) into main (93249de) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #1099   +/-   ##
=======================================
  Coverage   93.39%   93.39%           
=======================================
  Files         165      165           
  Lines        6229     6229           
=======================================
  Hits         5817     5817           
  Misses        412      412           

@ThomsonTan
Copy link
Contributor

@esigo thanks for looking into it and the fix. Could you please share a little information on how the race condition happens in the original implementation?

}
std::this_thread::yield();
},
[](std::atomic<bool> &l) { l.store(false, std::memory_order_release); });
[](std::atomic_flag &l) { l.clear(std::memory_order_release); });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix. would you help understand this change (probably enrich PR description) how it is fixing the race condition.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the PR description. Please let me know if it needs more information is needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. Should we either

  • Change the test comment ( // SpinLock thrashing with thread::yield() after N spins ) as there is no more yield happening.
  • Add the yield after every few tries in the tight loop to be in sync with the benchmark comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, thanks. I updated the comment and added yield.

@esigo
Copy link
Member Author

esigo commented Nov 30, 2021

@esigo thanks for looking into it and the fix. Could you please share a little information on how the race condition happens in the original implementation?

@ThomsonTan, does this answer the question?

@ThomsonTan ThomsonTan merged commit 15155f7 into open-telemetry:main Dec 2, 2021
@ThomsonTan
Copy link
Contributor

Thanks @esigo for the explanation.

@esigo esigo deleted the data-race-in-BM_ThreadYieldSpinLockThrashing branch December 2, 2021 17:45
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.

data race in BM_ThreadYieldSpinLockThrashing
3 participants