Browse Source

CCriticalSection using wxWidgets instead of Windows OS calls

git-svn-id: https://bitcoin.svn.sourceforge.net/svnroot/bitcoin/trunk@19 1a98c847-1fd6-4fd8-948a-caf3550aa51b
miguelfreitas
s_nakamoto 15 years ago
parent
commit
dc73b326f9
  1. 6
      net.h
  2. 20
      util.cpp
  3. 31
      util.h

6
net.h

@ -604,7 +604,7 @@ public:
void BeginMessage(const char* pszCommand) void BeginMessage(const char* pszCommand)
{ {
EnterCriticalSection(&cs_vSend); cs_vSend.Enter();
if (nPushPos != -1) if (nPushPos != -1)
AbortMessage(); AbortMessage();
nPushPos = vSend.size(); nPushPos = vSend.size();
@ -618,7 +618,7 @@ public:
return; return;
vSend.resize(nPushPos); vSend.resize(nPushPos);
nPushPos = -1; nPushPos = -1;
LeaveCriticalSection(&cs_vSend); cs_vSend.Leave();
printf("(aborted)\n"); printf("(aborted)\n");
} }
@ -643,7 +643,7 @@ public:
printf("\n"); printf("\n");
nPushPos = -1; nPushPos = -1;
LeaveCriticalSection(&cs_vSend); cs_vSend.Leave();
} }
void EndMessageAbortIfEmpty() void EndMessageAbortIfEmpty()

20
util.cpp

@ -13,14 +13,14 @@ bool fPrintToConsole = false;
// Init openssl library multithreading support // Init openssl library multithreading support
static HANDLE* lock_cs; static wxMutex** ppmutexOpenSSL;
void win32_locking_callback(int mode, int type, const char* file, int line) void win32_locking_callback(int mode, int i, const char* file, int line)
{ {
if (mode & CRYPTO_LOCK) if (mode & CRYPTO_LOCK)
WaitForSingleObject(lock_cs[type], INFINITE); ppmutexOpenSSL[i]->Lock();
else else
ReleaseMutex(lock_cs[type]); ppmutexOpenSSL[i]->Unlock();
} }
// Init // Init
@ -30,9 +30,9 @@ public:
CInit() CInit()
{ {
// Init openssl library multithreading support // Init openssl library multithreading support
lock_cs = (HANDLE*)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE)); ppmutexOpenSSL = (wxMutex**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(wxMutex*));
for (int i = 0; i < CRYPTO_num_locks(); i++) for (int i = 0; i < CRYPTO_num_locks(); i++)
lock_cs[i] = CreateMutex(NULL,FALSE,NULL); ppmutexOpenSSL[i] = new wxMutex();
CRYPTO_set_locking_callback(win32_locking_callback); CRYPTO_set_locking_callback(win32_locking_callback);
// Seed random number generator with screen scrape and other hardware sources // Seed random number generator with screen scrape and other hardware sources
@ -46,8 +46,8 @@ public:
// Shutdown openssl library multithreading support // Shutdown openssl library multithreading support
CRYPTO_set_locking_callback(NULL); CRYPTO_set_locking_callback(NULL);
for (int i =0 ; i < CRYPTO_num_locks(); i++) for (int i =0 ; i < CRYPTO_num_locks(); i++)
CloseHandle(lock_cs[i]); delete ppmutexOpenSSL[i];
OPENSSL_free(lock_cs); OPENSSL_free(ppmutexOpenSSL);
} }
} }
instance_of_cinit; instance_of_cinit;
@ -55,6 +55,10 @@ instance_of_cinit;
void RandAddSeed() void RandAddSeed()
{ {
// Seed with CPU performance counter // Seed with CPU performance counter

31
util.h

@ -106,28 +106,38 @@ void AddTimeData(unsigned int ip, int64 nTime);
// Could use wxCriticalSection for portability, but it doesn't support TryEnterCriticalSection // Could use wxCriticalSection for portability, but it doesn't support TryEnterCriticalSection
class CCriticalSection class CCriticalSection
{ {
#ifdef __WXMSW__
protected: protected:
CRITICAL_SECTION cs; CRITICAL_SECTION cs;
public: public:
char* pszFile;
int nLine;
explicit CCriticalSection() { InitializeCriticalSection(&cs); } explicit CCriticalSection() { InitializeCriticalSection(&cs); }
~CCriticalSection() { DeleteCriticalSection(&cs); } ~CCriticalSection() { DeleteCriticalSection(&cs); }
void Enter() { EnterCriticalSection(&cs); } void Enter() { EnterCriticalSection(&cs); }
void Leave() { LeaveCriticalSection(&cs); } void Leave() { LeaveCriticalSection(&cs); }
bool TryEnter() { return TryEnterCriticalSection(&cs); } bool TryEnter() { return TryEnterCriticalSection(&cs); }
CRITICAL_SECTION* operator&() { return &cs; } #else
protected:
wxMutex mutex;
public:
explicit CCriticalSection() { }
~CCriticalSection() { }
void Enter() { mutex.Lock(); }
void Leave() { mutex.Unlock(); }
bool TryEnter() { return mutex.TryLock() == wxMUTEX_NO_ERROR; }
#endif
public:
char* pszFile;
int nLine;
}; };
// Automatically leave critical section when leaving block, needed for exception safety // Automatically leave critical section when leaving block, needed for exception safety
class CCriticalBlock class CCriticalBlock
{ {
protected: protected:
CRITICAL_SECTION* pcs; CCriticalSection* pcs;
public: public:
CCriticalBlock(CRITICAL_SECTION& csIn) { pcs = &csIn; EnterCriticalSection(pcs); } CCriticalBlock(CCriticalSection& csIn) { pcs = &csIn; pcs->Enter(); }
CCriticalBlock(CCriticalSection& csIn) { pcs = &csIn; EnterCriticalSection(pcs); } ~CCriticalBlock() { pcs->Leave(); }
~CCriticalBlock() { LeaveCriticalSection(pcs); }
}; };
// WARNING: This will catch continue and break! // WARNING: This will catch continue and break!
@ -141,11 +151,10 @@ public:
class CTryCriticalBlock class CTryCriticalBlock
{ {
protected: protected:
CRITICAL_SECTION* pcs; CCriticalSection* pcs;
public: public:
CTryCriticalBlock(CRITICAL_SECTION& csIn) { pcs = (TryEnterCriticalSection(&csIn) ? &csIn : NULL); } CTryCriticalBlock(CCriticalSection& csIn) { pcs = (csIn.TryEnter() ? &csIn : NULL); }
CTryCriticalBlock(CCriticalSection& csIn) { pcs = (TryEnterCriticalSection(&csIn) ? &csIn : NULL); } ~CTryCriticalBlock() { if (pcs) pcs->Leave(); }
~CTryCriticalBlock() { if (pcs) LeaveCriticalSection(pcs); }
bool Entered() { return pcs != NULL; } bool Entered() { return pcs != NULL; }
}; };

Loading…
Cancel
Save