aboutsummaryrefslogtreecommitdiff
path: root/src/util.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.h')
-rw-r--r--src/util.h74
1 files changed, 66 insertions, 8 deletions
diff --git a/src/util.h b/src/util.h
index d129d13692..4d7c81be0b 100644
--- a/src/util.h
+++ b/src/util.h
@@ -15,6 +15,8 @@
typedef int pid_t; /* define for Windows compatibility */
#endif
#include <map>
+#include <list>
+#include <utility>
#include <vector>
#include <string>
@@ -98,13 +100,16 @@ T* alignup(T* p)
#endif
#else
#define MAX_PATH 1024
-inline void Sleep(int64 n)
+#endif
+
+inline void MilliSleep(int64 n)
{
- /*Boost has a year 2038 problem— if the request sleep time is past epoch+2^31 seconds the sleep returns instantly.
- So we clamp our sleeps here to 10 years and hope that boost is fixed by 2028.*/
- boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(n>315576000000LL?315576000000LL:n));
-}
+#if BOOST_VERSION >= 105000
+ boost::this_thread::sleep_for(boost::chrono::milliseconds(n));
+#else
+ boost::this_thread::sleep(boost::posix_time::milliseconds(n));
#endif
+}
/* This GNU C extension enables the compiler to check the format string against the parameters provided.
* X is the number of the "format string" parameter, and Y is the number of the first variadic parameter.
@@ -129,8 +134,6 @@ extern bool fDebug;
extern bool fDebugNet;
extern bool fPrintToConsole;
extern bool fPrintToDebugger;
-extern volatile bool fRequestShutdown;
-extern bool fShutdown;
extern bool fDaemon;
extern bool fServer;
extern bool fCommandLine;
@@ -522,5 +525,60 @@ inline uint32_t ByteReverse(uint32_t value)
return (value<<16) | (value>>16);
}
-#endif
+// Standard wrapper for do-something-forever thread functions.
+// "Forever" really means until the thread is interrupted.
+// Use it like:
+// new boost::thread(boost::bind(&LoopForever<void (*)()>, "dumpaddr", &DumpAddresses, 10000));
+// or maybe:
+// boost::function<void()> f = boost::bind(&FunctionWithArg, argument);
+// threadGroup.create_thread(boost::bind(&LoopForever<boost::function<void()> >, "nothing", f, milliseconds));
+template <typename Callable> void LoopForever(const char* name, Callable func, int64 msecs)
+{
+ std::string s = strprintf("bitcoin-%s", name);
+ RenameThread(s.c_str());
+ printf("%s thread start\n", name);
+ try
+ {
+ while (1)
+ {
+ func();
+ MilliSleep(msecs);
+ }
+ }
+ catch (boost::thread_interrupted)
+ {
+ printf("%s thread stop\n", name);
+ throw;
+ }
+ catch (std::exception& e) {
+ PrintException(&e, name);
+ }
+ catch (...) {
+ PrintException(NULL, name);
+ }
+}
+// .. 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
+ {
+ printf("%s thread start\n", name);
+ func();
+ printf("%s thread exit\n", name);
+ }
+ catch (boost::thread_interrupted)
+ {
+ printf("%s thread interrupt\n", name);
+ throw;
+ }
+ catch (std::exception& e) {
+ PrintException(&e, name);
+ }
+ catch (...) {
+ PrintException(NULL, name);
+ }
+}
+#endif