From 0a96f3a2490f2434722178dd528e5b422f94f65e Mon Sep 17 00:00:00 2001 From: Nik Bougalis Date: Sat, 9 Jan 2016 02:43:30 -0800 Subject: [PATCH] Simplify Beast: * Remove obsolete RNG facilities * Flatten directory structure * Use std::recursive_mutex instead of beast::RecursiveMutex --- Builds/VisualStudio2015/RippleD.vcxproj | 16 +- .../VisualStudio2015/RippleD.vcxproj.filters | 28 +- src/beast/beast/crypto/detail/mac_facade.h | 6 +- src/beast/beast/hash/impl/hash_speed_test.cpp | 4 +- .../beast/hash/tests/hash_append_test.cpp | 6 +- .../beast/hash/tests/hash_speed_test.cpp | 4 +- src/beast/beast/module/core/core.h | 1 - src/beast/beast/module/core/core.unity.cpp | 2 - .../core/diagnostic/UnitTestUtilities.h | 77 ----- src/beast/beast/module/core/files/File.cpp | 16 +- src/beast/beast/module/core/maths/Random.cpp | 152 ---------- src/beast/beast/module/core/maths/Random.h | 130 --------- .../beast/module/core/text/LexicalCast.cpp | 9 +- .../module/core/thread/detail/ScopedLock.h | 275 ------------------ src/beast/beast/nudb/tests/callgrind_test.cpp | 2 +- src/beast/beast/nudb/tests/common.h | 2 +- src/beast/beast/nudb/tests/recover_test.cpp | 2 +- src/beast/beast/nudb/tests/store_test.cpp | 2 +- src/beast/beast/{random => }/rngfill.h | 0 src/beast/beast/threads/RecursiveMutex.h | 85 ------ src/beast/beast/threads/Thread.h | 4 +- src/beast/beast/threads/Threads.unity.cpp | 1 - .../beast/threads/impl/RecursiveMutex.cpp | 105 ------- src/beast/beast/threads/impl/Thread.cpp | 6 +- src/beast/beast/utility/Debug.h | 6 - src/beast/beast/utility/impl/Debug.cpp | 12 - .../beast/{random => }/xor_shift_engine.h | 0 src/ripple/app/misc/NetworkOPs.cpp | 2 +- src/ripple/basics/random.h | 2 +- src/ripple/crypto/impl/ECIES.cpp | 2 +- src/ripple/nodestore/tests/Base.test.h | 4 +- src/ripple/nodestore/tests/Timing.test.cpp | 2 +- src/ripple/protocol/impl/RippleAddress.cpp | 2 +- src/ripple/protocol/impl/SecretKey.cpp | 2 +- src/ripple/protocol/tests/digest_test.cpp | 4 +- src/ripple/rpc/handlers/Random.cpp | 2 +- src/ripple/shamap/tests/FetchPack.test.cpp | 2 +- 37 files changed, 56 insertions(+), 921 deletions(-) delete mode 100644 src/beast/beast/module/core/maths/Random.cpp delete mode 100644 src/beast/beast/module/core/maths/Random.h delete mode 100644 src/beast/beast/module/core/thread/detail/ScopedLock.h rename src/beast/beast/{random => }/rngfill.h (100%) delete mode 100644 src/beast/beast/threads/RecursiveMutex.h delete mode 100644 src/beast/beast/threads/impl/RecursiveMutex.cpp rename src/beast/beast/{random => }/xor_shift_engine.h (100%) diff --git a/Builds/VisualStudio2015/RippleD.vcxproj b/Builds/VisualStudio2015/RippleD.vcxproj index c33a87d7b6..a33e3e9c0e 100644 --- a/Builds/VisualStudio2015/RippleD.vcxproj +++ b/Builds/VisualStudio2015/RippleD.vcxproj @@ -653,11 +653,6 @@ - - True - - - True @@ -861,9 +856,7 @@ - - - + @@ -918,9 +911,6 @@ - - True - True @@ -933,8 +923,6 @@ True - - @@ -1049,6 +1037,8 @@ + + diff --git a/Builds/VisualStudio2015/RippleD.vcxproj.filters b/Builds/VisualStudio2015/RippleD.vcxproj.filters index e70a976ba8..bc549509ac 100644 --- a/Builds/VisualStudio2015/RippleD.vcxproj.filters +++ b/Builds/VisualStudio2015/RippleD.vcxproj.filters @@ -103,9 +103,6 @@ {E2E1AA2E-7817-DCA7-66A3-DBC4BAB06D5C} - - {85C3F5F1-0A60-FF1F-2FE4-AD0868832D6B} - {EE59B06D-092E-4C80-21B1-3781040C8FF3} @@ -154,9 +151,6 @@ {ACEF9E32-BEA2-86E3-BDE3-E772564EA55B} - - {94B0990A-9ABE-B1EC-C220-83FD8C2F529F} - {C8013957-E624-4A24-C0F8-CBAAC144AF09} @@ -1206,12 +1200,6 @@ beast\module\core\logging - - beast\module\core\maths - - - beast\module\core\maths - beast\module\core\memory @@ -1470,11 +1458,8 @@ beast\nudb - - beast\random - - - beast\random + + beast beast @@ -1548,9 +1533,6 @@ beast - - beast\threads\impl - beast\threads\impl @@ -1563,9 +1545,6 @@ beast\threads\impl - - beast\threads - beast\threads @@ -1719,6 +1698,9 @@ beast + + beast + ed25519-donna diff --git a/src/beast/beast/crypto/detail/mac_facade.h b/src/beast/beast/crypto/detail/mac_facade.h index c30217431e..8fb80506ae 100644 --- a/src/beast/beast/crypto/detail/mac_facade.h +++ b/src/beast/beast/crypto/detail/mac_facade.h @@ -39,9 +39,11 @@ public: static beast::endian const endian = beast::endian::native; + static std::size_t const digest_size = + Context::digest_size; + using result_type = - std::array; + std::array; mac_facade() noexcept { diff --git a/src/beast/beast/hash/impl/hash_speed_test.cpp b/src/beast/beast/hash/impl/hash_speed_test.cpp index acc3abae03..7ad3e52580 100644 --- a/src/beast/beast/hash/impl/hash_speed_test.cpp +++ b/src/beast/beast/hash/impl/hash_speed_test.cpp @@ -23,8 +23,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include diff --git a/src/beast/beast/hash/tests/hash_append_test.cpp b/src/beast/beast/hash/tests/hash_append_test.cpp index e40ed70c4e..e8b656b2bc 100644 --- a/src/beast/beast/hash/tests/hash_append_test.cpp +++ b/src/beast/beast/hash/tests/hash_append_test.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -284,7 +285,7 @@ private: public: SlowKey() { - static std::mt19937_64 eng; + static beast::xor_shift_engine eng; std::uniform_int_distribution yeardata(1900, 2014); std::uniform_int_distribution monthdata(1, 12); std::uniform_int_distribution daydata(1, 28); @@ -331,8 +332,7 @@ private: public: FastKey() { - static std::conditional_t eng; + static beast::xor_shift_engine eng; for (auto& v : m_values) v = eng(); } diff --git a/src/beast/beast/hash/tests/hash_speed_test.cpp b/src/beast/beast/hash/tests/hash_speed_test.cpp index c48386d877..36c9e3ff5f 100644 --- a/src/beast/beast/hash/tests/hash_speed_test.cpp +++ b/src/beast/beast/hash/tests/hash_speed_test.cpp @@ -24,8 +24,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include diff --git a/src/beast/beast/module/core/core.h b/src/beast/beast/module/core/core.h index 5f9a5d5eeb..6615d4364b 100644 --- a/src/beast/beast/module/core/core.h +++ b/src/beast/beast/module/core/core.h @@ -131,7 +131,6 @@ class FileOutputStream; #include #include #include -#include #include #include #include diff --git a/src/beast/beast/module/core/core.unity.cpp b/src/beast/beast/module/core/core.unity.cpp index 3e4286a0e0..166b47dffd 100644 --- a/src/beast/beast/module/core/core.unity.cpp +++ b/src/beast/beast/module/core/core.unity.cpp @@ -139,8 +139,6 @@ #include #include -#include - #include #include diff --git a/src/beast/beast/module/core/diagnostic/UnitTestUtilities.h b/src/beast/beast/module/core/diagnostic/UnitTestUtilities.h index cb6df79d26..12853d1d2f 100644 --- a/src/beast/beast/module/core/diagnostic/UnitTestUtilities.h +++ b/src/beast/beast/module/core/diagnostic/UnitTestUtilities.h @@ -21,87 +21,10 @@ #define BEAST_MODULE_CORE_DIAGNOSTIC_UNITTESTUTILITIES_H_INCLUDED #include -#include namespace beast { namespace UnitTestUtilities { -/** Fairly shuffle an array pseudo-randomly. -*/ -template -void repeatableShuffle (int const numberOfItems, T& arrayOfItems, Random& r) -{ - for (int i = numberOfItems - 1; i > 0; --i) - { - int const choice = r.nextInt (i + 1); - std::swap (arrayOfItems [i], arrayOfItems [choice]); - } -} - -template -void repeatableShuffle (int const numberOfItems, T& arrayOfItems, std::int64_t seedValue) -{ - Random r (seedValue); - repeatableShuffle (numberOfItems, arrayOfItems, r); -} - -//------------------------------------------------------------------------------ - -/** A block of memory used for test data. -*/ -struct Payload -{ - /** Construct a payload with a buffer of the specified maximum size. - - @param maximumBytes The size of the buffer, in bytes. - */ - explicit Payload (int maxBufferSize) - : bufferSize (maxBufferSize) - , data (maxBufferSize) - { - } - - /** Generate a random block of data within a certain size range. - - @param minimumBytes The smallest number of bytes in the resulting payload. - @param maximumBytes The largest number of bytes in the resulting payload. - @param seedValue The value to seed the random number generator with. - */ - void repeatableRandomFill (int minimumBytes, int maximumBytes, std::int64_t seedValue) noexcept - { - bassert (minimumBytes >=0 && maximumBytes <= bufferSize); - - Random r (seedValue); - - bytes = minimumBytes + r.nextInt (1 + maximumBytes - minimumBytes); - - bassert (bytes >= minimumBytes && bytes <= bufferSize); - - for (int i = 0; i < bytes; ++i) - data [i] = static_cast (r.nextInt ()); - } - - /** Compare two payloads for equality. - */ - bool operator== (Payload const& other) const noexcept - { - if (bytes == other.bytes) - { - return memcmp (data.getData (), other.data.getData (), bytes) == 0; - } - else - { - return false; - } - } - -public: - int const bufferSize; - - int bytes; - HeapBlock data; -}; - class TempDirectory { public: diff --git a/src/beast/beast/module/core/files/File.cpp b/src/beast/beast/module/core/files/File.cpp index 613c44bded..e8635ea9e3 100644 --- a/src/beast/beast/module/core/files/File.cpp +++ b/src/beast/beast/module/core/files/File.cpp @@ -23,6 +23,7 @@ #include #include +#include namespace beast { @@ -512,12 +513,17 @@ bool File::appendText (const String& text, //============================================================================== File File::createTempFile (const String& fileNameEnding) { - const File tempFile (getSpecialLocation (tempDirectory) - .getChildFile ("temp_" + String::toHexString (Random::getSystemRandom().nextInt())) - .withFileExtension (fileNameEnding)); + auto const tempDir = getSpecialLocation (tempDirectory); + std::random_device rng; + File tempFile; - if (tempFile.exists()) - return createTempFile (fileNameEnding); + do + { + tempFile = tempDir.getChildFile ( + "temp_" + std::to_string(rng())) + .withFileExtension (fileNameEnding); + } + while (tempFile.exists()); return tempFile; } diff --git a/src/beast/beast/module/core/maths/Random.cpp b/src/beast/beast/module/core/maths/Random.cpp deleted file mode 100644 index ce0519ad1b..0000000000 --- a/src/beast/beast/module/core/maths/Random.cpp +++ /dev/null @@ -1,152 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of Beast: https://github.com/vinniefalco/Beast - Copyright 2013, Vinnie Falco - - 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. -*/ -//============================================================================== - -#include - -namespace beast { - -Random::Random (const std::int64_t seedValue) noexcept - : seed (seedValue) -{ - nextInt (); // fixes a bug where the first int is always 0 -} - -Random::Random() - : seed (1) -{ - setSeedRandomly(); -} - -Random::~Random() noexcept -{ -} - -void Random::setSeed (const std::int64_t newSeed) noexcept -{ - seed = newSeed; - - nextInt (); // fixes a bug where the first int is always 0 -} - -void Random::combineSeed (const std::int64_t seedValue) noexcept -{ - seed ^= nextInt64() ^ seedValue; -} - -void Random::setSeedRandomly() -{ - static std::int64_t globalSeed = 0; - - combineSeed (globalSeed ^ (std::int64_t) (std::intptr_t) this); - combineSeed (Time::currentTimeMillis()); - globalSeed ^= seed; - - nextInt (); // fixes a bug where the first int is always 0 -} - -Random& Random::getSystemRandom() noexcept -{ - static Random sysRand; - return sysRand; -} - -//============================================================================== -int Random::nextInt() noexcept -{ - seed = (seed * 0x5deece66dLL + 11) & 0xffffffffffffULL; - - return (int) (seed >> 16); -} - -int Random::nextInt (const int maxValue) noexcept -{ - bassert (maxValue > 0); - return (int) ((((unsigned int) nextInt()) * (std::uint64_t) maxValue) >> 32); -} - -std::int64_t Random::nextInt64() noexcept -{ - return (((std::int64_t) nextInt()) << 32) | (std::int64_t) (std::uint64_t) (std::uint32_t) nextInt(); -} - -bool Random::nextBool() noexcept -{ - return (nextInt() & 0x40000000) != 0; -} - -float Random::nextFloat() noexcept -{ - return static_cast (nextInt()) / (float) 0xffffffff; -} - -double Random::nextDouble() noexcept -{ - return static_cast (nextInt()) / (double) 0xffffffff; -} - -void Random::fillBitsRandomly (void* const buffer, size_t bytes) -{ - int* d = static_cast (buffer); - - for (; bytes >= sizeof (int); bytes -= sizeof (int)) - *d++ = nextInt(); - - if (bytes > 0) - { - const int lastBytes = nextInt(); - memcpy (d, &lastBytes, bytes); - } -} - -//============================================================================== - -class Random_test : public unit_test::suite -{ -public: - void run() - { - for (int j = 10; --j >= 0;) - { - Random r; - r.setSeedRandomly(); - - for (int i = 20; --i >= 0;) - { - expect (r.nextDouble() >= 0.0 && r.nextDouble() < 1.0); - expect (r.nextFloat() >= 0.0f && r.nextFloat() < 1.0f); - expect (r.nextInt (5) >= 0 && r.nextInt (5) < 5); - expect (r.nextInt (1) == 0); - - int n = r.nextInt (50) + 1; - expect (r.nextInt (n) >= 0 && r.nextInt (n) < n); - - n = r.nextInt (0x7ffffffe) + 1; - expect (r.nextInt (n) >= 0 && r.nextInt (n) < n); - } - } - } -}; - -BEAST_DEFINE_TESTSUITE(Random,beast_core,beast); - -} // beast diff --git a/src/beast/beast/module/core/maths/Random.h b/src/beast/beast/module/core/maths/Random.h deleted file mode 100644 index 45b311b20b..0000000000 --- a/src/beast/beast/module/core/maths/Random.h +++ /dev/null @@ -1,130 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of Beast: https://github.com/vinniefalco/Beast - Copyright 2013, Vinnie Falco - - 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_MODULE_CORE_MATHS_RANDOM_H_INCLUDED -#define BEAST_MODULE_CORE_MATHS_RANDOM_H_INCLUDED - -#include -#include - -namespace beast { - -//============================================================================== -/** - A random number generator. - - You can create a Random object and use it to generate a sequence of random numbers. -*/ -class Random -{ -public: - //============================================================================== - /** Creates a Random object based on a seed value. - - For a given seed value, the subsequent numbers generated by this object - will be predictable, so a good idea is to set this value based - on the time, e.g. - - new Random (Time::currentTimeMillis()) - */ - explicit Random (std::int64_t seedValue) noexcept; - - /** Creates a Random object using a random seed value. - Internally, this calls setSeedRandomly() to randomise the seed. - */ - Random(); - - /** Destructor. */ - ~Random() noexcept; - - /** Returns the next random 32 bit integer. - - @returns a random integer from the full range 0x80000000 to 0x7fffffff - */ - int nextInt() noexcept; - - /** Returns the next random number, limited to a given range. - The maxValue parameter may not be negative, or zero. - @returns a random integer between 0 (inclusive) and maxValue (exclusive). - */ - int nextInt (int maxValue) noexcept; - - /** Returns the next 64-bit random number. - - @returns a random integer from the full range 0x8000000000000000 to 0x7fffffffffffffff - */ - std::int64_t nextInt64() noexcept; - - /** Returns the next random floating-point number. - - @returns a random value in the range 0 to 1.0 - */ - float nextFloat() noexcept; - - /** Returns the next random floating-point number. - - @returns a random value in the range 0 to 1.0 - */ - double nextDouble() noexcept; - - /** Returns the next random boolean value. - */ - bool nextBool() noexcept; - - /** Fills a block of memory with random values. */ - void fillBitsRandomly (void* bufferToFill, size_t sizeInBytes); - - //============================================================================== - /** Resets this Random object to a given seed value. */ - void setSeed (std::int64_t newSeed) noexcept; - - /** Merges this object's seed with another value. - This sets the seed to be a value created by combining the current seed and this - new value. - */ - void combineSeed (std::int64_t seedValue) noexcept; - - /** Reseeds this generator using a value generated from various semi-random system - properties like the current time, etc. - - Because this function convolves the time with the last seed value, calling - it repeatedly will increase the randomness of the final result. - */ - void setSeedRandomly(); - - /** The overhead of creating a new Random object is fairly small, but if you want to avoid - it, you can call this method to get a global shared Random object. - - It's not thread-safe though, so threads should use their own Random object, otherwise - you run the risk of your random numbers becoming.. erm.. randomly corrupted.. - */ - static Random& getSystemRandom() noexcept; - -private: - //============================================================================== - std::int64_t seed; -}; - -} // beast - -#endif // BEAST_RANDOM_H_INCLUDED diff --git a/src/beast/beast/module/core/text/LexicalCast.cpp b/src/beast/beast/module/core/text/LexicalCast.cpp index bc0a172469..64868dd305 100644 --- a/src/beast/beast/module/core/text/LexicalCast.cpp +++ b/src/beast/beast/module/core/text/LexicalCast.cpp @@ -18,6 +18,7 @@ //============================================================================== #include +#include namespace beast { @@ -25,9 +26,9 @@ class LexicalCast_test : public unit_test::suite { public: template - static IntType nextRandomInt (Random& r) + static IntType nextRandomInt (xor_shift_engine& r) { - return static_cast (r.nextInt64 ()); + return static_cast (r()); } template @@ -42,7 +43,7 @@ public: } template - void testIntegers (Random& r) + void testIntegers (xor_shift_engine& r) { { std::stringstream ss; @@ -254,7 +255,7 @@ public: { std::int64_t const seedValue = 50; - Random r (seedValue); + xor_shift_engine r (seedValue); testIntegers (r); testIntegers (r); diff --git a/src/beast/beast/module/core/thread/detail/ScopedLock.h b/src/beast/beast/module/core/thread/detail/ScopedLock.h deleted file mode 100644 index 43c84accaa..0000000000 --- a/src/beast/beast/module/core/thread/detail/ScopedLock.h +++ /dev/null @@ -1,275 +0,0 @@ -//------------------------------------------------------------------------------ -/* - 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. -*/ -//============================================================================== - -#ifndef BEAST_MODULE_CORE_THREAD_DETAIL_SCOPEDLOCK_H_INCLUDED -#define BEAST_MODULE_CORE_THREAD_DETAIL_SCOPEDLOCK_H_INCLUDED - -#include - -namespace beast -{ - -namespace detail -{ - -template -class TrackedScopedLock -{ -public: - inline explicit TrackedScopedLock (Mutex const& mutex, - char const* fileName, int lineNumber) noexcept - : m_mutex (mutex) - , m_lock_count (0) - { - lock (fileName, lineNumber); - } - - TrackedScopedLock (TrackedScopedLock const&) = delete; - TrackedScopedLock& operator= (TrackedScopedLock const&) = delete; - - inline ~TrackedScopedLock () noexcept - { - if (m_lock_count > 0) - unlock (); - } - - inline void lock (char const* fileName, int lineNumber) noexcept - { - ++m_lock_count; - m_mutex.lock (fileName, lineNumber); - } - - inline void unlock () noexcept - { - m_mutex.unlock (); - --m_lock_count; - } - -private: - Mutex const& m_mutex; - int m_lock_count; -}; - -//-------------------------------------------------------------------------- - -template -class TrackedScopedTryLock -{ -public: - inline explicit TrackedScopedTryLock (Mutex const& mutex, - char const* fileName, int lineNumber) noexcept - : m_mutex (mutex) - , m_lock_count (0) - { - try_lock (fileName, lineNumber); - } - - TrackedScopedTryLock (TrackedScopedTryLock const&) = delete; - TrackedScopedTryLock& operator= (TrackedScopedTryLock const&) = delete; - - inline ~TrackedScopedTryLock () noexcept - { - if (m_lock_count > 0) - unlock (); - } - - inline bool owns_lock () const noexcept - { - return m_lock_count > 0; - } - - inline bool try_lock (char const* fileName, int lineNumber) noexcept - { - bool const success = m_mutex.try_lock (fileName, lineNumber); - if (success) - ++m_lock_count; - return success; - } - - inline void unlock () noexcept - { - m_mutex.unlock (); - --m_lock_count; - } - -private: - Mutex const& m_mutex; - int m_lock_count; -}; - -//-------------------------------------------------------------------------- - -template -class TrackedScopedUnlock -{ -public: - inline explicit TrackedScopedUnlock (Mutex const& mutex, - char const* fileName, int lineNumber) noexcept - : m_mutex (mutex) - , m_fileName (fileName) - , m_lineNumber (lineNumber) - { - m_mutex.unlock (); - } - - TrackedScopedUnlock (TrackedScopedUnlock const&) = delete; - TrackedScopedUnlock& operator= (TrackedScopedUnlock const&) = delete; - - inline ~TrackedScopedUnlock () noexcept - { - m_mutex.lock (m_fileName, m_lineNumber); - } - -private: - Mutex const& m_mutex; - char const* const m_fileName; - int const m_lineNumber; -}; - -//-------------------------------------------------------------------------- - -template -class UntrackedScopedLock -{ -public: - inline explicit UntrackedScopedLock (Mutex const& mutex, - char const*, int) noexcept - : m_mutex (mutex) - , m_lock_count (0) - { - lock (); - } - - UntrackedScopedLock (UntrackedScopedLock const&) = delete; - UntrackedScopedLock& operator= (UntrackedScopedLock const&) = delete; - - inline ~UntrackedScopedLock () noexcept - { - if (m_lock_count > 0) - unlock (); - } - - inline void lock () noexcept - { - ++m_lock_count; - m_mutex.lock (); - } - - inline void lock (char const*, int) noexcept - { - lock (); - } - - inline void unlock () noexcept - { - m_mutex.unlock (); - --m_lock_count; - } - -private: - Mutex const& m_mutex; - int m_lock_count; -}; - -//-------------------------------------------------------------------------- - -template -class UntrackedScopedTryLock -{ -public: - inline explicit UntrackedScopedTryLock (Mutex const& mutex, - char const*, int) noexcept - : m_mutex (mutex) - , m_lock_count (0) - { - try_lock (); - } - - UntrackedScopedTryLock (UntrackedScopedTryLock const&) = delete; - UntrackedScopedTryLock& operator= (UntrackedScopedTryLock const&) = delete; - - inline ~UntrackedScopedTryLock () noexcept - { - if (m_lock_count > 0) - unlock (); - } - - inline bool owns_lock () const noexcept - { - return m_lock_count > 0; - } - - inline bool try_lock () noexcept - { - bool const success = m_mutex.try_lock (); - if (success) - ++m_lock_count; - return success; - } - - inline bool try_lock (char const*, int) noexcept - { - return try_lock (); - } - - inline void unlock () noexcept - { - m_mutex.unlock (); - --m_lock_count; - } - -private: - Mutex const& m_mutex; - int m_lock_count; -}; - -//-------------------------------------------------------------------------- - -template -class UntrackedScopedUnlock -{ -public: - UntrackedScopedUnlock (Mutex const& mutex, - char const*, int) noexcept - : m_mutex (mutex) - , m_owns_lock (true) - { - MutexTraits ::unlock (m_mutex); - m_owns_lock = false; - } - - UntrackedScopedUnlock (UntrackedScopedUnlock const&) = delete; - UntrackedScopedUnlock& operator= (UntrackedScopedUnlock const&) = delete; - - ~UntrackedScopedUnlock () noexcept - { - MutexTraits ::lock (m_mutex); - m_owns_lock = true; - } - -private: - Mutex const& m_mutex; - bool m_owns_lock; -}; - -} // detail - -} // beast - -#endif diff --git a/src/beast/beast/nudb/tests/callgrind_test.cpp b/src/beast/beast/nudb/tests/callgrind_test.cpp index f4a9960aea..63510050e6 100644 --- a/src/beast/beast/nudb/tests/callgrind_test.cpp +++ b/src/beast/beast/nudb/tests/callgrind_test.cpp @@ -20,7 +20,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/beast/beast/nudb/tests/common.h b/src/beast/beast/nudb/tests/common.h index 4f7e456f2c..1930d457b3 100644 --- a/src/beast/beast/nudb/tests/common.h +++ b/src/beast/beast/nudb/tests/common.h @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/beast/beast/nudb/tests/recover_test.cpp b/src/beast/beast/nudb/tests/recover_test.cpp index 0cf9da881c..740d4185e3 100644 --- a/src/beast/beast/nudb/tests/recover_test.cpp +++ b/src/beast/beast/nudb/tests/recover_test.cpp @@ -19,7 +19,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/beast/beast/nudb/tests/store_test.cpp b/src/beast/beast/nudb/tests/store_test.cpp index 32b84759cf..9bdda9f619 100644 --- a/src/beast/beast/nudb/tests/store_test.cpp +++ b/src/beast/beast/nudb/tests/store_test.cpp @@ -21,7 +21,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/beast/beast/random/rngfill.h b/src/beast/beast/rngfill.h similarity index 100% rename from src/beast/beast/random/rngfill.h rename to src/beast/beast/rngfill.h diff --git a/src/beast/beast/threads/RecursiveMutex.h b/src/beast/beast/threads/RecursiveMutex.h deleted file mode 100644 index 57edf8cecd..0000000000 --- a/src/beast/beast/threads/RecursiveMutex.h +++ /dev/null @@ -1,85 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of Beast: https://github.com/vinniefalco/Beast - Copyright 2013, Vinnie Falco - - 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_THREADS_RECURSIVEMUTEX_H_INCLUDED -#define BEAST_THREADS_RECURSIVEMUTEX_H_INCLUDED - -#include -#include -#include - -#include - -#if ! BEAST_WINDOWS -#include -#endif - -namespace beast { - -class RecursiveMutex -{ -public: - using ScopedLockType = std::lock_guard ; - using ScopedUnlockType = UnlockGuard ; - using ScopedTryLockType = TryLockGuard ; - - /** Create the mutex. - The mutux is initially unowned. - */ - RecursiveMutex (); - - /** Destroy the mutex. - If the lock is owned, the result is undefined. - */ - ~RecursiveMutex (); - - // Boost concept compatibility: - // http://www.boost.org/doc/libs/1_54_0/doc/html/thread/synchronization.html#thread.synchronization.mutex_concepts - - /** BasicLockable */ - /** @{ */ - void lock () const; - void unlock () const; - /** @} */ - - /** Lockable */ - bool try_lock () const; - -private: -// To avoid including windows.h in the public Beast headers, we'll just -// reserve storage here that's big enough to be used internally as a windows -// CRITICAL_SECTION structure. -#if BEAST_WINDOWS -# if BEAST_64BIT - char section[44]; -# else - char section[24]; -# endif -#else - mutable pthread_mutex_t mutex; -#endif -}; - -} - -#endif diff --git a/src/beast/beast/threads/Thread.h b/src/beast/beast/threads/Thread.h index ddf1850f72..897b6f235f 100644 --- a/src/beast/beast/threads/Thread.h +++ b/src/beast/beast/threads/Thread.h @@ -24,9 +24,9 @@ #ifndef BEAST_THREADS_THREAD_H_INCLUDED #define BEAST_THREADS_THREAD_H_INCLUDED -#include #include +#include #include namespace beast { @@ -168,7 +168,7 @@ private: //============================================================================== std::string const threadName; void* volatile threadHandle; - RecursiveMutex startStopLock; + std::recursive_mutex startStopLock; WaitableEvent startSuspensionEvent, defaultEvent; bool volatile shouldExit; diff --git a/src/beast/beast/threads/Threads.unity.cpp b/src/beast/beast/threads/Threads.unity.cpp index ddb7343798..dd0de1b53d 100644 --- a/src/beast/beast/threads/Threads.unity.cpp +++ b/src/beast/beast/threads/Threads.unity.cpp @@ -21,7 +21,6 @@ #include #endif -#include #include #include #include diff --git a/src/beast/beast/threads/impl/RecursiveMutex.cpp b/src/beast/beast/threads/impl/RecursiveMutex.cpp deleted file mode 100644 index b5005184b1..0000000000 --- a/src/beast/beast/threads/impl/RecursiveMutex.cpp +++ /dev/null @@ -1,105 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of Beast: https://github.com/vinniefalco/Beast - Copyright 2013, Vinnie Falco - - 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. -*/ -//============================================================================== - -#include - -#if BEAST_WINDOWS - -#include -#undef check -#undef direct -#undef max -#undef min -#undef TYPE_BOOL - -namespace beast { - -RecursiveMutex::RecursiveMutex() -{ - // (just to check the MS haven't changed this structure and broken things...) - static_assert (sizeof (CRITICAL_SECTION) <= sizeof (section), ""); - - InitializeCriticalSection ((CRITICAL_SECTION*) section); -} - -RecursiveMutex::~RecursiveMutex() -{ - DeleteCriticalSection ((CRITICAL_SECTION*) section); -} - -void RecursiveMutex::lock() const -{ - EnterCriticalSection ((CRITICAL_SECTION*) section); -} - -void RecursiveMutex::unlock() const -{ - LeaveCriticalSection ((CRITICAL_SECTION*) section); -} - -bool RecursiveMutex::try_lock() const -{ - return TryEnterCriticalSection ((CRITICAL_SECTION*) section) != FALSE; -} - -} - -#else - -namespace beast { - -RecursiveMutex::RecursiveMutex() -{ - pthread_mutexattr_t atts; - pthread_mutexattr_init (&atts); - pthread_mutexattr_settype (&atts, PTHREAD_MUTEX_RECURSIVE); -#if ! BEAST_ANDROID - pthread_mutexattr_setprotocol (&atts, PTHREAD_PRIO_INHERIT); -#endif - pthread_mutex_init (&mutex, &atts); - pthread_mutexattr_destroy (&atts); -} - -RecursiveMutex::~RecursiveMutex() -{ - pthread_mutex_destroy (&mutex); -} - -void RecursiveMutex::lock() const -{ - pthread_mutex_lock (&mutex); -} - -void RecursiveMutex::unlock() const -{ - pthread_mutex_unlock (&mutex); -} - -bool RecursiveMutex::try_lock() const -{ - return pthread_mutex_trylock (&mutex) == 0; -} - -} - -#endif diff --git a/src/beast/beast/threads/impl/Thread.cpp b/src/beast/beast/threads/impl/Thread.cpp index 83696e3215..1bac318ce1 100644 --- a/src/beast/beast/threads/impl/Thread.cpp +++ b/src/beast/beast/threads/impl/Thread.cpp @@ -73,7 +73,7 @@ void beast_threadEntryPoint (void* userData) //============================================================================== void Thread::startThread() { - const RecursiveMutex::ScopedLockType sl (startStopLock); + std::lock_guard sl (startStopLock); shouldExit = false; @@ -103,7 +103,7 @@ void Thread::waitForThreadToExit () const void Thread::stopThread () { - const RecursiveMutex::ScopedLockType sl (startStopLock); + std::lock_guard sl (startStopLock); if (isThreadRunning()) { @@ -115,7 +115,7 @@ void Thread::stopThread () void Thread::stopThreadAsync () { - const RecursiveMutex::ScopedLockType sl (startStopLock); + std::lock_guard sl (startStopLock); if (isThreadRunning()) { diff --git a/src/beast/beast/utility/Debug.h b/src/beast/beast/utility/Debug.h index 7a9e1896bc..29f1c47c83 100644 --- a/src/beast/beast/utility/Debug.h +++ b/src/beast/beast/utility/Debug.h @@ -29,12 +29,6 @@ namespace beast { namespace Debug { -/** Break to debugger if a debugger is attached to a debug build. - - Does nothing if no debugger is attached, or the build is not a debug build. -*/ -extern void breakPoint (); - /** Given a file and line number this formats a suitable string. Usually you will pass __FILE__ and __LINE__ here. */ diff --git a/src/beast/beast/utility/impl/Debug.cpp b/src/beast/beast/utility/impl/Debug.cpp index 30b17840cf..e269188780 100644 --- a/src/beast/beast/utility/impl/Debug.cpp +++ b/src/beast/beast/utility/impl/Debug.cpp @@ -25,18 +25,6 @@ namespace beast { namespace Debug { -void breakPoint () -{ -#if BEAST_DEBUG - if (beast_isRunningUnderDebugger ()) - beast_breakDebugger; - -#else - bassertfalse; - -#endif -} - //------------------------------------------------------------------------------ #if BEAST_MSVC && defined (_DEBUG) diff --git a/src/beast/beast/random/xor_shift_engine.h b/src/beast/beast/xor_shift_engine.h similarity index 100% rename from src/beast/beast/random/xor_shift_engine.h rename to src/beast/beast/xor_shift_engine.h diff --git a/src/ripple/app/misc/NetworkOPs.cpp b/src/ripple/app/misc/NetworkOPs.cpp index 5752aaa567..3c3a6f62fe 100644 --- a/src/ripple/app/misc/NetworkOPs.cpp +++ b/src/ripple/app/misc/NetworkOPs.cpp @@ -71,7 +71,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/ripple/basics/random.h b/src/ripple/basics/random.h index 3734afe682..ce76783188 100644 --- a/src/ripple/basics/random.h +++ b/src/ripple/basics/random.h @@ -20,7 +20,7 @@ #ifndef RIPPLE_BASICS_RANDOM_H_INCLUDED #define RIPPLE_BASICS_RANDOM_H_INCLUDED -#include +#include #include #include #include diff --git a/src/ripple/crypto/impl/ECIES.cpp b/src/ripple/crypto/impl/ECIES.cpp index 8e1588a33f..4681b687b7 100644 --- a/src/ripple/crypto/impl/ECIES.cpp +++ b/src/ripple/crypto/impl/ECIES.cpp @@ -23,7 +23,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/ripple/nodestore/tests/Base.test.h b/src/ripple/nodestore/tests/Base.test.h index 52918b63d4..53df31ad40 100644 --- a/src/ripple/nodestore/tests/Base.test.h +++ b/src/ripple/nodestore/tests/Base.test.h @@ -24,8 +24,8 @@ #include #include #include -#include -#include +#include +#include #include #include diff --git a/src/ripple/nodestore/tests/Timing.test.cpp b/src/ripple/nodestore/tests/Timing.test.cpp index 163cb1f867..0b3497ac39 100644 --- a/src/ripple/nodestore/tests/Timing.test.cpp +++ b/src/ripple/nodestore/tests/Timing.test.cpp @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/ripple/protocol/impl/RippleAddress.cpp b/src/ripple/protocol/impl/RippleAddress.cpp index 799ff7de91..bf2d24da5e 100644 --- a/src/ripple/protocol/impl/RippleAddress.cpp +++ b/src/ripple/protocol/impl/RippleAddress.cpp @@ -33,7 +33,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/ripple/protocol/impl/SecretKey.cpp b/src/ripple/protocol/impl/SecretKey.cpp index 2c6cab9166..8421d2ed82 100644 --- a/src/ripple/protocol/impl/SecretKey.cpp +++ b/src/ripple/protocol/impl/SecretKey.cpp @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/ripple/protocol/tests/digest_test.cpp b/src/ripple/protocol/tests/digest_test.cpp index 7cc8d9709b..3ff42ba6cb 100644 --- a/src/ripple/protocol/tests/digest_test.cpp +++ b/src/ripple/protocol/tests/digest_test.cpp @@ -20,8 +20,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include diff --git a/src/ripple/rpc/handlers/Random.cpp b/src/ripple/rpc/handlers/Random.cpp index 4845848352..beb7502a6c 100644 --- a/src/ripple/rpc/handlers/Random.cpp +++ b/src/ripple/rpc/handlers/Random.cpp @@ -24,7 +24,7 @@ #include #include #include -#include +#include namespace ripple { diff --git a/src/ripple/shamap/tests/FetchPack.test.cpp b/src/ripple/shamap/tests/FetchPack.test.cpp index 73aa9663f3..b56f8df6ca 100644 --- a/src/ripple/shamap/tests/FetchPack.test.cpp +++ b/src/ripple/shamap/tests/FetchPack.test.cpp @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include #include #include