2011-12-16 16:56:36 +01:00
|
|
|
#ifndef _UTIL_H_
|
|
|
|
#define _UTIL_H_ 1
|
|
|
|
|
2011-12-16 18:37:05 +01:00
|
|
|
#include <pthread.h>
|
2011-12-19 23:18:06 +01:00
|
|
|
#include <errno.h>
|
2011-12-20 05:20:50 +01:00
|
|
|
#include <openssl/sha.h>
|
|
|
|
|
|
|
|
#include "uint256.h"
|
2011-12-16 18:37:05 +01:00
|
|
|
|
2011-12-16 16:56:36 +01:00
|
|
|
#define loop for (;;)
|
|
|
|
#define BEGIN(a) ((char*)&(a))
|
|
|
|
#define END(a) ((char*)&((&(a))[1]))
|
|
|
|
#define UBEGIN(a) ((unsigned char*)&(a))
|
|
|
|
#define UEND(a) ((unsigned char*)&((&(a))[1]))
|
|
|
|
#define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
|
|
|
|
|
|
|
|
#define WSAGetLastError() errno
|
|
|
|
#define WSAEINVAL EINVAL
|
|
|
|
#define WSAEALREADY EALREADY
|
|
|
|
#define WSAEWOULDBLOCK EWOULDBLOCK
|
|
|
|
#define WSAEMSGSIZE EMSGSIZE
|
|
|
|
#define WSAEINTR EINTR
|
|
|
|
#define WSAEINPROGRESS EINPROGRESS
|
|
|
|
#define WSAEADDRINUSE EADDRINUSE
|
|
|
|
#define WSAENOTSOCK EBADF
|
|
|
|
#define INVALID_SOCKET (SOCKET)(~0)
|
|
|
|
#define SOCKET_ERROR -1
|
|
|
|
|
|
|
|
inline int myclosesocket(SOCKET& hSocket)
|
|
|
|
{
|
|
|
|
if (hSocket == INVALID_SOCKET)
|
|
|
|
return WSAENOTSOCK;
|
|
|
|
#ifdef WIN32
|
|
|
|
int ret = closesocket(hSocket);
|
|
|
|
#else
|
|
|
|
int ret = close(hSocket);
|
|
|
|
#endif
|
|
|
|
hSocket = INVALID_SOCKET;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#define closesocket(s) myclosesocket(s)
|
|
|
|
|
2011-12-16 18:37:05 +01:00
|
|
|
|
|
|
|
// Wrapper to automatically initialize mutex
|
|
|
|
class CCriticalSection
|
|
|
|
{
|
|
|
|
protected:
|
2011-12-20 09:31:28 +01:00
|
|
|
pthread_rwlock_t mutex;
|
2011-12-16 18:37:05 +01:00
|
|
|
public:
|
2011-12-20 09:31:28 +01:00
|
|
|
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); }
|
2011-12-16 18:37:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Automatically leave critical section when leaving block, needed for exception safety
|
|
|
|
class CCriticalBlock
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
CCriticalSection* pcs;
|
|
|
|
public:
|
2011-12-20 09:31:28 +01:00
|
|
|
CCriticalBlock(CCriticalSection& cs, bool fShared = false) : pcs(&cs) { pcs->Enter(fShared); }
|
2011-12-16 18:37:05 +01:00
|
|
|
operator bool() const { return true; }
|
|
|
|
~CCriticalBlock() { pcs->Leave(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
#define CRITICAL_BLOCK(cs) \
|
|
|
|
if (CCriticalBlock criticalblock = CCriticalBlock(cs))
|
|
|
|
|
2011-12-20 09:31:28 +01:00
|
|
|
#define SHARED_CRITICAL_BLOCK(cs) \
|
|
|
|
if (CCriticalBlock criticalblock = CCriticalBlock(cs, true))
|
|
|
|
|
2011-12-20 05:20:50 +01:00
|
|
|
template<typename T1> inline uint256 Hash(const T1 pbegin, const T1 pend)
|
|
|
|
{
|
|
|
|
static unsigned char pblank[1];
|
|
|
|
uint256 hash1;
|
|
|
|
SHA256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), (unsigned char*)&hash1);
|
|
|
|
uint256 hash2;
|
|
|
|
SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
|
|
|
|
return hash2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void static inline Sleep(int nMilliSec) {
|
|
|
|
struct timespec wa;
|
|
|
|
wa.tv_sec = nMilliSec/1000;
|
|
|
|
wa.tv_nsec = (nMilliSec % 1000) * 1000000;
|
|
|
|
nanosleep(&wa, NULL);
|
|
|
|
}
|
|
|
|
|
2011-12-16 16:56:36 +01:00
|
|
|
#endif
|