mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-04 19:25:51 +00:00
Use more C++11 features:
* Remove beast::static_initializer * Remove noexcept VS2013 workaround * Use [[noreturn]] attribute
This commit is contained in:
@@ -26,8 +26,6 @@
|
||||
|
||||
#include <beast/Config.h>
|
||||
|
||||
#include <beast/utility/noexcept.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <algorithm>
|
||||
|
||||
@@ -27,7 +27,6 @@
|
||||
#include <beast/Config.h>
|
||||
#include <beast/strings/String.h>
|
||||
|
||||
#include <beast/utility/noexcept.h>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
@@ -29,7 +29,6 @@
|
||||
|
||||
#include <beast/Config.h>
|
||||
|
||||
#include <beast/utility/noexcept.h>
|
||||
#include <ctime>
|
||||
#include <locale>
|
||||
|
||||
|
||||
@@ -22,5 +22,4 @@
|
||||
#ifndef BEAST_CONFIG_COMPILER_VISUALC_H_INCLUDED
|
||||
#define BEAST_CONFIG_COMPILER_VISUALC_H_INCLUDED
|
||||
|
||||
#include <beast/utility/noexcept.h>
|
||||
#endif
|
||||
|
||||
@@ -22,7 +22,6 @@
|
||||
|
||||
#include <beast/crypto/secure_erase.h>
|
||||
#include <beast/hash/endian.h>
|
||||
#include <beast/utility/noexcept.h>
|
||||
#include <type_traits>
|
||||
#include <array>
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#ifndef BEAST_CRYPTO_RIPEMD_CONTEXT_H_INCLUDED
|
||||
#define BEAST_CRYPTO_RIPEMD_CONTEXT_H_INCLUDED
|
||||
|
||||
#include <beast/utility/noexcept.h>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#ifndef BEAST_CRYPTO_SHA512_CONTEXT_H_INCLUDED
|
||||
#define BEAST_CRYPTO_SHA512_CONTEXT_H_INCLUDED
|
||||
|
||||
#include <beast/utility/noexcept.h>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
#ifndef BEAST_HASH_FNV1A_H_INCLUDED
|
||||
#define BEAST_HASH_FNV1A_H_INCLUDED
|
||||
|
||||
#include <beast/utility/noexcept.h>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <beast/cxx14/type_traits.h> // <type_traits>
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
#include <beast/config/CompilerConfig.h> // for constexpr
|
||||
#include <beast/hash/endian.h>
|
||||
#include <beast/utility/meta.h>
|
||||
#include <beast/utility/noexcept.h>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
#ifndef BEAST_HASH_SIPHASH_H_INCLUDED
|
||||
#define BEAST_HASH_SIPHASH_H_INCLUDED
|
||||
|
||||
#include <beast/utility/noexcept.h>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
@@ -28,7 +28,6 @@
|
||||
|
||||
#include <beast/hash/endian.h>
|
||||
#include <beast/hash/impl/xxhash.h>
|
||||
#include <beast/utility/noexcept.h>
|
||||
#include <beast/cxx14/type_traits.h> // <type_traits>
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
@@ -52,22 +52,7 @@ public:
|
||||
URL& operator= (URL const& other) = default;
|
||||
|
||||
/** Move construct a URL. */
|
||||
#ifdef _MSC_VER
|
||||
URL (URL&& other)
|
||||
: m_scheme(std::move(other.m_scheme))
|
||||
, m_host(std::move(other.m_host))
|
||||
, m_port(other.m_port)
|
||||
, m_port_string(std::move(other.m_port_string))
|
||||
, m_path(std::move(other.m_path))
|
||||
, m_query(std::move(other.m_query))
|
||||
, m_fragment(std::move(other.m_fragment))
|
||||
, m_userinfo(std::move(other.m_userinfo))
|
||||
{
|
||||
}
|
||||
|
||||
#else
|
||||
URL (URL&& other) = default;
|
||||
#endif
|
||||
|
||||
/** Returns `true` if this is an empty URL. */
|
||||
bool
|
||||
|
||||
@@ -27,7 +27,6 @@
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <beast/utility/noexcept.h>
|
||||
|
||||
namespace beast {
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#include <beast/http/basic_parser.h>
|
||||
#include <beast/http/impl/joyent_parser.h>
|
||||
#include <beast/http/rfc2616.h>
|
||||
#include <beast/utility/noexcept.h>
|
||||
#include <boost/system/error_code.hpp>
|
||||
|
||||
namespace beast {
|
||||
|
||||
@@ -76,16 +76,9 @@ public:
|
||||
template <class = void>
|
||||
message();
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER <= 1800
|
||||
message (message&& other);
|
||||
message& operator= (message&& other);
|
||||
|
||||
#else
|
||||
message (message&& other) = default;
|
||||
message& operator= (message&& other) = default;
|
||||
|
||||
#endif
|
||||
|
||||
// Memberspace
|
||||
beast::http::headers headers;
|
||||
|
||||
@@ -220,38 +213,6 @@ message::message()
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER <= 1800
|
||||
inline
|
||||
message::message (message&& other)
|
||||
: request_ (other.request_)
|
||||
, method_ (std::move(other.method_))
|
||||
, url_ (std::move(other.url_))
|
||||
, status_ (other.status_)
|
||||
, reason_ (std::move(other.reason_))
|
||||
, version_ (other.version_)
|
||||
, keep_alive_ (other.keep_alive_)
|
||||
, upgrade_ (other.upgrade_)
|
||||
, headers (std::move(other.headers))
|
||||
{
|
||||
}
|
||||
|
||||
inline
|
||||
message&
|
||||
message::operator= (message&& other)
|
||||
{
|
||||
request_ = other.request_;
|
||||
method_ = std::move(other.method_);
|
||||
url_ = std::move(other.url_);
|
||||
status_ = other.status_;
|
||||
reason_ = std::move(other.reason_);
|
||||
version_ = other.version_;
|
||||
keep_alive_ = other.keep_alive_;
|
||||
upgrade_ = other.upgrade_;
|
||||
headers = std::move(other.headers);
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template <class Streambuf>
|
||||
|
||||
@@ -66,14 +66,8 @@ public:
|
||||
message_.get().request(request);
|
||||
}
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER <= 1800
|
||||
parser& operator= (parser&& other);
|
||||
|
||||
#else
|
||||
parser& operator= (parser&& other) = default;
|
||||
|
||||
#endif
|
||||
|
||||
private:
|
||||
template <class = void>
|
||||
void
|
||||
@@ -141,18 +135,6 @@ private:
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER <= 1800
|
||||
inline
|
||||
parser&
|
||||
parser::operator= (parser&& other)
|
||||
{
|
||||
basic_parser::operator= (std::move(other));
|
||||
message_ = std::move (other.message_);
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
|
||||
template <class>
|
||||
void
|
||||
parser::do_start()
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include <beast/module/core/diagnostic/FatalError.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdlib>
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
@@ -27,49 +28,38 @@
|
||||
namespace beast {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
[[noreturn]]
|
||||
void
|
||||
FatalError (char const* message, char const* file, int line)
|
||||
FatalError (
|
||||
char const* message,
|
||||
char const* file,
|
||||
int line) noexcept
|
||||
{
|
||||
static std::atomic <int> error_count (0);
|
||||
static std::recursive_mutex gate;
|
||||
static std::mutex gate;
|
||||
|
||||
// We only allow one thread to report a fatal error. Other threads that
|
||||
// encounter fatal errors while we are reporting get blocked here.
|
||||
std::lock_guard<std::recursive_mutex> lock(gate);
|
||||
std::lock_guard<std::mutex> lock(gate);
|
||||
|
||||
// If we encounter a recursive fatal error, then we want to terminate
|
||||
// unconditionally.
|
||||
if (error_count++ != 0)
|
||||
return std::terminate ();
|
||||
std::cerr << "An error has occurred. The application will terminate.\n";
|
||||
|
||||
// We protect this entire block of code since writing to cerr might trigger
|
||||
// exceptions.
|
||||
try
|
||||
if (message != nullptr && message [0] != 0)
|
||||
std::cerr << "Message: " << message << '\n';
|
||||
|
||||
if (file != nullptr && file [0] != 0)
|
||||
std::cerr << " File: " << file << ":" << line << '\n';
|
||||
|
||||
auto const backtrace = getStackBacktrace ();
|
||||
|
||||
if (!backtrace.empty ())
|
||||
{
|
||||
std::cerr << "An error has occurred. The application will terminate.\n";
|
||||
std::cerr << " Stack:" << std::endl;
|
||||
|
||||
if (message != nullptr && message [0] != 0)
|
||||
std::cerr << "Message: " << message << '\n';
|
||||
|
||||
if (file != nullptr && file [0] != 0)
|
||||
std::cerr << " File: " << file << ":" << line << '\n';
|
||||
|
||||
auto const backtrace = getStackBacktrace ();
|
||||
|
||||
if (!backtrace.empty ())
|
||||
{
|
||||
std::cerr << " Stack:" << std::endl;
|
||||
|
||||
for (auto const& frame : backtrace)
|
||||
std::cerr << " " << frame << '\n';
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
// nothing we can do - just fall through and terminate
|
||||
for (auto const& frame : backtrace)
|
||||
std::cerr << " " << frame << '\n';
|
||||
}
|
||||
|
||||
return std::terminate ();
|
||||
std::abort ();
|
||||
}
|
||||
|
||||
} // beast
|
||||
|
||||
@@ -35,8 +35,12 @@ namespace beast
|
||||
If multiple threads raise an error, only one will succeed while the others
|
||||
will be blocked before the process terminates.
|
||||
*/
|
||||
[[noreturn]]
|
||||
void
|
||||
FatalError (char const* message, char const* file = nullptr, int line = 0);
|
||||
FatalError (
|
||||
char const* message,
|
||||
char const* file = nullptr,
|
||||
int line = 0) noexcept;
|
||||
|
||||
} // beast
|
||||
|
||||
|
||||
@@ -23,8 +23,6 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include <beast/utility/noexcept.h>
|
||||
|
||||
namespace beast {
|
||||
|
||||
/** A Semantic Version number.
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <beast/utility/static_initializer.h>
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
|
||||
@@ -29,8 +28,8 @@ namespace beast {
|
||||
|
||||
File const& File::nonexistent()
|
||||
{
|
||||
static beast::static_initializer<File> instance;
|
||||
return *instance;
|
||||
static File const instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
@@ -17,8 +17,6 @@
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <beast/utility/static_initializer.h>
|
||||
|
||||
namespace beast
|
||||
{
|
||||
|
||||
@@ -47,8 +45,8 @@ public:
|
||||
Manager&
|
||||
instance()
|
||||
{
|
||||
static beast::static_initializer<Manager> m;
|
||||
return *m;
|
||||
static Manager m;
|
||||
return m;
|
||||
}
|
||||
|
||||
// Okay to call on an active timer.
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
#include <beast/net/IPAddressV6.h>
|
||||
#include <beast/hash/hash_append.h>
|
||||
#include <beast/hash/uhash.h>
|
||||
#include <beast/utility/noexcept.h>
|
||||
#include <boost/functional/hash.hpp>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#ifndef BEAST_NUDB_DETAIL_GENTEX_H_INCLUDED
|
||||
#define BEAST_NUDB_DETAIL_GENTEX_H_INCLUDED
|
||||
|
||||
#include <beast/utility/noexcept.h>
|
||||
#include <condition_variable>
|
||||
#include <cstddef>
|
||||
#include <mutex>
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
#define BEAST_THREADS_SPINLOCK_H_INCLUDED
|
||||
|
||||
#include <beast/threads/UnlockGuard.h>
|
||||
#include <beast/utility/noexcept.h>
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include <mutex>
|
||||
|
||||
@@ -22,7 +22,6 @@
|
||||
|
||||
#include <beast/unit_test/runner.h>
|
||||
|
||||
#include <beast/utility/noexcept.h>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#ifndef BEAST_UNIT_TEST_THREAD_H_INCLUDED
|
||||
#define BEAST_UNIT_TEST_THREAD_H_INCLUDED
|
||||
|
||||
#include <beast/utility/noexcept.h>
|
||||
#include <beast/unit_test/suite.h>
|
||||
#include <functional>
|
||||
#include <thread>
|
||||
|
||||
@@ -28,7 +28,6 @@
|
||||
#include <beast/utility/tests/bassert.test.cpp>
|
||||
#include <beast/utility/tests/empty_base_optimization.test.cpp>
|
||||
#include <beast/utility/tests/Journal.test.cpp>
|
||||
#include <beast/utility/tests/static_initializer.test.cpp>
|
||||
#include <beast/utility/tests/tagged_integer.test.cpp>
|
||||
#include <beast/utility/tests/weak_fn.test.cpp>
|
||||
#include <beast/utility/tests/Zero.test.cpp>
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
#ifndef BEAST_UTILITY_EMPTY_BASE_OPTIMIZATION_H_INCLUDED
|
||||
#define BEAST_UTILITY_EMPTY_BASE_OPTIMIZATION_H_INCLUDED
|
||||
|
||||
#include <beast/utility/noexcept.h>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
//==============================================================================
|
||||
|
||||
#include <beast/utility/Journal.h>
|
||||
#include <beast/utility/static_initializer.h>
|
||||
|
||||
namespace beast {
|
||||
|
||||
@@ -60,8 +59,8 @@ public:
|
||||
|
||||
Journal::Sink& Journal::getNullSink ()
|
||||
{
|
||||
static beast::static_initializer<NullJournalSink> sink;
|
||||
return *sink;
|
||||
static NullJournalSink sink;
|
||||
return sink;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of Beast: https://github.com/vinniefalco/Beast
|
||||
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
|
||||
|
||||
Portions of this file are from JUCE.
|
||||
Copyright (c) 2013 - Raw Material Software Ltd.
|
||||
Please visit http://www.juce.com
|
||||
|
||||
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.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef BEAST_UTILITY_NOEXCEPT_H_INCLUDED
|
||||
#define BEAST_UTILITY_NOEXCEPT_H_INCLUDED
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# if _MSC_VER < 1900
|
||||
# ifdef noexcept
|
||||
# undef noexcept
|
||||
# endif
|
||||
# define noexcept throw()
|
||||
# endif
|
||||
# if _MSC_VER > 1600 && ! _ALLOW_KEYWORD_MACROS
|
||||
# define _ALLOW_KEYWORD_MACROS 1 // (to stop VC2012 complaining)
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,258 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of Beast: https://github.com/vinniefalco/Beast
|
||||
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
|
||||
|
||||
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.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef BEAST_UTILITY_STATIC_INITIALIZER_H_INCLUDED
|
||||
#define BEAST_UTILITY_STATIC_INITIALIZER_H_INCLUDED
|
||||
|
||||
#include <beast/utility/noexcept.h>
|
||||
#include <utility>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#include <cassert>
|
||||
#include <new>
|
||||
#include <thread>
|
||||
#include <type_traits>
|
||||
#include <intrin.h>
|
||||
#endif
|
||||
|
||||
namespace beast {
|
||||
|
||||
/** Returns an object with static storage duration.
|
||||
This is a workaround for Visual Studio 2013 and earlier non-thread
|
||||
safe initialization of function local objects with static storage duration.
|
||||
|
||||
Usage:
|
||||
@code
|
||||
my_class& foo()
|
||||
{
|
||||
static static_initializer <my_class> instance;
|
||||
return *instance;
|
||||
}
|
||||
@endcode
|
||||
*/
|
||||
#ifdef _MSC_VER
|
||||
template <
|
||||
class T,
|
||||
class Tag = void
|
||||
>
|
||||
class static_initializer
|
||||
{
|
||||
private:
|
||||
struct data_t
|
||||
{
|
||||
// 0 = unconstructed
|
||||
// 1 = constructing
|
||||
// 2 = constructed
|
||||
long volatile state;
|
||||
|
||||
typename std::aligned_storage <sizeof(T),
|
||||
std::alignment_of <T>::value>::type storage;
|
||||
};
|
||||
|
||||
struct destroyer
|
||||
{
|
||||
T* t_;
|
||||
explicit destroyer (T* t) : t_(t) { }
|
||||
~destroyer() { t_->~T(); }
|
||||
};
|
||||
|
||||
static
|
||||
data_t&
|
||||
data() noexcept;
|
||||
|
||||
public:
|
||||
template <class... Args>
|
||||
explicit static_initializer (Args&&... args);
|
||||
|
||||
T&
|
||||
get() noexcept;
|
||||
|
||||
T const&
|
||||
get() const noexcept
|
||||
{
|
||||
return const_cast<static_initializer&>(*this).get();
|
||||
}
|
||||
|
||||
T&
|
||||
operator*() noexcept
|
||||
{
|
||||
return get();
|
||||
}
|
||||
|
||||
T const&
|
||||
operator*() const noexcept
|
||||
{
|
||||
return get();
|
||||
}
|
||||
|
||||
T*
|
||||
operator->() noexcept
|
||||
{
|
||||
return &get();
|
||||
}
|
||||
|
||||
T const*
|
||||
operator->() const noexcept
|
||||
{
|
||||
return &get();
|
||||
}
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template <class T, class Tag>
|
||||
auto
|
||||
static_initializer <T, Tag>::data() noexcept ->
|
||||
data_t&
|
||||
{
|
||||
static data_t _; // zero-initialized
|
||||
return _;
|
||||
}
|
||||
|
||||
template <class T, class Tag>
|
||||
template <class... Args>
|
||||
static_initializer <T, Tag>::static_initializer (Args&&... args)
|
||||
{
|
||||
data_t& _(data());
|
||||
|
||||
// Double Checked Locking Pattern
|
||||
|
||||
if (_.state != 2)
|
||||
{
|
||||
T* const t (reinterpret_cast<T*>(&_.storage));
|
||||
|
||||
for(;;)
|
||||
{
|
||||
long prev;
|
||||
prev = _InterlockedCompareExchange(&_.state, 1, 0);
|
||||
if (prev == 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
::new(t) T (std::forward<Args>(args)...);
|
||||
static destroyer on_exit (t);
|
||||
_InterlockedIncrement(&_.state);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
// Constructors that throw exceptions are unsupported
|
||||
std::terminate();
|
||||
}
|
||||
}
|
||||
else if (prev == 1)
|
||||
{
|
||||
std::this_thread::yield();
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(prev == 2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <class T, class Tag>
|
||||
T&
|
||||
static_initializer <T, Tag>::get() noexcept
|
||||
{
|
||||
data_t& _(data());
|
||||
for(;;)
|
||||
{
|
||||
if (_.state == 2)
|
||||
break;
|
||||
std::this_thread::yield();
|
||||
}
|
||||
return *reinterpret_cast<T*>(&_.storage);
|
||||
}
|
||||
|
||||
#else
|
||||
template <
|
||||
class T,
|
||||
class Tag = void
|
||||
>
|
||||
class static_initializer
|
||||
{
|
||||
private:
|
||||
T* instance_;
|
||||
|
||||
public:
|
||||
template <class... Args>
|
||||
explicit
|
||||
static_initializer (Args&&... args);
|
||||
|
||||
static_initializer ();
|
||||
|
||||
T&
|
||||
get() noexcept
|
||||
{
|
||||
return *instance_;
|
||||
}
|
||||
|
||||
T const&
|
||||
get() const noexcept
|
||||
{
|
||||
return const_cast<static_initializer&>(*this).get();
|
||||
}
|
||||
|
||||
T&
|
||||
operator*() noexcept
|
||||
{
|
||||
return get();
|
||||
}
|
||||
|
||||
T const&
|
||||
operator*() const noexcept
|
||||
{
|
||||
return get();
|
||||
}
|
||||
|
||||
T*
|
||||
operator->() noexcept
|
||||
{
|
||||
return &get();
|
||||
}
|
||||
|
||||
T const*
|
||||
operator->() const noexcept
|
||||
{
|
||||
return &get();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T, class Tag>
|
||||
static_initializer <T, Tag>::static_initializer ()
|
||||
{
|
||||
static T t;
|
||||
instance_ = &t;
|
||||
}
|
||||
|
||||
template <class T, class Tag>
|
||||
template <class... Args>
|
||||
static_initializer <T, Tag>::static_initializer (Args&&... args)
|
||||
{
|
||||
static T t (std::forward<Args>(args)...);
|
||||
instance_ = &t;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -22,8 +22,6 @@
|
||||
|
||||
#include <beast/hash/hash_append.h>
|
||||
|
||||
#include <beast/utility/noexcept.h>
|
||||
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <type_traits>
|
||||
|
||||
@@ -1,209 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of Beast: https://github.com/vinniefalco/Beast
|
||||
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
|
||||
|
||||
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 <beast/utility/static_initializer.h>
|
||||
#include <beast/unit_test/suite.h>
|
||||
#include <atomic>
|
||||
#include <sstream>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
|
||||
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 <std::size_t N, class Tag>
|
||||
struct Case
|
||||
{
|
||||
enum { count = N };
|
||||
using type = Tag;
|
||||
};
|
||||
|
||||
struct Counts
|
||||
{
|
||||
Counts()
|
||||
: calls (0)
|
||||
, constructed (0)
|
||||
, access (0)
|
||||
{
|
||||
}
|
||||
|
||||
// number of calls to the constructor
|
||||
std::atomic <long> calls;
|
||||
|
||||
// incremented after construction completes
|
||||
std::atomic <long> constructed;
|
||||
|
||||
// increment when class is accessed before construction
|
||||
std::atomic <long> access;
|
||||
};
|
||||
|
||||
/* This testing singleton detects two conditions:
|
||||
1. Being accessed before getting fully constructed
|
||||
2. Getting constructed twice
|
||||
*/
|
||||
template <class Tag>
|
||||
class Test;
|
||||
|
||||
template <class Function>
|
||||
static
|
||||
void
|
||||
run_many (std::size_t n, Function f);
|
||||
|
||||
template <class Tag>
|
||||
void
|
||||
test (cxx11_tag);
|
||||
|
||||
template <class Tag>
|
||||
void
|
||||
test (beast_tag);
|
||||
|
||||
template <class Tag>
|
||||
void
|
||||
test();
|
||||
|
||||
void
|
||||
run ();
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template <class Tag>
|
||||
class static_initializer_test::Test
|
||||
{
|
||||
public:
|
||||
explicit
|
||||
Test (Counts& counts);
|
||||
|
||||
void
|
||||
operator() (Counts& counts);
|
||||
};
|
||||
|
||||
template <class Tag>
|
||||
static_initializer_test::Test<Tag>::Test (Counts& counts)
|
||||
{
|
||||
++counts.calls;
|
||||
std::this_thread::sleep_for (std::chrono::milliseconds (10));
|
||||
++counts.constructed;
|
||||
}
|
||||
|
||||
template <class Tag>
|
||||
void
|
||||
static_initializer_test::Test<Tag>::operator() (Counts& counts)
|
||||
{
|
||||
if (! counts.constructed)
|
||||
++counts.access;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
template <class Function>
|
||||
void
|
||||
static_initializer_test::run_many (std::size_t n, Function f)
|
||||
{
|
||||
std::atomic <bool> start (false);
|
||||
std::vector <std::thread> 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 <class Tag>
|
||||
void
|
||||
static_initializer_test::test (cxx11_tag)
|
||||
{
|
||||
testcase << "cxx11 " << Tag::count << " threads";
|
||||
|
||||
Counts counts;
|
||||
|
||||
run_many (Tag::count, [&]()
|
||||
{
|
||||
static Test <Tag> t (counts);
|
||||
t(counts);
|
||||
});
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER < 1900
|
||||
// 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 <class Tag>
|
||||
void
|
||||
static_initializer_test::test (beast_tag)
|
||||
{
|
||||
testcase << "beast " << Tag::count << " threads";
|
||||
|
||||
Counts counts;
|
||||
|
||||
run_many (Tag::count, [&counts]()
|
||||
{
|
||||
static static_initializer <Test <Tag>> t (counts);
|
||||
(*t)(counts);
|
||||
});
|
||||
|
||||
expect (counts.constructed == 1 && counts.access == 0);
|
||||
}
|
||||
|
||||
template <class Tag>
|
||||
void
|
||||
static_initializer_test::test()
|
||||
{
|
||||
test <Tag> (typename Tag::type {});
|
||||
}
|
||||
|
||||
void
|
||||
static_initializer_test::run ()
|
||||
{
|
||||
test <Case< 4, cxx11_tag>> ();
|
||||
test <Case< 4, beast_tag>> ();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(static_initializer,utility,beast);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user