#ifndef BITCOIN_ALLOCATORS_H
#define BITCOIN_ALLOCATORS_H
-#include <string.h>
+#include <openssl/crypto.h> // for OPENSSL_cleanse()
+
+#include <cstring>
#include <string>
-#include <boost/thread/mutex.hpp>
+#include <mutex>
+#include <cassert>
#include <map>
-#include <openssl/crypto.h> // for OPENSSL_cleanse()
#ifdef WIN32
#ifdef _WIN32_WINNT
// the pagefile except in rare circumstances where memory is extremely low.
#else
#include <sys/mman.h>
-#include <limits.h> // for PAGESIZE
+#include <climits> // for PAGESIZE
#include <unistd.h> // for sysconf
#endif
// For all pages in affected range, increase lock count
void LockRange(void *p, size_t size)
{
- boost::mutex::scoped_lock lock(mutex);
+ std::scoped_lock lock(mutex);
if(!size) return;
const size_t base_addr = reinterpret_cast<size_t>(p);
const size_t start_page = base_addr & page_mask;
// For all pages in affected range, decrease lock count
void UnlockRange(void *p, size_t size)
{
- boost::mutex::scoped_lock lock(mutex);
+ std::scoped_lock lock(mutex);
if(!size) return;
const size_t base_addr = reinterpret_cast<size_t>(p);
const size_t start_page = base_addr & page_mask;
// Get number of locked pages for diagnostics
int GetLockedPageCount()
{
- boost::mutex::scoped_lock lock(mutex);
+ std::scoped_lock lock(mutex);
return histogram.size();
}
private:
Locker locker;
- boost::mutex mutex;
+ std::mutex mutex;
size_t page_size, page_mask;
// map of page base address to lock count
typedef std::map<size_t,int> Histogram;
#ifndef BITCOIN_SYNC_H
#define BITCOIN_SYNC_H
-#include <boost/thread/mutex.hpp>
-#include <boost/thread/recursive_mutex.hpp>
-#include <boost/thread/locks.hpp>
-#include <boost/thread/condition_variable.hpp>
-
-
-
+#include <mutex>
+#include <condition_variable>
/** Wrapped boost mutex: supports recursive locking, but no waiting */
-typedef boost::recursive_mutex CCriticalSection;
+typedef std::recursive_mutex CCriticalSection;
/** Wrapped boost mutex: supports waiting but not recursive locking */
-typedef boost::mutex CWaitableCriticalSection;
+typedef std::mutex CWaitableCriticalSection;
#ifdef DEBUG_LOCKORDER
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
void PrintLockContention(const char* pszName, const char* pszFile, int nLine);
#endif
-/** Wrapper around boost::unique_lock<Mutex> */
+/** Wrapper around std::unique_lock<Mutex> */
template<typename Mutex>
class CMutexLock
{
private:
- boost::unique_lock<Mutex> lock;
+ std::unique_lock<Mutex> lock;
public:
void Enter(const char* pszName, const char* pszFile, int nLine)
return lock.owns_lock();
}
- CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) : lock(mutexIn, boost::defer_lock)
+ CMutexLock(Mutex& mutexIn, const char* pszName, const char* pszFile, int nLine, bool fTry = false) : lock(mutexIn, std::defer_lock)
{
if (fTry)
TryEnter(pszName, pszFile, nLine);
LeaveCritical();
}
- operator bool()
+ operator bool() const
{
return lock.owns_lock();
}
- boost::unique_lock<Mutex> &GetLock()
+ std::unique_lock<Mutex> &GetLock()
{
return lock;
}
class CSemaphore
{
private:
- boost::condition_variable condition;
- boost::mutex mutex;
+ std::condition_variable condition;
+ std::mutex mutex;
int value;
public:
- CSemaphore(int init) : value(init) {}
+ explicit CSemaphore(int init) : value(init) {}
void wait() {
- boost::unique_lock<boost::mutex> lock(mutex);
- while (value < 1) {
- condition.wait(lock);
- }
+ //std::unique_lock<std::mutex> lock(mutex);
+ //while (value < 1) {
+ // condition.wait(lock);
+ //}
+ //value--;
+ std::unique_lock<std::mutex> lock(mutex);
+ condition.wait(lock, [&]() { return value >= 1; });
value--;
}
bool try_wait() {
- boost::unique_lock<boost::mutex> lock(mutex);
+ std::unique_lock<std::mutex> lock(mutex);
if (value < 1)
return false;
value--;
void post() {
{
- boost::unique_lock<boost::mutex> lock(mutex);
+ std::unique_lock<std::mutex> lock(mutex);
value++;
}
condition.notify_one();
public:
void Acquire() {
- if (fHaveGrant)
- return;
- sem->wait();
- fHaveGrant = true;
+ if (!fHaveGrant) {
+ sem->wait();
+ fHaveGrant = true;
+ }
}
void Release() {
- if (!fHaveGrant)
- return;
- sem->post();
- fHaveGrant = false;
+ if (fHaveGrant) {
+ sem->post();
+ fHaveGrant = false;
+ }
}
bool TryAcquire() {
grant.Release();
grant.sem = sem;
grant.fHaveGrant = fHaveGrant;
- sem = NULL;
+ sem = nullptr;
fHaveGrant = false;
}
- CSemaphoreGrant() : sem(NULL), fHaveGrant(false) {}
+ CSemaphoreGrant() : sem(nullptr), fHaveGrant(false) {}
CSemaphoreGrant(CSemaphore &sema, bool fTry = false) : sem(&sema), fHaveGrant(false) {
if (fTry)
Release();
}
- operator bool() {
+ operator bool() const {
return fHaveGrant;
}
};
#endif
-
#include <io.h> /* for _commit */
#include "shlobj.h"
#elif defined(__linux__)
-# include <sys/prctl.h>
+#include <sys/prctl.h>
#endif
#if !defined(WIN32) && !defined(ANDROID)
// Since the order of destruction of static/global objects is undefined,
// allocate mutexDebugLog on the heap the first time this routine
// is called to avoid crashes during shutdown.
- static boost::mutex* mutexDebugLog = NULL;
- if (mutexDebugLog == NULL) mutexDebugLog = new boost::mutex();
- boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
+ static std::mutex* mutexDebugLog = nullptr;
+ if (mutexDebugLog == nullptr) mutexDebugLog = new std::mutex();
+ std::scoped_lock scoped_lock(*mutexDebugLog);
// reopen the log file, if requested
if (fReopenDebugLog) {