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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
/* -*- 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"

#include "jscntxt.h"

/*
 * We test that if a GC callback cancels the GC on a child thread the GC can
 * still proceed on the main thread even if the child thread continue to
 * run uninterrupted.
 */

struct SharedData {
    enum ChildState {
        CHILD_STARTING,
        CHILD_RUNNING,
        CHILD_DONE,
        CHILD_ERROR
    };

    JSRuntime   *const runtime;
    PRThread    *const mainThread;
    PRLock      *const lock;
    PRCondVar   *const signal;
    ChildState  childState;
    bool        childShouldStop;
    JSContext   *childContext;

    SharedData(JSRuntime *rt, bool *ok)
      : runtime(rt),
        mainThread(PR_GetCurrentThread()),
        lock(PR_NewLock()),
        signal(lock ? PR_NewCondVar(lock) : NULL),
        childState(CHILD_STARTING),
        childShouldStop(false),
        childContext(NULL)
    {
        JS_ASSERT(!*ok);
        *ok = !!signal;
    }

    ~SharedData() {
        if (signal)
            PR_DestroyCondVar(signal);
        if (lock)
            PR_DestroyLock(lock);
    }
};

static SharedData *shared;

static JSBool
CancelNonMainThreadGCCallback(JSContext *cx, JSGCStatus status)
{
    return status != JSGC_BEGIN || PR_GetCurrentThread() == shared->mainThread;
}

static JSBool
StopChildOperationCallback(JSContext *cx)
{
    bool shouldStop;
    PR_Lock(shared->lock);
    shouldStop = shared->childShouldStop;
    PR_Unlock(shared->lock);
    return !shouldStop;
}

static JSBool
NotifyMainThreadAboutBusyLoop(JSContext *cx, uintN argc, jsval *vp)
{
    PR_Lock(shared->lock);
    JS_ASSERT(shared->childState == SharedData::CHILD_STARTING);
    shared->childState = SharedData::CHILD_RUNNING;
    shared->childContext = cx;
    PR_NotifyCondVar(shared->signal);
    PR_Unlock(shared->lock);

    return true;
}

static void
ChildThreadMain(void *arg)
{
    JS_ASSERT(!arg);
    bool error = true;
    JSContext *cx = JS_NewContext(shared->runtime, 8192);
    if (cx) {
        JS_SetOperationCallback(cx, StopChildOperationCallback);
        JSAutoRequest ar(cx);
        JSObject *global = JS_NewCompartmentAndGlobalObject(cx, JSAPITest::basicGlobalClass(),
                                                            NULL);
        if (global) {
            JS_SetGlobalObject(cx, global);
            if (JS_InitStandardClasses(cx, global) &&
                JS_DefineFunction(cx, global, "notify", NotifyMainThreadAboutBusyLoop, 0, 0)) {

                jsval rval;
                static const char code[] = "var i = 0; notify(); for (var i = 0; ; ++i);";
                JSBool ok = JS_EvaluateScript(cx, global, code, strlen(code),
                                              __FILE__, __LINE__, &rval);
                if (!ok && !JS_IsExceptionPending(cx)) {
                    /* Evaluate should only return via the callback cancellation. */
                    error = false;
                }
            }
        }
    }

    PR_Lock(shared->lock);
    shared->childState = error ? SharedData::CHILD_DONE : SharedData::CHILD_ERROR;
    shared->childContext = NULL;
    PR_NotifyCondVar(shared->signal);
    PR_Unlock(shared->lock);

    if (cx)
        JS_DestroyContextNoGC(cx);
}

BEGIN_TEST(testThreadGC_bug590533)
    {
        /*
         * Test the child thread busy running while the current thread calls
         * the GC both with JSRuntime->gcIsNeeded set and unset.
         */
        bool ok = TestChildThread(true);
        CHECK(ok);
        ok = TestChildThread(false);
        CHECK(ok);
        return ok;
    }

    bool TestChildThread(bool setGCIsNeeded)
    {
        bool ok = false;
        shared = new SharedData(rt, &ok);
        CHECK(ok);

        JSGCCallback oldGCCallback = JS_SetGCCallback(cx, CancelNonMainThreadGCCallback);

        PRThread *thread =
            PR_CreateThread(PR_USER_THREAD, ChildThreadMain, NULL,
                            PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
        if (!thread)
            return false;

        PR_Lock(shared->lock);
        while (shared->childState == SharedData::CHILD_STARTING)
            PR_WaitCondVar(shared->signal, PR_INTERVAL_NO_TIMEOUT);
        JS_ASSERT(shared->childState != SharedData::CHILD_DONE);
        ok = (shared->childState == SharedData::CHILD_RUNNING);
        PR_Unlock(shared->lock);

        CHECK(ok);

        if (setGCIsNeeded) {
            /*
             * Use JS internal API to set the GC trigger flag after we know
             * that the child is in a request and is about to run an infinite
             * loop. Then run the GC with JSRuntime->gcIsNeeded flag set.
             */
            js::AutoLockGC lock(rt);
            js::TriggerGC(rt);
        }

        JS_GC(cx);

        PR_Lock(shared->lock);
        shared->childShouldStop = true;
        while (shared->childState == SharedData::CHILD_RUNNING) {
            JS_TriggerOperationCallback(shared->childContext);
            PR_WaitCondVar(shared->signal, PR_INTERVAL_NO_TIMEOUT);
        }
        JS_ASSERT(shared->childState != SharedData::CHILD_STARTING);
        ok = (shared->childState == SharedData::CHILD_DONE);
        PR_Unlock(shared->lock);

        JS_SetGCCallback(cx, oldGCCallback);

        PR_JoinThread(thread);

        delete shared;
        shared = NULL;

        return true;
    }


END_TEST(testThreadGC_bug590533)

#endif