Googles appar
Huvudmeny

Post a Comment On: cbloom rants

"07-18-10 - Mystery - Do Mutexes need More than Acquire-Release -"

6 Comments -

1 – 6 of 6
Blogger Brian said...

Certainly moving stuff in a lock could cause problems. Supposed you allow moving a wait loop into the lock or a yield statement? Not that a compiler would actually do such a thing, but if you are not careful you could sacrifice liveness.

July 20, 2010 at 4:38 PM

Blogger cbloom said...

" Certainly moving stuff in a lock could cause problems. Supposed you allow moving a wait loop into the lock or a yield statement? Not that a compiler would actually do such a thing, but if you are not careful you could sacrifice liveness. "

Hmm. Interesting point. Yield should never cause a problem (I don't think). Any point in your program could be a yield or not, it should never affect correctness.

Wait() in the Windows sense (Wait on Event) is of course a problem. Presumably the compiler won't move around anything like that.

July 20, 2010 at 8:05 PM

Blogger cbloom said...

BTW a more direct issue is moving mutexes against each other and thereby causing deadlock.

If this :

Lock(A)
..
Unlock(A)
Lock(B)
..
Unlock(B)


Can be rearranged to this :

Lock(A)
..
Lock(B)
Unlock(A)
..
Unlock(B)

you can have deadlock. So Locks must never be able to move past unlocks.

That is satisfied if Lock is Acquire_Release and Unlock is Release. In that case Lock has a #StoreStore barrier before it, so it can't move up before the Unlock.

July 20, 2010 at 8:07 PM

Blogger Dmitry Vyukov said...

Hi Charles,

> Hans Boehm : Reordering Constraints for Pthread-Style Locks goes into this question in a bit of detail, but it's fucking slides...

Check out the technical report:
http://www.hpl.hp.com/techreports/2005/HPL-2005-217R1.pdf

> 1. Does either Mutex Lock or Unlock need to be Sequential Consistent?

I think that they do not need to. Mutex provides mutual exclusion and synchronization for a *particular* data, and you do not care what happens with outside world. And if you do care you either have data races, or have acquired mutexes for that other data and we back to where we begin.

> 2. Does either Lock or Unlock need to keep instructions from moving IN?

I think that they do not need to. It's either indistinguishable or you have races.

As for fairness, I believe that it's just improper level for fairness in general case. The low level primitive must be fast in the first place, and fairness must be handled on higher architectural levels (for example, with FIFO queues, or special fair mutexes (which are different thing from the low-level synchronization primitive)).

However there is a quirk. Mutex unlock must indeed be not able to sink below another mutex lock on *program* level, this can lead to a deadlock.
You not actually need Acquire_Release for lock to achieve that, Acquire_Release is too strong, it will prevent sinking on *hardware* level.
What you need is just a compiler fence (std::atomic_signal_fence() (or maybe even just C++ volatile) to prevent code movement on compiler level, and compiler+hardware fence to prevent code movement OUT.

Btw, who has a nice article with analysis of my implementation is Anthony Williams. And Viva64 are Russian folks who deal with static verification of OpenMP and 64-bit C/C++ code, they have a nice interview with me on Relacy Race Detector (http://www.viva64.com/articles/code-analyzers/) :)

--
Dmitriy V'jukov

July 24, 2010 at 12:24 AM

Comment deleted

This comment has been removed by the author.

July 24, 2010 at 12:25 AM

Anonymous Anonymous said...

I understand that there is no need for mutex operations to be Seq_Cst, because even non-sc acquire semantics on lock and release on unlock provides SC (total order) for all the data which are protected by different mutexes.

Just try to see non-SC behaviour of data using mutexes to access them. You would not be able! :-)

Mutexes provide SC, even implemented with non-sc operations.

March 30, 2013 at 8:36 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.