2011-05-14 04:31:46 -04:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-02-08 18:50:24 -03:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin developers
|
2011-05-14 04:31:46 -04:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
2012-05-18 10:02:28 -04:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 02:13:08 -03:00
|
|
|
|
2011-05-14 04:31:46 -04:00
|
|
|
#ifndef BITCOIN_UTIL_H
|
|
|
|
#define BITCOIN_UTIL_H
|
|
|
|
|
2013-04-13 02:13:08 -03:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
2014-06-23 14:04:24 -04:00
|
|
|
#include "config/bitcoin-config.h"
|
2013-04-13 02:13:08 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "compat.h"
|
|
|
|
#include "serialize.h"
|
2014-01-16 11:52:37 -03:00
|
|
|
#include "tinyformat.h"
|
2011-05-14 04:31:46 -04:00
|
|
|
|
2013-04-13 02:13:08 -03:00
|
|
|
#include <cstdio>
|
|
|
|
#include <exception>
|
|
|
|
#include <map>
|
2013-05-24 09:45:08 -04:00
|
|
|
#include <stdarg.h>
|
2013-04-13 02:13:08 -03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2013-05-24 09:45:08 -04:00
|
|
|
|
2011-10-07 12:02:21 -03:00
|
|
|
#ifndef WIN32
|
2011-05-14 05:18:39 -04:00
|
|
|
#include <sys/resource.h>
|
2013-04-13 02:13:08 -03:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
2011-05-15 10:50:28 -04:00
|
|
|
#endif
|
2011-05-14 04:31:46 -04:00
|
|
|
|
2012-04-09 18:50:56 -03:00
|
|
|
#include <boost/filesystem/path.hpp>
|
2013-04-13 02:13:08 -03:00
|
|
|
#include <boost/thread.hpp>
|
2011-05-14 04:31:46 -04:00
|
|
|
|
2013-04-13 02:13:08 -03:00
|
|
|
class uint256;
|
2011-12-21 18:33:19 -03:00
|
|
|
|
2013-04-13 02:13:08 -03:00
|
|
|
static const int64_t COIN = 100000000;
|
|
|
|
static const int64_t CENT = 1000000;
|
2012-04-15 17:10:54 -03:00
|
|
|
|
2011-05-14 04:31:46 -04:00
|
|
|
#define BEGIN(a) ((char*)&(a))
|
|
|
|
#define END(a) ((char*)&((&(a))[1]))
|
|
|
|
#define UBEGIN(a) ((unsigned char*)&(a))
|
|
|
|
#define UEND(a) ((unsigned char*)&((&(a))[1]))
|
|
|
|
#define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
|
|
|
|
|
|
|
|
// This is needed because the foreach macro can't get over the comma in pair<t1, t2>
|
2011-06-01 11:13:25 -04:00
|
|
|
#define PAIRTYPE(t1, t2) std::pair<t1, t2>
|
2011-05-14 04:31:46 -04:00
|
|
|
|
2011-10-07 12:02:21 -03:00
|
|
|
#ifdef WIN32
|
2011-05-14 04:31:46 -04:00
|
|
|
#define MSG_DONTWAIT 0
|
2011-12-19 19:08:25 -03:00
|
|
|
|
2011-05-14 04:31:46 -04:00
|
|
|
#ifndef S_IRUSR
|
|
|
|
#define S_IRUSR 0400
|
|
|
|
#define S_IWUSR 0200
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define MAX_PATH 1024
|
2013-03-07 16:25:21 -03:00
|
|
|
#endif
|
2013-05-05 01:36:42 -04:00
|
|
|
// As Solaris does not have the MSG_NOSIGNAL flag for send(2) syscall, it is defined as 0
|
2013-04-13 02:13:08 -03:00
|
|
|
#if !defined(HAVE_MSG_NOSIGNAL) && !defined(MSG_NOSIGNAL)
|
2013-05-05 01:36:42 -04:00
|
|
|
#define MSG_NOSIGNAL 0
|
|
|
|
#endif
|
2013-03-07 16:25:21 -03:00
|
|
|
|
2013-04-13 02:13:08 -03:00
|
|
|
inline void MilliSleep(int64_t n)
|
2011-05-14 04:31:46 -04:00
|
|
|
{
|
2013-06-13 23:46:08 -04:00
|
|
|
// Boost's sleep_for was uninterruptable when backed by nanosleep from 1.50
|
|
|
|
// until fixed in 1.52. Use the deprecated sleep method for the broken case.
|
|
|
|
// See: https://svn.boost.org/trac/boost/ticket/7238
|
2013-05-27 19:55:01 -04:00
|
|
|
#if defined(HAVE_WORKING_BOOST_SLEEP_FOR)
|
2013-03-07 16:25:21 -03:00
|
|
|
boost::this_thread::sleep_for(boost::chrono::milliseconds(n));
|
2013-05-27 19:55:01 -04:00
|
|
|
#elif defined(HAVE_WORKING_BOOST_SLEEP)
|
2013-03-07 16:25:21 -03:00
|
|
|
boost::this_thread::sleep(boost::posix_time::milliseconds(n));
|
2013-05-27 19:55:01 -04:00
|
|
|
#else
|
2013-11-11 12:20:39 -03:00
|
|
|
//should never get here
|
2013-05-27 19:55:01 -04:00
|
|
|
#error missing boost sleep implementation
|
2011-05-14 04:31:46 -04:00
|
|
|
#endif
|
2013-03-07 16:25:21 -03:00
|
|
|
}
|
2011-05-14 04:31:46 -04:00
|
|
|
|
|
|
|
extern std::map<std::string, std::string> mapArgs;
|
|
|
|
extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
|
|
|
|
extern bool fDebug;
|
|
|
|
extern bool fPrintToConsole;
|
2013-12-14 09:51:11 -03:00
|
|
|
extern bool fPrintToDebugLog;
|
2011-05-14 04:31:46 -04:00
|
|
|
extern bool fServer;
|
|
|
|
extern std::string strMiscWarning;
|
|
|
|
extern bool fLogTimestamps;
|
2014-02-26 22:55:04 -03:00
|
|
|
extern bool fLogIPs;
|
2013-01-01 19:12:30 -03:00
|
|
|
extern volatile bool fReopenDebugLog;
|
2011-05-14 04:31:46 -04:00
|
|
|
|
2014-05-13 06:15:00 -04:00
|
|
|
void SetupEnvironment();
|
2013-09-18 05:03:21 -03:00
|
|
|
|
2014-01-16 11:52:37 -03:00
|
|
|
/* Return true if log accepts specified category */
|
|
|
|
bool LogAcceptCategory(const char* category);
|
|
|
|
/* Send a string to the log output */
|
|
|
|
int LogPrintStr(const std::string &str);
|
|
|
|
|
2013-09-18 07:38:08 -03:00
|
|
|
#define LogPrintf(...) LogPrint(NULL, __VA_ARGS__)
|
2012-03-18 19:14:03 -03:00
|
|
|
|
2014-01-16 11:52:37 -03:00
|
|
|
/* When we switch to C++11, this can be switched to variadic templates instead
|
|
|
|
* of this macro-based construction (see tinyformat.h).
|
2012-09-09 09:43:06 -03:00
|
|
|
*/
|
2014-01-16 11:52:37 -03:00
|
|
|
#define MAKE_ERROR_AND_LOG_FUNC(n) \
|
|
|
|
/* Print to debug.log if -debug=category switch is given OR category is NULL. */ \
|
|
|
|
template<TINYFORMAT_ARGTYPES(n)> \
|
|
|
|
static inline int LogPrint(const char* category, const char* format, TINYFORMAT_VARARGS(n)) \
|
|
|
|
{ \
|
|
|
|
if(!LogAcceptCategory(category)) return 0; \
|
|
|
|
return LogPrintStr(tfm::format(format, TINYFORMAT_PASSARGS(n))); \
|
|
|
|
} \
|
|
|
|
/* Log error and return false */ \
|
|
|
|
template<TINYFORMAT_ARGTYPES(n)> \
|
|
|
|
static inline bool error(const char* format, TINYFORMAT_VARARGS(n)) \
|
|
|
|
{ \
|
2014-01-29 04:10:22 -03:00
|
|
|
LogPrintStr("ERROR: " + tfm::format(format, TINYFORMAT_PASSARGS(n)) + "\n"); \
|
2014-01-16 11:52:37 -03:00
|
|
|
return false; \
|
|
|
|
}
|
|
|
|
|
|
|
|
TINYFORMAT_FOREACH_ARGNUM(MAKE_ERROR_AND_LOG_FUNC)
|
|
|
|
|
|
|
|
/* Zero-arg versions of logging and error, these are not covered by
|
|
|
|
* TINYFORMAT_FOREACH_ARGNUM
|
|
|
|
*/
|
|
|
|
static inline int LogPrint(const char* category, const char* format)
|
|
|
|
{
|
|
|
|
if(!LogAcceptCategory(category)) return 0;
|
|
|
|
return LogPrintStr(format);
|
|
|
|
}
|
|
|
|
static inline bool error(const char* format)
|
|
|
|
{
|
2014-01-29 04:10:22 -03:00
|
|
|
LogPrintStr(std::string("ERROR: ") + format + "\n");
|
2014-01-16 11:52:37 -03:00
|
|
|
return false;
|
|
|
|
}
|
2012-09-09 09:43:06 -03:00
|
|
|
|
2011-05-14 04:31:46 -04:00
|
|
|
void PrintExceptionContinue(std::exception* pex, const char* pszThread);
|
2013-04-13 02:13:08 -03:00
|
|
|
std::string FormatMoney(int64_t n, bool fPlus=false);
|
|
|
|
bool ParseMoney(const std::string& str, int64_t& nRet);
|
|
|
|
bool ParseMoney(const char* pszIn, int64_t& nRet);
|
2013-11-01 16:27:42 -03:00
|
|
|
std::string SanitizeString(const std::string& str);
|
2011-05-14 04:31:46 -04:00
|
|
|
std::vector<unsigned char> ParseHex(const char* psz);
|
|
|
|
std::vector<unsigned char> ParseHex(const std::string& str);
|
2014-06-28 11:35:22 -04:00
|
|
|
signed char HexDigit(char c);
|
2012-01-04 23:40:52 -03:00
|
|
|
bool IsHex(const std::string& str);
|
2011-09-20 10:38:29 -03:00
|
|
|
std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid = NULL);
|
|
|
|
std::string DecodeBase64(const std::string& str);
|
|
|
|
std::string EncodeBase64(const unsigned char* pch, size_t len);
|
|
|
|
std::string EncodeBase64(const std::string& str);
|
2012-01-22 16:32:58 -03:00
|
|
|
std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid = NULL);
|
|
|
|
std::string DecodeBase32(const std::string& str);
|
|
|
|
std::string EncodeBase32(const unsigned char* pch, size_t len);
|
|
|
|
std::string EncodeBase32(const std::string& str);
|
2012-02-06 14:37:49 -03:00
|
|
|
void ParseParameters(int argc, const char*const argv[]);
|
2012-05-12 01:24:27 -04:00
|
|
|
void FileCommit(FILE *fileout);
|
2013-01-30 00:17:33 -03:00
|
|
|
bool TruncateFile(FILE *file, unsigned int length);
|
2013-04-25 19:46:47 -03:00
|
|
|
int RaiseFileDescriptorLimit(int nMinFD);
|
2012-08-15 20:21:28 -04:00
|
|
|
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
|
2012-05-12 01:24:27 -04:00
|
|
|
bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest);
|
2014-03-23 22:14:43 -03:00
|
|
|
bool TryCreateDirectory(const boost::filesystem::path& p);
|
2012-04-09 18:50:56 -03:00
|
|
|
boost::filesystem::path GetDefaultDataDir();
|
|
|
|
const boost::filesystem::path &GetDataDir(bool fNetSpecific = true);
|
|
|
|
boost::filesystem::path GetConfigFile();
|
|
|
|
boost::filesystem::path GetPidFile();
|
2013-07-24 03:30:09 -04:00
|
|
|
#ifndef WIN32
|
2012-04-09 18:50:56 -03:00
|
|
|
void CreatePidFile(const boost::filesystem::path &path, pid_t pid);
|
2013-07-24 03:30:09 -04:00
|
|
|
#endif
|
2012-04-22 09:35:22 -03:00
|
|
|
void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
|
2012-04-22 11:22:45 -03:00
|
|
|
#ifdef WIN32
|
|
|
|
boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
|
|
|
|
#endif
|
2012-11-28 20:33:12 -03:00
|
|
|
boost::filesystem::path GetTempPath();
|
2011-05-14 04:31:46 -04:00
|
|
|
void ShrinkDebugFile();
|
2013-04-13 02:13:08 -03:00
|
|
|
int64_t GetTime();
|
|
|
|
void SetMockTime(int64_t nMockTimeIn);
|
2011-05-14 04:31:46 -04:00
|
|
|
std::string FormatFullVersion();
|
2011-12-16 18:26:14 -03:00
|
|
|
std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments);
|
2012-05-23 23:10:59 -04:00
|
|
|
void runCommand(std::string strCommand);
|
2011-05-14 04:31:46 -04:00
|
|
|
|
2013-04-13 02:13:08 -03:00
|
|
|
inline std::string i64tostr(int64_t n)
|
2011-05-14 04:31:46 -04:00
|
|
|
{
|
2014-02-24 05:08:56 -03:00
|
|
|
return strprintf("%d", n);
|
2011-05-14 04:31:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string itostr(int n)
|
|
|
|
{
|
|
|
|
return strprintf("%d", n);
|
|
|
|
}
|
|
|
|
|
2013-04-13 02:13:08 -03:00
|
|
|
inline int64_t atoi64(const char* psz)
|
2011-05-14 04:31:46 -04:00
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
return _atoi64(psz);
|
|
|
|
#else
|
|
|
|
return strtoll(psz, NULL, 10);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-04-13 02:13:08 -03:00
|
|
|
inline int64_t atoi64(const std::string& str)
|
2011-05-14 04:31:46 -04:00
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
return _atoi64(str.c_str());
|
|
|
|
#else
|
|
|
|
return strtoll(str.c_str(), NULL, 10);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int atoi(const std::string& str)
|
|
|
|
{
|
|
|
|
return atoi(str.c_str());
|
|
|
|
}
|
|
|
|
|
2014-05-03 04:20:58 -04:00
|
|
|
/**
|
|
|
|
* Convert string to signed 32-bit integer with strict parse error feedback.
|
|
|
|
* @returns true if the entire string could be parsed as valid integer,
|
|
|
|
* false if not the entire string could be parsed or when overflow or underflow occured.
|
|
|
|
*/
|
|
|
|
bool ParseInt32(const std::string& str, int32_t *out);
|
|
|
|
|
2011-05-14 04:31:46 -04:00
|
|
|
inline int roundint(double d)
|
|
|
|
{
|
|
|
|
return (int)(d > 0 ? d + 0.5 : d - 0.5);
|
|
|
|
}
|
|
|
|
|
2013-04-13 02:13:08 -03:00
|
|
|
inline int64_t roundint64(double d)
|
2011-05-14 04:31:46 -04:00
|
|
|
{
|
2013-04-13 02:13:08 -03:00
|
|
|
return (int64_t)(d > 0 ? d + 0.5 : d - 0.5);
|
2011-05-14 04:31:46 -04:00
|
|
|
}
|
|
|
|
|
2013-04-13 02:13:08 -03:00
|
|
|
inline int64_t abs64(int64_t n)
|
2011-05-14 04:31:46 -04:00
|
|
|
{
|
|
|
|
return (n >= 0 ? n : -n);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
|
|
|
|
{
|
2012-09-09 09:52:07 -03:00
|
|
|
std::string rv;
|
|
|
|
static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
|
|
|
|
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
2012-04-21 15:15:25 -03:00
|
|
|
rv.reserve((itend-itbegin)*3);
|
|
|
|
for(T it = itbegin; it < itend; ++it)
|
|
|
|
{
|
|
|
|
unsigned char val = (unsigned char)(*it);
|
|
|
|
if(fSpaces && it != itbegin)
|
|
|
|
rv.push_back(' ');
|
|
|
|
rv.push_back(hexmap[val>>4]);
|
|
|
|
rv.push_back(hexmap[val&15]);
|
|
|
|
}
|
|
|
|
|
2012-09-09 09:52:07 -03:00
|
|
|
return rv;
|
2011-05-14 04:31:46 -04:00
|
|
|
}
|
|
|
|
|
2013-04-30 15:56:04 -04:00
|
|
|
template<typename T>
|
|
|
|
inline std::string HexStr(const T& vch, bool fSpaces=false)
|
2011-05-14 04:31:46 -04:00
|
|
|
{
|
|
|
|
return HexStr(vch.begin(), vch.end(), fSpaces);
|
|
|
|
}
|
|
|
|
|
2014-06-10 10:02:29 -04:00
|
|
|
/** Format a paragraph of text to a fixed width, adding spaces for
|
|
|
|
* indentation to any added line.
|
|
|
|
*/
|
|
|
|
std::string FormatParagraph(const std::string in, size_t width=79, size_t indent=0);
|
|
|
|
|
2013-04-13 02:13:08 -03:00
|
|
|
inline int64_t GetTimeMillis()
|
2011-05-14 04:31:46 -04:00
|
|
|
{
|
|
|
|
return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) -
|
|
|
|
boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
|
|
|
|
}
|
|
|
|
|
2013-04-13 02:13:08 -03:00
|
|
|
inline int64_t GetTimeMicros()
|
2012-12-01 14:07:55 -03:00
|
|
|
{
|
|
|
|
return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) -
|
|
|
|
boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
|
|
|
|
}
|
|
|
|
|
2014-05-08 12:01:10 -04:00
|
|
|
std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime);
|
2011-05-14 04:31:46 -04:00
|
|
|
|
|
|
|
inline bool IsSwitchChar(char c)
|
|
|
|
{
|
2011-10-07 12:02:21 -03:00
|
|
|
#ifdef WIN32
|
2011-05-14 04:31:46 -04:00
|
|
|
return c == '-' || c == '/';
|
|
|
|
#else
|
|
|
|
return c == '-';
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-02-06 14:37:49 -03:00
|
|
|
/**
|
|
|
|
* Return string argument or default value
|
|
|
|
*
|
|
|
|
* @param strArg Argument to get (e.g. "-foo")
|
|
|
|
* @param default (e.g. "1")
|
|
|
|
* @return command-line argument or default value
|
|
|
|
*/
|
|
|
|
std::string GetArg(const std::string& strArg, const std::string& strDefault);
|
2011-05-14 04:31:46 -04:00
|
|
|
|
2012-02-06 14:37:49 -03:00
|
|
|
/**
|
|
|
|
* Return integer argument or default value
|
|
|
|
*
|
|
|
|
* @param strArg Argument to get (e.g. "-foo")
|
|
|
|
* @param default (e.g. 1)
|
|
|
|
* @return command-line argument (0 if invalid number) or default value
|
|
|
|
*/
|
2013-04-13 02:13:08 -03:00
|
|
|
int64_t GetArg(const std::string& strArg, int64_t nDefault);
|
2011-05-14 04:31:46 -04:00
|
|
|
|
2012-02-06 14:37:49 -03:00
|
|
|
/**
|
|
|
|
* Return boolean argument or default value
|
|
|
|
*
|
|
|
|
* @param strArg Argument to get (e.g. "-foo")
|
|
|
|
* @param default (true or false)
|
|
|
|
* @return command-line argument or default value
|
|
|
|
*/
|
2013-04-28 11:37:50 -04:00
|
|
|
bool GetBoolArg(const std::string& strArg, bool fDefault);
|
2011-05-14 04:31:46 -04:00
|
|
|
|
2012-01-03 12:14:22 -03:00
|
|
|
/**
|
|
|
|
* Set an argument if it doesn't already have a value
|
|
|
|
*
|
|
|
|
* @param strArg Argument to set (e.g. "-foo")
|
|
|
|
* @param strValue Value (e.g. "1")
|
|
|
|
* @return true if argument gets set, false if it already had a value
|
|
|
|
*/
|
|
|
|
bool SoftSetArg(const std::string& strArg, const std::string& strValue);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a boolean argument if it doesn't already have a value
|
|
|
|
*
|
|
|
|
* @param strArg Argument to set (e.g. "-foo")
|
|
|
|
* @param fValue Value (e.g. false)
|
|
|
|
* @return true if argument gets set, false if it already had a value
|
|
|
|
*/
|
2012-02-06 17:48:00 -03:00
|
|
|
bool SoftSetBoolArg(const std::string& strArg, bool fValue);
|
2011-05-14 04:31:46 -04:00
|
|
|
|
2013-08-08 05:58:57 -04:00
|
|
|
/**
|
|
|
|
* Timing-attack-resistant comparison.
|
|
|
|
* Takes time proportional to length
|
|
|
|
* of first argument.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
bool TimingResistantEqual(const T& a, const T& b)
|
|
|
|
{
|
|
|
|
if (b.size() == 0) return a.size() == 0;
|
|
|
|
size_t accumulator = a.size() ^ b.size();
|
|
|
|
for (size_t i = 0; i < a.size(); i++)
|
|
|
|
accumulator |= a[i] ^ b[i%b.size()];
|
|
|
|
return accumulator == 0;
|
|
|
|
}
|
|
|
|
|
2011-10-07 12:02:21 -03:00
|
|
|
#ifdef WIN32
|
2011-05-14 04:31:46 -04:00
|
|
|
inline void SetThreadPriority(int nPriority)
|
|
|
|
{
|
|
|
|
SetThreadPriority(GetCurrentThread(), nPriority);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
2013-05-05 01:36:42 -04:00
|
|
|
// PRIO_MAX is not defined on Solaris
|
|
|
|
#ifndef PRIO_MAX
|
2013-08-30 19:11:12 -04:00
|
|
|
#define PRIO_MAX 20
|
2013-05-05 01:36:42 -04:00
|
|
|
#endif
|
2011-05-14 04:31:46 -04:00
|
|
|
#define THREAD_PRIORITY_LOWEST PRIO_MAX
|
|
|
|
#define THREAD_PRIORITY_BELOW_NORMAL 2
|
|
|
|
#define THREAD_PRIORITY_NORMAL 0
|
2013-04-23 06:36:54 -03:00
|
|
|
#define THREAD_PRIORITY_ABOVE_NORMAL (-2)
|
2011-05-14 04:31:46 -04:00
|
|
|
|
|
|
|
inline void SetThreadPriority(int nPriority)
|
|
|
|
{
|
|
|
|
// It's unclear if it's even possible to change thread priorities on Linux,
|
|
|
|
// but we really and truly need it for the generation threads.
|
|
|
|
#ifdef PRIO_THREAD
|
|
|
|
setpriority(PRIO_THREAD, 0, nPriority);
|
|
|
|
#else
|
|
|
|
setpriority(PRIO_PROCESS, 0, nPriority);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-06-24 11:03:57 -04:00
|
|
|
void RenameThread(const char* name);
|
2011-05-14 04:31:46 -04:00
|
|
|
|
2013-03-08 22:19:17 -03:00
|
|
|
// Standard wrapper for do-something-forever thread functions.
|
|
|
|
// "Forever" really means until the thread is interrupted.
|
|
|
|
// Use it like:
|
2013-06-23 18:23:28 -04:00
|
|
|
// new boost::thread(boost::bind(&LoopForever<void (*)()>, "dumpaddr", &DumpAddresses, 900000));
|
2013-03-08 22:19:17 -03:00
|
|
|
// or maybe:
|
|
|
|
// boost::function<void()> f = boost::bind(&FunctionWithArg, argument);
|
|
|
|
// threadGroup.create_thread(boost::bind(&LoopForever<boost::function<void()> >, "nothing", f, milliseconds));
|
2013-04-13 02:13:08 -03:00
|
|
|
template <typename Callable> void LoopForever(const char* name, Callable func, int64_t msecs)
|
2013-03-08 22:19:17 -03:00
|
|
|
{
|
|
|
|
std::string s = strprintf("bitcoin-%s", name);
|
|
|
|
RenameThread(s.c_str());
|
2013-09-18 07:38:08 -03:00
|
|
|
LogPrintf("%s thread start\n", name);
|
2013-03-08 22:19:17 -03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
MilliSleep(msecs);
|
2013-06-23 18:23:28 -04:00
|
|
|
func();
|
2013-03-08 22:19:17 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (boost::thread_interrupted)
|
|
|
|
{
|
2013-09-18 07:38:08 -03:00
|
|
|
LogPrintf("%s thread stop\n", name);
|
2013-03-08 22:19:17 -03:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
2014-02-26 09:23:52 -03:00
|
|
|
PrintExceptionContinue(&e, name);
|
|
|
|
throw;
|
2013-03-08 22:19:17 -03:00
|
|
|
}
|
|
|
|
catch (...) {
|
2014-02-26 09:23:52 -03:00
|
|
|
PrintExceptionContinue(NULL, name);
|
|
|
|
throw;
|
2013-03-08 22:19:17 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// .. and a wrapper that just calls func once
|
|
|
|
template <typename Callable> void TraceThread(const char* name, Callable func)
|
|
|
|
{
|
|
|
|
std::string s = strprintf("bitcoin-%s", name);
|
|
|
|
RenameThread(s.c_str());
|
|
|
|
try
|
|
|
|
{
|
2013-09-18 07:38:08 -03:00
|
|
|
LogPrintf("%s thread start\n", name);
|
2013-03-08 22:19:17 -03:00
|
|
|
func();
|
2013-09-18 07:38:08 -03:00
|
|
|
LogPrintf("%s thread exit\n", name);
|
2013-03-08 22:19:17 -03:00
|
|
|
}
|
|
|
|
catch (boost::thread_interrupted)
|
|
|
|
{
|
2013-09-18 07:38:08 -03:00
|
|
|
LogPrintf("%s thread interrupt\n", name);
|
2013-03-08 22:19:17 -03:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
2014-02-26 09:23:52 -03:00
|
|
|
PrintExceptionContinue(&e, name);
|
|
|
|
throw;
|
2013-03-08 22:19:17 -03:00
|
|
|
}
|
|
|
|
catch (...) {
|
2014-02-26 09:23:52 -03:00
|
|
|
PrintExceptionContinue(NULL, name);
|
|
|
|
throw;
|
2013-03-08 22:19:17 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-14 04:31:46 -04:00
|
|
|
#endif
|