js/src/jsapi-tests/testThreadingThread.cpp
author Jon Coppeard <jcoppeard@mozilla.com>
Sat, 18 Jun 2016 10:46:13 +0100
changeset 302054 afc3c6a5f93a4e5b5309659a13a84e7ec5c8fb2e
parent 302053 c6f2a2408e4d4904ba5f0da56f2eb2f58e511672
child 302055 0d735d33bd844616bf224f3fabd56dd694cd03e9
permissions -rw-r--r--
Bug 1280407 - Use SystemAllocPolicy rather that the default with mozilla::Vector in the JS engine r=sfink r=fitzgen r=jandem

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "jsalloc.h"

#include "mozilla/Atomics.h"
#include "mozilla/IntegerRange.h"
#include "mozilla/Move.h"
#include "mozilla/Vector.h"
#include "jsapi-tests/tests.h"
#include "threading/Thread.h"

BEGIN_TEST(testThreadingThreadJoin)
{
    bool flag = false;
    js::Thread thread([](bool* flagp){*flagp = true;}, &flag);
    CHECK(thread.joinable());
    thread.join();
    CHECK(flag);
    CHECK(!thread.joinable());
    return true;
}
END_TEST(testThreadingThreadJoin)

BEGIN_TEST(testThreadingThreadDetach)
{
    // We are going to detach this thread. Unlike join, we can't have it pointing at the stack
    // because it might do the write after we have returned and pushed a new frame.
    bool* flag = js_new<bool>(false);
    js::Thread thread([](bool* flag){*flag = true; js_delete(flag);}, mozilla::Move(flag));
    CHECK(thread.joinable());
    thread.detach();
    CHECK(!thread.joinable());

    return true;
}
END_TEST(testThreadingThreadDetach)

BEGIN_TEST(testThreadingThreadSetName)
{
    js::Thread thread([](){js::ThisThread::SetName("JSAPI Test Thread");});
    thread.detach();
    return true;
}
END_TEST(testThreadingThreadSetName)

BEGIN_TEST(testThreadingThreadId)
{
    CHECK(js::Thread::Id() == js::Thread::Id());
    js::Thread::Id fromOther;
    js::Thread thread([](js::Thread::Id* idp){*idp = js::ThisThread::GetId();}, &fromOther);
    js::Thread::Id fromMain = thread.get_id();
    thread.join();
    CHECK(fromOther == fromMain);
    return true;
}
END_TEST(testThreadingThreadId)

BEGIN_TEST(testThreadingThreadVectorMoveConstruct)
{
    const static size_t N = 10;
    mozilla::Atomic<int> count(0);
    mozilla::Vector<js::Thread, 0, js::SystemAllocPolicy> v;
    for (auto i : mozilla::MakeRange(N)) {
        CHECK(v.emplaceBack([](mozilla::Atomic<int>* countp){(*countp)++;}, &count));
        CHECK(v.length() == i + 1);
    }
    for (auto& th : v)
        th.join();
    CHECK(count == 10);
    return true;
}
END_TEST(testThreadingThreadVectorMoveConstruct)

// This test is checking that args are using "decay" copy, per spec. If we do
// not use decay copy properly, the rvalue reference |bool&& b| in the
// constructor will automatically become an lvalue reference |bool& b| in the
// trampoline, causing us to read through the reference when passing |bool bb|
// from the trampoline. If the parent runs before the child, the bool may have
// already become false, causing the trampoline to read the changed value, thus
// causing the child's assertion to fail.
BEGIN_TEST(testThreadingThreadArgCopy)
{
    for (size_t i = 0; i < 10000; ++i) {
        bool b = true;
        js::Thread thread([](bool bb){MOZ_RELEASE_ASSERT(bb);}, b);
        b = false;
        thread.join();
    }
    return true;
}
END_TEST(testThreadingThreadArgCopy)