//------------------------------------------------------------------------------ /* This file is part of Beast: https://github.com/vinniefalco/Beast Copyright 2013, Vinnie Falco Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ //============================================================================== #include #include #include #include #include #include namespace beast { static_assert(__alignof(long) >= 4, ""); class static_initializer_test : public unit_test::suite { public: // Used to create separate instances for each test struct cxx11_tag { }; struct beast_tag { }; template struct Case { enum { count = N }; typedef Tag type; }; struct Counts { Counts() : calls (0) , constructed (0) , access (0) { } // number of calls to the constructor std::atomic calls; // incremented after construction completes std::atomic constructed; // increment when class is accessed before construction std::atomic access; }; /* This testing singleton detects two conditions: 1. Being accessed before getting fully constructed 2. Getting constructed twice */ template class Test; template static void run_many (std::size_t n, Function f); template void test (cxx11_tag); template void test (beast_tag); template void test(); void run (); }; //------------------------------------------------------------------------------ template class static_initializer_test::Test { public: explicit Test (Counts& counts); void operator() (Counts& counts); }; template static_initializer_test::Test::Test (Counts& counts) { ++counts.calls; std::this_thread::sleep_for (std::chrono::milliseconds (10)); ++counts.constructed; } template void static_initializer_test::Test::operator() (Counts& counts) { if (! counts.constructed) ++counts.access; } //------------------------------------------------------------------------------ template void static_initializer_test::run_many (std::size_t n, Function f) { std::atomic start (false); std::vector threads; threads.reserve (n); for (auto i (n); i-- ;) { threads.emplace_back([&]() { while (! start.load()) std::this_thread::yield(); f(); }); } std::this_thread::sleep_for (std::chrono::milliseconds (10)); std::this_thread::yield(); start.store (true); for (auto& thread : threads) thread.join(); } template void static_initializer_test::test (cxx11_tag) { testcase << "cxx11 " << Tag::count << " threads"; Counts counts; run_many (Tag::count, [&]() { static Test t (counts); t(counts); }); #ifdef _MSC_VER // Visual Studio 2013 and earlier can exhibit both double // construction, and access before construction when function // local statics are initialized concurrently. // expect (counts.constructed > 1 || counts.access > 0); #else expect (counts.constructed == 1 && counts.access == 0); #endif } template void static_initializer_test::test (beast_tag) { testcase << "beast " << Tag::count << " threads"; Counts counts; run_many (Tag::count, [&counts]() { static static_initializer > t (counts); (*t)(counts); }); expect (counts.constructed == 1 && counts.access == 0); } template void static_initializer_test::test() { test (typename Tag::type {}); } void static_initializer_test::run () { test > (); test > (); test > (); test > (); test > (); test > (); test > (); test > (); } //------------------------------------------------------------------------------ BEAST_DEFINE_TESTSUITE(static_initializer,utility,beast); }