Skip to content

Document that Get*MappedRange is not thread-safe with Unmap #544

@kainino0x

Description

@kainino0x

From a discussion with @lokokung about making mapping thread-safe in Dawn (without taking device-wide locks).

GetMappedRange()/GetConstMappedRange() return raw pointers which means that calling code always needs to be thread-safe w.r.t. Unmap(). For example this can never be safe:

  • Thread 1: p = GetMappedRange(); mutex A { use p }
  • Thread 2: mutex A { Unmap() }

because the unmap could happen between GetMappedRange and a lock being taken on A. The only way to be safe is to include GetMappedRange in the lock:

  • Thread 1: mutex A { p = GetMappedRange(); use p }
  • Thread 2: mutex A { Unmap() }

Thus, I think there is no use in having GetMappedRange be thread-safe w.r.t. Unmap.


GetMappedRange should still be safe w.r.t. MapAsync, as you could do something like:

  • Thread 1: MapAsync()
  • Thread 2: p = GetMappedRange(); if (p) { do something if the map happens to be complete; Unmap() }

(even though there are alternatives like using GetMapState or polling the MapAsync future).


Aside: technically it's not very useful for MapAsync and Unmap to be thread-safe w.r.t. one another, but I think they still should be, as they're not as hot.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions