Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-81797

Make our own portable implementation of atomic notify() and wait() with timeout support

    • Service Arch
    • Fully Compatible
    • Service Arch 2023-12-11, Service Arch 2023-12-25, Service Arch 2024-01-08, Service Arch 2024-01-22, Service Arch 2024-02-05, Service Arch 2024-02-19

      In a few places we've either wanted to use C++20's std::atomic::wait() and notify apis, or have code currently using a mutex+condvar+enum/bool that could be using that instead. Unfortunately, the sandardized APIs don't support timeouts or deadlines (due to issues around freestanding support) while effectively all of our usages need them. Luckily, all of the OSes we support provide APIs that do take timeouts or deadlines, so we should just build our own wait/notify abstraction on top of them. We can use the try_wait_for and try_wait_until APIs proposed here. We can base it on the implementation in libc++ (covering linux, apple/darwin, and freebsd) and use WaitOnAddress / WakeByAddressSingle / WakeByAddressAll on windows (the MSSTL implementation is convoluted to support old windows versions without those APIs, but we dont need to support them).

      I think it would be best to add a new AtomicWaitable<T> type that publicly derives from AtomicWord<T> but ensures that `T` is a size that is supported natively by the current platform (all platforms seem to support 32bit words, so we could just require that everywhere). We could also consider adding a fallback implementation that adds a mutex+condvar to the subclass and uses them for notifications. If sizeof(T) is smaller than the platform natively supports (eg bool on linux) we could consider padding it out and using an underlying AtomicWaitable<uint32_t> rather than failing to compile and requiring the caller to do their own padding.

            Assignee:
            mathias@mongodb.com Mathias Stearn
            Reporter:
            mathias@mongodb.com Mathias Stearn
            Votes:
            1 Vote for this issue
            Watchers:
            21 Start watching this issue

              Created:
              Updated:
              Resolved: