[WTF] Use Ref<> for the result type of non-failing factory functions
authorutatane.tea@gmail.com <utatane.tea@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 23 Jun 2018 09:59:04 +0000 (09:59 +0000)
committerutatane.tea@gmail.com <utatane.tea@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 23 Jun 2018 09:59:04 +0000 (09:59 +0000)
https://bugs.webkit.org/show_bug.cgi?id=186920

Reviewed by Darin Adler.

Source/JavaScriptCore:

* dfg/DFGWorklist.cpp:
(JSC::DFG::Worklist::ThreadBody::ThreadBody):
(JSC::DFG::Worklist::finishCreation):
* dfg/DFGWorklist.h:
* heap/Heap.cpp:
(JSC::Heap::Thread::Thread):
* heap/Heap.h:
* jit/JITWorklist.cpp:
(JSC::JITWorklist::Thread::Thread):
* jit/JITWorklist.h:
* runtime/VMTraps.cpp:
* runtime/VMTraps.h:
* wasm/WasmWorklist.cpp:
* wasm/WasmWorklist.h:

Source/WTF:

Use Ref<> instead of RefPtr<> if the `create` function do not return nullptr.

* wtf/AutomaticThread.cpp:
(WTF::AutomaticThreadCondition::create):
(WTF::AutomaticThread::AutomaticThread):
* wtf/AutomaticThread.h:
* wtf/ParallelHelperPool.cpp:
(WTF::ParallelHelperPool::Thread::Thread):
* wtf/ParallelHelperPool.h:
* wtf/WorkerPool.cpp:
(WTF::WorkerPool::WorkerPool):
* wtf/WorkerPool.h:
* wtf/win/WorkQueueWin.cpp:
(WTF::TimerContext::create):
(WTF::WorkQueue::dispatchAfter):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@233123 268f45cc-cd09-0410-ab3c-d52691b4dbfc

19 files changed:
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/dfg/DFGWorklist.cpp
Source/JavaScriptCore/dfg/DFGWorklist.h
Source/JavaScriptCore/heap/Heap.cpp
Source/JavaScriptCore/heap/Heap.h
Source/JavaScriptCore/jit/JITWorklist.cpp
Source/JavaScriptCore/jit/JITWorklist.h
Source/JavaScriptCore/runtime/VMTraps.cpp
Source/JavaScriptCore/runtime/VMTraps.h
Source/JavaScriptCore/wasm/WasmWorklist.cpp
Source/JavaScriptCore/wasm/WasmWorklist.h
Source/WTF/ChangeLog
Source/WTF/wtf/AutomaticThread.cpp
Source/WTF/wtf/AutomaticThread.h
Source/WTF/wtf/ParallelHelperPool.cpp
Source/WTF/wtf/ParallelHelperPool.h
Source/WTF/wtf/WorkerPool.cpp
Source/WTF/wtf/WorkerPool.h
Source/WTF/wtf/win/WorkQueueWin.cpp

index f2578c1..92dd9b4 100644 (file)
@@ -1,3 +1,25 @@
+2018-06-22  Yusuke Suzuki  <utatane.tea@gmail.com>
+
+        [WTF] Use Ref<> for the result type of non-failing factory functions
+        https://bugs.webkit.org/show_bug.cgi?id=186920
+
+        Reviewed by Darin Adler.
+
+        * dfg/DFGWorklist.cpp:
+        (JSC::DFG::Worklist::ThreadBody::ThreadBody):
+        (JSC::DFG::Worklist::finishCreation):
+        * dfg/DFGWorklist.h:
+        * heap/Heap.cpp:
+        (JSC::Heap::Thread::Thread):
+        * heap/Heap.h:
+        * jit/JITWorklist.cpp:
+        (JSC::JITWorklist::Thread::Thread):
+        * jit/JITWorklist.h:
+        * runtime/VMTraps.cpp:
+        * runtime/VMTraps.h:
+        * wasm/WasmWorklist.cpp:
+        * wasm/WasmWorklist.h:
+
 2018-06-23  Yusuke Suzuki  <utatane.tea@gmail.com>
 
         [WTF] Add user-defined literal for ASCIILiteral
