Googles appar
Huvudmeny

Post a Comment On: cbloom rants

"05-08-13 - A Lock Free Weak Reference Table"

12 Comments -

1 – 12 of 12
Anonymous Anonymous said...

What is the purpose of the loop, i.e. what is the state==old_state case?

If the semantics of compare_exchange_weak is both to check whether the existing value is state, and if it is exchange, otherwise write the existing value to state, then by definition surely if the compare fails, state must != old_state.

May 9, 2013 at 12:05 PM

Blogger cbloom said...

Well compare_exchange_weak can fail spuriously, so the loop actually is necessary there. But you're right, I should just change it to compare_exchange_strong and then not loop, that's simpler/clearer.

May 9, 2013 at 1:02 PM

Anonymous Anonymous said...

Ah, makes sense, thanks

May 9, 2013 at 2:58 PM

Blogger Brian said...

Your library seems to be missing the code for IncRef/DecRef.

What is the usage scenario?

I'm a little worried that since the DecRef atomic decrement is relaxed, that its effects could move above the threads usage of the object such that another thread sees the first thread's decrement, decrements the count to zero, and frees the object before the first thread is done using it.

May 9, 2013 at 5:28 PM

Blogger cbloom said...

"Your library seems to be missing the code for IncRef/DecRef."

Uh, yup. Fixed.

"I'm a little worried that since the DecRef atomic decrement is relaxed, that its effects could move above the threads usage of the object such that another thread sees the first thread's decrement, decrements the count to zero, and frees the object before the first thread is done using it."

Yeah, you have a point. I'll have to think about that a little more.

May 9, 2013 at 7:17 PM

Blogger cbloom said...

Yep, of course you're right - DecRef needs to be at least mo_release. That keeps it from moving up before ops on the pointer it protects. I think that's it (?)

That need is hidden if the object is internally protected by a mutex, or if the shared reference is protected by a mutex.

I really need to get CDSChecker running cuz Relacy is no good at modeling relaxed atomics.

Some threads I found on minimal memory ordering required for refcounts :

https://groups.google.com/forum/?fromgroups=#!topic/comp.programming.threads/Wne5asVbNfA

https://groups.google.com/forum/?fromgroups=#!msg/comp.programming.threads/Sev_8xKh3RU/wEkEqnOhs_oJ

May 10, 2013 at 10:18 AM

Blogger Brian said...

I would think you'd need a release on the normal decrement path and load acquire (or consume??) of the count on the path that you actually do the deletion...

You have to force a synchronization between the computation that uses the object and the computation that frees it.

I expect that the release alone would keep compilers in practice from breaking your code, but it isn't technically correct.

May 10, 2013 at 11:18 AM

Blogger johnb said...

Herb Sutter briefly discussed the memory ordering requirements for refcounts in his "atomic<> Weapons" talk:

Part 1:
http://channel9.msdn.com/Shows/Going+Deep/Cpp-and-Beyond-2012-Herb-Sutter-atomic-Weapons-1-of-

Part 2:
http://channel9.msdn.com/Shows/Going+Deep/Cpp-and-Beyond-2012-Herb-Sutter-atomic-Weapons-2-of-2

It's a long talk. The bit about ref-counts is near the end of part 2, (timecode about 1h 20)

He says: relaxed increment. Acquire+Release decrement. Having said that, he wasn't talking about weak pointers; I don't know if that adds any extra twists.

May 10, 2013 at 12:25 PM

Blogger johnb said...

Sorry, first URL got truncated. Should be:

http://channel9.msdn.com/Shows/Going+Deep/Cpp-and-Beyond-2012-Herb-Sutter-atomic-Weapons-1-of-2

May 10, 2013 at 12:27 PM

Blogger Brian said...

An acquire fence would also work on free path of DecREf.

May 10, 2013 at 1:51 PM

Blogger cbloom said...

Right. The actual ref decrement here is release, but then before an object is freed there's also the acq_rel to change the guid which is in the same atomic word. The guid change provides the acquire for the freeing of the object and slot.

(I suppose it's not obvious in the posted code, but state_get_guid and state_get_refcount are just pulling bit fields out of a single word.)

May 10, 2013 at 2:50 PM

Blogger cbloom said...

Addendum to post : alternative DecRef.

May 10, 2013 at 3:40 PM

You can use some HTML tags, such as <b>, <i>, <a>

This blog does not allow anonymous comments.

Comment moderation has been enabled. All comments must be approved by the blog author.

You will be asked to sign in after submitting your comment.