DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (5216dd412535)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sw=4 et tw=99:
 */

#ifdef JS_THREADSAFE

#include "tests.h"
#include "prthread.h"

struct ThreadData {
    JSRuntime *rt;
    JSObject *obj;
    const char *code;
    bool ok;
};

BEGIN_TEST(testThreads_bug561444)
    {
        const char *code = "<a><b/></a>.b.@c = '';";
        EXEC(code);

        jsrefcount rc = JS_SuspendRequest(cx);
        {
            ThreadData data = {rt, global, code, false};
            PRThread *thread = 
                PR_CreateThread(PR_USER_THREAD, threadMain, &data,
                                PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
            CHECK(thread);
            PR_JoinThread(thread);
            CHECK(data.ok);
        }
        JS_ResumeRequest(cx, rc);
        return true;
    }

    static void threadMain(void *arg) {
        ThreadData *d = (ThreadData *) arg;

        JSContext *cx = JS_NewContext(d->rt, 8192);
        if (!cx)
            return;
        JS_BeginRequest(cx);
        {
            JSAutoEnterCompartment ac;
            jsval v;
            d->ok = ac.enter(cx, d->obj) &&
                    JS_EvaluateScript(cx, d->obj, d->code, strlen(d->code), __FILE__, __LINE__,
                                      &v);
        }
        JS_DestroyContext(cx);
    }
END_TEST(testThreads_bug561444)

const PRUint32 NATIVE_STACK_SIZE = 64 * 1024;
const PRUint32 NATIVE_STACK_HEADROOM = 8 * 1024;

template <class T>
class Repeat {
    size_t n;
    const T &t;

  public:
    Repeat(size_t n, const T &t) : n(n), t(t) {}

    bool operator()() const {
	for (size_t i = 0; i < n; i++)
	    if (!t())
		return false;
	return true;
    }
};

template <class T> Repeat<T> repeat(size_t n, const T &t) { return Repeat<T>(n, t); }

/* Class of callable that does something in n parallel threads. */
template <class T>
class Parallel {
    size_t n;
    const T &t;

    struct pair { const Parallel *self; bool ok; };

    static void threadMain(void *arg) {
	pair *p = (pair *) arg;
	if (!p->self->t())
	    p->ok = false;
    }

  public:
    Parallel(size_t n, const T &t) : n(n), t(t) {}

    bool operator()() const {
	pair p = {this, true};

        PRThread **thread = new PRThread *[n];
	if (!thread)
	    return false;

        size_t i;
        for (i = 0; i < n; i++) {
            thread[i] = PR_CreateThread(PR_USER_THREAD, threadMain, &p, PR_PRIORITY_NORMAL,
                                        PR_LOCAL_THREAD, PR_JOINABLE_THREAD, NATIVE_STACK_SIZE);
            if (thread[i] == NULL) {
                p.ok = false;
                break;
            }
        }
        while (i--)
            PR_JoinThread(thread[i]);

	delete[] thread;
        return p.ok;
    }
};

template <class T> Parallel<T> parallel(size_t n, const T &t) { return Parallel<T>(n, t); }

/* Class of callable that creates a compartment and runs some code in it. */
class eval {
    JSRuntime *rt;
    const char *code;

  public:
    eval(JSRuntime *rt, const char *code) : rt(rt), code(code) {}

    bool operator()() const {
        JSContext *cx = JS_NewContext(rt, 8192);
	if (!cx)
	    return false;

        JS_SetNativeStackQuota(cx, NATIVE_STACK_SIZE - NATIVE_STACK_HEADROOM);
        bool ok = false;
	{
	    JSAutoRequest ar(cx);
	    JSObject *global =
		JS_NewCompartmentAndGlobalObject(cx, JSAPITest::basicGlobalClass(), NULL);
	    if (global) {
		JS_SetGlobalObject(cx, global);
		jsval rval;
		ok = JS_InitStandardClasses(cx, global) &&
		    JS_EvaluateScript(cx, global, code, strlen(code), "", 0, &rval);
	    }
	}
	JS_DestroyContextMaybeGC(cx);
        return ok;
    }
};

BEGIN_TEST(testThreads_bug604782)
{
    jsrefcount rc = JS_SuspendRequest(cx);
    bool ok = repeat(20, parallel(3, eval(rt, "for(i=0;i<1000;i++);")))();
    JS_ResumeRequest(cx, rc);
    CHECK(ok);
    return true;
}
END_TEST(testThreads_bug604782)

BEGIN_TEST(testThreads_bug609103)
{
    const char *code = 
        "var x = {};\n"
        "for (var i = 0; i < 10000; i++)\n"
        "    x = {next: x};\n";

    jsrefcount rc = JS_SuspendRequest(cx);
    bool ok = parallel(2, eval(rt, code))();
    JS_ResumeRequest(cx, rc);
    CHECK(ok);
    return true;
}
END_TEST(testThreads_bug609103)

#endif