index 5bc88c4..7536d0c 100644 (file)
@@ -39,8 +39,8 @@ namespace JSC { namespace DFG {
 
 class Worklist::ThreadBody : public AutomaticThread {
 public:
-    ThreadBody(const AbstractLocker& locker, Worklist& worklist, ThreadData& data, Box<Lock> lock, RefPtr<AutomaticThreadCondition> condition, int relativePriority)
-        : AutomaticThread(locker, lock, condition)
+    ThreadBody(const AbstractLocker& locker, Worklist& worklist, ThreadData& data, Box<Lock> lock, Ref<AutomaticThreadCondition>&& condition, int relativePriority)
+        : AutomaticThread(locker, lock, WTFMove(condition))
         , m_worklist(worklist)
         , m_data(data)
         , m_relativePriority(relativePriority)
@@ -201,7 +201,7 @@ void Worklist::finishCreation(unsigned numberOfThreads, int relativePriority)
     LockHolder locker(*m_lock);
     for (unsigned i = numberOfThreads; i--;) {
         std::unique_ptr<ThreadData> data = std::make_unique<ThreadData>(this);
-        data->m_thread = adoptRef(new ThreadBody(locker, *this, *data, m_lock, m_planEnqueued, relativePriority));
+        data->m_thread = adoptRef(new ThreadBody(locker, *this, *data, m_lock, m_planEnqueued.copyRef(), relativePriority));
         m_threads.append(WTFMove(data));
     }
 }
index 4120559..7b681d1 100644 (file)
@@ -113,7 +113,7 @@ private:
     Lock m_suspensionLock;
     
     Box<Lock> m_lock;
-    RefPtr<AutomaticThreadCondition> m_planEnqueued;
+    Ref<AutomaticThreadCondition> m_planEnqueued;
     Condition m_planCompiled;
     
     Vector<std::unique_ptr<ThreadData>> m_threads;
index 1a35a75..57fbed3 100644 (file)
@@ -234,7 +234,7 @@ private:
 class Heap::Thread : public AutomaticThread {
 public:
     Thread(const AbstractLocker& locker, Heap& heap)
-        : AutomaticThread(locker, heap.m_threadLock, heap.m_threadCondition)
+        : AutomaticThread(locker, heap.m_threadLock, heap.m_threadCondition.copyRef())
         , m_heap(heap)
     {
     }
index ecc86b1..020226b 100644 (file)
@@ -701,7 +701,7 @@ private:
     uint64_t m_mutatorExecutionVersion { 0 };
     uint64_t m_phaseVersion { 0 };
     Box<Lock> m_threadLock;
-    RefPtr<AutomaticThreadCondition> m_threadCondition; // The mutator must not wait on this. It would cause a deadlock.
+    Ref<AutomaticThreadCondition> m_threadCondition; // The mutator must not wait on this. It would cause a deadlock.
     RefPtr<AutomaticThread> m_thread;
 
 #if PLATFORM(IOS)
index 0b32e1f..982102f 100644 (file)
@@ -100,7 +100,7 @@ private:
 class JITWorklist::Thread : public AutomaticThread {
 public:
     Thread(const AbstractLocker& locker, JITWorklist& worklist)
-        : AutomaticThread(locker, worklist.m_lock, worklist.m_condition)
+        : AutomaticThread(locker, worklist.m_lock, worklist.m_condition.copyRef())
         , m_worklist(worklist)
     {
         m_worklist.m_numAvailableThreads++;
index 7ea1c1e..adf9fb9 100644 (file)
@@ -72,7 +72,7 @@ private:
     HashSet<CodeBlock*> m_planned;
     
     Box<Lock> m_lock;
-    RefPtr<AutomaticThreadCondition> m_condition; // We use One True Condition for everything because that's easier.
+    Ref<AutomaticThreadCondition> m_condition; // We use One True Condition for everything because that's easier.
     RefPtr<AutomaticThread> m_thread;
     
     unsigned m_numAvailableThreads { 0 };
index 6c14c0a..c263419 100644 (file)
@@ -180,7 +180,7 @@ class VMTraps::SignalSender final : public AutomaticThread {
 public:
     using Base = AutomaticThread;
     SignalSender(const AbstractLocker& locker, VM& vm)
-        : Base(locker, vm.traps().m_lock, vm.traps().m_trapSet)
+        : Base(locker, vm.traps().m_lock, vm.traps().m_trapSet.copyRef())
         , m_vm(vm)
     {
         static std::once_flag once;
index 9b06cdd..9beef99 100644 (file)
@@ -146,7 +146,7 @@ private:
 #endif
 
     Box<Lock> m_lock;
-    RefPtr<AutomaticThreadCondition> m_trapSet;
+    Ref<AutomaticThreadCondition> m_trapSet;
     union {
         BitField m_needTrapHandling { 0 };
         BitField m_trapsBitField;
index 6b32533..51e7840 100644 (file)
@@ -58,7 +58,7 @@ class Worklist::Thread final : public AutomaticThread {
 public:
     using Base = AutomaticThread;
     Thread(const AbstractLocker& locker, Worklist& work)
-        : Base(locker, work.m_lock, work.m_planEnqueued)
+        : Base(locker, work.m_lock, work.m_planEnqueued.copyRef())
         , worklist(work)
     {
 
index 748a9ec..8edf249 100644 (file)
@@ -82,7 +82,7 @@ private:
     }
 
     Box<Lock> m_lock;
-    RefPtr<AutomaticThreadCondition> m_planEnqueued;
+    Ref<AutomaticThreadCondition> m_planEnqueued;
     // Technically, this could overflow but that's unlikely. Even if it did, we will just compile things of the same
     // Priority it the wrong order, which isn't wrong, just suboptimal.
     Ticket m_lastGrantedTicket { 0 };
index 2280a4d..cfd2118 100644 (file)
@@ -1,3 +1,26 @@
+2018-06-22  Yusuke Suzuki  <utatane.tea@gmail.com>
+
+        [WTF] Use Ref<> for the result type of non-failing factory functions
+        https://bugs.webkit.org/show_bug.cgi?id=186920
+
+        Reviewed by Darin Adler.
+
+        Use Ref<> instead of RefPtr<> if the `create` function do not return nullptr.
+
+        * wtf/AutomaticThread.cpp:
+        (WTF::AutomaticThreadCondition::create):
+        (WTF::AutomaticThread::AutomaticThread):
+        * wtf/AutomaticThread.h:
+        * wtf/ParallelHelperPool.cpp:
+        (WTF::ParallelHelperPool::Thread::Thread):
+        * wtf/ParallelHelperPool.h:
+        * wtf/WorkerPool.cpp:
+        (WTF::WorkerPool::WorkerPool):
+        * wtf/WorkerPool.h:
+        * wtf/win/WorkQueueWin.cpp:
+        (WTF::TimerContext::create):
+        (WTF::WorkQueue::dispatchAfter):
+
 2018-06-23  Yusuke Suzuki  <utatane.tea@gmail.com>
 
         [WTF] Add user-defined literal for ASCIILiteral
index f1156ac..afc65d5 100644 (file)
@@ -33,9 +33,9 @@ namespace WTF {
 
 static const bool verbose = false;
 
-RefPtr<AutomaticThreadCondition> AutomaticThreadCondition::create()
+Ref<AutomaticThreadCondition> AutomaticThreadCondition::create()
 {
-    return adoptRef(new AutomaticThreadCondition());
+    return adoptRef(*new AutomaticThreadCondition);
 }
 
 AutomaticThreadCondition::AutomaticThreadCondition()
@@ -104,9 +104,9 @@ bool AutomaticThreadCondition::contains(const AbstractLocker&, AutomaticThread*
     return m_threads.contains(thread);
 }
 
-AutomaticThread::AutomaticThread(const AbstractLocker& locker, Box<Lock> lock, RefPtr<AutomaticThreadCondition> condition, Seconds timeout)
+AutomaticThread::AutomaticThread(const AbstractLocker& locker, Box<Lock> lock, Ref<AutomaticThreadCondition>&& condition, Seconds timeout)
     : m_lock(lock)
-    , m_condition(condition)
+    , m_condition(WTFMove(condition))
     , m_timeout(timeout)
 {
     if (verbose)
index 387fd8f..a314628 100644 (file)
@@ -69,7 +69,7 @@ class AutomaticThread;
 
 class AutomaticThreadCondition : public ThreadSafeRefCounted<AutomaticThreadCondition> {
 public:
-    static WTF_EXPORT_PRIVATE RefPtr<AutomaticThreadCondition> create();
+    static WTF_EXPORT_PRIVATE Ref<AutomaticThreadCondition> create();
     
     WTF_EXPORT_PRIVATE ~AutomaticThreadCondition();
     
@@ -131,7 +131,7 @@ public:
 protected:
     // This logically creates the thread, but in reality the thread won't be created until someone
     // calls AutomaticThreadCondition::notifyOne() or notifyAll().
-    AutomaticThread(const AbstractLocker&, Box<Lock>, RefPtr<AutomaticThreadCondition>, Seconds timeout = 10_s);
+    AutomaticThread(const AbstractLocker&, Box<Lock>, Ref<AutomaticThreadCondition>&&, Seconds timeout = 10_s);
     
     // To understand PollResult and WorkResult, imagine that poll() and work() are being called like
     // so:
@@ -182,7 +182,7 @@ private:
     void start(const AbstractLocker&);
     
     Box<Lock> m_lock;
-    RefPtr<AutomaticThreadCondition> m_condition;
+    Ref<AutomaticThreadCondition> m_condition;
     Seconds m_timeout;
     bool m_isRunning { true };
     bool m_isWaiting { false };
index 8d0f9e4..48a7bc7 100644 (file)
@@ -171,7 +171,7 @@ void ParallelHelperPool::doSomeHelping()
 class ParallelHelperPool::Thread : public AutomaticThread {
 public:
     Thread(const AbstractLocker& locker, ParallelHelperPool& pool)
-        : AutomaticThread(locker, pool.m_lock, pool.m_workAvailableCondition)
+        : AutomaticThread(locker, pool.m_lock, pool.m_workAvailableCondition.copyRef())
         , m_pool(pool)
     {
     }
index 21859cb..734cdf4 100644 (file)
@@ -200,7 +200,7 @@ private:
     ParallelHelperClient* waitForClientWithTask(const AbstractLocker&);
     
     Box<Lock> m_lock; // AutomaticThread wants this in a box for safety.
-    RefPtr<AutomaticThreadCondition> m_workAvailableCondition;
+    Ref<AutomaticThreadCondition> m_workAvailableCondition;
     Condition m_workCompleteCondition;
 
     WeakRandom m_random;
index bdbdfc5..484b57c 100644 (file)
@@ -34,8 +34,8 @@ class WorkerPool::Worker final : public AutomaticThread {
 public:
     friend class WorkerPool;
 
-    Worker(const AbstractLocker& locker, WorkerPool& pool, Box<Lock> lock, RefPtr<AutomaticThreadCondition> condition, Seconds timeout)
-        : AutomaticThread(locker, lock, condition, timeout)
+    Worker(const AbstractLocker& locker, WorkerPool& pool, Box<Lock> lock, Ref<AutomaticThreadCondition>&& condition, Seconds timeout)
+        : AutomaticThread(locker, lock, WTFMove(condition), timeout)
         , m_pool(pool)
     {
     }
@@ -91,7 +91,7 @@ WorkerPool::WorkerPool(ASCIILiteral name, unsigned numberOfWorkers, Seconds time
 {
     LockHolder locker(*m_lock);
     for (unsigned i = 0; i < numberOfWorkers; ++i)
-        m_workers.append(adoptRef(*new Worker(locker, *this, m_lock, m_condition, timeout)));
+        m_workers.append(adoptRef(*new Worker(locker, *this, m_lock, m_condition.copyRef(), timeout)));
 }
 
 WorkerPool::~WorkerPool()
index 7676ac9..6235e73 100644 (file)
@@ -58,7 +58,7 @@ private:
     bool shouldSleep(const AbstractLocker&);
 
     Box<Lock> m_lock;
-    RefPtr<AutomaticThreadCondition> m_condition;
+    Ref<AutomaticThreadCondition> m_condition;
     Seconds m_timeout;
     MonotonicTime m_lastTimeoutTime { MonotonicTime::nan() };
     unsigned m_numberOfActiveWorkers { 0 };
index 78230c0..365f188 100644 (file)
@@ -116,7 +116,7 @@ void WorkQueue::dispatch(Function<void()>&& function)
 }
 
 struct TimerContext : public ThreadSafeRefCounted<TimerContext> {
-    static RefPtr<TimerContext> create() { return adoptRef(new TimerContext); }
+    static Ref<TimerContext> create() { return adoptRef(*new TimerContext); }
 
     Lock timerLock;
     WorkQueue* queue { nullptr };
@@ -151,7 +151,7 @@ void WorkQueue::dispatchAfter(Seconds duration, Function<void()>&& function)
     ASSERT(m_timerQueue);
     ref();
 
-    RefPtr<TimerContext> context = TimerContext::create();
+    Ref<TimerContext> context = TimerContext::create();
     context->queue = this;
     context->function = WTFMove(function);
 
@@ -177,7 +177,7 @@ void WorkQueue::dispatchAfter(Seconds duration, Function<void()>&& function)
 
         // Since our timer callback is quick, we can execute in the timer thread itself and avoid
         // an extra thread switch over to a worker thread.
-        if (!::CreateTimerQueueTimer(&context->timer, m_timerQueue, timerCallback, context.get(), clampTo<DWORD>(milliseconds), 0, WT_EXECUTEINTIMERTHREAD)) {
+        if (!::CreateTimerQueueTimer(&context->timer, m_timerQueue, timerCallback, context.ptr(), clampTo<DWORD>(milliseconds), 0, WT_EXECUTEINTIMERTHREAD)) {
             ASSERT_WITH_MESSAGE(false, "::CreateTimerQueueTimer failed with error %lu", ::GetLastError());
             return;
         }