From efa1d6bdc6a159854802747d147b15f60b800058 Mon Sep 17 00:00:00 2001 From: Daniel Hams Date: Fri, 14 Feb 2014 17:17:50 +0000 Subject: [PATCH 1/2] Change logging "name" to be class instance thread specific ptr for unified code between Linux/OSX. --- libethereum/CMakeLists.txt | 1 + libethereum/Client.cpp | 9 --------- libethereum/Client.h | 4 ---- libethereum/Common.cpp | 5 +---- libethereum/Common.h | 24 ++++++++++-------------- 5 files changed, 12 insertions(+), 31 deletions(-) diff --git a/libethereum/CMakeLists.txt b/libethereum/CMakeLists.txt index 15b33a477..d79ceb3f9 100644 --- a/libethereum/CMakeLists.txt +++ b/libethereum/CMakeLists.txt @@ -35,6 +35,7 @@ else () target_link_libraries(ethereum ${CRYPTOPP_LIBRARIES}) target_link_libraries(ethereum boost_system) target_link_libraries(ethereum boost_filesystem) + target_link_libraries(ethereum boost_thread) find_package(Threads REQUIRED) target_link_libraries(ethereum ${CMAKE_THREAD_LIBS_INIT}) endif () diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index a3ef271e6..607487f31 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -46,17 +46,8 @@ Client::Client(std::string const& _clientVersion, Address _us, std::string const static const char* c_threadName = "eth"; -#if defined(__APPLE__) - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - m_work = dispatch_queue_create(c_threadName, DISPATCH_QUEUE_SERIAL); - }); - - dispatch_async(m_work, ^{ -#else m_work = new thread([&](){ setThreadName(c_threadName); -#endif while (m_workState != Deleting) work(); m_workState = Deleted; }); diff --git a/libethereum/Client.h b/libethereum/Client.h index 1ef75c9f5..f117d6537 100644 --- a/libethereum/Client.h +++ b/libethereum/Client.h @@ -138,11 +138,7 @@ private: State m_mined; ///< The state of the client which we're mining (i.e. it'll have all the rewards added). PeerServer* m_net = nullptr; ///< Should run in background and send us events when blocks found and allow us to send blocks as required. -#if defined(__APPLE__) - dispatch_queue_t m_work; -#else std::thread* m_work; ///< The work thread. -#endif std::mutex m_lock; enum { Active = 0, Deleting, Deleted } m_workState = Active; diff --git a/libethereum/Common.cpp b/libethereum/Common.cpp index 17a8b21f3..1efb953c3 100644 --- a/libethereum/Common.cpp +++ b/libethereum/Common.cpp @@ -44,10 +44,7 @@ using namespace eth; int eth::g_logVerbosity = 8; map eth::g_logOverride; -#if !defined(__APPLE__) -thread_local char const* eth::t_logThreadName = "???"; -static char const* g_mainThreadName = (eth::t_logThreadName = "main"); -#endif +ThreadLocalLogName eth::t_logThreadName("main"); void eth::simpleDebugOut(std::string const& _s, char const*) { diff --git a/libethereum/Common.h b/libethereum/Common.h index ab3871ce1..898bf1c58 100644 --- a/libethereum/Common.h +++ b/libethereum/Common.h @@ -39,13 +39,9 @@ #include #include #include +#include #include "vector_ref.h" -// OSX likes GCD whichs runs threads within queues -#if defined(__APPLE__) -#include -#endif - namespace eth { @@ -160,10 +156,14 @@ public: extern std::map g_logOverride; -#if !defined(__APPLE__) -extern thread_local char const* t_logThreadName; -inline void setThreadName(char const* _n) { t_logThreadName = _n; } -#endif +struct ThreadLocalLogName +{ + ThreadLocalLogName( std::string _name ) { m_name.reset( new std::string(_name) ); }; + boost::thread_specific_ptr m_name; +}; + +extern ThreadLocalLogName t_logThreadName; +inline void setThreadName(char const* _n) { t_logThreadName.m_name.reset( new std::string(_n) ); } struct LogChannel { static const char constexpr* name = " "; static const int verbosity = 1; }; struct LeftChannel: public LogChannel { static const char constexpr* name = "<<<"; }; @@ -191,11 +191,7 @@ public: char buf[24]; if (strftime(buf, 24, "%X", localtime(&rawTime)) == 0) buf[0] = '\0'; // empty if case strftime fails -#if defined(__APPLE__) - sstr << Id::name << " [ " << buf << " | " << dispatch_queue_get_label(dispatch_get_current_queue()) << (_term ? " ] " : ""); -#else - sstr << Id::name << " [ " << buf << " | " << t_logThreadName << (_term ? " ] " : ""); -#endif + sstr << Id::name << " [ " << buf << " | " << *(t_logThreadName.m_name.get()) << (_term ? " ] " : ""); } } ~LogOutputStream() { if (Id::verbosity <= g_logVerbosity) g_logPost(sstr.str(), Id::name); } From 0386ad0cb6f277746095c32cb6a58c1452478272 Mon Sep 17 00:00:00 2001 From: Daniel Hams Date: Fri, 14 Feb 2014 17:57:26 +0000 Subject: [PATCH 2/2] Fix formatting issue. --- libethereum/Common.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libethereum/Common.h b/libethereum/Common.h index 898bf1c58..da6786d5f 100644 --- a/libethereum/Common.h +++ b/libethereum/Common.h @@ -158,12 +158,12 @@ extern std::map g_logOverride; struct ThreadLocalLogName { - ThreadLocalLogName( std::string _name ) { m_name.reset( new std::string(_name) ); }; + ThreadLocalLogName(std::string _name) { m_name.reset(new std::string(_name)); }; boost::thread_specific_ptr m_name; }; extern ThreadLocalLogName t_logThreadName; -inline void setThreadName(char const* _n) { t_logThreadName.m_name.reset( new std::string(_n) ); } +inline void setThreadName(char const* _n) { t_logThreadName.m_name.reset(new std::string(_n)); } struct LogChannel { static const char constexpr* name = " "; static const int verbosity = 1; }; struct LeftChannel: public LogChannel { static const char constexpr* name = "<<<"; };