[mempool] A new (atomic) way to deal with data races #5255
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
While slowly discovering all the secrets of Mono, I also discovered atomic.h which provides extremely useful functions to deal with a ton of Mono's currently known data races. I know, only recently did I ask to blacklist the reporting counter races in mempool.c (#5191). However, having atomic.h at hand, I think it's way better to fix the races (lock-free) than simply blacklisting them (and their surroundings)?
In addition, I added
InterlockedSubtract ()
as well asInterlockedSubtract64 ()
toatomic.h
as these functions do not seem to exist but, IMO, they would really help with the readability. E.g. when converting races likefoo -= bar
we could useInterlockedSubtract (&foo, bar)
instead of usingInterlockedAdd (&foo, -1 * bar)
). I tried my best to research all (cross-platform) aspects and compared everything toInterlockedAdd ()
andInterlockedAdd64 ()
. However, of course, I would very much appreciate it, if experts in this field have a close look at this.As soon as these
InterlockedSubtract* ()
functions are approved, I will add a follow-up PR where I can finally tackle a large amount of data races - in a clean and very satisfactory way.