diff --git a/modules/ripple_basics/containers/ripple_RangeSet.h b/modules/ripple_basics/containers/ripple_RangeSet.h index 30e4b7382e..53708ff05c 100644 --- a/modules/ripple_basics/containers/ripple_RangeSet.h +++ b/modules/ripple_basics/containers/ripple_RangeSet.h @@ -68,6 +68,39 @@ namespace boost }; } +// VFALCO: Maybe not the best place for this but it sort of makes sense here + +// VFALCO: NOTE, these three are unused. +/* +template T range_check(const T& value, const T& minimum, const T& maximum) +{ + if ((value < minimum) || (value > maximum)) + throw std::runtime_error("Value out of range"); + return value; +} + +template T range_check_min(const T& value, const T& minimum) +{ + if (value < minimum) + throw std::runtime_error("Value out of range"); + return value; +} + +template T range_check_max(const T& value, const T& maximum) +{ + if (value > maximum) + throw std::runtime_error("Value out of range"); + return value; +} +*/ + +template T range_check_cast(const U& value, const T& minimum, const T& maximum) +{ + if ((value < minimum) || (value > maximum)) + throw std::runtime_error("Value out of range"); + return static_cast(value); +} + #endif // vim:ts=4 diff --git a/modules/ripple_basics/ripple_basics.cpp b/modules/ripple_basics/ripple_basics.cpp index 182f231b3d..21b6a20c47 100644 --- a/modules/ripple_basics/ripple_basics.cpp +++ b/modules/ripple_basics/ripple_basics.cpp @@ -66,6 +66,8 @@ #include "memory/ripple_ByteOrder.cpp" #include "system/ripple_RandomNumbers.cpp" +#include "system/ripple_ThreadName.cpp" +#include "system/ripple_Time.cpp" #ifdef _MSC_VER //#pragma warning (pop) diff --git a/modules/ripple_basics/ripple_basics.h b/modules/ripple_basics/ripple_basics.h index 949e09cb15..58d4835d40 100644 --- a/modules/ripple_basics/ripple_basics.h +++ b/modules/ripple_basics/ripple_basics.h @@ -38,6 +38,11 @@ #include #include +#include +#if BOOST_VERSION < 104700 +#error Boost 1.47 or later is required +#endif + // Log #include // Forward declaration @@ -68,6 +73,9 @@ namespace boost { #include #include +// RippleTime +#include + // ByteOrder #ifdef WIN32 // (nothing) @@ -103,5 +111,7 @@ namespace boost { #include "system/ripple_PlatformMacros.h" #include "system/ripple_RandomNumbers.h" +#include "system/ripple_ThreadName.h" +#include "system/ripple_Time.h" #endif diff --git a/modules/ripple_basics/system/ripple_ThreadName.cpp b/modules/ripple_basics/system/ripple_ThreadName.cpp new file mode 100644 index 0000000000..cad7ae1c24 --- /dev/null +++ b/modules/ripple_basics/system/ripple_ThreadName.cpp @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +/* + Copyright (c) 2011-2013, OpenCoin, Inc. + + 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. +*/ +//============================================================================== + +// VFALCO: TODO, use VFLIB_MSVC macro instead +#if _MSC_VER + +void setCallingThreadName (char const* threadName) +{ + struct ThreadInfo + { + DWORD dwType; + LPCSTR szName; + DWORD dwThreadID; + DWORD dwFlags; + }; + + ThreadInfo info; + + info.dwType = 0x1000; + info.szName = threadName; + info.dwThreadID = GetCurrentThreadId (); + info.dwFlags = 0; + + __try + { + // This is a VisualStudio specific exception + RaiseException (0x406d1388, 0, sizeof (info) / sizeof (ULONG_PTR), (ULONG_PTR*) &info); + } + __except (EXCEPTION_CONTINUE_EXECUTION) + { + } +} + +#else + #ifdef PR_SET_NAME + #define HAVE_NAME_THREAD + extern void setCallingThreadName (const char* n) + { + static std::string pName; + + if (pName.empty()) + { + std::ifstream cLine("/proc/self/cmdline", std::ios::in); + cLine >> pName; + if (pName.empty()) + pName = "rippled"; + else + { + size_t zero = pName.find_first_of('\0'); + if ((zero != std::string::npos) && (zero != 0)) + pName = pName.substr(0, zero); + size_t slash = pName.find_last_of('/'); + if (slash != std::string::npos) + pName = pName.substr(slash + 1); + } + pName += " "; + } + + prctl(PR_SET_NAME, (pName + n).c_str(), 0, 0, 0); + } + #endif + + #ifndef HAVE_NAME_THREAD + extern void setCallingThreadName(const char*) + { ; } + #endif +#endif diff --git a/modules/ripple_basics/system/ripple_ThreadName.h b/modules/ripple_basics/system/ripple_ThreadName.h new file mode 100644 index 0000000000..67f4558410 --- /dev/null +++ b/modules/ripple_basics/system/ripple_ThreadName.h @@ -0,0 +1,24 @@ +//------------------------------------------------------------------------------ +/* + Copyright (c) 2011-2013, OpenCoin, Inc. + + 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 RIPPLE_THREADNAME_H +#define RIPPLE_THREADNAME_H + +extern void setCallingThreadName (char const*); + +#endif diff --git a/modules/ripple_basics/system/ripple_Time.cpp b/modules/ripple_basics/system/ripple_Time.cpp new file mode 100644 index 0000000000..3e3e9a9585 --- /dev/null +++ b/modules/ripple_basics/system/ripple_Time.cpp @@ -0,0 +1,54 @@ +//------------------------------------------------------------------------------ +/* + Copyright (c) 2011-2013, OpenCoin, Inc. + + 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. +*/ +//============================================================================== + +// +// Time support +// We have our own epoch. +// + +boost::posix_time::ptime ptEpoch() +{ + return boost::posix_time::ptime(boost::gregorian::date(2000, boost::gregorian::Jan, 1)); +} + +int iToSeconds(boost::posix_time::ptime ptWhen) +{ + return ptWhen.is_not_a_date_time() + ? -1 + : (ptWhen-ptEpoch()).total_seconds(); +} + +// Convert our time in seconds to a ptime. +boost::posix_time::ptime ptFromSeconds(int iSeconds) +{ + return iSeconds < 0 + ? boost::posix_time::ptime(boost::posix_time::not_a_date_time) + : ptEpoch() + boost::posix_time::seconds(iSeconds); +} + +// Convert from our time to UNIX time in seconds. +uint64_t utFromSeconds(int iSeconds) +{ + boost::posix_time::time_duration tdDelta = + boost::posix_time::ptime(boost::gregorian::date(2000, boost::gregorian::Jan, 1)) + -boost::posix_time::ptime(boost::gregorian::date(1970, boost::gregorian::Jan, 1)) + +boost::posix_time::seconds(iSeconds) + ; + + return tdDelta.total_seconds(); +} diff --git a/modules/ripple_basics/system/ripple_Time.h b/modules/ripple_basics/system/ripple_Time.h new file mode 100644 index 0000000000..375f11b192 --- /dev/null +++ b/modules/ripple_basics/system/ripple_Time.h @@ -0,0 +1,27 @@ +//------------------------------------------------------------------------------ +/* + Copyright (c) 2011-2013, OpenCoin, Inc. + + 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 RIPPLE_TIME_H +#define RIPPLE_TIME_H + +boost::posix_time::ptime ptEpoch(); +int iToSeconds(boost::posix_time::ptime ptWhen); +boost::posix_time::ptime ptFromSeconds(int iSeconds); +uint64_t utFromSeconds(int iSeconds); + +#endif diff --git a/newcoin.vcxproj b/newcoin.vcxproj index 589fdca6ab..6f972f8903 100644 --- a/newcoin.vcxproj +++ b/newcoin.vcxproj @@ -216,6 +216,18 @@ true true + + true + true + true + true + + + true + true + true + true + @@ -1198,6 +1210,8 @@ + + diff --git a/newcoin.vcxproj.filters b/newcoin.vcxproj.filters index 942b084fde..db8a5ba6b8 100644 --- a/newcoin.vcxproj.filters +++ b/newcoin.vcxproj.filters @@ -765,6 +765,12 @@ 1. Modules\ripple_basics\system + + 1. Modules\ripple_basics\system + + + 1. Modules\ripple_basics\system + @@ -1424,6 +1430,12 @@ 1. Modules\ripple_basics\system + + 1. Modules\ripple_basics\system + + + 1. Modules\ripple_basics\system + diff --git a/src/cpp/ripple/Application.cpp b/src/cpp/ripple/Application.cpp index ea28fccfde..2f526499e9 100644 --- a/src/cpp/ripple/Application.cpp +++ b/src/cpp/ripple/Application.cpp @@ -112,13 +112,13 @@ void sigIntHandler(int) static void runAux(boost::asio::io_service& svc) { - NameThread("aux"); + setCallingThreadName("aux"); svc.run(); } static void runIO(boost::asio::io_service& io) { - NameThread("io"); + setCallingThreadName("io"); io.run(); } diff --git a/src/cpp/ripple/JobQueue.cpp b/src/cpp/ripple/JobQueue.cpp index 66a47460f3..2d9ba8e555 100644 --- a/src/cpp/ripple/JobQueue.cpp +++ b/src/cpp/ripple/JobQueue.cpp @@ -273,7 +273,7 @@ void JobQueue::IOThread(boost::mutex::scoped_lock& sl) { // call with a lock ++mIOThreadCount; sl.unlock(); - NameThread("IO+"); + setCallingThreadName("IO+"); try { mIOService.poll(); @@ -282,7 +282,7 @@ void JobQueue::IOThread(boost::mutex::scoped_lock& sl) { WriteLog (lsWARNING, JobQueue) << "Exception in IOThread"; } - NameThread("waiting"); + setCallingThreadName("waiting"); sl.lock(); --mIOThreadCount; } @@ -292,7 +292,7 @@ void JobQueue::threadEntry() boost::mutex::scoped_lock sl(mJobLock); while (1) { - NameThread("waiting"); + setCallingThreadName("waiting"); // bool didIO = false; while (mJobSet.empty() && !mShuttingDown) { @@ -325,7 +325,7 @@ void JobQueue::threadEntry() ++(mJobCounts[type].second); sl.unlock(); - NameThread(Job::toString(type)); + setCallingThreadName(Job::toString(type)); WriteLog (lsTRACE, JobQueue) << "Doing " << Job::toString(type) << " job"; job.doJob(); } // must destroy job without holding lock diff --git a/src/cpp/ripple/LoadManager.cpp b/src/cpp/ripple/LoadManager.cpp index 3fc29b701f..3df5ddf63d 100644 --- a/src/cpp/ripple/LoadManager.cpp +++ b/src/cpp/ripple/LoadManager.cpp @@ -324,7 +324,7 @@ static void LogDeadLock(int dlTime) void LoadManager::threadEntry() { - NameThread("loadmgr"); + setCallingThreadName("loadmgr"); boost::posix_time::ptime t = boost::posix_time::microsec_clock::universal_time(); while (1) { diff --git a/src/cpp/ripple/WSDoor.cpp b/src/cpp/ripple/WSDoor.cpp index 9ae6e87d1f..1e6c223790 100644 --- a/src/cpp/ripple/WSDoor.cpp +++ b/src/cpp/ripple/WSDoor.cpp @@ -41,7 +41,7 @@ DECLARE_INSTANCE(WebSocketConnection); void WSDoor::startListening() { - NameThread("websocket"); + setCallingThreadName("websocket"); // Generate a single SSL context for use by all connections. boost::shared_ptr mCtx; mCtx = boost::make_shared(boost::asio::ssl::context::sslv23); diff --git a/src/cpp/ripple/main.cpp b/src/cpp/ripple/main.cpp index 8a14ab29ee..476d4bada2 100644 --- a/src/cpp/ripple/main.cpp +++ b/src/cpp/ripple/main.cpp @@ -128,7 +128,7 @@ void printHelp(const po::options_description& desc) int main(int argc, char* argv[]) { - NameThread("main"); + setCallingThreadName("main"); int iResult = 0; po::variables_map vm; // Map of options. @@ -269,14 +269,14 @@ int main(int argc, char* argv[]) { // No arguments. Run server. setupServer(); - NameThread("io"); + setCallingThreadName("io"); startServer(); InstanceType::shutdown(); } else { // Have a RPC command. - NameThread("rpc"); + setCallingThreadName("rpc"); std::vector vCmd = vm["parameters"].as >(); iResult = commandLineRPC(vCmd); diff --git a/src/cpp/ripple/utils.cpp b/src/cpp/ripple/utils.cpp index 71abdb662d..46d154e806 100644 --- a/src/cpp/ripple/utils.cpp +++ b/src/cpp/ripple/utils.cpp @@ -28,43 +28,6 @@ #include "utils.h" #include "uint256.h" -// -// Time support -// We have our own epoch. -// - -boost::posix_time::ptime ptEpoch() -{ - return boost::posix_time::ptime(boost::gregorian::date(2000, boost::gregorian::Jan, 1)); -} - -int iToSeconds(boost::posix_time::ptime ptWhen) -{ - return ptWhen.is_not_a_date_time() - ? -1 - : (ptWhen-ptEpoch()).total_seconds(); -} - -// Convert our time in seconds to a ptime. -boost::posix_time::ptime ptFromSeconds(int iSeconds) -{ - return iSeconds < 0 - ? boost::posix_time::ptime(boost::posix_time::not_a_date_time) - : ptEpoch() + boost::posix_time::seconds(iSeconds); -} - -// Convert from our time to UNIX time in seconds. -uint64_t utFromSeconds(int iSeconds) -{ - boost::posix_time::time_duration tdDelta = - boost::posix_time::ptime(boost::gregorian::date(2000, boost::gregorian::Jan, 1)) - -boost::posix_time::ptime(boost::gregorian::date(1970, boost::gregorian::Jan, 1)) - +boost::posix_time::seconds(iSeconds) - ; - - return tdDelta.total_seconds(); -} - // // DH support // @@ -97,39 +60,6 @@ DH* DH_der_load(const std::string& strDer) return d2i_DHparams(NULL, &pbuf, strDer.size()); } -#ifdef PR_SET_NAME -#define HAVE_NAME_THREAD -extern void NameThread(const char* n) -{ - static std::string pName; - - if (pName.empty()) - { - std::ifstream cLine("/proc/self/cmdline", std::ios::in); - cLine >> pName; - if (pName.empty()) - pName = "rippled"; - else - { - size_t zero = pName.find_first_of('\0'); - if ((zero != std::string::npos) && (zero != 0)) - pName = pName.substr(0, zero); - size_t slash = pName.find_last_of('/'); - if (slash != std::string::npos) - pName = pName.substr(slash + 1); - } - pName += " "; - } - - prctl(PR_SET_NAME, (pName + n).c_str(), 0, 0, 0); -} -#endif - -#ifndef HAVE_NAME_THREAD -extern void NameThread(const char*) -{ ; } -#endif - #ifdef __unix__ static pid_t pManager = static_cast(0); @@ -175,14 +105,14 @@ std::string DoSustain() _exit(0); if (pChild == 0) { - NameThread("main"); + setCallingThreadName("main"); signal(SIGINT, SIG_DFL); signal(SIGHUP, SIG_DFL); signal(SIGUSR1, SIG_DFL); signal(SIGUSR2, SIG_DFL); return str(boost::format("Launching child %d") % childCount);; } - NameThread(boost::str(boost::format("#%d") % childCount).c_str()); + setCallingThreadName(boost::str(boost::format("#%d") % childCount).c_str()); do { int i; diff --git a/src/cpp/ripple/utils.h b/src/cpp/ripple/utils.h index 0aeea1f76c..7c0b773b49 100644 --- a/src/cpp/ripple/utils.h +++ b/src/cpp/ripple/utils.h @@ -1,13 +1,6 @@ #ifndef __UTILS__ #define __UTILS__ -#include -#include - -#if BOOST_VERSION < 104700 -#error Boost 1.47 or later is required -#endif - #include #define nothing() do {} while (0) @@ -18,47 +11,9 @@ #define isSetBit(x,y) (!!((x) & (y))) -boost::posix_time::ptime ptEpoch(); -int iToSeconds(boost::posix_time::ptime ptWhen); -boost::posix_time::ptime ptFromSeconds(int iSeconds); -uint64_t utFromSeconds(int iSeconds); - DH* DH_der_load(const std::string& strDer); std::string DH_der_gen(int iKeyLength); -// VFALCO: NOTE, these three are unused. -/* -template T range_check(const T& value, const T& minimum, const T& maximum) -{ - if ((value < minimum) || (value > maximum)) - throw std::runtime_error("Value out of range"); - return value; -} - -template T range_check_min(const T& value, const T& minimum) -{ - if (value < minimum) - throw std::runtime_error("Value out of range"); - return value; -} - -template T range_check_max(const T& value, const T& maximum) -{ - if (value > maximum) - throw std::runtime_error("Value out of range"); - return value; -} -*/ - -template T range_check_cast(const U& value, const T& minimum, const T& maximum) -{ - if ((value < minimum) || (value > maximum)) - throw std::runtime_error("Value out of range"); - return static_cast(value); -} - -extern void NameThread(const char *); - extern bool HaveSustain(); extern std::string StopSustain(); extern std::string DoSustain();