Browse Source

usleep moved to std impl.

cl-refactor
Gav Wood 11 years ago
parent
commit
5736e7e15c
  1. 4
      eth/main.cpp
  2. 9
      libethereum/BlockChain.h
  3. 12
      libethereum/Client.cpp
  4. 26
      libethereum/Client.h
  5. 4
      libethereum/Common.cpp
  6. 4
      libethereum/Common.h
  7. 3
      libethereum/PeerNetwork.cpp
  8. 4
      test/peer.cpp

4
eth/main.cpp

@ -20,6 +20,8 @@
* Ethereum client. * Ethereum client.
*/ */
#include <thread>
#include <chrono>
#include <fstream> #include <fstream>
#include "Defaults.h" #include "Defaults.h"
#include "Client.h" #include "Client.h"
@ -236,7 +238,7 @@ int main(int argc, char** argv)
c.stopMining(); c.stopMining();
else else
c.startMining(); c.startMining();
usleep(100000); this_thread::sleep_for(chrono::milliseconds(100000));
} }
} }

9
libethereum/BlockChain.h

@ -22,6 +22,7 @@
#pragma once #pragma once
#include "Common.h" #include "Common.h"
#include "AddressState.h"
namespace ldb = leveldb; namespace ldb = leveldb;
namespace eth namespace eth
@ -91,6 +92,10 @@ public:
h256 genesisHash() const { return m_genesisHash; } h256 genesisHash() const { return m_genesisHash; }
std::vector<std::pair<Address, AddressState>> interestQueue() { std::vector<std::pair<Address, AddressState>> ret; swap(ret, m_interestQueue); return ret; }
void pushInterest(Address _a) { m_interest[_a]++; }
void popInterest(Address _a) { if (m_interest[_a] > 1) m_interest[_a]--; else if (m_interest[_a]) m_interest.erase(_a); }
private: private:
void checkConsistency(); void checkConsistency();
@ -98,6 +103,10 @@ private:
mutable std::map<h256, BlockDetails> m_details; mutable std::map<h256, BlockDetails> m_details;
mutable std::map<h256, std::string> m_cache; mutable std::map<h256, std::string> m_cache;
/// The queue of transactions that have happened that we're interested in.
std::map<Address, int> m_interest;
std::vector<std::pair<Address, AddressState>> m_interestQueue;
ldb::DB* m_db; ldb::DB* m_db;
ldb::DB* m_detailsDB; ldb::DB* m_detailsDB;

12
libethereum/Client.cpp

@ -21,6 +21,8 @@
#include "Client.h" #include "Client.h"
#include <chrono>
#include <thread>
#include "Common.h" #include "Common.h"
#include "Defaults.h" #include "Defaults.h"
using namespace std; using namespace std;
@ -41,18 +43,18 @@ Client::Client(std::string const& _clientVersion, Address _us, std::string const
m_s.sync(m_tq); m_s.sync(m_tq);
m_changed = true; m_changed = true;
static std::string thread_name = "eth"; static const char* c_threadName = "eth";
#if defined(__APPLE__) #if defined(__APPLE__)
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
m_work = dispatch_queue_create(thread_name.c_str(), DISPATCH_QUEUE_SERIAL); m_work = dispatch_queue_create(c_threadName, DISPATCH_QUEUE_SERIAL);
}); });
dispatch_async(m_work, ^{ dispatch_async(m_work, ^{
#else #else
m_work = new thread([&](){ m_work = new thread([&](){
setThreadName(thread_name); setThreadName(c_threadName);
#endif #endif
while (m_workState != Deleting) work(); m_workState = Deleted; while (m_workState != Deleting) work(); m_workState = Deleted;
@ -64,7 +66,7 @@ Client::~Client()
if (m_workState == Active) if (m_workState == Active)
m_workState = Deleting; m_workState = Deleting;
while (m_workState != Deleted) while (m_workState != Deleted)
usleep(10000); this_thread::sleep_for(chrono::milliseconds(10000));
} }
void Client::startNetwork(short _listenPort, std::string const& _seedHost, short _port, NodeMode _mode, unsigned _peers, string const& _publicIP, bool _upnp) void Client::startNetwork(short _listenPort, std::string const& _seedHost, short _port, NodeMode _mode, unsigned _peers, string const& _publicIP, bool _upnp)
@ -158,7 +160,7 @@ void Client::work()
} }
} }
else else
usleep(100000); this_thread::sleep_for(chrono::milliseconds(100000));
} }
void Client::lock() void Client::lock()

26
libethereum/Client.h

