752bb4d64a56a690b0a16cda5950edfb2f846e96
[novacoin.git] / src / util.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Copyright (c) 2012 The PPCoin developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_UTIL_H
7 #define BITCOIN_UTIL_H
8
9 #include "uint256.h"
10
11 #ifndef WIN32
12 #include <sys/types.h>
13 #include <sys/time.h>
14 #include <sys/resource.h>
15 #else
16 typedef int pid_t; /* define for windows compatiblity */
17 #endif
18 #include <map>
19 #include <vector>
20 #include <string>
21
22 #include <boost/thread.hpp>
23 #include <boost/filesystem.hpp>
24 #include <boost/filesystem/path.hpp>
25 #include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
26 #include <boost/interprocess/sync/scoped_lock.hpp>
27 #include <boost/interprocess/sync/interprocess_semaphore.hpp>
28 #include <boost/interprocess/sync/lock_options.hpp>
29 #include <boost/date_time/gregorian/gregorian_types.hpp>
30 #include <boost/date_time/posix_time/posix_time_types.hpp>
31
32 #include <openssl/sha.h>
33 #include <openssl/ripemd.h>
34
35 #include "netbase.h" // for AddTimeData
36
37 typedef long long  int64;
38 typedef unsigned long long  uint64;
39
40 static const int64 COIN = 1000000;
41 static const int64 CENT = 10000;
42
43 #define loop                for (;;)
44 #define BEGIN(a)            ((char*)&(a))
45 #define END(a)              ((char*)&((&(a))[1]))
46 #define UBEGIN(a)           ((unsigned char*)&(a))
47 #define UEND(a)             ((unsigned char*)&((&(a))[1]))
48 #define ARRAYLEN(array)     (sizeof(array)/sizeof((array)[0]))
49 #define printf              OutputDebugStringF
50
51 #ifdef snprintf
52 #undef snprintf
53 #endif
54 #define snprintf my_snprintf
55
56 #ifndef PRI64d
57 #if defined(_MSC_VER) || defined(__MSVCRT__)
58 #define PRI64d  "I64d"
59 #define PRI64u  "I64u"
60 #define PRI64x  "I64x"
61 #else
62 #define PRI64d  "lld"
63 #define PRI64u  "llu"
64 #define PRI64x  "llx"
65 #endif
66 #endif
67
68 // This is needed because the foreach macro can't get over the comma in pair<t1, t2>
69 #define PAIRTYPE(t1, t2)    std::pair<t1, t2>
70
71 // Align by increasing pointer, must have extra space at end of buffer
72 template <size_t nBytes, typename T>
73 T* alignup(T* p)
74 {
75     union
76     {
77         T* ptr;
78         size_t n;
79     } u;
80     u.ptr = p;
81     u.n = (u.n + (nBytes-1)) & ~(nBytes-1);
82     return u.ptr;
83 }
84
85 #ifdef WIN32
86 #define MSG_NOSIGNAL        0
87 #define MSG_DONTWAIT        0
88
89 #ifndef S_IRUSR
90 #define S_IRUSR             0400
91 #define S_IWUSR             0200
92 #endif
93 #define unlink              _unlink
94 #else
95 #define _vsnprintf(a,b,c,d) vsnprintf(a,b,c,d)
96 #define strlwr(psz)         to_lower(psz)
97 #define _strlwr(psz)        to_lower(psz)
98 #define MAX_PATH            1024
99 inline void Sleep(int64 n)
100 {
101     /*Boost has a year 2038 problem— if the request sleep time is past epoch+2^31 seconds the sleep returns instantly.
102       So we clamp our sleeps here to 10 years and hope that boost is fixed by 2028.*/
103     boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(n>315576000000LL?315576000000LL:n));
104 }
105 #endif
106
107 #ifndef THROW_WITH_STACKTRACE
108 #define THROW_WITH_STACKTRACE(exception)  \
109 {                                         \
110     LogStackTrace();                      \
111     throw (exception);                    \
112 }
113 void LogStackTrace();
114 #endif
115
116
117
118
119
120
121 extern std::map<std::string, std::string> mapArgs;
122 extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
123 extern bool fDebug;
124 extern bool fPrintToConsole;
125 extern bool fPrintToDebugger;
126 extern bool fRequestShutdown;
127 extern bool fShutdown;
128 extern bool fDaemon;
129 extern bool fServer;
130 extern bool fCommandLine;
131 extern std::string strMiscWarning;
132 extern bool fTestNet;
133 extern bool fNoListen;
134 extern bool fLogTimestamps;
135
136 void RandAddSeed();
137 void RandAddSeedPerfmon();
138 int OutputDebugStringF(const char* pszFormat, ...);
139 int my_snprintf(char* buffer, size_t limit, const char* format, ...);
140
141 /* It is not allowed to use va_start with a pass-by-reference argument.
142    (C++ standard, 18.7, paragraph 3). Use a dummy argument to work around this, and use a
143    macro to keep similar semantics.
144 */
145 std::string real_strprintf(const std::string &format, int dummy, ...);
146 #define strprintf(format, ...) real_strprintf(format, 0, __VA_ARGS__)
147
148 bool error(const char *format, ...);
149 void LogException(std::exception* pex, const char* pszThread);
150 void PrintException(std::exception* pex, const char* pszThread);
151 void PrintExceptionContinue(std::exception* pex, const char* pszThread);
152 void ParseString(const std::string& str, char c, std::vector<std::string>& v);
153 std::string FormatMoney(int64 n, bool fPlus=false);
154 bool ParseMoney(const std::string& str, int64& nRet);
155 bool ParseMoney(const char* pszIn, int64& nRet);
156 std::vector<unsigned char> ParseHex(const char* psz);
157 std::vector<unsigned char> ParseHex(const std::string& str);
158 bool IsHex(const std::string& str);
159 std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid = NULL);
160 std::string DecodeBase64(const std::string& str);
161 std::string EncodeBase64(const unsigned char* pch, size_t len);
162 std::string EncodeBase64(const std::string& str);
163 void ParseParameters(int argc, const char*const argv[]);
164 bool WildcardMatch(const char* psz, const char* mask);
165 bool WildcardMatch(const std::string& str, const std::string& mask);
166 int GetFilesize(FILE* file);
167 boost::filesystem::path GetDefaultDataDir();
168 const boost::filesystem::path &GetDataDir(bool fNetSpecific = true);
169 boost::filesystem::path GetConfigFile();
170 boost::filesystem::path GetPidFile();
171 void CreatePidFile(const boost::filesystem::path &path, pid_t pid);
172 void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
173 bool GetStartOnSystemStartup();
174 bool SetStartOnSystemStartup(bool fAutoStart);
175 void ShrinkDebugFile();
176 int GetRandInt(int nMax);
177 uint64 GetRand(uint64 nMax);
178 uint256 GetRandHash();
179 int64 GetTime();
180 void SetMockTime(int64 nMockTimeIn);
181 int64 GetAdjustedTime();
182 std::string FormatFullVersion();
183 std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments);
184 void AddTimeData(const CNetAddr& ip, int64 nTime);
185
186
187
188
189
190
191
192
193
194
195
196 /** Wrapped boost mutex: supports recursive locking, but no waiting  */
197 typedef boost::interprocess::interprocess_recursive_mutex CCriticalSection;
198
199 /** Wrapped boost mutex: supports waiting but not recursive locking */
200 typedef boost::interprocess::interprocess_mutex CWaitableCriticalSection;
201
202 #ifdef DEBUG_LOCKORDER
203 void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
204 void LeaveCritical();
205 #else
206 void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
207 void static inline LeaveCritical() {}
208 #endif
209
210 /** Wrapper around boost::interprocess::scoped_lock */
211 template<typename Mutex>
212 class CMutexLock
213 {
214 private:
215     boost::interprocess::scoped_lock<Mutex> lock;
216 public:
217
218     void Enter(const char* pszName, const char* pszFile, int nLine)
219     {
220         if (!lock.owns())
221         {
222             EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()));
223 #ifdef DEBUG_LOCKCONTENTION
224             if (!lock.try_lock())
225             {
226                 printf("LOCKCONTENTION: %s\n", pszName);
227                 printf("Locker: %s:%d\n", pszFile, nLine);
228 #endif
229             lock.lock();
230 #ifdef DEBUG_LOCKCONTENTION
231             }
232 #endif
233         }
234     }
235
236     void Leave()
237     {
238         if (lock.owns())
239         {
240             lock.unlock();
241             LeaveCritical();
242         }
243     }
244
245     bool TryEnter(const char* pszName, const char* pszFile, int nLine)
246     {
247         if (!lock.owns())
248         {
249             EnterCritical(pszName, pszFile, nLine, (void*)(lock.mutex()), true);
250             lock.try_lock();
251             if (!lock.owns())
252                 LeaveCritical();
253         }
254         return lock.owns();
255     }
256
257     CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) : lock(mutexIn, boost::interprocess::defer_lock)
258     {
259         if (fTry)
260             TryEnter(pszName, pszFile, nLine);
261         else
262             Enter(pszName, pszFile, nLine);
263     }
264
265     ~CMutexLock()
266     {
267         if (lock.owns())
268             LeaveCritical();
269     }
270
271     operator bool()
272     {
273         return lock.owns();
274     }
275
276     boost::interprocess::scoped_lock<Mutex> &GetLock()
277     {
278         return lock;
279     }
280 };
281
282 typedef CMutexLock<CCriticalSection> CCriticalBlock;
283
284 #define LOCK(cs) CCriticalBlock criticalblock(cs, #cs, __FILE__, __LINE__)
285 #define LOCK2(cs1,cs2) CCriticalBlock criticalblock1(cs1, #cs1, __FILE__, __LINE__),criticalblock2(cs2, #cs2, __FILE__, __LINE__)
286 #define TRY_LOCK(cs,name) CCriticalBlock name(cs, #cs, __FILE__, __LINE__, true)
287
288 #define ENTER_CRITICAL_SECTION(cs) \
289     { \
290         EnterCritical(#cs, __FILE__, __LINE__, (void*)(&cs)); \
291         (cs).lock(); \
292     }
293
294 #define LEAVE_CRITICAL_SECTION(cs) \
295     { \
296         (cs).unlock(); \
297         LeaveCritical(); \
298     }
299
300 #ifdef MAC_OSX
301 // boost::interprocess::interprocess_semaphore seems to spinlock on OSX; prefer polling instead
302 class CSemaphore
303 {
304 private:
305     CCriticalSection cs;
306     int val;
307
308 public:
309     CSemaphore(int init) : val(init) {}
310
311     void wait() {
312         do {
313             {
314                 LOCK(cs);
315                 if (val>0) {
316                     val--;
317                     return;
318                 }
319             }
320             Sleep(100);
321         } while(1);
322     }
323
324     bool try_wait() {
325         LOCK(cs);
326         if (val>0) {
327             val--;
328             return true;
329         }
330         return false;
331     }
332
333     void post() {
334         LOCK(cs);
335         val++;
336     }
337 };
338 #else
339 typedef boost::interprocess::interprocess_semaphore CSemaphore;
340 #endif
341
342 inline std::string i64tostr(int64 n)
343 {
344     return strprintf("%"PRI64d, n);
345 }
346
347 inline std::string itostr(int n)
348 {
349     return strprintf("%d", n);
350 }
351
352 inline int64 atoi64(const char* psz)
353 {
354 #ifdef _MSC_VER
355     return _atoi64(psz);
356 #else
357     return strtoll(psz, NULL, 10);
358 #endif
359 }
360
361 inline int64 atoi64(const std::string& str)
362 {
363 #ifdef _MSC_VER
364     return _atoi64(str.c_str());
365 #else
366     return strtoll(str.c_str(), NULL, 10);
367 #endif
368 }
369
370 inline int atoi(const std::string& str)
371 {
372     return atoi(str.c_str());
373 }
374
375 inline int roundint(double d)
376 {
377     return (int)(d > 0 ? d + 0.5 : d - 0.5);
378 }
379
380 inline int64 roundint64(double d)
381 {
382     return (int64)(d > 0 ? d + 0.5 : d - 0.5);
383 }
384
385 inline int64 abs64(int64 n)
386 {
387     return (n >= 0 ? n : -n);
388 }
389
390 template<typename T>
391 std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
392 {
393     std::vector<char> rv;
394     static char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
395                                '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
396     rv.reserve((itend-itbegin)*3);
397     for(T it = itbegin; it < itend; ++it)
398     {
399         unsigned char val = (unsigned char)(*it);
400         if(fSpaces && it != itbegin)
401             rv.push_back(' ');
402         rv.push_back(hexmap[val>>4]);
403         rv.push_back(hexmap[val&15]);
404     }
405
406     return std::string(rv.begin(), rv.end());
407 }
408
409 inline std::string HexStr(const std::vector<unsigned char>& vch, bool fSpaces=false)
410 {
411     return HexStr(vch.begin(), vch.end(), fSpaces);
412 }
413
414 template<typename T>
415 void PrintHex(const T pbegin, const T pend, const char* pszFormat="%s", bool fSpaces=true)
416 {
417     printf(pszFormat, HexStr(pbegin, pend, fSpaces).c_str());
418 }
419
420 inline void PrintHex(const std::vector<unsigned char>& vch, const char* pszFormat="%s", bool fSpaces=true)
421 {
422     printf(pszFormat, HexStr(vch, fSpaces).c_str());
423 }
424
425 inline int64 GetPerformanceCounter()
426 {
427     int64 nCounter = 0;
428 #ifdef WIN32
429     QueryPerformanceCounter((LARGE_INTEGER*)&nCounter);
430 #else
431     timeval t;
432     gettimeofday(&t, NULL);
433     nCounter = t.tv_sec * 1000000 + t.tv_usec;
434 #endif
435     return nCounter;
436 }
437
438 inline int64 GetTimeMillis()
439 {
440     return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) -
441             boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
442 }
443
444 inline std::string DateTimeStrFormat(const char* pszFormat, int64 nTime)
445 {
446     time_t n = nTime;
447     struct tm* ptmTime = gmtime(&n);
448     char pszTime[200];
449     strftime(pszTime, sizeof(pszTime), pszFormat, ptmTime);
450     return pszTime;
451 }
452
453 static const std::string strTimestampFormat = "%Y-%m-%d %H:%M:%S UTC";
454 inline std::string DateTimeStrFormat(int64 nTime)
455 {
456     return DateTimeStrFormat(strTimestampFormat.c_str(), nTime);
457 }
458
459 template<typename T>
460 void skipspaces(T& it)
461 {
462     while (isspace(*it))
463         ++it;
464 }
465
466 inline bool IsSwitchChar(char c)
467 {
468 #ifdef WIN32
469     return c == '-' || c == '/';
470 #else
471     return c == '-';
472 #endif
473 }
474
475 /**
476  * Return string argument or default value
477  *
478  * @param strArg Argument to get (e.g. "-foo")
479  * @param default (e.g. "1")
480  * @return command-line argument or default value
481  */
482 std::string GetArg(const std::string& strArg, const std::string& strDefault);
483
484 /**
485  * Return integer argument or default value
486  *
487  * @param strArg Argument to get (e.g. "-foo")
488  * @param default (e.g. 1)
489  * @return command-line argument (0 if invalid number) or default value
490  */
491 int64 GetArg(const std::string& strArg, int64 nDefault);
492
493 /**
494  * Return boolean argument or default value
495  *
496  * @param strArg Argument to get (e.g. "-foo")
497  * @param default (true or false)
498  * @return command-line argument or default value
499  */
500 bool GetBoolArg(const std::string& strArg, bool fDefault=false);
501
502 /**
503  * Set an argument if it doesn't already have a value
504  *
505  * @param strArg Argument to set (e.g. "-foo")
506  * @param strValue Value (e.g. "1")
507  * @return true if argument gets set, false if it already had a value
508  */
509 bool SoftSetArg(const std::string& strArg, const std::string& strValue);
510
511 /**
512  * Set a boolean argument if it doesn't already have a value
513  *
514  * @param strArg Argument to set (e.g. "-foo")
515  * @param fValue Value (e.g. false)
516  * @return true if argument gets set, false if it already had a value
517  */
518 bool SoftSetBoolArg(const std::string& strArg, bool fValue);
519
520
521
522
523
524
525
526
527
528 // Randomize the stack to help protect against buffer overrun exploits
529 #define IMPLEMENT_RANDOMIZE_STACK(ThreadFn)     \
530     {                                           \
531         static char nLoops;                     \
532         if (nLoops <= 0)                        \
533             nLoops = GetRand(20) + 1;           \
534         if (nLoops-- > 1)                       \
535         {                                       \
536             ThreadFn;                           \
537             return;                             \
538         }                                       \
539     }
540
541
542 template<typename T1>
543 inline uint256 Hash(const T1 pbegin, const T1 pend)
544 {
545     static unsigned char pblank[1];
546     uint256 hash1;
547     SHA256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), (unsigned char*)&hash1);
548     uint256 hash2;
549     SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
550     return hash2;
551 }
552
553 template<typename T1, typename T2>
554 inline uint256 Hash(const T1 p1begin, const T1 p1end,
555                     const T2 p2begin, const T2 p2end)
556 {
557     static unsigned char pblank[1];
558     uint256 hash1;
559     SHA256_CTX ctx;
560     SHA256_Init(&ctx);
561     SHA256_Update(&ctx, (p1begin == p1end ? pblank : (unsigned char*)&p1begin[0]), (p1end - p1begin) * sizeof(p1begin[0]));
562     SHA256_Update(&ctx, (p2begin == p2end ? pblank : (unsigned char*)&p2begin[0]), (p2end - p2begin) * sizeof(p2begin[0]));
563     SHA256_Final((unsigned char*)&hash1, &ctx);
564     uint256 hash2;
565     SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
566     return hash2;
567 }
568
569 template<typename T1, typename T2, typename T3>
570 inline uint256 Hash(const T1 p1begin, const T1 p1end,
571                     const T2 p2begin, const T2 p2end,
572                     const T3 p3begin, const T3 p3end)
573 {
574     static unsigned char pblank[1];
575     uint256 hash1;
576     SHA256_CTX ctx;
577     SHA256_Init(&ctx);
578     SHA256_Update(&ctx, (p1begin == p1end ? pblank : (unsigned char*)&p1begin[0]), (p1end - p1begin) * sizeof(p1begin[0]));
579     SHA256_Update(&ctx, (p2begin == p2end ? pblank : (unsigned char*)&p2begin[0]), (p2end - p2begin) * sizeof(p2begin[0]));
580     SHA256_Update(&ctx, (p3begin == p3end ? pblank : (unsigned char*)&p3begin[0]), (p3end - p3begin) * sizeof(p3begin[0]));
581     SHA256_Final((unsigned char*)&hash1, &ctx);
582     uint256 hash2;
583     SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
584     return hash2;
585 }
586
587 template<typename T>
588 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
589 {
590     // Most of the time is spent allocating and deallocating CDataStream's
591     // buffer.  If this ever needs to be optimized further, make a CStaticStream
592     // class with its buffer on the stack.
593     CDataStream ss(nType, nVersion);
594     ss.reserve(10000);
595     ss << obj;
596     return Hash(ss.begin(), ss.end());
597 }
598
599 inline uint160 Hash160(const std::vector<unsigned char>& vch)
600 {
601     uint256 hash1;
602     SHA256(&vch[0], vch.size(), (unsigned char*)&hash1);
603     uint160 hash2;
604     RIPEMD160((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
605     return hash2;
606 }
607
608
609 /** Median filter over a stream of values. 
610  * Returns the median of the last N numbers
611  */
612 template <typename T> class CMedianFilter
613 {
614 private:
615     std::vector<T> vValues;
616     std::vector<T> vSorted;
617     unsigned int nSize;
618 public:
619     CMedianFilter(unsigned int size, T initial_value):
620         nSize(size)
621     {
622         vValues.reserve(size);
623         vValues.push_back(initial_value);
624         vSorted = vValues;
625     }
626     
627     void input(T value)
628     {
629         if(vValues.size() == nSize)
630         {
631             vValues.erase(vValues.begin());
632         }
633         vValues.push_back(value);
634
635         vSorted.resize(vValues.size());
636         std::copy(vValues.begin(), vValues.end(), vSorted.begin());
637         std::sort(vSorted.begin(), vSorted.end());
638     }
639
640     T median() const
641     {
642         int size = vSorted.size();
643         assert(size>0);
644         if(size & 1) // Odd number of elements
645         {
646             return vSorted[size/2];
647         }
648         else // Even number of elements
649         {
650             return (vSorted[size/2-1] + vSorted[size/2]) / 2;
651         }
652     }
653
654     int size() const
655     {
656         return vValues.size();
657     }
658
659     std::vector<T> sorted () const
660     {
661         return vSorted;
662     }
663 };
664
665
666
667
668
669
670
671
672
673
674 // Note: It turns out we might have been able to use boost::thread
675 // by using TerminateThread(boost::thread.native_handle(), 0);
676 #ifdef WIN32
677 typedef HANDLE bitcoin_pthread_t;
678
679 inline bitcoin_pthread_t CreateThread(void(*pfn)(void*), void* parg, bool fWantHandle=false)
680 {
681     DWORD nUnused = 0;
682     HANDLE hthread =
683         CreateThread(
684             NULL,                        // default security
685             0,                           // inherit stack size from parent
686             (LPTHREAD_START_ROUTINE)pfn, // function pointer
687             parg,                        // argument
688             0,                           // creation option, start immediately
689             &nUnused);                   // thread identifier
690     if (hthread == NULL)
691     {
692         printf("Error: CreateThread() returned %d\n", GetLastError());
693         return (bitcoin_pthread_t)0;
694     }
695     if (!fWantHandle)
696     {
697         CloseHandle(hthread);
698         return (bitcoin_pthread_t)-1;
699     }
700     return hthread;
701 }
702
703 inline void SetThreadPriority(int nPriority)
704 {
705     SetThreadPriority(GetCurrentThread(), nPriority);
706 }
707 #else
708 inline pthread_t CreateThread(void(*pfn)(void*), void* parg, bool fWantHandle=false)
709 {
710     pthread_t hthread = 0;
711     int ret = pthread_create(&hthread, NULL, (void*(*)(void*))pfn, parg);
712     if (ret != 0)
713     {
714         printf("Error: pthread_create() returned %d\n", ret);
715         return (pthread_t)0;
716     }
717     if (!fWantHandle)
718     {
719         pthread_detach(hthread);
720         return (pthread_t)-1;
721     }
722     return hthread;
723 }
724
725 #define THREAD_PRIORITY_LOWEST          PRIO_MAX
726 #define THREAD_PRIORITY_BELOW_NORMAL    2
727 #define THREAD_PRIORITY_NORMAL          0
728 #define THREAD_PRIORITY_ABOVE_NORMAL    0
729
730 inline void SetThreadPriority(int nPriority)
731 {
732     // It's unclear if it's even possible to change thread priorities on Linux,
733     // but we really and truly need it for the generation threads.
734 #ifdef PRIO_THREAD
735     setpriority(PRIO_THREAD, 0, nPriority);
736 #else
737     setpriority(PRIO_PROCESS, 0, nPriority);
738 #endif
739 }
740
741 inline void ExitThread(size_t nExitCode)
742 {
743     pthread_exit((void*)nExitCode);
744 }
745 #endif
746
747
748
749
750
751 inline uint32_t ByteReverse(uint32_t value)
752 {
753     value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
754     return (value<<16) | (value>>16);
755 }
756
757 #endif
758