<p dir="ltr">Yep. I expect to succeed at that in about two more patches.</p>
<br><div class="gmail_quote"><div dir="ltr">On Thu, Jun 2, 2016, 16:11 Rafael Espíndola <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Cool, cas_flag is almost dead now. Looks like we can remove it once<br>
std::call_once works everywhere.<br>
<br>
Cheers,<br>
Rafael<br>
<br>
<br>
On 2 June 2016 at 01:44, Chandler Carruth via llvm-commits<br>
<<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>> wrote:<br>
> Author: chandlerc<br>
> Date: Thu Jun 2 03:44:05 2016<br>
> New Revision: 271504<br>
><br>
> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=271504&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=271504&view=rev</a><br>
> Log:<br>
> Switch statistics to use relaxed updates to a std::atomic.<br>
><br>
> This removes usage of the hacky, incorrect, and TSan-unfriendly<br>
> home-grown atomics. It should actually be more efficient in some cases.<br>
><br>
> Based on our existing usage of <atomic>, all of this is portably<br>
> available AFAICT. One small challenge is initializing the stastic, but<br>
> I've tried a comparable sample out on MSVC (the most likely to complain<br>
> here) and it seems to work. Will have to watch the build bots of course.<br>
><br>
> Modified:<br>
> llvm/trunk/include/llvm/ADT/Statistic.h<br>
><br>
> Modified: llvm/trunk/include/llvm/ADT/Statistic.h<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/Statistic.h?rev=271504&r1=271503&r2=271504&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/Statistic.h?rev=271504&r1=271503&r2=271504&view=diff</a><br>
> ==============================================================================<br>
> --- llvm/trunk/include/llvm/ADT/Statistic.h (original)<br>
> +++ llvm/trunk/include/llvm/ADT/Statistic.h Thu Jun 2 03:44:05 2016<br>
> @@ -28,6 +28,7 @@<br>
><br>
> #include "llvm/Support/Atomic.h"<br>
> #include "llvm/Support/Compiler.h"<br>
> +#include <atomic><br>
> #include <memory><br>
><br>
> namespace llvm {<br>
> @@ -38,10 +39,10 @@ class Statistic {<br>
> public:<br>
> const char *Name;<br>
> const char *Desc;<br>
> - volatile llvm::sys::cas_flag Value;<br>
> + std::atomic<unsigned> Value;<br>
> bool Initialized;<br>
><br>
> - llvm::sys::cas_flag getValue() const { return Value; }<br>
> + unsigned getValue() const { return Value.load(std::memory_order_relaxed); }<br>
> const char *getName() const { return Name; }<br>
> const char *getDesc() const { return Desc; }<br>
><br>
> @@ -52,51 +53,45 @@ public:<br>
> }<br>
><br>
> // Allow use of this class as the value itself.<br>
> - operator unsigned() const { return Value; }<br>
> + operator unsigned() const { return getValue(); }<br>
><br>
> #if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS)<br>
> const Statistic &operator=(unsigned Val) {<br>
> - Value = Val;<br>
> + Value.store(Val, std::memory_order_relaxed);<br>
> return init();<br>
> }<br>
><br>
> const Statistic &operator++() {<br>
> - // FIXME: This function and all those that follow carefully use an<br>
> - // atomic operation to update the value safely in the presence of<br>
> - // concurrent accesses, but not to read the return value, so the<br>
> - // return value is not thread safe.<br>
> - sys::AtomicIncrement(&Value);<br>
> + Value.fetch_add(1, std::memory_order_relaxed);<br>
> return init();<br>
> }<br>
><br>
> unsigned operator++(int) {<br>
> init();<br>
> - unsigned OldValue = Value;<br>
> - sys::AtomicIncrement(&Value);<br>
> - return OldValue;<br>
> + return Value.fetch_add(1, std::memory_order_relaxed);<br>
> }<br>
><br>
> const Statistic &operator--() {<br>
> - sys::AtomicDecrement(&Value);<br>
> + Value.fetch_sub(1, std::memory_order_relaxed);<br>
> return init();<br>
> }<br>
><br>
> unsigned operator--(int) {<br>
> init();<br>
> - unsigned OldValue = Value;<br>
> - sys::AtomicDecrement(&Value);<br>
> - return OldValue;<br>
> + return Value.fetch_sub(1, std::memory_order_relaxed);<br>
> }<br>
><br>
> - const Statistic &operator+=(const unsigned &V) {<br>
> - if (!V) return *this;<br>
> - sys::AtomicAdd(&Value, V);<br>
> + const Statistic &operator+=(unsigned V) {<br>
> + if (V == 0)<br>
> + return *this;<br>
> + Value.fetch_add(V, std::memory_order_relaxed);<br>
> return init();<br>
> }<br>
><br>
> - const Statistic &operator-=(const unsigned &V) {<br>
> - if (!V) return *this;<br>
> - sys::AtomicAdd(&Value, -V);<br>
> + const Statistic &operator-=(unsigned V) {<br>
> + if (V == 0)<br>
> + return *this;<br>
> + Value.fetch_sub(V, std::memory_order_relaxed);<br>
> return init();<br>
> }<br>
><br>
> @@ -145,8 +140,8 @@ protected:<br>
><br>
> // STATISTIC - A macro to make definition of statistics really simple. This<br>
> // automatically passes the DEBUG_TYPE of the file into the statistic.<br>
> -#define STATISTIC(VARNAME, DESC) \<br>
> - static llvm::Statistic VARNAME = { DEBUG_TYPE, DESC, 0, 0 }<br>
> +#define STATISTIC(VARNAME, DESC) \<br>
> + static llvm::Statistic VARNAME = {DEBUG_TYPE, DESC, {0}, 0}<br>
><br>
> /// \brief Enable the collection and printing of statistics.<br>
> void EnableStatistics();<br>
><br>
><br>
> _______________________________________________<br>
> llvm-commits mailing list<br>
> <a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>