<!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>[199760] trunk/Source/WTF</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/199760">199760</a></dd>
<dt>Author</dt> <dd>fpizlo@apple.com</dd>
<dt>Date</dt> <dd>2016-04-19 21:25:02 -0700 (Tue, 19 Apr 2016)</dd>
</dl>
<h3>Log Message</h3>
<pre>Clean up the ParkingLot uparking API a bit
https://bugs.webkit.org/show_bug.cgi?id=156746
Reviewed by Saam Barati and Geoffrey Garen.
Previously, unparkOne() would either return a boolean to tell you if there are any more threads on
the queue or it would pass your callback a pair of booleans - one to tell if a thread was unparked
and another to tell if there are any more threads. This was an annoying inconsistency. What if you
wanted to know if unparkOne() unparked a thread but you don't care to use callbacks?
This fixes unparkOne() to use a struct called UnparkResult for both of its variants. This makes the
code a bit cleaner.
* wtf/Atomics.h: Add some more atomic ops.
(WTF::Atomic::exchangeAndAdd):
(WTF::Atomic::exchange):
* wtf/Condition.h: Change calls to unparkOne().
(WTF::ConditionBase::notifyOne):
* wtf/Lock.cpp: Change calls to unparkOne().
(WTF::LockBase::unlockSlow):
* wtf/ParkingLot.cpp:
(WTF::ParkingLot::parkConditionally):
(WTF::ParkingLot::unparkOne):
* wtf/ParkingLot.h: Switch to using ScopedLambda and introduce UnparkResult.</pre>
<h3>Modified Paths</h3>
<ul>
<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="#trunkSourceWTFwtfConditionh">trunk/Source/WTF/wtf/Condition.h</a></li>
<li><a href="#trunkSourceWTFwtfLockcpp">trunk/Source/WTF/wtf/Lock.cpp</a></li>
<li><a href="#trunkSourceWTFwtfParkingLotcpp">trunk/Source/WTF/wtf/ParkingLot.cpp</a></li>
<li><a href="#trunkSourceWTFwtfParkingLoth">trunk/Source/WTF/wtf/ParkingLot.h</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWTFChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/ChangeLog (199759 => 199760)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/ChangeLog        2016-04-20 04:14:02 UTC (rev 199759)
+++ trunk/Source/WTF/ChangeLog        2016-04-20 04:25:02 UTC (rev 199760)
</span><span class="lines">@@ -1,3 +1,30 @@
</span><ins>+2016-04-19 Filip Pizlo <fpizlo@apple.com>
+
+ Clean up the ParkingLot uparking API a bit
+ https://bugs.webkit.org/show_bug.cgi?id=156746
+
+ Reviewed by Saam Barati and Geoffrey Garen.
+
+ Previously, unparkOne() would either return a boolean to tell you if there are any more threads on
+ the queue or it would pass your callback a pair of booleans - one to tell if a thread was unparked
+ and another to tell if there are any more threads. This was an annoying inconsistency. What if you
+ wanted to know if unparkOne() unparked a thread but you don't care to use callbacks?
+
+ This fixes unparkOne() to use a struct called UnparkResult for both of its variants. This makes the
+ code a bit cleaner.
+
+ * wtf/Atomics.h: Add some more atomic ops.
+ (WTF::Atomic::exchangeAndAdd):
+ (WTF::Atomic::exchange):
+ * wtf/Condition.h: Change calls to unparkOne().
+ (WTF::ConditionBase::notifyOne):
+ * wtf/Lock.cpp: Change calls to unparkOne().
+ (WTF::LockBase::unlockSlow):
+ * wtf/ParkingLot.cpp:
+ (WTF::ParkingLot::parkConditionally):
+ (WTF::ParkingLot::unparkOne):
+ * wtf/ParkingLot.h: Switch to using ScopedLambda and introduce UnparkResult.
+
</ins><span class="cx"> 2016-04-19 Commit Queue <commit-queue@webkit.org>
</span><span class="cx">
</span><span class="cx"> Unreviewed, rolling out r199726.
</span></span></pre></div>
<a id="trunkSourceWTFwtfAtomicsh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/Atomics.h (199759 => 199760)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/Atomics.h        2016-04-20 04:14:02 UTC (rev 199759)
+++ trunk/Source/WTF/wtf/Atomics.h        2016-04-20 04:25:02 UTC (rev 199760)
</span><span class="lines">@@ -76,6 +76,25 @@
</span><span class="cx"> T expectedOrActual = expected;
</span><span class="cx"> return value.compare_exchange_strong(expectedOrActual, desired, order);
</span><span class="cx"> }
</span><ins>+
+ template<typename U>
+ T exchangeAndAdd(U addend, std::memory_order order = std::memory_order_seq_cst)
+ {
+#if OS(WINDOWS)
+ // See above.
+ order = std::memory_order_seq_cst;
+#endif
+ return value.fetch_add(addend, order);
+ }
+
+ T exchange(T newValue, std::memory_order order = std::memory_order_seq_cst)
+ {
+#if OS(WINDOWS)
+ // See above.
+ order = std::memory_order_seq_cst;
+#endif
+ return value.exchange(newValue, order);
+ }
</ins><span class="cx">
</span><span class="cx"> std::atomic<T> value;
</span><span class="cx"> };
</span></span></pre></div>
<a id="trunkSourceWTFwtfConditionh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/Condition.h (199759 => 199760)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/Condition.h        2016-04-20 04:14:02 UTC (rev 199759)
+++ trunk/Source/WTF/wtf/Condition.h        2016-04-20 04:25:02 UTC (rev 199760)
</span><span class="lines">@@ -180,8 +180,8 @@
</span><span class="cx">
</span><span class="cx"> ParkingLot::unparkOne(
</span><span class="cx"> &m_hasWaiters,
</span><del>- [this] (bool, bool mayHaveMoreThreads) {
- if (!mayHaveMoreThreads)
</del><ins>+ [this] (ParkingLot::UnparkResult result) {
+ if (!result.mayHaveMoreThreads)
</ins><span class="cx"> m_hasWaiters.store(false);
</span><span class="cx"> });
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWTFwtfLockcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/Lock.cpp (199759 => 199760)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/Lock.cpp        2016-04-20 04:14:02 UTC (rev 199759)
+++ trunk/Source/WTF/wtf/Lock.cpp        2016-04-20 04:25:02 UTC (rev 199760)
</span><span class="lines">@@ -94,12 +94,12 @@
</span><span class="cx"> ASSERT(oldByteValue == (isHeldBit | hasParkedBit));
</span><span class="cx"> ParkingLot::unparkOne(
</span><span class="cx"> &m_byte,
</span><del>- [this] (bool, bool mayHaveMoreThreads) {
</del><ins>+ [this] (ParkingLot::UnparkResult result) {
</ins><span class="cx"> // We are the only ones that can clear either the isHeldBit or the hasParkedBit,
</span><span class="cx"> // so we should still see both bits set right now.
</span><span class="cx"> ASSERT(m_byte.load() == (isHeldBit | hasParkedBit));
</span><span class="cx">
</span><del>- if (mayHaveMoreThreads)
</del><ins>+ if (result.mayHaveMoreThreads)
</ins><span class="cx"> m_byte.store(hasParkedBit);
</span><span class="cx"> else
</span><span class="cx"> m_byte.store(0);
</span></span></pre></div>
<a id="trunkSourceWTFwtfParkingLotcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/ParkingLot.cpp (199759 => 199760)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/ParkingLot.cpp        2016-04-20 04:14:02 UTC (rev 199759)
+++ trunk/Source/WTF/wtf/ParkingLot.cpp        2016-04-20 04:25:02 UTC (rev 199760)
</span><span class="lines">@@ -530,10 +530,10 @@
</span><span class="cx">
</span><span class="cx"> } // anonymous namespace
</span><span class="cx">
</span><del>-NEVER_INLINE bool ParkingLot::parkConditionally(
</del><ins>+NEVER_INLINE bool ParkingLot::parkConditionallyImpl(
</ins><span class="cx"> const void* address,
</span><del>- std::function<bool()> validation,
- std::function<void()> beforeSleep,
</del><ins>+ const ScopedLambda<bool()>& validation,
+ const ScopedLambda<void()>& beforeSleep,
</ins><span class="cx"> Clock::time_point timeout)
</span><span class="cx"> {
</span><span class="cx"> if (verbose)
</span><span class="lines">@@ -591,14 +591,11 @@
</span><span class="cx"> // probably worthwhile to detect when this happens, and return true in that case, to ensure
</span><span class="cx"> // that when we return false it really means that no unpark could have been responsible for us
</span><span class="cx"> // waking up, and that if an unpark call did happen, it woke someone else up.
</span><del>- bool didFind = false;
</del><span class="cx"> dequeue(
</span><span class="cx"> address, BucketMode::IgnoreEmpty,
</span><span class="cx"> [&] (ThreadData* element) {
</span><del>- if (element == me) {
- didFind = true;
</del><ins>+ if (element == me)
</ins><span class="cx"> return DequeueResult::RemoveAndStop;
</span><del>- }
</del><span class="cx"> return DequeueResult::Ignore;
</span><span class="cx"> },
</span><span class="cx"> [] (bool) { });
</span><span class="lines">@@ -612,29 +609,35 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> // If we were not found in the search above, then we know that someone unparked us.
</span><del>- return !didFind;
</del><ins>+ return false;
</ins><span class="cx"> }
</span><span class="cx">
</span><del>-NEVER_INLINE bool ParkingLot::unparkOne(const void* address)
</del><ins>+NEVER_INLINE ParkingLot::UnparkResult ParkingLot::unparkOne(const void* address)
</ins><span class="cx"> {
</span><span class="cx"> if (verbose)
</span><span class="cx"> dataLog(toString(currentThread(), ": unparking one.\n"));
</span><ins>+
+ UnparkResult result;
</ins><span class="cx">
</span><span class="cx"> ThreadData* threadData = nullptr;
</span><del>- bool result = dequeue(
</del><ins>+ result.mayHaveMoreThreads = dequeue(
</ins><span class="cx"> address,
</span><del>- BucketMode::IgnoreEmpty,
</del><ins>+ BucketMode::EnsureNonEmpty,
</ins><span class="cx"> [&] (ThreadData* element) {
</span><span class="cx"> if (element->address != address)
</span><span class="cx"> return DequeueResult::Ignore;
</span><span class="cx"> threadData = element;
</span><ins>+ result.didUnparkThread = true;
</ins><span class="cx"> return DequeueResult::RemoveAndStop;
</span><span class="cx"> },
</span><span class="cx"> [] (bool) { });
</span><span class="cx">
</span><del>- if (!threadData)
- return false;
-
</del><ins>+ if (!threadData) {
+ ASSERT(!result.didUnparkThread);
+ result.mayHaveMoreThreads = false;
+ return result;
+ }
+
</ins><span class="cx"> ASSERT(threadData->address);
</span><span class="cx">
</span><span class="cx"> {
</span><span class="lines">@@ -646,9 +649,9 @@
</span><span class="cx"> return result;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-NEVER_INLINE void ParkingLot::unparkOne(
</del><ins>+NEVER_INLINE void ParkingLot::unparkOneImpl(
</ins><span class="cx"> const void* address,
</span><del>- std::function<void(bool didUnparkThread, bool mayHaveMoreThreads)> callback)
</del><ins>+ const ScopedLambda<void(ParkingLot::UnparkResult)>& callback)
</ins><span class="cx"> {
</span><span class="cx"> if (verbose)
</span><span class="cx"> dataLog(toString(currentThread(), ": unparking one the hard way.\n"));
</span><span class="lines">@@ -664,7 +667,10 @@
</span><span class="cx"> return DequeueResult::RemoveAndStop;
</span><span class="cx"> },
</span><span class="cx"> [&] (bool mayHaveMoreThreads) {
</span><del>- callback(!!threadData, threadData && mayHaveMoreThreads);
</del><ins>+ UnparkResult result;
+ result.didUnparkThread = !!threadData;
+ result.mayHaveMoreThreads = result.didUnparkThread && mayHaveMoreThreads;
+ callback(result);
</ins><span class="cx"> });
</span><span class="cx">
</span><span class="cx"> if (!threadData)
</span><span class="lines">@@ -713,7 +719,7 @@
</span><span class="cx"> dataLog(toString(currentThread(), ": done unparking.\n"));
</span><span class="cx"> }
</span><span class="cx">
</span><del>-NEVER_INLINE void ParkingLot::forEach(std::function<void(ThreadIdentifier, const void*)> callback)
</del><ins>+NEVER_INLINE void ParkingLot::forEachImpl(const ScopedLambda<void(ThreadIdentifier, const void*)>& callback)
</ins><span class="cx"> {
</span><span class="cx"> Vector<Bucket*> bucketsToUnlock = lockHashtable();
</span><span class="cx">
</span></span></pre></div>
<a id="trunkSourceWTFwtfParkingLoth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/ParkingLot.h (199759 => 199760)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/ParkingLot.h        2016-04-20 04:14:02 UTC (rev 199759)
+++ trunk/Source/WTF/wtf/ParkingLot.h        2016-04-20 04:25:02 UTC (rev 199760)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2015 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2015-2016 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">@@ -29,6 +29,7 @@
</span><span class="cx"> #include <chrono>
</span><span class="cx"> #include <functional>
</span><span class="cx"> #include <wtf/Atomics.h>
</span><ins>+#include <wtf/ScopedLambda.h>
</ins><span class="cx"> #include <wtf/Threading.h>
</span><span class="cx">
</span><span class="cx"> namespace WTF {
</span><span class="lines">@@ -51,11 +52,19 @@
</span><span class="cx"> // you don't recursively call parkConditionally(). You can call unparkOne()/unparkAll() though.
</span><span class="cx"> // It's useful to use beforeSleep() to unlock some mutex in the implementation of
</span><span class="cx"> // Condition::wait().
</span><del>- WTF_EXPORT_PRIVATE static bool parkConditionally(
</del><ins>+ template<typename ValidationFunctor, typename BeforeSleepFunctor>
+ static bool parkConditionally(
</ins><span class="cx"> const void* address,
</span><del>- std::function<bool()> validation,
- std::function<void()> beforeSleep,
- Clock::time_point timeout);
</del><ins>+ ValidationFunctor&& validation,
+ BeforeSleepFunctor&& beforeSleep,
+ Clock::time_point timeout)
+ {
+ return parkConditionallyImpl(
+ address,
+ scopedLambda<bool()>(std::forward<ValidationFunctor>(validation)),
+ scopedLambda<void()>(std::forward<BeforeSleepFunctor>(beforeSleep)),
+ timeout);
+ }
</ins><span class="cx">
</span><span class="cx"> // Simple version of parkConditionally() that covers the most common case: you want to park
</span><span class="cx"> // indefinitely so long as the value at the given address hasn't changed.
</span><span class="lines">@@ -75,7 +84,11 @@
</span><span class="cx"> // Unparks one thread from the queue associated with the given address, which cannot be null.
</span><span class="cx"> // Returns true if there may still be other threads on that queue, or false if there definitely
</span><span class="cx"> // are no more threads on the queue.
</span><del>- WTF_EXPORT_PRIVATE static bool unparkOne(const void* address);
</del><ins>+ struct UnparkResult {
+ bool didUnparkThread { false };
+ bool mayHaveMoreThreads { false };
+ };
+ WTF_EXPORT_PRIVATE static UnparkResult unparkOne(const void* address);
</ins><span class="cx">
</span><span class="cx"> // Unparks one thread from the queue associated with the given address, and calls the given
</span><span class="cx"> // functor while the address is locked. Reports to the callback whether any thread got unparked
</span><span class="lines">@@ -88,9 +101,11 @@
</span><span class="cx"> // that race - see Rusty Russel's well-known usersem library - but it's not pretty. This form
</span><span class="cx"> // allows that race to be completely avoided, since there is no way that a thread can be parked
</span><span class="cx"> // while the callback is running.
</span><del>- WTF_EXPORT_PRIVATE static void unparkOne(
- const void* address,
- std::function<void(bool didUnparkThread, bool mayHaveMoreThreads)> callback);
</del><ins>+ template<typename CallbackFunctor>
+ static void unparkOne(const void* address, CallbackFunctor&& callback)
+ {
+ unparkOneImpl(address, scopedLambda<void(UnparkResult)>(std::forward<CallbackFunctor>(callback)));
+ }
</ins><span class="cx">
</span><span class="cx"> // Unparks every thread from the queue associated with the given address, which cannot be null.
</span><span class="cx"> WTF_EXPORT_PRIVATE static void unparkAll(const void* address);
</span><span class="lines">@@ -108,7 +123,23 @@
</span><span class="cx"> // As well as many other possible interleavings that all have T1 before T2 and T3 before T4 but are
</span><span class="cx"> // otherwise unconstrained. This method is useful primarily for debugging. It's also used by unit
</span><span class="cx"> // tests.
</span><del>- WTF_EXPORT_PRIVATE static void forEach(std::function<void(ThreadIdentifier, const void*)>);
</del><ins>+ template<typename CallbackFunctor>
+ static void forEach(CallbackFunctor&& callback)
+ {
+ forEachImpl(scopedLambda<void(ThreadIdentifier, const void*)>(std::forward<CallbackFunctor>(callback)));
+ }
+
+private:
+ WTF_EXPORT_PRIVATE static bool parkConditionallyImpl(
+ const void* address,
+ const ScopedLambda<bool()>& validation,
+ const ScopedLambda<void()>& beforeSleep,
+ Clock::time_point timeout);
+
+ WTF_EXPORT_PRIVATE static void unparkOneImpl(
+ const void* address, const ScopedLambda<void(UnparkResult)>& callback);
+
+ WTF_EXPORT_PRIVATE static void forEachImpl(const ScopedLambda<void(ThreadIdentifier, const void*)>&);
</ins><span class="cx"> };
</span><span class="cx">
</span><span class="cx"> } // namespace WTF
</span></span></pre>
</div>
</div>
</body>
</html>