2009-02-03 Dmitry Titov <dimich@chromium.org>
authorap@webkit.org <ap@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 3 Feb 2009 09:11:35 +0000 (09:11 +0000)
committerap@webkit.org <ap@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 3 Feb 2009 09:11:35 +0000 (09:11 +0000)
        Reviewed by Alexey Proskuryakov.

        https://bugs.webkit.org/show_bug.cgi?id=23560
        Implement SharedTimer for Workers.

        * dom/WorkerRunLoop.cpp:
        Added private class WorkerSharedTimer that implements SharedTimer interface for worker threads.
        (WebCore::WorkerSharedTimer::WorkerSharedTimer):
        (WebCore::WorkerSharedTimer::setFiredFunction):
        (WebCore::WorkerSharedTimer::setFireTime):
        (WebCore::WorkerSharedTimer::stop):
        (WebCore::WorkerSharedTimer::isActive):
        (WebCore::WorkerSharedTimer::fireTime):
        (WebCore::WorkerSharedTimer::fire):
        (WebCore::WorkerRunLoop::WorkerRunLoop):
        Initializes a WorkerSharedTimer instance.
        (WebCore::WorkerRunLoop::~WorkerRunLoop):
        The destructor is here so compiler is ok with having OwnPtr<WorkerSharedTimer> in .h file w/o defining WorkerSharedTimer.
        (WebCore::WorkerRunLoop::run):
        Set/reset shared timer interface on ThreadTimers, use MessageQueue::waitForMessageTimed() if timer is active.
        * dom/WorkerRunLoop.h:
        Add member of type OwnPtr<WorkerSharedTimer>

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

WebCore/ChangeLog
WebCore/dom/WorkerRunLoop.cpp
WebCore/dom/WorkerRunLoop.h

index 5cd46450f6865039a5050bb01be75f643fa5017e..597f3dc5af57b0aa3e96668b4370597a37689209 100644 (file)
@@ -1,3 +1,28 @@
+2009-02-03  Dmitry Titov  <dimich@chromium.org>
+
+        Reviewed by Alexey Proskuryakov.
+
+        https://bugs.webkit.org/show_bug.cgi?id=23560
+        Implement SharedTimer for Workers.
+
+        * dom/WorkerRunLoop.cpp:
+        Added private class WorkerSharedTimer that implements SharedTimer interface for worker threads.
+        (WebCore::WorkerSharedTimer::WorkerSharedTimer):
+        (WebCore::WorkerSharedTimer::setFiredFunction):
+        (WebCore::WorkerSharedTimer::setFireTime):
+        (WebCore::WorkerSharedTimer::stop):
+        (WebCore::WorkerSharedTimer::isActive):
+        (WebCore::WorkerSharedTimer::fireTime):
+        (WebCore::WorkerSharedTimer::fire):
+        (WebCore::WorkerRunLoop::WorkerRunLoop):
+        Initializes a WorkerSharedTimer instance.
+        (WebCore::WorkerRunLoop::~WorkerRunLoop):
+        The destructor is here so compiler is ok with having OwnPtr<WorkerSharedTimer> in .h file w/o defining WorkerSharedTimer.
+        (WebCore::WorkerRunLoop::run):
+        Set/reset shared timer interface on ThreadTimers, use MessageQueue::waitForMessageTimed() if timer is active.
+        * dom/WorkerRunLoop.h:
+        Add member of type OwnPtr<WorkerSharedTimer>
+
 2009-02-03  Steve Falkenburg  <sfalken@apple.com>
 
         Build fix.
index e929b7bc16b2eb4e033bb429b4cef35a3f91e232..2cba5c8f9326ac1280a5d0a19f11b0021d55b397 100644 (file)
 #if ENABLE(WORKERS)
 
 #include "ScriptExecutionContext.h"
+#include "ThreadGlobalData.h"
+#include "ThreadTimers.h"
 #include "WorkerRunLoop.h"
 #include "WorkerContext.h"
 #include "WorkerThread.h"
 
 namespace WebCore {
 
+class WorkerSharedTimer : public SharedTimer {
+public:
+    WorkerSharedTimer()
+        : m_sharedTimerFunction(0)
+        , m_nextFireTime(0)
+    {
+    }
+
+    // SharedTimer interface.
+    virtual void setFiredFunction(void (*function)()) { m_sharedTimerFunction = function; }
+    virtual void setFireTime(double fireTime) { m_nextFireTime = fireTime; }
+    virtual void stop() { m_nextFireTime = 0; }
+
+    bool isActive() { return m_sharedTimerFunction && m_nextFireTime; }
+    double fireTime() { return m_nextFireTime; }
+    void fire() { m_sharedTimerFunction(); }
+
+private:
+    void (*m_sharedTimerFunction)();
+    double m_nextFireTime;
+};
+
+WorkerRunLoop::WorkerRunLoop()
+    : m_sharedTimer(new WorkerSharedTimer)
+{
+}
+
+WorkerRunLoop::~WorkerRunLoop()
+{
+}
+
 void WorkerRunLoop::run(WorkerContext* context)
 {
     ASSERT(context);
     ASSERT(context->thread());
     ASSERT(context->thread()->threadID() == currentThread());
-    
+
+    threadGlobalData().threadTimers().setSharedTimer(m_sharedTimer.get());
+
     while (true) {
         RefPtr<ScriptExecutionContext::Task> task;
-        if (!m_messageQueue.waitForMessage(task))
-            break;
+        MessageQueueWaitResult result;
+
+        if (m_sharedTimer->isActive())
+            result = m_messageQueue.waitForMessageTimed(task, m_sharedTimer->fireTime());
+        else
+            result = (m_messageQueue.waitForMessage(task) ? MessageQueueMessageReceived : MessageQueueTerminated);
 
-        task->performTask(context);
+        if (result == MessageQueueTerminated)
+            break;
+        
+        if (result == MessageQueueMessageReceived)
+            task->performTask(context);
+        else {
+            ASSERT(result == MessageQueueTimeout);
+            m_sharedTimer->fire();
+        }
     }
+
+    threadGlobalData().threadTimers().setSharedTimer(0);
 }
 
 void WorkerRunLoop::terminate()
index 339fe1c16ab72e5e7b216946b079803ba3766f36..aeaf88060edc32971fbc26d752fdc6dccbe424d7 100644 (file)
 #if ENABLE(WORKERS)
 
 #include "ScriptExecutionContext.h"
+#include "SharedTimer.h"
 #include <wtf/MessageQueue.h>
+#include <wtf/OwnPtr.h>
 #include <wtf/PassRefPtr.h>
 
 namespace WebCore {
 
     class WorkerContext;
+    class WorkerSharedTimer;
 
     class WorkerRunLoop {
     public:
-        WorkerRunLoop() {}
+        WorkerRunLoop();
+        ~WorkerRunLoop();
         
         // Blocking call. Waits for tasks and timers, invokes the callbacks.
         void run(WorkerContext*);
@@ -52,9 +56,10 @@ namespace WebCore {
         bool terminated() { return m_messageQueue.killed(); }
 
         void postTask(PassRefPtr<ScriptExecutionContext::Task>);
-        
+
     private:
         MessageQueue<RefPtr<ScriptExecutionContext::Task> > m_messageQueue;
+        OwnPtr<WorkerSharedTimer> m_sharedTimer;
     };
 
 } // namespace WebCore