Skip to content

Commit

Permalink
rwlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed Dec 20, 2011
1 parent 88c4c26 commit d0d2428
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
4 changes: 2 additions & 2 deletions db.h
Expand Up @@ -85,7 +85,7 @@ class CAddrDb {
IMPLEMENT_SERIALIZE (({
int nVersion = 0;
READWRITE(nVersion);
CRITICAL_BLOCK(cs) {
SHARED_CRITICAL_BLOCK(cs) {
if (fWrite) {
CAddrDb *db = const_cast<CAddrDb*>(this);
int nOur = ourId.size();
Expand Down Expand Up @@ -158,7 +158,7 @@ class CAddrDb {
return Get_(ip, wait);
}
void GetIPs(std::set<CIP>& ips, int max, bool fOnlyIPv4 = true) {
CRITICAL_BLOCK(cs)
SHARED_CRITICAL_BLOCK(cs)
GetIPs_(ips, max, fOnlyIPv4);
}
};
16 changes: 10 additions & 6 deletions main.cpp
Expand Up @@ -3,6 +3,8 @@
#include "bitcoin.h"
#include "db.h"

#define NTHREADS 100

using namespace std;

extern "C" {
Expand All @@ -17,7 +19,9 @@ extern "C" void* ThreadCrawler(void* data) {
CIPPort ip;
int wait = 5;
if (!db.Get(ip, wait)) {
Sleep(wait*1000);
wait *= 1000;
wait += rand() % (500 * NTHREADS);
Sleep(wait);
continue;
}
int ban = 0;
Expand Down Expand Up @@ -75,13 +79,13 @@ int main(void) {
for (vector<CIP>::iterator it = ips.begin(); it != ips.end(); it++) {
db.Add(CIPPort(*it, 8333));
}
pthread_t thread[NTHREADS];
for (int i=0; i<NTHREADS-2; i++) {
pthread_t thread[NTHREADS+2];
for (int i=0; i<NTHREADS; i++) {
pthread_create(&thread[i], NULL, ThreadCrawler, NULL);
}
pthread_create(&thread[NTHREADS-2], NULL, ThreadDumper, NULL);
pthread_create(&thread[NTHREADS-1], NULL, ThreadDNS, NULL);
for (int i=0; i<NTHREADS; i++) {
pthread_create(&thread[NTHREADS], NULL, ThreadDumper, NULL);
pthread_create(&thread[NTHREADS+1], NULL, ThreadDNS, NULL);
for (int i=0; i<NTHREADS+2; i++) {
void* res;
pthread_join(thread[i], &res);
}
Expand Down
21 changes: 15 additions & 6 deletions util.h
Expand Up @@ -45,12 +45,18 @@ inline int myclosesocket(SOCKET& hSocket)
class CCriticalSection
{
protected:
pthread_mutex_t mutex;
pthread_rwlock_t mutex;
public:
explicit CCriticalSection() { pthread_mutex_init(&mutex, NULL); }
~CCriticalSection() { pthread_mutex_destroy(&mutex); }
void Enter() { pthread_mutex_lock(&mutex); }
void Leave() { pthread_mutex_unlock(&mutex); }
explicit CCriticalSection() { pthread_rwlock_init(&mutex, NULL); }
~CCriticalSection() { pthread_rwlock_destroy(&mutex); }
void Enter(bool fShared = false) {
if (fShared) {
pthread_rwlock_rdlock(&mutex);
} else {
pthread_rwlock_wrlock(&mutex);
}
}
void Leave() { pthread_rwlock_unlock(&mutex); }
};

// Automatically leave critical section when leaving block, needed for exception safety
Expand All @@ -59,14 +65,17 @@ class CCriticalBlock
protected:
CCriticalSection* pcs;
public:
CCriticalBlock(CCriticalSection& cs) : pcs(&cs) { pcs->Enter(); }
CCriticalBlock(CCriticalSection& cs, bool fShared = false) : pcs(&cs) { pcs->Enter(fShared); }
operator bool() const { return true; }
~CCriticalBlock() { pcs->Leave(); }
};

#define CRITICAL_BLOCK(cs) \
if (CCriticalBlock criticalblock = CCriticalBlock(cs))

#define SHARED_CRITICAL_BLOCK(cs) \
if (CCriticalBlock criticalblock = CCriticalBlock(cs, true))

template<typename T1> inline uint256 Hash(const T1 pbegin, const T1 pend)
{
static unsigned char pblank[1];
Expand Down

0 comments on commit d0d2428

Please sign in to comment.