Simplify Beast:

* Remove obsolete RNG facilities
* Flatten directory structure
* Use std::recursive_mutex instead of beast::RecursiveMutex
This commit is contained in:
Nik Bougalis
2016-01-09 02:43:30 -08:00
parent 3c6534dc91
commit 8064e82774
25 changed files with 36 additions and 873 deletions

View File

@@ -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::uint8_t,
Context::digest_size>;
std::array<std::uint8_t, digest_size>;
mac_facade() noexcept
{

View File

@@ -23,8 +23,8 @@
#include <beast/container/fnv1a.h>
#include <beast/container/siphash.h>
#include <beast/container/xxhasher.h>
#include <beast/random/rngfill.h>
#include <beast/random/xor_shift_engine.h>
#include <beast/rngfill.h>
#include <beast/xor_shift_engine.h>
#include <beast/unit_test/suite.h>
#include <array>
#include <chrono>

View File

@@ -25,6 +25,7 @@
#include <beast/hash/tests/hash_metrics.h>
#include <beast/hash/hash_append.h>
#include <beast/chrono/chrono_io.h>
#include <beast/xor_shift_engine.h>
#include <beast/unit_test/suite.h>
#include <beast/utility/type_name.h>
#include <array>
@@ -284,7 +285,7 @@ private:
public:
SlowKey()
{
static std::mt19937_64 eng;
static beast::xor_shift_engine eng;
std::uniform_int_distribution<short> yeardata(1900, 2014);
std::uniform_int_distribution<unsigned> monthdata(1, 12);
std::uniform_int_distribution<unsigned> daydata(1, 28);
@@ -331,8 +332,7 @@ private:
public:
FastKey()
{
static std::conditional_t <sizeof(std::size_t)==sizeof(std::uint64_t),
std::mt19937_64, std::mt19937> eng;
static beast::xor_shift_engine eng;
for (auto& v : m_values)
v = eng();
}

View File

@@ -24,8 +24,8 @@
#include <beast/hash/siphash.h>
#include <beast/hash/xxhasher.h>
#include <beast/chrono/chrono_io.h>
#include <beast/random/rngfill.h>
#include <beast/random/xor_shift_engine.h>
#include <beast/rngfill.h>
#include <beast/xor_shift_engine.h>
#include <beast/unit_test/suite.h>
#include <array>
#include <chrono>

View File

@@ -131,7 +131,6 @@ class FileOutputStream;
#include <beast/module/core/diagnostic/FatalError.h>
#include <beast/module/core/text/LexicalCast.h>
#include <beast/module/core/logging/Logger.h>
#include <beast/module/core/maths/Random.h>
#include <beast/module/core/text/StringPairArray.h>
#include <beast/module/core/files/DirectoryIterator.h>
#include <beast/module/core/streams/InputStream.h>

View File

@@ -139,8 +139,6 @@
#include <beast/module/core/files/FileInputStream.cpp>
#include <beast/module/core/files/FileOutputStream.cpp>
#include <beast/module/core/maths/Random.cpp>
#include <beast/module/core/memory/MemoryBlock.cpp>
#include <beast/module/core/misc/Result.cpp>

View File

@@ -21,87 +21,10 @@
#define BEAST_MODULE_CORE_DIAGNOSTIC_UNITTESTUTILITIES_H_INCLUDED
#include <beast/module/core/files/File.h>
#include <beast/module/core/maths/Random.h>
namespace beast {
namespace UnitTestUtilities {
/** Fairly shuffle an array pseudo-randomly.
*/
template <class T>
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 <class T>
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 <unsigned char> (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 <char> data;
};
class TempDirectory
{
public:

View File

@@ -23,6 +23,7 @@
#include <algorithm>
#include <memory>
#include <random>
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;
}

View File

@@ -1,152 +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.
*/
//==============================================================================
#include <beast/unit_test/suite.h>
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 <std::uint32_t> (nextInt()) / (float) 0xffffffff;
}
double Random::nextDouble() noexcept
{
return static_cast <std::uint32_t> (nextInt()) / (double) 0xffffffff;
}
void Random::fillBitsRandomly (void* const buffer, size_t bytes)
{
int* d = static_cast<int*> (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

View File

@@ -1,130 +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_MODULE_CORE_MATHS_RANDOM_H_INCLUDED
#define BEAST_MODULE_CORE_MATHS_RANDOM_H_INCLUDED
#include <cstddef>
#include <cstdint>
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

View File

@@ -18,6 +18,7 @@
//==============================================================================
#include <beast/unit_test/suite.h>
#include <beast/xor_shift_engine.h>
namespace beast {
@@ -25,9 +26,9 @@ class LexicalCast_test : public unit_test::suite
{
public:
template <class IntType>
static IntType nextRandomInt (Random& r)
static IntType nextRandomInt (xor_shift_engine& r)
{
return static_cast <IntType> (r.nextInt64 ());
return static_cast <IntType> (r());
}
template <class IntType>
@@ -42,7 +43,7 @@ public:
}
template <class IntType>
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 <int> (r);
testIntegers <unsigned int> (r);

View File

@@ -1,275 +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_MODULE_CORE_THREAD_DETAIL_SCOPEDLOCK_H_INCLUDED
#define BEAST_MODULE_CORE_THREAD_DETAIL_SCOPEDLOCK_H_INCLUDED
#include <beast/module/beast_core/thread/MutexTraits.h>
namespace beast
{
namespace detail
{
template <typename Mutex>
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 <typename Mutex>
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 <typename Mutex>
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 <typename Mutex>
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 <typename Mutex>
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 <typename Mutex>
class UntrackedScopedUnlock
{
public:
UntrackedScopedUnlock (Mutex const& mutex,
char const*, int) noexcept
: m_mutex (mutex)
, m_owns_lock (true)
{
MutexTraits <Mutex>::unlock (m_mutex);
m_owns_lock = false;
}
UntrackedScopedUnlock (UntrackedScopedUnlock const&) = delete;
UntrackedScopedUnlock& operator= (UntrackedScopedUnlock const&) = delete;
~UntrackedScopedUnlock () noexcept
{
MutexTraits <Mutex>::lock (m_mutex);
m_owns_lock = true;
}
private:
Mutex const& m_mutex;
bool m_owns_lock;
};
} // detail
} // beast
#endif

View File

@@ -20,7 +20,7 @@
#include <beast/nudb/tests/common.h>
#include <beast/module/core/diagnostic/UnitTestUtilities.h>
#include <beast/module/core/files/File.h>
#include <beast/random/xor_shift_engine.h>
#include <beast/xor_shift_engine.h>
#include <beast/unit_test/suite.h>
#include <cmath>
#include <cstring>

View File

@@ -24,7 +24,7 @@
#include <beast/nudb/identity.h>
#include <beast/nudb/tests/fail_file.h>
#include <beast/hash/xxhasher.h>
#include <beast/random/xor_shift_engine.h>
#include <beast/xor_shift_engine.h>
#include <cstdint>
#include <iomanip>
#include <memory>

View File

@@ -19,7 +19,7 @@
#include <beast/nudb/tests/common.h>
#include <beast/module/core/files/File.h>
#include <beast/random/xor_shift_engine.h>
#include <beast/xor_shift_engine.h>
#include <beast/unit_test/suite.h>
#include <cmath>
#include <cstring>

View File

@@ -21,7 +21,7 @@
#include <beast/nudb/tests/common.h>
#include <beast/module/core/diagnostic/UnitTestUtilities.h>
#include <beast/module/core/files/File.h>
#include <beast/random/xor_shift_engine.h>
#include <beast/xor_shift_engine.h>
#include <beast/unit_test/suite.h>
#include <cmath>
#include <iomanip>

View File

@@ -1,85 +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_THREADS_RECURSIVEMUTEX_H_INCLUDED
#define BEAST_THREADS_RECURSIVEMUTEX_H_INCLUDED
#include <beast/Config.h>
#include <beast/threads/UnlockGuard.h>
#include <beast/threads/TryLockGuard.h>
#include <mutex>
#if ! BEAST_WINDOWS
#include <pthread.h>
#endif
namespace beast {
class RecursiveMutex
{
public:
using ScopedLockType = std::lock_guard <RecursiveMutex>;
using ScopedUnlockType = UnlockGuard <RecursiveMutex>;
using ScopedTryLockType = TryLockGuard <RecursiveMutex>;
/** 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

View File

@@ -24,9 +24,9 @@
#ifndef BEAST_THREADS_THREAD_H_INCLUDED
#define BEAST_THREADS_THREAD_H_INCLUDED
#include <beast/threads/RecursiveMutex.h>
#include <beast/threads/WaitableEvent.h>
#include <mutex>
#include <string>
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;

View File

@@ -21,7 +21,6 @@
#include <BeastConfig.h>
#endif
#include <beast/threads/impl/RecursiveMutex.cpp>
#include <beast/threads/impl/Stoppable.cpp>
#include <beast/threads/impl/Stoppable.test.cpp>
#include <beast/threads/impl/Thread.cpp>

View File

@@ -1,105 +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.
*/
//==============================================================================
#include <beast/threads/RecursiveMutex.h>
#if BEAST_WINDOWS
#include <Windows.h>
#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

View File

@@ -73,7 +73,7 @@ void beast_threadEntryPoint (void* userData)
//==============================================================================
void Thread::startThread()
{
const RecursiveMutex::ScopedLockType sl (startStopLock);
std::lock_guard<std::recursive_mutex> sl (startStopLock);
shouldExit = false;
@@ -103,7 +103,7 @@ void Thread::waitForThreadToExit () const
void Thread::stopThread ()
{
const RecursiveMutex::ScopedLockType sl (startStopLock);
std::lock_guard<std::recursive_mutex> sl (startStopLock);
if (isThreadRunning())
{
@@ -115,7 +115,7 @@ void Thread::stopThread ()
void Thread::stopThreadAsync ()
{
const RecursiveMutex::ScopedLockType sl (startStopLock);
std::lock_guard<std::recursive_mutex> sl (startStopLock);
if (isThreadRunning())
{

View File

@@ -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.
*/

View File

@@ -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)