paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 1 | /* |
darin@apple.com | 8f72329 | 2017-07-25 04:29:08 +0000 | [diff] [blame] | 2 | * Copyright (C) 2005-2017 Apple Inc. All rights reserved. |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 3 | * (C) 2007 Graham Dennis (graham.dennis@gmail.com) |
| 4 | * (C) 2007 Eric Seidel <eric@webkit.org> |
| 5 | * (C) 2012 Patrick Ganstere <paroga@paroga.com> |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
mjs@apple.com | 9204733 | 2014-03-15 04:08:27 +0000 | [diff] [blame] | 16 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 17 | * its contributors may be used to endorse or promote products derived |
| 18 | * from this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 21 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 23 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 24 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 29 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #include "config.h" |
| 33 | #include "JavaScriptThreading.h" |
| 34 | |
| 35 | #include <JavaScriptCore/JavaScriptCore.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <wtf/Assertions.h> |
| 38 | #include <wtf/HashSet.h> |
fpizlo@apple.com | 7398598 | 2015-08-18 19:31:28 +0000 | [diff] [blame] | 39 | #include <wtf/Lock.h> |
darin@apple.com | 8f72329 | 2017-07-25 04:29:08 +0000 | [diff] [blame] | 40 | #include <wtf/NeverDestroyed.h> |
andersca@apple.com | 5d79e75 | 2014-01-25 19:24:14 +0000 | [diff] [blame] | 41 | #include <wtf/Threading.h> |
| 42 | #include <wtf/ThreadingPrimitives.h> |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 43 | #include <wtf/Vector.h> |
| 44 | |
| 45 | static const size_t javaScriptThreadsCount = 4; |
| 46 | static bool javaScriptThreadsShouldTerminate; |
| 47 | static JSContextGroupRef javaScriptThreadsGroup; |
utatane.tea@gmail.com | e71a872 | 2018-04-05 17:22:21 +0000 | [diff] [blame] | 48 | static Lock javaScriptThreadsLock; |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 49 | |
utatane.tea@gmail.com | 1e94a26 | 2017-04-12 12:08:29 +0000 | [diff] [blame] | 50 | typedef HashSet<RefPtr<Thread>> ThreadSet; |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 51 | static ThreadSet& javaScriptThreads() |
| 52 | { |
darin@apple.com | 8f72329 | 2017-07-25 04:29:08 +0000 | [diff] [blame] | 53 | static NeverDestroyed<ThreadSet> staticJavaScriptThreads; |
utatane.tea@gmail.com | d4c4a51 | 2017-12-08 01:31:10 +0000 | [diff] [blame] | 54 | ASSERT(!javaScriptThreadsLock.tryLock()); |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 55 | return staticJavaScriptThreads; |
| 56 | } |
| 57 | |
| 58 | // This function exercises JSC in a loop until javaScriptThreadsShouldTerminate |
| 59 | // becomes true or it probabilistically decides to spawn a replacement thread and exit. |
utatane.tea@gmail.com | 573f09c | 2017-06-26 18:19:36 +0000 | [diff] [blame] | 60 | void runJavaScriptThread() |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 61 | { |
| 62 | static const char* const script = |
| 63 | "var array = [];" |
| 64 | "for (var i = 0; i < 1024; i++) {" |
| 65 | " array.push(String(i));" |
| 66 | "}"; |
| 67 | |
| 68 | JSGlobalContextRef ctx; |
| 69 | { |
cdumez@apple.com | 8c8c0bd | 2021-05-22 04:34:08 +0000 | [diff] [blame] | 70 | Locker locker { javaScriptThreadsLock }; |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 71 | ctx = JSGlobalContextCreateInGroup(javaScriptThreadsGroup, 0); |
| 72 | } |
| 73 | |
| 74 | JSStringRef scriptRef; |
| 75 | { |
cdumez@apple.com | 8c8c0bd | 2021-05-22 04:34:08 +0000 | [diff] [blame] | 76 | Locker locker { javaScriptThreadsLock }; |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 77 | scriptRef = JSStringCreateWithUTF8CString(script); |
| 78 | } |
| 79 | |
| 80 | while (true) { |
| 81 | { |
cdumez@apple.com | 8c8c0bd | 2021-05-22 04:34:08 +0000 | [diff] [blame] | 82 | Locker locker { javaScriptThreadsLock }; |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 83 | JSValueRef exception = 0; |
| 84 | JSEvaluateScript(ctx, scriptRef, 0, 0, 1, &exception); |
| 85 | ASSERT(!exception); |
| 86 | } |
| 87 | |
| 88 | { |
cdumez@apple.com | 8c8c0bd | 2021-05-22 04:34:08 +0000 | [diff] [blame] | 89 | Locker locker { javaScriptThreadsLock }; |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 90 | const size_t valuesCount = 1024; |
| 91 | JSValueRef values[valuesCount]; |
| 92 | for (size_t i = 0; i < valuesCount; ++i) |
| 93 | values[i] = JSObjectMake(ctx, 0, 0); |
| 94 | } |
| 95 | |
| 96 | { |
cdumez@apple.com | 8c8c0bd | 2021-05-22 04:34:08 +0000 | [diff] [blame] | 97 | Locker locker { javaScriptThreadsLock }; |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 98 | if (javaScriptThreadsShouldTerminate) |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | // Respawn probabilistically. |
| 103 | if (rand() % 5) |
| 104 | continue; |
| 105 | |
cdumez@apple.com | 8c8c0bd | 2021-05-22 04:34:08 +0000 | [diff] [blame] | 106 | Locker locker { javaScriptThreadsLock }; |
utatane.tea@gmail.com | 1e94a26 | 2017-04-12 12:08:29 +0000 | [diff] [blame] | 107 | Thread& thread = Thread::current(); |
| 108 | thread.detach(); |
| 109 | javaScriptThreads().remove(&thread); |
utatane.tea@gmail.com | 573f09c | 2017-06-26 18:19:36 +0000 | [diff] [blame] | 110 | javaScriptThreads().add(Thread::create("JavaScript Thread", &runJavaScriptThread)); |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 111 | break; |
| 112 | } |
| 113 | |
cdumez@apple.com | 8c8c0bd | 2021-05-22 04:34:08 +0000 | [diff] [blame] | 114 | Locker locker { javaScriptThreadsLock }; |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 115 | JSStringRelease(scriptRef); |
| 116 | JSGarbageCollect(ctx); |
| 117 | JSGlobalContextRelease(ctx); |
| 118 | } |
| 119 | |
| 120 | void startJavaScriptThreads() |
| 121 | { |
| 122 | javaScriptThreadsGroup = JSContextGroupCreate(); |
| 123 | |
cdumez@apple.com | 8c8c0bd | 2021-05-22 04:34:08 +0000 | [diff] [blame] | 124 | Locker locker { javaScriptThreadsLock }; |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 125 | |
| 126 | for (size_t i = 0; i < javaScriptThreadsCount; ++i) |
utatane.tea@gmail.com | 573f09c | 2017-06-26 18:19:36 +0000 | [diff] [blame] | 127 | javaScriptThreads().add(Thread::create("JavaScript Thread", &runJavaScriptThread)); |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void stopJavaScriptThreads() |
| 131 | { |
| 132 | { |
cdumez@apple.com | 8c8c0bd | 2021-05-22 04:34:08 +0000 | [diff] [blame] | 133 | Locker locker { javaScriptThreadsLock }; |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 134 | javaScriptThreadsShouldTerminate = true; |
| 135 | } |
| 136 | |
utatane.tea@gmail.com | 1e94a26 | 2017-04-12 12:08:29 +0000 | [diff] [blame] | 137 | Vector<RefPtr<Thread>, javaScriptThreadsCount> threads; |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 138 | { |
cdumez@apple.com | 8c8c0bd | 2021-05-22 04:34:08 +0000 | [diff] [blame] | 139 | Locker locker { javaScriptThreadsLock }; |
weinig@apple.com | b6e19c5 | 2017-10-12 15:38:42 +0000 | [diff] [blame] | 140 | threads = copyToVector(javaScriptThreads()); |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 141 | ASSERT(threads.size() == javaScriptThreadsCount); |
| 142 | } |
| 143 | |
| 144 | for (size_t i = 0; i < javaScriptThreadsCount; ++i) |
utatane.tea@gmail.com | 1e94a26 | 2017-04-12 12:08:29 +0000 | [diff] [blame] | 145 | threads[i]->waitForCompletion(); |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 146 | |
ggaren@apple.com | 96fea77 | 2015-01-29 00:58:13 +0000 | [diff] [blame] | 147 | { |
cdumez@apple.com | 8c8c0bd | 2021-05-22 04:34:08 +0000 | [diff] [blame] | 148 | Locker locker { javaScriptThreadsLock }; |
ggaren@apple.com | 96fea77 | 2015-01-29 00:58:13 +0000 | [diff] [blame] | 149 | javaScriptThreads().clear(); |
| 150 | } |
paroga@webkit.org | 332bfe0 | 2013-05-09 21:35:01 +0000 | [diff] [blame] | 151 | |
| 152 | JSContextGroupRelease(javaScriptThreadsGroup); |
| 153 | } |