Browse Source

Merge pull request #53 from danielhams/osxthreadlocal

Change logging "name" to be class instance thread specific ptr.
cl-refactor
Gav Wood 11 years ago
parent
commit
31b9e84eec
  1. 1
      libethereum/CMakeLists.txt
  2. 9
      libethereum/Client.cpp
  3. 4
      libethereum/Client.h
  4. 5
      libethereum/Common.cpp
  5. 24
      libethereum/Common.h

1
libethereum/CMakeLists.txt

@ -35,6 +35,7 @@ else ()
target_link_libraries(ethereum ${CRYPTOPP_LIBRARIES}) target_link_libraries(ethereum ${CRYPTOPP_LIBRARIES})
target_link_libraries(ethereum boost_system) target_link_libraries(ethereum boost_system)
target_link_libraries(ethereum boost_filesystem) target_link_libraries(ethereum boost_filesystem)
target_link_libraries(ethereum boost_thread)
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
target_link_libraries(ethereum ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries(ethereum ${CMAKE_THREAD_LIBS_INIT})
endif () endif ()

9
libethereum/Client.cpp

@ -46,17 +46,8 @@ Client::Client(std::string const& _clientVersion, Address _us, std::string const
static const char* c_threadName = "eth"; 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([&](){ m_work = new thread([&](){
setThreadName(c_threadName); setThreadName(c_threadName);
#endif
while (m_workState != Deleting) work(); m_workState = Deleted; while (m_workState != Deleting) work(); m_workState = Deleted;
}); });

4
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). 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. 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. std::thread* m_work; ///< The work thread.
#endif
std::mutex m_lock; std::mutex m_lock;
enum { Active = 0, Deleting, Deleted } m_workState = Active; enum { Active = 0, Deleting, Deleted } m_workState = Active;

5
libethereum/Common.cpp

@ -44,10 +44,7 @@ using namespace eth;
int eth::g_logVerbosity = 8; int eth::g_logVerbosity = 8;
map<type_info const*, bool> eth::g_logOverride; map<type_info const*, bool> eth::g_logOverride;
#if !defined(__APPLE__) ThreadLocalLogName eth::t_logThreadName("main");
thread_local char const* eth::t_logThreadName = "???";
static char const* g_mainThreadName = (eth::t_logThreadName = "main");
#endif
void eth::simpleDebugOut(std::string const& _s, char const*) void eth::simpleDebugOut(std::string const& _s, char const*)
{ {

24
libethereum/Common.h

@ -39,13 +39,9 @@
#include <cstdint> #include <cstdint>
#include <type_traits> #include <type_traits>
#include <boost/multiprecision/cpp_int.hpp> #include <boost/multiprecision/cpp_int.hpp>
#include <boost/thread.hpp>
#include "vector_ref.h" #include "vector_ref.h"
// OSX likes GCD whichs runs threads within queues
#if defined(__APPLE__)
#include <dispatch/dispatch.h>
#endif
namespace eth namespace eth
{ {
@ -160,10 +156,14 @@ public:
extern std::map<std::type_info const*, bool> g_logOverride; extern std::map<std::type_info const*, bool> g_logOverride;
#if !defined(__APPLE__) struct ThreadLocalLogName
extern thread_local char const* t_logThreadName; {
inline void setThreadName(char const* _n) { t_logThreadName = _n; } ThreadLocalLogName(std::string _name) { m_name.reset(new std::string(_name)); };
#endif boost::thread_specific_ptr<std::string> 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 LogChannel { static const char constexpr* name = " "; static const int verbosity = 1; };
struct LeftChannel: public LogChannel { static const char constexpr* name = "<<<"; }; struct LeftChannel: public LogChannel { static const char constexpr* name = "<<<"; };
@ -191,11 +191,7 @@ public:
char buf[24]; char buf[24];
if (strftime(buf, 24, "%X", localtime(&rawTime)) == 0) if (strftime(buf, 24, "%X", localtime(&rawTime)) == 0)
buf[0] = '\0'; // empty if case strftime fails buf[0] = '\0'; // empty if case strftime fails
#if defined(__APPLE__) sstr << Id::name << " [ " << buf << " | " << *(t_logThreadName.m_name.get()) << (_term ? " ] " : "");
sstr << Id::name << " [ " << buf << " | " << dispatch_queue_get_label(dispatch_get_current_queue()) << (_term ? " ] " : "");
#else
sstr << Id::name << " [ " << buf << " | " << t_logThreadName << (_term ? " ] " : "");
#endif
} }
} }
~LogOutputStream() { if (Id::verbosity <= g_logVerbosity) g_logPost(sstr.str(), Id::name); } ~LogOutputStream() { if (Id::verbosity <= g_logVerbosity) g_logPost(sstr.str(), Id::name); }

Loading…
Cancel
Save