Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ThreadDumpAddresses with a timer. #1787

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bitcoin-qt.pro
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \
src/rpcwallet.cpp \
src/rpcblockchain.cpp \
src/rpcrawtransaction.cpp \
src/timer.cpp \
src/qt/overviewpage.cpp \
src/qt/csvmodelwriter.cpp \
src/crypter.cpp \
Expand Down
1 change: 1 addition & 0 deletions src/makefile.linux-mingw
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ OBJS= \
obj/rpcwallet.o \
obj/rpcblockchain.o \
obj/rpcrawtransaction.o \
obj/timer.o \
obj/script.o \
obj/sync.o \
obj/util.o \
Expand Down
1 change: 1 addition & 0 deletions src/makefile.mingw
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ OBJS= \
obj/rpcwallet.o \
obj/rpcblockchain.o \
obj/rpcrawtransaction.o \
obj/timer.o \
obj/script.o \
obj/sync.o \
obj/util.o \
Expand Down
1 change: 1 addition & 0 deletions src/makefile.osx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ OBJS= \
obj/rpcwallet.o \
obj/rpcblockchain.o \
obj/rpcrawtransaction.o \
obj/timer.o \
obj/script.o \
obj/sync.o \
obj/util.o \
Expand Down
1 change: 1 addition & 0 deletions src/makefile.unix
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ OBJS= \
obj/rpcwallet.o \
obj/rpcblockchain.o \
obj/rpcrawtransaction.o \
obj/timer.o \
obj/script.o \
obj/sync.o \
obj/util.o \
Expand Down
40 changes: 13 additions & 27 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "strlcpy.h"
#include "addrman.h"
#include "ui_interface.h"
#include "timer.h"

#ifdef WIN32
#include <string.h>
Expand Down Expand Up @@ -66,6 +67,7 @@ map<CInv, CDataStream> mapRelay;
deque<pair<int64, CInv> > vRelayExpiration;
CCriticalSection cs_mapRelay;
map<CInv, int64> mapAlreadyAskedFor;
static CTimerList timerList;

static deque<string> vOneShots;
CCriticalSection cs_vOneShots;
Expand Down Expand Up @@ -980,6 +982,8 @@ void ThreadSocketHandler2(void* parg)
pnode->Release();
}

timerList.runTimers();

Sleep(10);
}
}
Expand Down Expand Up @@ -1313,34 +1317,17 @@ void DumpAddresses()
addrman.size(), GetTimeMillis() - nStart);
}

void ThreadDumpAddress2(void* parg)
static void DumpAddressTimer(CTimer *t, void *p)
{
vnThreadsRunning[THREAD_DUMPADDRESS]++;
while (!fShutdown)
{
DumpAddresses();
vnThreadsRunning[THREAD_DUMPADDRESS]--;
Sleep(100000);
vnThreadsRunning[THREAD_DUMPADDRESS]++;
}
vnThreadsRunning[THREAD_DUMPADDRESS]--;
DumpAddresses();

t->expire_time += 100000;
}

void ThreadDumpAddress(void* parg)
static bool StartDumpAddressTimer()
{
IMPLEMENT_RANDOMIZE_STACK(ThreadDumpAddress(parg));

// Make this thread recognisable as the address dumping thread
RenameThread("bitcoin-adrdump");

try
{
ThreadDumpAddress2(parg);
}
catch (std::exception& e) {
PrintException(&e, "ThreadDumpAddress()");
}
printf("ThreadDumpAddress exited\n");
CTimer t = { NULL, GetTime() + 100000, DumpAddressTimer };
return timerList.addTimer(t);
}

void ThreadOpenConnections(void* parg)
Expand Down Expand Up @@ -1939,8 +1926,8 @@ void StartNode(void* parg)
printf("Error: NewThread(ThreadMessageHandler) failed\n");

// Dump network addresses
if (!NewThread(ThreadDumpAddress, NULL))
printf("Error; NewThread(ThreadDumpAddress) failed\n");
if (!StartDumpAddressTimer())
printf("Error; StartDumpAddressTimer() failed\n");

