Reordering constraints for pthread-style locks

C or C++ programs relying on the pthreads interface for concurrency are required to use a specified set of functions to avoid data races, and to ensure memory visibility across threads. Although the detailed rules are not completely, it is not hard to refine them to a simple set of clear and uncontroversial rules for at least a subset of the C language that excludes structures (and hence bit-fields). We precisely address the question of how locks in this subset must be implemented, and particularly when other memory operations can be reordered with respect to locks. This impacts the memory fences required in lock implementations, and hence has significant performance impact. Along the way, we show that a significant class of common compiler transformations are actually safe in the presence of pthreads, something which appears to have received minimal attention in the past. We show that, surprisingly to us, the reordering constraints are not symmetric for the lock and unlock operations. In particular, it is not always safe to move memory operations into a locked region by delaying them past a pthread_mutex_lock() call, but it is safe to move them into such a region by advancing them to before a pthread_mutex_unlock() call. We believe that this was not previously recognized, and there is evidence that it is under-appreciated among implementors of thread libraries. Although our precise arguments are expressed in terms of statement reordering within a small subset language, we believe that our results capture the situation for a full C/C++ implementation. We also argue that our results are insensitive to the details of our literal (and reasonable, though possibly unintended) interpretation of the pthread standard. We believe that they accurately reflect hardware memory ordering constraints in addition to compiler constraints. And they appear to have implications beyond pthread environments.

[1]  Hans-J. Boehm Fast Multiprocessor Memory Allocation and Garbage Collection , 2000 .

[2]  Sarita V. Adve,et al.  Designing memory consistency models for shared-memory multiprocessors , 1993 .

[3]  Jeremy Manson,et al.  JSR-133: Java Memory Model and Thread Specification , 2003 .

[4]  Andrei Alexandrescu,et al.  Memory model for multithreaded C + + : Issues , 2005 .

[5]  David A. Padua,et al.  Basic compiler algorithms for parallel programs , 1999, PPoPP '99.

[6]  V AdveSarita,et al.  Weak orderinga new definition , 1990 .

[7]  Leslie Lamport,et al.  How to Make a Multiprocessor Computer That Correctly Executes Multiprocess Programs , 2016, IEEE Transactions on Computers.

[8]  Katherine A. Yelick,et al.  Optimizing parallel programs with explicit synchronization , 1995, PLDI '95.

[9]  V AdveSarita,et al.  Shared Memory Consistency Models , 1996 .

[10]  Hans-Juergen Boehm,et al.  HP Laboratories , 2006 .

[11]  M. Hill,et al.  Weak ordering-a new definition , 1990, [1990] Proceedings. The 17th Annual International Symposium on Computer Architecture.

[12]  Jeremy Manson,et al.  The Java memory model , 2005, POPL '05.

[13]  Kourosh Gharachorloo,et al.  Retrospective: Memory Consistency and Event Ordering in Scalable Shared-Memory Multiprocessors , 1998, 25 Years ISCA: Retrospectives and Reprints.

[14]  Doug Lea,et al.  Memory model for multithreaded C++: August 2005 status update , 2005 .

[15]  Gurindar S. Sohi 25 Years of the International Symposia on Computer Architecture (Selected Papers) , 1998, ISCA Selected Papers.

[16]  Jonathan Walpole,et al.  Exploiting deferred destruction: an analysis of read-copy-update techniques in operating system kernels , 2004 .

[17]  William N. Scherer,et al.  Scalable queue-based spin locks with timeout , 2001, PPoPP '01.

[18]  Sarita V. Adve,et al.  Shared Memory Consistency Models: A Tutorial , 1996, Computer.

[19]  Mauricio J. Serrano,et al.  Thin locks: featherweight Synchronization for Java , 2004, SIGP.

[20]  Dennis Shasha,et al.  Efficient and correct execution of parallel programs that share memory , 1988, TOPL.