<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
p.msonormal0, li.msonormal0, div.msonormal0
        {mso-style-name:msonormal;
        mso-margin-top-alt:auto;
        margin-right:0in;
        mso-margin-bottom-alt:auto;
        margin-left:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
span.EmailStyle18
        {mso-style-type:personal;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
span.EmailStyle19
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt">Seems fine to me.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">Dumb question: What (if anything) should be done to support platforms with a single threaded system.  Assert if not immediately available?  Optimistically spinlock in case they are using this on “elsewhere”
 memory of some sort?<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">Pedantic: I probably wouldn’t call the Windows implementation futex based (
</span><a href="https://devblogs.microsoft.com/oldnewthing/20170601-00/?p=96265">https://devblogs.microsoft.com/oldnewthing/20170601-00/?p=96265</a> ).  I wouldn’t go and call the Linux implementation WaitOnAddress based either.<span style="font-size:11.0pt"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:11.0pt">From:</span></b><span style="font-size:11.0pt"> libcxx-dev <libcxx-dev-bounces@lists.llvm.org>
<b>On Behalf Of </b>Olivier Giroux via libcxx-dev<br>
<b>Sent:</b> Tuesday, May 28, 2019 5:55 PM<br>
<b>To:</b> libcxx-dev@lists.llvm.org<br>
<b>Subject:</b> [EXTERNAL] [libcxx-dev] A proposed approach to std::atomic_wait for libcxx<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt">Hi everyone,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">To start the discussion of how libcxx should go about implementing this feature, I’ve prepared a cross-platform implementation of atomic_wait/atomic_notify_* for Mac/Linux/Windows/CUDA/unidentified-platform.
 I don’t claim that it’s fully tuned for your platform, nor do I claim that it’s perfect for every possible use, but it should not be terribly bad for any use either. It has various knobs to turn paths On/Off, so you can choose a different path on each platform,
 so long as it’s supported at all on that platform.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">You can find the implementation here:
<a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_ogiroux_atomic-5Fwait_&d=DwMGaQ&c=I_0YwoKy7z5LMTVdyO6YCiE2uzI1jjZZuIPelcSjixA&r=y8mub81SfUi-UCZRX0Vl1g&m=52uONGiTLQpEB5Xuf3oJR6LnVXwuDGP-HiraUV4P5I8&s=_UMntk1qhxJm9tQFWUvq7zDZ3A9Pfse6ouh9047XIE8&e=">
https://github.com/ogiroux/atomic_wait/</a>.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">It has these strategies implemented:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">* Contention table. Used to optimize futex notify, or to hold CVs. Disable with __NO_TABLE.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">* Futex. Supported on Linux and Windows. For performance requires a table on Linux. Disable with __NO_FUTEX.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">* Condition variables. Supported on Linux and Mac. Requires table to function. Disable with __NO_CONDVAR.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">* Timed back-off. Supported on everything. Disable with __NO_SLEEP.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">* Spinlock. Supported on everything. Force with __NO_IDENT.
<i>Note:</i> <i>performance is too terrible to use</i>.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">The strategy is chosen this way, by platform:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">* Linux: default to futex (with table), fallback to futex (no table) -> CVs -> timed backoff -> spin.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">* Mac: default to CVs (table), fallback to timed backoff -> spin.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">* Windows: default to futex (no table), fallback to timed backoff -> spin.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">* CUDA: default to timed backoff, fallback to spin. (This is not all checked in in this tree.)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">* Unidentified platform: default to spin.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">The unidentified platform support could be better. For instance, we should probably assume that <thread> is implemented and use the sleeping/yielding facilities there. It should not fall all the way back to
 __NO_IDENT, it should instead fall back to about where CUDA is expected to be.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">One of the main discussion points I’d like to drive with this, is the design of the contention management table, to go along the sharded lock table that backs __atomic_* and __c11_atomic_* built-ins. Ideally
 this would be handled the same way, meaning that it should live in libatomic.a or your substitute, and be shared with other C++ standard libraries on your platform.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">Please discuss!<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">Sincerely,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">Olivier<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<div>
<div class="MsoNormal" align="center" style="text-align:center"><span style="font-size:11.0pt">
<hr size="2" width="100%" align="center">
</span></div>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt">This email message is for the sole use of the intended recipient(s) and may contain confidential information.  Any unauthorized review, use, disclosure or distribution is prohibited.  If you are not the intended
 recipient, please contact the sender by reply email and destroy all copies of the original message.
<o:p></o:p></span></p>
</div>
<div>
<div class="MsoNormal" align="center" style="text-align:center"><span style="font-size:11.0pt">
<hr size="2" width="100%" align="center">
</span></div>
</div>
</div>
</div>
</body>
</html>