1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=99:
17 BEGIN_TEST(testThreads_bug561444)
19 const char *code = "<a><b/></a>.b.@c = '';";
22 jsrefcount rc = JS_SuspendRequest(cx);
24 ThreadData data = {rt, global, code, false};
26 PR_CreateThread(PR_USER_THREAD, threadMain, &data,
27 PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
29 PR_JoinThread(thread);
32 JS_ResumeRequest(cx, rc);
36 static void threadMain(void *arg) {
37 ThreadData *d = (ThreadData *) arg;
39 JSContext *cx = JS_NewContext(d->rt, 8192);
44 JSAutoEnterCompartment ac;
46 d->ok = ac.enter(cx, d->obj) &&
47 JS_EvaluateScript(cx, d->obj, d->code, strlen(d->code), __FILE__, __LINE__,
50 JS_DestroyContext(cx);
52 END_TEST(testThreads_bug561444)
54 const PRUint32 NATIVE_STACK_SIZE = 64 * 1024;
55 const PRUint32 NATIVE_STACK_HEADROOM = 8 * 1024;
63 Repeat(size_t n, const T &t) : n(n), t(t) {}
65 bool operator()() const {
66 for (size_t i = 0; i < n; i++)
73 template <class T> Repeat<T> repeat(size_t n, const T &t) { return Repeat<T>(n, t); }
75 /* Class of callable that does something in n parallel threads. */
81 struct pair { const Parallel *self; bool ok; };
83 static void threadMain(void *arg) {
84 pair *p = (pair *) arg;
90 Parallel(size_t n, const T &t) : n(n), t(t) {}
92 bool operator()() const {
93 pair p = {this, true};
95 PRThread **thread = new PRThread *[n];
100 for (i = 0; i < n; i++) {
101 thread[i] = PR_CreateThread(PR_USER_THREAD, threadMain, &p, PR_PRIORITY_NORMAL,
102 PR_LOCAL_THREAD, PR_JOINABLE_THREAD, NATIVE_STACK_SIZE);
103 if (thread[i] == NULL) {
109 PR_JoinThread(thread[i]);
116 template <class T> Parallel<T> parallel(size_t n, const T &t) { return Parallel<T>(n, t); }
118 /* Class of callable that creates a compartment and runs some code in it. */
124 eval(JSRuntime *rt, const char *code) : rt(rt), code(code) {}
126 bool operator()() const {
127 JSContext *cx = JS_NewContext(rt, 8192);
131 JS_SetNativeStackQuota(cx, NATIVE_STACK_SIZE - NATIVE_STACK_HEADROOM);
134 JSAutoRequest ar(cx);
136 JS_NewCompartmentAndGlobalObject(cx, JSAPITest::basicGlobalClass(), NULL);
138 JS_SetGlobalObject(cx, global);
140 ok = JS_InitStandardClasses(cx, global) &&
141 JS_EvaluateScript(cx, global, code, strlen(code), "", 0, &rval);
144 JS_DestroyContextMaybeGC(cx);
149 BEGIN_TEST(testThreads_bug604782)
151 jsrefcount rc = JS_SuspendRequest(cx);
152 bool ok = repeat(20, parallel(3, eval(rt, "for(i=0;i<1000;i++);")))();
153 JS_ResumeRequest(cx, rc);
157 END_TEST(testThreads_bug604782)
159 BEGIN_TEST(testThreads_bug609103)
163 "for (var i = 0; i < 10000; i++)\n"
166 jsrefcount rc = JS_SuspendRequest(cx);
167 bool ok = parallel(2, eval(rt, code))();
168 JS_ResumeRequest(cx, rc);
172 END_TEST(testThreads_bug609103)