Updates to C++ Memory Model Based on Formalization

ISO/IEC JTC 1/SC 22/WG 14/N1446

ISO/IEC JTC1 SC22 WG21 N3074 = 10-0064 - 2010-03-11

Paul E. McKenney, paulmck@linux.vnet.ibm.com
Mark Batty, mjb220@cl.cam.ac.uk
Clark Nelson, clark.nelson@intel.com
N.M. Maclaren, nmm1@cam.ac.uk
Hans Boehm, hans.boehm@hp.com
Anthony Williams, anthony@justsoftwaresolutions.co.uk
Peter Dimov, pdimov@mmltd.net
Lawrence Crowl, crowl@google.com, Lawrence@Crowl.org

Introduction

Mark Batty recently undertook a partial formalization of the C++ memory model, which Mark summarized in N2955. This paper summarizes the discussions on Mark's paper, both verbal and email, recommending appropriate actions for the Core Working Group. Library issues are dealt with in a separate N3057 paper.

This paper is based on N3045, and has been updated to reflect discussions in the Concurrency subgroup of the Library Working Group in Pittsburgh. This paper also carries the C-language side of N3040, which was also discussed in the Concurrency subgroup of the Library Working Group in Pittsburgh.

Core Issues

Core Issue 1: 1.10p2 “Might Be” Might Be Indefinite (Editorial)

The phrase “might be” is indefinite and should be reworded.

Replace “might be” with “is”:

Core Issue 2: 1.10p14 Lock and SC Operations Interleave Simply (Non-Normative)

N2955 suggests two changes to 1.10p14:

  1. Qualifying the conflicting accesses as being both non-atomic. This change is unnecessary, because a single non-atomic operation in a set of conflicting operations is all that is required to result in a data race. This change is further incorrect, because it is possible to publish a reference to an atomic object within its constructor, which would permit a data race between an atomic operation from some other thread on the one hand and the remainder of the (non-atomic) accesses from the constructor on the other.

    Recommendation: no change.

  2. Correct use of simple locks results in simple interleaving, as currently stated in 1.10p14. If atomic memory_order_seq_cst operations are also used outside of lock-based critical sections, the result is still simple interleaving. If atomic memory_order_seq_cst operations are also used both inside and outside of lock-based critical sections, the result is still sequentially consistent, but the individual lock-based critical sections are no longer simply interleaved. However, the result will be consistent with at least one simple interleaving of the individual operations making up each critical section.

    Recommendation: update note to include atomic memory_order_seq_cst, reworking the wording appropriately.

Core Non-Issues

Core Non-Issue 1: 1.10p4 Overhead of Access to Atomic Objects

Atomic and locking objects are not trivially copyable [29.5.1p2, 29.5.2p1, 29.5.3p2], so the result of copying them (for example, via std::memcpy) are not specified by the standard [3.9]. Additionally, if the memcpy operation results in a data race, then undefined behavior is explicitly specified by the working draft [1.10p14].

There was some spirited discussion of the non-data-race case on the email reflector, with the following positions outlined:

  1. Peter Dimov argued that atomic integral types have standard layout [29.5.1p2, 29.5.2p1], and that there was therefore no good reason to prohibit copying out the underlying memory locations of an atomic object. Peter further argued that atomic accesses to large objects can incur high overheads, even when using memory_order_relaxed, and that there are a number of situations (including some implementations of resizeable hash tables) where most accesses to a given object are not subject to data races. In such cases, there is good reason to avoid memory_order_relaxed's overhead for accesses known to be data-race free.
  2. Clark Nelson argued against copying to atomic objects, even in absence of a data race, given that some implementations might have non-trivial representations. Clark was willing to entertain the thought of copying from atomic objects, but only in absence of data races.
  3. An informal poll of the Core Working Group resulted in the position that copying non-trivially copyable objects (e.g., via memcpy) was at best unspecified, at worst undefined.
  4. Paul McKenney argued that mandating copyability might rule out active-memory hardware optimizations, and that the behavior should thus remain undefined. The effect of copying out an atomic object's underlying representation can be efficiently emulated via a memory_order_relaxed load, so it is not necessary to define the effect of copying the underlying representation. Furthermore, the effect of copying an underlying representation to an atomic object can be both safely and efficiently emulated via a memory_order_relaxed store for machine-word-sized accesses, which are the most common in practice.
  5. Some time back, Alexander Terekhov is said to have proposed an additional memory_order enum member that would permit the implementation to access the atomic object non-atomically (for the purposes of this paper, call it memory_order_nonatomic). This could be thought of as specifying memory ordering that is so relaxed that the implementation need not even guarantee indivisibility of different accesses to the same atomic object. A memory_order_nonatomic operation would therefore be subject to data races.
  6. Hans Boehm proposed leaving 1.10p4 as is and stating some form of the prohibition in clause 29 or 30. Peter Dimov and Paul McKenney agreed with this approach, with Paul suggesting 29.3p1.

