<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[181481] trunk/Source</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/181481">181481</a></dd>
<dt>Author</dt> <dd>mark.lam@apple.com</dd>
<dt>Date</dt> <dd>2015-03-13 11:02:40 -0700 (Fri, 13 Mar 2015)</dd>
</dl>
<h3>Log Message</h3>
<pre>Introduce WTF::Atomic to wrap std::atomic for a friendlier CAS.
<https://webkit.org/b/142661>
Reviewed by Filip Pizlo.
Source/JavaScriptCore:
Changed CodeBlock, and the DFG's crashLock to use WTF::Atomic instead of
std::atomic.
* bytecode/CodeBlock.cpp:
(JSC::CodeBlock::CodeBlock):
(JSC::CodeBlock::visitAggregate):
* bytecode/CodeBlock.h:
* dfg/DFGCommon.cpp:
(JSC::DFG::startCrashing):
Source/WTF:
The CAS functions provided by std::atomic takes a reference to the expected
value and modifies it if the CAS fails. However, in a lot of our CAS usage,
we don't want the expected value to change. The solution to this is to
provide a WTF::Atomic struct that wraps std::atomic, and provide CAS
methods that won't alter the expected value if the CAS fails.
The method names in WTF::Atomic are chosen to be identical to those
in std::atomic so that WTF::Atomic can be a simple drop in replacement
for std::atomic.
Also changed ByteSpinLock to use WTF::Atomic instead of std::atomic.
* wtf/Atomics.h:
(WTF::Atomic::load):
(WTF::Atomic::store):
(WTF::Atomic::compare_exchange_weak):
(WTF::Atomic::compare_exchange_strong):
* wtf/ByteSpinLock.h:
(WTF::ByteSpinLock::ByteSpinLock):
(WTF::ByteSpinLock::lock):</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCorebytecodeCodeBlockcpp">trunk/Source/JavaScriptCore/bytecode/CodeBlock.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorebytecodeCodeBlockh">trunk/Source/JavaScriptCore/bytecode/CodeBlock.h</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGCommoncpp">trunk/Source/JavaScriptCore/dfg/DFGCommon.cpp</a></li>
<li><a href="#trunkSourceWTFChangeLog">trunk/Source/WTF/ChangeLog</a></li>
<li><a href="#trunkSourceWTFwtfAtomicsh">trunk/Source/WTF/wtf/Atomics.h</a></li>
<li><a href="#trunkSourceWTFwtfByteSpinLockh">trunk/Source/WTF/wtf/ByteSpinLock.h</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (181480 => 181481)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2015-03-13 17:58:50 UTC (rev 181480)
+++ trunk/Source/JavaScriptCore/ChangeLog        2015-03-13 18:02:40 UTC (rev 181481)
</span><span class="lines">@@ -1,3 +1,20 @@
</span><ins>+2015-03-13 Mark Lam <mark.lam@apple.com>
+
+ Introduce WTF::Atomic to wrap std::atomic for a friendlier CAS.
+ <https://webkit.org/b/142661>
+
+ Reviewed by Filip Pizlo.
+
+ Changed CodeBlock, and the DFG's crashLock to use WTF::Atomic instead of
+ std::atomic.
+
+ * bytecode/CodeBlock.cpp:
+ (JSC::CodeBlock::CodeBlock):
+ (JSC::CodeBlock::visitAggregate):
+ * bytecode/CodeBlock.h:
+ * dfg/DFGCommon.cpp:
+ (JSC::DFG::startCrashing):
+
</ins><span class="cx"> 2015-03-12 Mark Lam <mark.lam@apple.com>
</span><span class="cx">
</span><span class="cx"> Change the DFG crashLock to use std::atomic.
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorebytecodeCodeBlockcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/bytecode/CodeBlock.cpp (181480 => 181481)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/bytecode/CodeBlock.cpp        2015-03-13 17:58:50 UTC (rev 181480)
+++ trunk/Source/JavaScriptCore/bytecode/CodeBlock.cpp        2015-03-13 18:02:40 UTC (rev 181481)
</span><span class="lines">@@ -1645,7 +1645,6 @@
</span><span class="cx"> , m_isStrictMode(other.m_isStrictMode)
</span><span class="cx"> , m_needsActivation(other.m_needsActivation)
</span><span class="cx"> , m_mayBeExecuting(false)
</span><del>- , m_visitAggregateHasBeenCalled(false)
</del><span class="cx"> , m_source(other.m_source)
</span><span class="cx"> , m_sourceOffset(other.m_sourceOffset)
</span><span class="cx"> , m_firstLineColumnOffset(other.m_firstLineColumnOffset)
</span><span class="lines">@@ -1662,6 +1661,8 @@
</span><span class="cx"> , m_capabilityLevelState(DFG::CapabilityLevelNotSet)
</span><span class="cx"> #endif
</span><span class="cx"> {
</span><ins>+ m_visitAggregateHasBeenCalled.store(false, std::memory_order_relaxed);
+
</ins><span class="cx"> ASSERT(m_heap->isDeferred());
</span><span class="cx"> ASSERT(m_scopeRegister.isLocal());
</span><span class="cx">
</span><span class="lines">@@ -1707,7 +1708,6 @@
</span><span class="cx"> , m_isStrictMode(unlinkedCodeBlock->isStrictMode())
</span><span class="cx"> , m_needsActivation(unlinkedCodeBlock->hasActivationRegister() && unlinkedCodeBlock->codeType() == FunctionCode)
</span><span class="cx"> , m_mayBeExecuting(false)
</span><del>- , m_visitAggregateHasBeenCalled(false)
</del><span class="cx"> , m_source(sourceProvider)
</span><span class="cx"> , m_sourceOffset(sourceOffset)
</span><span class="cx"> , m_firstLineColumnOffset(firstLineColumnOffset)
</span><span class="lines">@@ -1719,6 +1719,8 @@
</span><span class="cx"> , m_capabilityLevelState(DFG::CapabilityLevelNotSet)
</span><span class="cx"> #endif
</span><span class="cx"> {
</span><ins>+ m_visitAggregateHasBeenCalled.store(false, std::memory_order_relaxed);
+
</ins><span class="cx"> ASSERT(m_heap->isDeferred());
</span><span class="cx"> ASSERT(m_scopeRegister.isLocal());
</span><span class="cx">
</span><span class="lines">@@ -2202,8 +2204,7 @@
</span><span class="cx"> // I may be asked to scan myself more than once, and it may even happen concurrently.
</span><span class="cx"> // To this end, use an atomic operation to check (and set) if I've been called already.
</span><span class="cx"> // Only one thread may proceed past this point - whichever one wins the atomic set race.
</span><del>- bool expected = false;
- bool setByMe = m_visitAggregateHasBeenCalled.compare_exchange_strong(expected, true, std::memory_order_acquire);
</del><ins>+ bool setByMe = m_visitAggregateHasBeenCalled.compare_exchange_strong(false, true, std::memory_order_acquire);
</ins><span class="cx"> if (!setByMe)
</span><span class="cx"> return;
</span><span class="cx"> #endif // ENABLE(PARALLEL_GC)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorebytecodeCodeBlockh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/bytecode/CodeBlock.h (181480 => 181481)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/bytecode/CodeBlock.h        2015-03-13 17:58:50 UTC (rev 181480)
+++ trunk/Source/JavaScriptCore/bytecode/CodeBlock.h        2015-03-13 18:02:40 UTC (rev 181481)
</span><span class="lines">@@ -1065,7 +1065,7 @@
</span><span class="cx"> bool m_isStrictMode;
</span><span class="cx"> bool m_needsActivation;
</span><span class="cx"> bool m_mayBeExecuting;
</span><del>- std::atomic<bool> m_visitAggregateHasBeenCalled;
</del><ins>+ Atomic<bool> m_visitAggregateHasBeenCalled;
</ins><span class="cx">
</span><span class="cx"> RefPtr<SourceProvider> m_source;
</span><span class="cx"> unsigned m_sourceOffset;
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGCommoncpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGCommon.cpp (181480 => 181481)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGCommon.cpp        2015-03-13 17:58:50 UTC (rev 181480)
+++ trunk/Source/JavaScriptCore/dfg/DFGCommon.cpp        2015-03-13 18:02:40 UTC (rev 181481)
</span><span class="lines">@@ -34,15 +34,12 @@
</span><span class="cx">
</span><span class="cx"> namespace JSC { namespace DFG {
</span><span class="cx">
</span><del>-static std::atomic<unsigned> crashLock;
</del><ins>+static Atomic<unsigned> crashLock;
</ins><span class="cx">
</span><span class="cx"> void startCrashing()
</span><span class="cx"> {
</span><del>- unsigned expected = 0;
- while (!crashLock.compare_exchange_weak(expected, 1, std::memory_order_acquire)) {
</del><ins>+ while (!crashLock.compare_exchange_weak(0, 1, std::memory_order_acquire))
</ins><span class="cx"> std::this_thread::yield();
</span><del>- expected = 0;
- }
</del><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> bool isCrashing()
</span></span></pre></div>
<a id="trunkSourceWTFChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/ChangeLog (181480 => 181481)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/ChangeLog        2015-03-13 17:58:50 UTC (rev 181480)
+++ trunk/Source/WTF/ChangeLog        2015-03-13 18:02:40 UTC (rev 181481)
</span><span class="lines">@@ -1,3 +1,31 @@
</span><ins>+2015-03-13 Mark Lam <mark.lam@apple.com>
+
+ Introduce WTF::Atomic to wrap std::atomic for a friendlier CAS.
+ <https://webkit.org/b/142661>
+
+ Reviewed by Filip Pizlo.
+
+ The CAS functions provided by std::atomic takes a reference to the expected
+ value and modifies it if the CAS fails. However, in a lot of our CAS usage,
+ we don't want the expected value to change. The solution to this is to
+ provide a WTF::Atomic struct that wraps std::atomic, and provide CAS
+ methods that won't alter the expected value if the CAS fails.
+
+ The method names in WTF::Atomic are chosen to be identical to those
+ in std::atomic so that WTF::Atomic can be a simple drop in replacement
+ for std::atomic.
+
+ Also changed ByteSpinLock to use WTF::Atomic instead of std::atomic.
+
+ * wtf/Atomics.h:
+ (WTF::Atomic::load):
+ (WTF::Atomic::store):
+ (WTF::Atomic::compare_exchange_weak):
+ (WTF::Atomic::compare_exchange_strong):
+ * wtf/ByteSpinLock.h:
+ (WTF::ByteSpinLock::ByteSpinLock):
+ (WTF::ByteSpinLock::lock):
+
</ins><span class="cx"> 2015-03-12 Filip Pizlo <fpizlo@apple.com>
</span><span class="cx">
</span><span class="cx"> Bytecode liveness analysis should have more lambdas and fewer sets
</span></span></pre></div>
<a id="trunkSourceWTFwtfAtomicsh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/Atomics.h (181480 => 181481)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/Atomics.h        2015-03-13 17:58:50 UTC (rev 181480)
+++ trunk/Source/WTF/wtf/Atomics.h        2015-03-13 18:02:40 UTC (rev 181481)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2007, 2008, 2010, 2012, 2013 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2007-2008, 2010, 2012-2013, 2015 Apple Inc. All rights reserved.
</ins><span class="cx"> * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com)
</span><span class="cx"> *
</span><span class="cx"> * Redistribution and use in source and binary forms, with or without
</span><span class="lines">@@ -59,6 +59,7 @@
</span><span class="cx"> #ifndef Atomics_h
</span><span class="cx"> #define Atomics_h
</span><span class="cx">
</span><ins>+#include <atomic>
</ins><span class="cx"> #include <wtf/StdLibExtras.h>
</span><span class="cx">
</span><span class="cx"> #if OS(WINDOWS)
</span><span class="lines">@@ -71,6 +72,35 @@
</span><span class="cx">
</span><span class="cx"> namespace WTF {
</span><span class="cx">
</span><ins>+// Atomic wraps around std::atomic with the sole purpose of making the compare_exchange
+// operations not alter the expected value. This is more in line with how we typically
+// use CAS in our code.
+//
+// Atomic is a struct without explicitly defined constructors so that it can be
+// initialized at compile time.
+
+template<typename T>
+struct Atomic {
+
+ T load(std::memory_order order) const { return value.load(order); }
+
+ void store(T desired, std::memory_order order) { value.store(desired, order); }
+
+ bool compare_exchange_weak(T expected, T desired, std::memory_order order)
+ {
+ T expectedOrActual = expected;
+ return value.compare_exchange_weak(expectedOrActual, desired, order);
+ }
+
+ bool compare_exchange_strong(T expected, T desired, std::memory_order order)
+ {
+ T expectedOrActual = expected;
+ return value.compare_exchange_strong(expectedOrActual, desired, order);
+ }
+
+ std::atomic<T> value;
+};
+
</ins><span class="cx"> #if OS(WINDOWS)
</span><span class="cx"> inline bool weakCompareAndSwap(volatile unsigned* location, unsigned expected, unsigned newValue)
</span><span class="cx"> {
</span><span class="lines">@@ -345,4 +375,6 @@
</span><span class="cx">
</span><span class="cx"> } // namespace WTF
</span><span class="cx">
</span><ins>+using WTF::Atomic;
+
</ins><span class="cx"> #endif // Atomics_h
</span></span></pre></div>
<a id="trunkSourceWTFwtfByteSpinLockh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/ByteSpinLock.h (181480 => 181481)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/ByteSpinLock.h        2015-03-13 17:58:50 UTC (rev 181480)
+++ trunk/Source/WTF/wtf/ByteSpinLock.h        2015-03-13 18:02:40 UTC (rev 181481)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2013 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2013, 2015 Apple Inc. All rights reserved.
</ins><span class="cx"> *
</span><span class="cx"> * Redistribution and use in source and binary forms, with or without
</span><span class="cx"> * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -26,9 +26,9 @@
</span><span class="cx"> #ifndef ByteSpinLock_h
</span><span class="cx"> #define ByteSpinLock_h
</span><span class="cx">
</span><del>-#include <atomic>
</del><span class="cx"> #include <thread>
</span><span class="cx"> #include <wtf/Assertions.h>
</span><ins>+#include <wtf/Atomics.h>
</ins><span class="cx"> #include <wtf/Locker.h>
</span><span class="cx"> #include <wtf/Noncopyable.h>
</span><span class="cx">
</span><span class="lines">@@ -38,17 +38,14 @@
</span><span class="cx"> WTF_MAKE_NONCOPYABLE(ByteSpinLock);
</span><span class="cx"> public:
</span><span class="cx"> ByteSpinLock()
</span><del>- : m_lock(false)
</del><span class="cx"> {
</span><ins>+ m_lock.store(false, std::memory_order_relaxed);
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> void lock()
</span><span class="cx"> {
</span><del>- bool expected = false;
- while (!m_lock.compare_exchange_weak(expected, true, std::memory_order_acquire)) {
</del><ins>+ while (!m_lock.compare_exchange_weak(false, true, std::memory_order_acquire))
</ins><span class="cx"> std::this_thread::yield();
</span><del>- expected = false;
- }
</del><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> void unlock()
</span><span class="lines">@@ -59,7 +56,7 @@
</span><span class="cx"> bool isHeld() const { return m_lock.load(std::memory_order_acquire); }
</span><span class="cx">
</span><span class="cx"> private:
</span><del>- std::atomic<bool> m_lock;
</del><ins>+ Atomic<bool> m_lock;
</ins><span class="cx"> };
</span><span class="cx">
</span><span class="cx"> typedef Locker<ByteSpinLock> ByteSpinLocker;
</span></span></pre>
</div>
</div>
</body>
</html>