@ -40,6 +40,18 @@ struct MineProgress
uint current; uint current;
}; };
class Client;
class ClientGuard
{
public:
inline ClientGuard(Client* _c);
inline ~ClientGuard();
private:
Client* m_client;
};
class Client class Client
{ {
public: public:
@ -56,10 +68,10 @@ public:
void setInterest(Address _dest); void setInterest(Address _dest);
/// @returns incoming minable transactions that we wanted to be notified of. Clears the queue. /// @returns incoming minable transactions that we wanted to be notified of. Clears the queue.
Transactions pendingQueue(); Transactions pendingQueue() { ClientGuard g(this); return m_tq.interestQueue(); }
/// @returns alterations in state of a mined block that we wanted to be notified of. Clears the queue. /// @returns alterations in state of a mined block that we wanted to be notified of. Clears the queue.
std::vector<std::pair<Address, AddressState>> minedQueue(); std::vector<std::pair<Address, AddressState>> minedQueue() { ClientGuard g(this); return m_bc.interestQueue(); }
// Not yet - probably best as using some sort of signals implementation. // Not yet - probably best as using some sort of signals implementation.
/// Calls @a _f when a valid transaction is received that involves @a _dest and once per such transaction. /// Calls @a _f when a valid transaction is received that involves @a _dest and once per such transaction.
@ -135,4 +147,14 @@ private:
mutable bool m_changed; mutable bool m_changed;
}; };
inline ClientGuard::ClientGuard(Client* _c): m_client(_c)
{
m_client->lock();
}
inline ClientGuard::~ClientGuard()
{
m_client->unlock();
}
} }

4
libethereum/Common.cpp

@ -43,8 +43,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__) #if !defined(__APPLE__)
thread_local std::string eth::t_logThreadName = "???"; thread_local char const* eth::t_logThreadName = "???";
static std::string g_mainThreadName = (eth::t_logThreadName = "main"); static char const* g_mainThreadName = (eth::t_logThreadName = "main");
#endif #endif
void eth::simpleDebugOut(std::string const& _s, char const*) void eth::simpleDebugOut(std::string const& _s, char const*)

4
libethereum/Common.h

@ -158,8 +158,8 @@ public:
extern std::map<std::type_info const*, bool> g_logOverride; extern std::map<std::type_info const*, bool> g_logOverride;
#if !defined(__APPLE__) #if !defined(__APPLE__)
extern thread_local std::string t_logThreadName; extern thread_local char const* t_logThreadName;
inline void setThreadName(std::string const& _n) { t_logThreadName = _n; } inline void setThreadName(char const* _n) { t_logThreadName = _n; }
#endif #endif
struct LogChannel { static const char constexpr* name = " "; static const int verbosity = 1; }; struct LogChannel { static const char constexpr* name = " "; static const int verbosity = 1; };

3
libethereum/PeerNetwork.cpp

@ -30,6 +30,7 @@
#endif #endif
#include <chrono> #include <chrono>
#include <thread>
#include "Exceptions.h" #include "Exceptions.h"
#include "Common.h" #include "Common.h"
#include "BlockChain.h" #include "BlockChain.h"
@ -919,7 +920,7 @@ bool PeerServer::process(BlockChain& _bc, TransactionQueue& _tq, Overlay& _o)
std::vector<PeerInfo> PeerServer::peers() const std::vector<PeerInfo> PeerServer::peers() const
{ {
const_cast<PeerServer*>(this)->pingAll(); const_cast<PeerServer*>(this)->pingAll();
usleep(200000); this_thread::sleep_for(chrono::milliseconds(200000));
std::vector<PeerInfo> ret; std::vector<PeerInfo> ret;
for (auto& i: m_peers) for (auto& i: m_peers)
if (auto j = i.lock()) if (auto j = i.lock())

4
test/peer.cpp

@ -20,6 +20,8 @@
* Peer Network test functions. * Peer Network test functions.
*/ */
#include <chrono>
#include <thread>
#include <BlockChain.h> #include <BlockChain.h>
#include <PeerNetwork.h> #include <PeerNetwork.h>
using namespace std; using namespace std;
@ -53,7 +55,7 @@ int peerTest(int argc, char** argv)
for (int i = 0; ; ++i) for (int i = 0; ; ++i)
{ {
usleep(100000); this_thread::sleep_for(chrono::milliseconds(100000));
pn.process(ch); pn.process(ch);
if (!(i % 10)) if (!(i % 10))
pn.pingAll(); pn.pingAll();

Loading…
Cancel
Save