Therefore, this paper recommends no changes to 1.10p4. This paper does not recommend adding memory_order_nonatomic to c++0x, but something similar should be considered for a later TR or a later version of the standard.

Core Non-Issue 2: 1.10p6 Mathematical Meaning of Maximal

The phrase “M is a maximal contiguous” could be interpreted as meaning the sequence having the maximum value or any of a number of alternative interpretations. However, there were other instances of this abbreviation that were not objected to, so recommend no change.

Core Non-Issue 3: 1.10p12 Initialization as Visible Side Effect

The intent of this paragraph is that initialization be considered a separate access, but this is not explicitly stated. There is some debate as to whether this needs to be explicitly stated. In absence of consensus, let those who read the words of this paragraph apply appropriate common sense.

Core Non-Issue 4: 1.10p13 Initialization as Visible Side Effect

As with 1.10p12, the intent of this paragraph is that initialization be considered a separate access, but this is not explicitly stated. There is some debate as to whether this needs to be explicitly stated. In absence of consensus, let those who read the words of this paragraph apply appropriate common sense.

WG21 C++-Language Wording

This section lists WG21 C++-language wording. The corresponding WG14 C-language wording is shown in a separate section to ease coordination of changes to the two working drafts.

Wording for Core Issue 1

Reword the 1.10p2 as follows:

The value of an object visible to a thread T at a particular point might beis the initial value of the object, a value assigned to the object by T , or a value assigned to the object by another thread, according to the rules below. [ Note: In some cases, there may instead be undefined behavior. Much of this section is motivated by the desire to support atomic operations with explicit and detailed visibility constraints. However, it also implicitly supports a simpler view for more restricted programs. — end note ]

Wording for Core Issue 2

Reword the non–normative note in 1.10p14 to include sequentially consistent atomic operations as well as lock–based critical sections, as follows:

The execution of a program contains a data race if it contains two conflicting actions in different threads, at least one of which is not atomic, and neither happens before the other. Any such data race results in undefined behavior. [ Note: It can be shown that programs that correctly use simple locks and memory_order_seq_cst operations to prevent all data races and that use no other synchronization operations behave as the executions of if the operations executed by their constituent threads were are simply interleaved, with each observed value value computation of an object being the last value assigned last side effect on that object in that interleaving. This is normally referred to as “sequential consistency”. However, this applies only to data–race–free programs, and data–race–free programs cannot observe most program transformations that do not change single–threaded program semantics. In fact, most single–threaded program transformations continue to be allowed, since any program that behaves differently as a result must perform an undefined operation. — end note ]

WG14 C–Language Wording

Change WG14 5.1.2.4p2 as follows:

The value of an object visible to a thread T at a particular point might be is the initial value of the object, a value assigned to the object by T , or a value assigned to the object by another thread, according to the rules below.

Change WG14 5.1.2.4p23 as follows:

NOTE 11 It can be shown that programs that correctly use simple locks and memory_order_seq_cst operations to prevent all data races, that and use no other synchronization operations, behave as though the executions of the operations executed by their constituent threads were are simply interleaved, with each observed value value computation of an object being the last value assigned last side effect on that object in that interleaving. This is normally referred to as “sequential consistency”. However, this applies only to data-race-free programs, and data-race-free programs cannot observe most program transformations that do not change single-threaded program semantics. In fact, most single-threaded program transformations continue to be allowed, since any program that behaves differently as a result must contain undefined behavior.