[SERVER-72064] Implement AtomicWord::wait similar to std::atomic::wait Created: 13/Dec/22  Updated: 03/Oct/23

Status: Backlog
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Jordi Olivares Provencio Assignee: Backlog - Service Architecture
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by SERVER-72616 Remove futex usage in favour of std::... Backlog
Related
related to SERVER-81797 Make our own portable implementation ... In Code Review
is related to SERVER-72033 Investigate different TicketHolder op... Closed
Assigned Teams:
Service Arch
Participants:

 Description   

In C++20 the functionality for waiting on an atomic variable is implemented in std::atomic::wait. This essentially brings an implementation of futexes that is cross-platform.

We should investigate whether we can implement this functionality in our AtomicWord once C++20 is allowed in the codebase.



 Comments   
Comment by Mathias Stearn [ 03/Oct/23 ]

I propose closing this as WONTFIX in favor of SERVER-81797. We can't use the APIs standardized in C++20 because they don't support deadlines or timeouts, while effectively all of our use cases need them.

I think adding the APIs directly to AtomicWord<T> would be a mistake because it limits our flexibility, assuming we want to keep the rule that sizeof(AtomicWord<T>) == sizeof(T). Using a separate AtomicWaitable<T> template that exposes the AtomicWord<T> API in addition to the wait/notify functions, gives us the flexability to make its size vary based on the requirement to efficiently wait, without impacting the size of all AtomicWords.

Comment by Billy Donahue [ 13/Dec/22 ]

I would switch to std::atomic the second AtomicWord is missing something I need. This #if -protected code wouldn't be future-proofing, because you can't use it until __cpp_lib_atomic_wait exists. There is no fallback #else clause. The AtomicWord functions just won't exist at all until std::atomic supports them.

Comment by Jordi Olivares Provencio [ 13/Dec/22 ]

billy.donahue@mongodb.com that would very much be appreciated and would future proof the code for when it becomes available. In my mind it would try to mimick as much as possible the same semantics of the corresponding methods in std::atomic.

Comment by Billy Donahue [ 13/Dec/22 ]

We could add this feature right now, enabled by the feature test macro __cpp_lib_atomic_wait.

mongo::AtomicWord just forwards calls to the underlying std::atomic, and these member functions could be added pretty directly, under control of an if block.

#if __cpp_lib_atomic_wait >= 201907L
    wait(...)
    notifyOne(...)
    notifyAll(...)
#endif

Generated at Thu Feb 08 06:20:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.