blob: e1e01e609122a432f484baf98fbf16b65862e64c [file] [log] [blame]
ap@webkit.org1f622052009-01-23 15:53:42 +00001/*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
mark.lam@apple.com25199882017-05-15 20:34:13 +00003 * Copyright (C) 2017 Apple Inc. All rights reserved.
4 *
ap@webkit.org1f622052009-01-23 15:53:42 +00005 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
commit-queue@webkit.org97866532016-10-19 18:09:50 +000032#pragma once
ap@webkit.org1f622052009-01-23 15:53:42 +000033
ap@webkit.org98153ce2009-02-03 07:38:38 +000034#include "ScriptExecutionContext.h"
zandobersek@gmail.com8cdf4082014-05-11 19:10:41 +000035#include <memory>
ap@webkit.org1f622052009-01-23 15:53:42 +000036#include <wtf/MessageQueue.h>
ap@webkit.org1f622052009-01-23 15:53:42 +000037
38namespace WebCore {
39
levin@chromium.org6c302bd2009-02-19 22:35:40 +000040 class ModePredicate;
ch.dumez@sisa.samsung.com14792a62013-06-27 06:21:54 +000041 class WorkerGlobalScope;
ap@webkit.org9892af82009-02-03 09:11:35 +000042 class WorkerSharedTimer;
ap@webkit.org1f622052009-01-23 15:53:42 +000043
44 class WorkerRunLoop {
45 public:
ap@webkit.org9892af82009-02-03 09:11:35 +000046 WorkerRunLoop();
47 ~WorkerRunLoop();
ap@webkit.org1f622052009-01-23 15:53:42 +000048
49 // Blocking call. Waits for tasks and timers, invokes the callbacks.
ch.dumez@sisa.samsung.com14792a62013-06-27 06:21:54 +000050 void run(WorkerGlobalScope*);
levin@chromium.org6c302bd2009-02-19 22:35:40 +000051
yurys@chromium.org8ad1f7f2012-02-21 13:10:35 +000052 enum WaitMode { WaitForMessage, DontWaitForMessage };
53
levin@chromium.org6c302bd2009-02-19 22:35:40 +000054 // Waits for a single task and returns.
ch.dumez@sisa.samsung.com14792a62013-06-27 06:21:54 +000055 MessageQueueWaitResult runInMode(WorkerGlobalScope*, const String& mode, WaitMode = WaitForMessage);
levin@chromium.org6c302bd2009-02-19 22:35:40 +000056
ap@webkit.org1f622052009-01-23 15:53:42 +000057 void terminate();
levin@chromium.orge6229e62011-12-09 22:05:12 +000058 bool terminated() const { return m_messageQueue.killed(); }
ap@webkit.org1f622052009-01-23 15:53:42 +000059
cdumez@apple.com0eec8ff2016-06-22 03:06:36 +000060 void postTask(ScriptExecutionContext::Task&&);
61 void postTaskAndTerminate(ScriptExecutionContext::Task&&);
62 void postTaskForMode(ScriptExecutionContext::Task&&, const String& mode);
ap@webkit.org9892af82009-02-03 09:11:35 +000063
levin@chromium.org066feb52009-02-25 18:06:35 +000064 unsigned long createUniqueId() { return ++m_uniqueId; }
65
levin@chromium.org6c302bd2009-02-19 22:35:40 +000066 static String defaultMode();
joepeck@webkit.org98861c82016-10-27 22:19:33 +000067 static String debuggerMode();
dimich@chromium.org142155a2009-11-02 21:31:22 +000068
ossy@webkit.org95c1bc42011-01-20 16:30:54 +000069 class Task {
70 WTF_MAKE_NONCOPYABLE(Task); WTF_MAKE_FAST_ALLOCATED;
dimich@chromium.org142155a2009-11-02 21:31:22 +000071 public:
cdumez@apple.com0eec8ff2016-06-22 03:06:36 +000072 Task(ScriptExecutionContext::Task&&, const String& mode);
dimich@chromium.org142155a2009-11-02 21:31:22 +000073 const String& mode() const { return m_mode; }
dimich@chromium.org142155a2009-11-02 21:31:22 +000074
75 private:
mark.lam@apple.com25199882017-05-15 20:34:13 +000076 void performTask(WorkerGlobalScope*);
77
zandobersek@gmail.coma27c36f2014-04-29 17:41:31 +000078 ScriptExecutionContext::Task m_task;
dimich@chromium.org142155a2009-11-02 21:31:22 +000079 String m_mode;
mark.lam@apple.com25199882017-05-15 20:34:13 +000080
81 friend class WorkerRunLoop;
dimich@chromium.org142155a2009-11-02 21:31:22 +000082 };
83
ap@webkit.org1f622052009-01-23 15:53:42 +000084 private:
levin@chromium.org6c302bd2009-02-19 22:35:40 +000085 friend class RunLoopSetup;
ch.dumez@sisa.samsung.com14792a62013-06-27 06:21:54 +000086 MessageQueueWaitResult runInMode(WorkerGlobalScope*, const ModePredicate&, WaitMode);
levin@chromium.org6c302bd2009-02-19 22:35:40 +000087
levin@chromium.orge6229e62011-12-09 22:05:12 +000088 // Runs any clean up tasks that are currently in the queue and returns.
89 // This should only be called when the context is closed or loop has been terminated.
ch.dumez@sisa.samsung.com14792a62013-06-27 06:21:54 +000090 void runCleanupTasks(WorkerGlobalScope*);
levin@chromium.orge6229e62011-12-09 22:05:12 +000091
joepeck@webkit.org00a86e32016-11-15 04:02:59 +000092 bool isNested() const { return m_nestedCount > 1; }
93
dimich@chromium.org142155a2009-11-02 21:31:22 +000094 MessageQueue<Task> m_messageQueue;
zandobersek@gmail.com8cdf4082014-05-11 19:10:41 +000095 std::unique_ptr<WorkerSharedTimer> m_sharedTimer;
levin@chromium.org6c302bd2009-02-19 22:35:40 +000096 int m_nestedCount;
levin@chromium.org066feb52009-02-25 18:06:35 +000097 unsigned long m_uniqueId;
ap@webkit.org1f622052009-01-23 15:53:42 +000098 };
99
100} // namespace WebCore