Skip to content

compiler_fence documentation is misleading #129189

Closed

Description

@RalfJung

The current docs for compiler_fence explain that function in terms of reorderings:

compiler_fence does not emit any machine code, but restricts the kinds of memory re-ordering the compiler is allowed to do. Specifically, depending on the given Ordering semantics, the compiler may be disallowed from moving reads or writes from before or after the call to the other side of the call to compiler_fence. Note that it does not prevent the hardware from doing such re-ordering. This is not a problem in a single-threaded, execution context, but when other threads may modify memory at the same time, stronger synchronization primitives such as fence are required.

That's somewhat misleading. For good reasons, our fence docs quickly transition from "reordering" to "synchronization" terminology:

Depending on the specified order, a fence prevents the compiler and CPU from reordering certain types of memory operations around it. That creates synchronizes-with relationships between it and atomic operations or fences in other threads.

compiler_fence should also be explained in terms of synchronizes-with, to avoid raising any false expectations. Specifically, compiler_fence is equivalent to C++ atomic_signal_fence, and so its synchronization rules are very similar to those of fence except that synchronization only occurs between a thread and its signal handler, not across different threads.

Cc @rust-lang/libs-api @rust-lang/opsem
Cc rust-lang/unsafe-code-guidelines#347 #54962

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Aug 17, 2024
  2. added
    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
    T-libs-api[DEPRECATED; DO NOT USE]
    T-opsemRelevant to the opsem team
    on Aug 17, 2024
  3. jieyouxu commented on Aug 17, 2024

    @jieyouxu
    Member

    (Sorry, race condition...)

  4. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Aug 17, 2024
  5. added
    C-discussionCategory: Discussion or questions that doesn't represent real issues.
    on Aug 17, 2024
  6. added a commit that references this issue on Sep 2, 2024
  7. added a commit that references this issue on Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-concurrencyArea: ConcurrencyA-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-discussionCategory: Discussion or questions that doesn't represent real issues.T-libs-api[DEPRECATED; DO NOT USE]T-opsemRelevant to the opsem team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions