ap@webkit.org | 1f62205 | 2009-01-23 15:53:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 Google Inc. All rights reserved. |
mark.lam@apple.com | 2519988 | 2017-05-15 20:34:13 +0000 | [diff] [blame] | 3 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
| 4 | * |
ap@webkit.org | 1f62205 | 2009-01-23 15:53:42 +0000 | [diff] [blame] | 5 | * 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.org | 9786653 | 2016-10-19 18:09:50 +0000 | [diff] [blame] | 32 | #pragma once |
ap@webkit.org | 1f62205 | 2009-01-23 15:53:42 +0000 | [diff] [blame] | 33 | |
ap@webkit.org | 98153ce | 2009-02-03 07:38:38 +0000 | [diff] [blame] | 34 | #include "ScriptExecutionContext.h" |
zandobersek@gmail.com | 8cdf408 | 2014-05-11 19:10:41 +0000 | [diff] [blame] | 35 | #include <memory> |
ap@webkit.org | 1f62205 | 2009-01-23 15:53:42 +0000 | [diff] [blame] | 36 | #include <wtf/MessageQueue.h> |
ap@webkit.org | 1f62205 | 2009-01-23 15:53:42 +0000 | [diff] [blame] | 37 | |
| 38 | namespace WebCore { |
| 39 | |
levin@chromium.org | 6c302bd | 2009-02-19 22:35:40 +0000 | [diff] [blame] | 40 | class ModePredicate; |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 41 | class WorkerGlobalScope; |
ap@webkit.org | 9892af8 | 2009-02-03 09:11:35 +0000 | [diff] [blame] | 42 | class WorkerSharedTimer; |
ap@webkit.org | 1f62205 | 2009-01-23 15:53:42 +0000 | [diff] [blame] | 43 | |
| 44 | class WorkerRunLoop { |
| 45 | public: |
ap@webkit.org | 9892af8 | 2009-02-03 09:11:35 +0000 | [diff] [blame] | 46 | WorkerRunLoop(); |
| 47 | ~WorkerRunLoop(); |
ap@webkit.org | 1f62205 | 2009-01-23 15:53:42 +0000 | [diff] [blame] | 48 | |
| 49 | // Blocking call. Waits for tasks and timers, invokes the callbacks. |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 50 | void run(WorkerGlobalScope*); |
levin@chromium.org | 6c302bd | 2009-02-19 22:35:40 +0000 | [diff] [blame] | 51 | |
yurys@chromium.org | 8ad1f7f | 2012-02-21 13:10:35 +0000 | [diff] [blame] | 52 | enum WaitMode { WaitForMessage, DontWaitForMessage }; |
| 53 | |
levin@chromium.org | 6c302bd | 2009-02-19 22:35:40 +0000 | [diff] [blame] | 54 | // Waits for a single task and returns. |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 55 | MessageQueueWaitResult runInMode(WorkerGlobalScope*, const String& mode, WaitMode = WaitForMessage); |
levin@chromium.org | 6c302bd | 2009-02-19 22:35:40 +0000 | [diff] [blame] | 56 | |
ap@webkit.org | 1f62205 | 2009-01-23 15:53:42 +0000 | [diff] [blame] | 57 | void terminate(); |
levin@chromium.org | e6229e6 | 2011-12-09 22:05:12 +0000 | [diff] [blame] | 58 | bool terminated() const { return m_messageQueue.killed(); } |
ap@webkit.org | 1f62205 | 2009-01-23 15:53:42 +0000 | [diff] [blame] | 59 | |
cdumez@apple.com | 0eec8ff | 2016-06-22 03:06:36 +0000 | [diff] [blame] | 60 | void postTask(ScriptExecutionContext::Task&&); |
| 61 | void postTaskAndTerminate(ScriptExecutionContext::Task&&); |
| 62 | void postTaskForMode(ScriptExecutionContext::Task&&, const String& mode); |
ap@webkit.org | 9892af8 | 2009-02-03 09:11:35 +0000 | [diff] [blame] | 63 | |
levin@chromium.org | 066feb5 | 2009-02-25 18:06:35 +0000 | [diff] [blame] | 64 | unsigned long createUniqueId() { return ++m_uniqueId; } |
| 65 | |
levin@chromium.org | 6c302bd | 2009-02-19 22:35:40 +0000 | [diff] [blame] | 66 | static String defaultMode(); |
joepeck@webkit.org | 98861c8 | 2016-10-27 22:19:33 +0000 | [diff] [blame] | 67 | static String debuggerMode(); |
dimich@chromium.org | 142155a | 2009-11-02 21:31:22 +0000 | [diff] [blame] | 68 | |
ossy@webkit.org | 95c1bc4 | 2011-01-20 16:30:54 +0000 | [diff] [blame] | 69 | class Task { |
| 70 | WTF_MAKE_NONCOPYABLE(Task); WTF_MAKE_FAST_ALLOCATED; |
dimich@chromium.org | 142155a | 2009-11-02 21:31:22 +0000 | [diff] [blame] | 71 | public: |
cdumez@apple.com | 0eec8ff | 2016-06-22 03:06:36 +0000 | [diff] [blame] | 72 | Task(ScriptExecutionContext::Task&&, const String& mode); |
dimich@chromium.org | 142155a | 2009-11-02 21:31:22 +0000 | [diff] [blame] | 73 | const String& mode() const { return m_mode; } |
dimich@chromium.org | 142155a | 2009-11-02 21:31:22 +0000 | [diff] [blame] | 74 | |
| 75 | private: |
mark.lam@apple.com | 2519988 | 2017-05-15 20:34:13 +0000 | [diff] [blame] | 76 | void performTask(WorkerGlobalScope*); |
| 77 | |
zandobersek@gmail.com | a27c36f | 2014-04-29 17:41:31 +0000 | [diff] [blame] | 78 | ScriptExecutionContext::Task m_task; |
dimich@chromium.org | 142155a | 2009-11-02 21:31:22 +0000 | [diff] [blame] | 79 | String m_mode; |
mark.lam@apple.com | 2519988 | 2017-05-15 20:34:13 +0000 | [diff] [blame] | 80 | |
| 81 | friend class WorkerRunLoop; |
dimich@chromium.org | 142155a | 2009-11-02 21:31:22 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
ap@webkit.org | 1f62205 | 2009-01-23 15:53:42 +0000 | [diff] [blame] | 84 | private: |
levin@chromium.org | 6c302bd | 2009-02-19 22:35:40 +0000 | [diff] [blame] | 85 | friend class RunLoopSetup; |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 86 | MessageQueueWaitResult runInMode(WorkerGlobalScope*, const ModePredicate&, WaitMode); |
levin@chromium.org | 6c302bd | 2009-02-19 22:35:40 +0000 | [diff] [blame] | 87 | |
levin@chromium.org | e6229e6 | 2011-12-09 22:05:12 +0000 | [diff] [blame] | 88 | // 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.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 90 | void runCleanupTasks(WorkerGlobalScope*); |
levin@chromium.org | e6229e6 | 2011-12-09 22:05:12 +0000 | [diff] [blame] | 91 | |
joepeck@webkit.org | 00a86e3 | 2016-11-15 04:02:59 +0000 | [diff] [blame] | 92 | bool isNested() const { return m_nestedCount > 1; } |
| 93 | |
dimich@chromium.org | 142155a | 2009-11-02 21:31:22 +0000 | [diff] [blame] | 94 | MessageQueue<Task> m_messageQueue; |
zandobersek@gmail.com | 8cdf408 | 2014-05-11 19:10:41 +0000 | [diff] [blame] | 95 | std::unique_ptr<WorkerSharedTimer> m_sharedTimer; |
levin@chromium.org | 6c302bd | 2009-02-19 22:35:40 +0000 | [diff] [blame] | 96 | int m_nestedCount; |
levin@chromium.org | 066feb5 | 2009-02-25 18:06:35 +0000 | [diff] [blame] | 97 | unsigned long m_uniqueId; |
ap@webkit.org | 1f62205 | 2009-01-23 15:53:42 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | } // namespace WebCore |