1 /* 2 * Copyright (C) 2005, 2006, 2007 Apple Inc. All rights reserved. 3 * (C) 2007 Graham Dennis (graham.dennis (at) gmail.com) 4 * (C) 2007 Eric Seidel <eric (at) webkit.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 16 * its contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 #include "JavaScriptThreading.h" 33 34 #include <CoreFoundation/CoreFoundation.h> 35 #include <JavaScriptCore/JavaScriptCore.h> 36 #include <pthread.h> 37 #include <wtf/Assertions.h> 38 #include <wtf/HashSet.h> 39 40 static pthread_mutex_t javaScriptThreadsMutex = PTHREAD_MUTEX_INITIALIZER; 41 static bool javaScriptThreadsShouldTerminate; 42 43 static const int javaScriptThreadsCount = 4; 44 45 typedef HashSet<pthread_t> ThreadSet; 46 47 static ThreadSet* javaScriptThreads() 48 { 49 ASSERT(pthread_mutex_trylock(&javaScriptThreadsMutex) == EBUSY); 50 static ThreadSet staticJavaScriptThreads; 51 return &staticJavaScriptThreads; 52 } 53 54 // Loops forever, running a script and randomly respawning, until 55 // javaScriptThreadsShouldTerminate becomes true. 56 void* runJavaScriptThread(void* arg) 57 { 58 const char* const script = 59 "var array = [];" 60 "for (var i = 0; i < 10; i++) {" 61 " array.push(String(i));" 62 "}"; 63 64 while (1) { 65 JSGlobalContextRef ctx = JSGlobalContextCreate(0); 66 JSStringRef scriptRef = JSStringCreateWithUTF8CString(script); 67 68 JSValueRef exception = 0; 69 JSEvaluateScript(ctx, scriptRef, 0, 0, 1, &exception); 70 ASSERT(!exception); 71 72 JSGarbageCollect(ctx); 73 JSGlobalContextRelease(ctx); 74 JSStringRelease(scriptRef); 75 76 JSGarbageCollect(0); 77 78 pthread_mutex_lock(&javaScriptThreadsMutex); 79 80 // Check for cancellation. 81 if (javaScriptThreadsShouldTerminate) { 82 javaScriptThreads()->remove(pthread_self()); 83 pthread_mutex_unlock(&javaScriptThreadsMutex); 84 return 0; 85 } 86 87 // Respawn probabilistically. 88 if (random() % 5 == 0) { 89 pthread_t pthread; 90 pthread_create(&pthread, 0, &runJavaScriptThread, 0); 91 pthread_detach(pthread); 92 93 javaScriptThreads()->remove(pthread_self()); 94 javaScriptThreads()->add(pthread); 95 96 pthread_mutex_unlock(&javaScriptThreadsMutex); 97 return 0; 98 } 99 100 pthread_mutex_unlock(&javaScriptThreadsMutex); 101 } 102 } 103 104 void startJavaScriptThreads() 105 { 106 pthread_mutex_lock(&javaScriptThreadsMutex); 107 108 for (int i = 0; i < javaScriptThreadsCount; i++) { 109 pthread_t pthread; 110 pthread_create(&pthread, 0, &runJavaScriptThread, 0); 111 pthread_detach(pthread); 112 javaScriptThreads()->add(pthread); 113 } 114 115 pthread_mutex_unlock(&javaScriptThreadsMutex); 116 } 117 118 void stopJavaScriptThreads() 119 { 120 pthread_mutex_lock(&javaScriptThreadsMutex); 121 122 javaScriptThreadsShouldTerminate = true; 123 124 ASSERT(javaScriptThreads()->size() == javaScriptThreadsCount); 125 126 pthread_mutex_unlock(&javaScriptThreadsMutex); 127 128 while (true) { 129 pthread_mutex_lock(&javaScriptThreadsMutex); 130 int threadCount = javaScriptThreads()->size(); 131 pthread_mutex_unlock(&javaScriptThreadsMutex); 132 133 if (!threadCount) 134 break; 135 136 usleep(1000); 137 } 138 } 139