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

Tagged pointers #3

Open
inspirit opened this issue Jun 19, 2015 · 2 comments
Open

Tagged pointers #3

inspirit opened this issue Jun 19, 2015 · 2 comments

Comments

@inspirit
Copy link

what is the reason to choose DCAS instead of Tagged pointers to solve ABA problem?
I assume tagged pointers should be faster at runtime due to easier atomic operation.

@skeeto
Copy link
Owner

skeeto commented Jun 19, 2015

A 16-byte alignment, which would typically be the case, leaves just 4
bits for the tag. Unlike a 32 bit counter, or especially a 64 bit
counter, that's certainly small enough to concern me about race
conditions. If a mere 16 updates are completed and a pointer re-used by
other threads within the cmpxchg loop, the ABA counter will wrap around
and the ABA test will incorrectly pass.

Also, while the current version relies on more advanced processor
features (DCAS), tagged pointers are formally-speaking non-portable:
there are no guarantees in the C standard about pointer representations.
(Though I'd be willing to bet that any implementation that supports DCAS
would also have a reasonable integer-like pointer representation.) Since
this is primary for demonstration purposes, I wanted to take the most
straightforward, correct approach.

As a side note, I was curious just how much slower DCAS is on x86_64.
According to this document,

http://www.agner.org/optimize/instruction_tables.pdf

Locked cmpxchg8b has a latency of 53 and locked cmpxchg16b has a latency
of 94, so it really is almost twice as slow.

@inspirit
Copy link
Author

You can force alignment of the elements to free up to 16 bits for the counter.

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