// Generate coins in the background
GenerateBitcoins(GetBoolArg("-gen", false), pwalletMain);
Expand Down Expand Up @@ -1977,7 +1964,6 @@ bool StopNode()
#endif
if (vnThreadsRunning[THREAD_DNSSEED] > 0) printf("ThreadDNSAddressSeed still running\n");
if (vnThreadsRunning[THREAD_ADDEDCONNECTIONS] > 0) printf("ThreadOpenAddedConnections still running\n");
if (vnThreadsRunning[THREAD_DUMPADDRESS] > 0) printf("ThreadDumpAddresses still running\n");
while (vnThreadsRunning[THREAD_MESSAGEHANDLER] > 0 || vnThreadsRunning[THREAD_RPCHANDLER] > 0)
Sleep(20);
Sleep(50);
Expand Down
1 change: 0 additions & 1 deletion src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ enum threadId
THREAD_UPNP,
THREAD_DNSSEED,
THREAD_ADDEDCONNECTIONS,
THREAD_DUMPADDRESS,
THREAD_RPCHANDLER,

THREAD_MAX
Expand Down
56 changes: 56 additions & 0 deletions src/test/timer_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <boost/test/unit_test.hpp>

#include "timer.h"

BOOST_AUTO_TEST_SUITE(timer_tests)

static void actor(CTimer *t, void *p)
{
}

static void actor2(CTimer *t, void *p)
{
t->expire_time += 10;
}

BOOST_AUTO_TEST_CASE(timer_basics)
{
CTimerList cl;
BOOST_CHECK(cl.size() == 0);

int64 nNow = GetTime();

CTimer t1 = { (char *) "t1", nNow - 1, actor };
CTimer t2 = { (char *) "t2", nNow, actor };
CTimer t3 = { (char *) "t3", nNow + 1, actor };
CTimer t4 = { (char *) "t4", nNow, actor2 };

BOOST_CHECK(cl.addTimer(t1) == true);
BOOST_CHECK(cl.addTimer(t2) == true);
BOOST_CHECK(cl.addTimer(t3) == true);

BOOST_CHECK(cl.size() == 3);
BOOST_CHECK(cl.nextExpire() == (nNow - 1));

cl.clear();
BOOST_CHECK(cl.size() == 0);

BOOST_CHECK(cl.addTimer(t1) == true);
BOOST_CHECK(cl.addTimer(t2) == true);
BOOST_CHECK(cl.addTimer(t3) == true);

BOOST_CHECK(cl.size() == 3);
BOOST_CHECK(cl.nextExpire() == (nNow - 1));

int nRun = cl.runTimers();
BOOST_CHECK(nRun == 2);
BOOST_CHECK(cl.size() == 1);

BOOST_CHECK(cl.addTimer(t4) == true);
nRun = cl.runTimers();
BOOST_CHECK(nRun == 1);
BOOST_CHECK(cl.size() == 2);
}

BOOST_AUTO_TEST_SUITE_END()

57 changes: 57 additions & 0 deletions src/timer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2010 Satoshi Nakamoto
// Copyright (c) 2009-2012 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "timer.h"

using namespace std;


bool CTimerList::addTimer(CTimer t)
{
if (fShutdown)
return false;

if (mapTimers.count(t.expire_time))
return false;

mapTimers[t.expire_time] = t;
return true;
}

unsigned int CTimerList::runTimers()
{
int64 nNow = GetTime();
unsigned int nRun = 0;

map<int64, CTimer>::iterator miLast = mapTimers.end();
vector<CTimer> vAdd;

// execute any expired timers
for (map<int64, CTimer>::iterator mi = mapTimers.begin();
mi != mapTimers.end(); ++mi) {
CTimer& t = (*mi).second;
if (t.expire_time > nNow)
break;

nRun++;
miLast = mi;

t.actor(&t, t.privdata);
if (t.expire_time > nNow)
vAdd.push_back(t);
}

// delete executed timers
if (miLast != mapTimers.end()) {
miLast++;
mapTimers.erase(mapTimers.begin(), miLast);
}

// re-add timers with new expired times
for (unsigned int i = 0; i < vAdd.size(); i++)
addTimer(vAdd[i]);

return nRun;
}
38 changes: 38 additions & 0 deletions src/timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2010 Satoshi Nakamoto
// Copyright (c) 2009-2012 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef __BITCOIN_TIMER_H__
#define __BITCOIN_TIMER_H__


#include "util.h"


class CTimer {
public:
void *privdata;
int64 expire_time;

void (*actor)(CTimer *t, void *p);
};


class CTimerList {
private:
std::map<int64, CTimer> mapTimers;

public:
unsigned int runTimers();
bool addTimer(CTimer t);
int64 nextExpire() {
std::map<int64, CTimer>::iterator mi = mapTimers.begin();
return (*mi).first;
}

unsigned int size() { return mapTimers.size(); }
void clear() { mapTimers.clear(); }
};


#endif // __BITCOIN_TIMER_H__