1/*
2 * Copyright (C) 2005, 2006, 2007 Apple Inc. All rights reserved.
3 *           (C) 2007 Graham Dennis (graham.dennis@gmail.com)
4 *           (C) 2007 Eric Seidel <eric@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
40static JSContextGroupRef javaScriptThreadsGroup;
41
42static pthread_mutex_t javaScriptThreadsMutex = PTHREAD_MUTEX_INITIALIZER;
43static bool javaScriptThreadsShouldTerminate;
44
45static const int javaScriptThreadsCount = 4;
46
47typedef HashSet<pthread_t> ThreadSet;
48
49static ThreadSet* javaScriptThreads()
50{
51    ASSERT(pthread_mutex_trylock(&javaScriptThreadsMutex) == EBUSY);
52    static ThreadSet staticJavaScriptThreads;
53    return &staticJavaScriptThreads;
54}
55
56// This function exercises JSC in a loop until javaScriptThreadsShouldTerminate
57// becomes true or it probabilistically decides to spawn a replacement thread and exit.
58void* runJavaScriptThread(void* arg)
59{
60    static const char* const script =
61        "var array = [];"
62        "for (var i = 0; i < 1024; i++) {"
63        "    array.push(String(i));"
64        "}";
65
66    pthread_mutex_lock(&javaScriptThreadsMutex);
67    JSGlobalContextRef ctx = JSGlobalContextCreateInGroup(javaScriptThreadsGroup, 0);
68    pthread_mutex_unlock(&javaScriptThreadsMutex);
69
70    pthread_mutex_lock(&javaScriptThreadsMutex);
71    JSStringRef scriptRef = JSStringCreateWithUTF8CString(script);
72    pthread_mutex_unlock(&javaScriptThreadsMutex);
73
74    while (1) {
75        pthread_mutex_lock(&javaScriptThreadsMutex);
76        JSValueRef exception = 0;
77        JSEvaluateScript(ctx, scriptRef, 0, 0, 1, &exception);
78        ASSERT(!exception);
79        pthread_mutex_unlock(&javaScriptThreadsMutex);
80
81        pthread_mutex_lock(&javaScriptThreadsMutex);
82        size_t valuesCount = 1024;
83        JSValueRef values[valuesCount];
84        for (size_t i = 0; i < valuesCount; ++i)
85            values[i] = JSObjectMake(ctx, 0, 0);
86        pthread_mutex_unlock(&javaScriptThreadsMutex);
87
88        // Check for cancellation.
89        if (javaScriptThreadsShouldTerminate)
90            goto done;
91
92        // Respawn probabilistically.
93        if (random() % 5 == 0) {
94            pthread_mutex_lock(&javaScriptThreadsMutex);
95            pthread_t pthread;
96            pthread_create(&pthread, 0, &runJavaScriptThread, 0);
97            pthread_detach(pthread);
98            javaScriptThreads()->add(pthread);
99            pthread_mutex_unlock(&javaScriptThreadsMutex);
100            goto done;
101        }
102    }
103
104done:
105    pthread_mutex_lock(&javaScriptThreadsMutex);
106    JSStringRelease(scriptRef);
107    JSGarbageCollect(ctx);
108    JSGlobalContextRelease(ctx);
109    javaScriptThreads()->remove(pthread_self());
110    pthread_mutex_unlock(&javaScriptThreadsMutex);
111    return 0;
112}
113
114void startJavaScriptThreads()
115{
116    javaScriptThreadsGroup = JSContextGroupCreate();
117
118    pthread_mutex_lock(&javaScriptThreadsMutex);
119
120    for (int i = 0; i < javaScriptThreadsCount; i++) {
121        pthread_t pthread;
122        pthread_create(&pthread, 0, &runJavaScriptThread, 0);
123        pthread_detach(pthread);
124        javaScriptThreads()->add(pthread);
125    }
126
127    pthread_mutex_unlock(&javaScriptThreadsMutex);
128}
129
130void stopJavaScriptThreads()
131{
132    pthread_mutex_lock(&javaScriptThreadsMutex);
133
134    javaScriptThreadsShouldTerminate = true;
135
136    pthread_mutex_unlock(&javaScriptThreadsMutex);
137
138    while (true) {
139        pthread_mutex_lock(&javaScriptThreadsMutex);
140        int threadCount = javaScriptThreads()->size();
141        pthread_mutex_unlock(&javaScriptThreadsMutex);
142
143        if (!threadCount)
144            break;
145
146        usleep(1000);
147    }
148}
149