Add lock to RdbMem

This commit is contained in:
Ai Lin Chia 2017-05-11 12:10:50 +02:00
parent 9e61bd1d71
commit 82b5bf47af
3 changed files with 31 additions and 9 deletions

View File

@ -2058,6 +2058,8 @@ int32_t Rdb::reclaimMemFromDeletedTreeNodes() {
// this only works for non-dumped RdbMem right now, i.e. doledb only
if ( m_rdbId != RDB_DOLEDB ) { g_process.shutdownAbort(true); }
ScopedLock sl2(m_mem.getLock());
// start scanning the mem pool
char *p = m_mem.m_mem;
char *pend = m_mem.m_ptr1;

View File

@ -2,6 +2,7 @@
#include "Errno.h"
#include "Mem.h"
#include "Log.h"
#include "ScopedLock.h"
// RdbMem allocates a fixed chunk of memory and initially sets m_ptr1 to point at the start and m_ptr2 at the end
// |--------------------------------------------------|
@ -53,6 +54,8 @@ RdbMem::~RdbMem() {
void RdbMem::reset() {
ScopedLock sl(m_mtx);
if(m_mem)
mfree(m_mem, m_memSize, m_allocName);
m_ptr1 = m_ptr2 = m_mem = NULL;
@ -61,6 +64,8 @@ void RdbMem::reset() {
void RdbMem::clear() {
ScopedLock sl(m_mtx);
// set up primary/secondary mem ptrs
m_ptr1 = m_mem;
// secondary mem initially grow downward
@ -114,6 +119,8 @@ void *RdbMem::dupData(const char *data, int32_t dataSize) {
void *RdbMem::allocData(int32_t dataSize) {
ScopedLock sl(m_mtx);
// . otherwise, use the primary mem
// . if primary mem growing down...
if(m_ptr1>m_ptr2){
@ -136,8 +143,21 @@ void *RdbMem::allocData(int32_t dataSize) {
// are we at the 90% limit?
if(m_ptr1>m_90up)
m_is90PercentFull = true;
// note it
//log("rdbmem: ptr1b=%" PRIu32" size=%" PRId32,(int32_t)m_ptr1-dataSize,dataSize);
// return the ptr
return m_ptr1 - dataSize;
}
int32_t RdbMem::getAvailMem() const {
ScopedLock sl(m_mtx);
// don't allow ptrs to equal each other...
if ( m_ptr1 == m_ptr2 ) return 0;
if ( m_ptr1 < m_ptr2 ) return m_ptr2 - m_ptr1 - 1;
return m_ptr1 - m_ptr2 - 1;
}
bool RdbMem::is90PercentFull() const {
ScopedLock sl(m_mtx);
return m_is90PercentFull;
}

View File

@ -8,6 +8,7 @@
#define GB_RDBMEM_H
#include "types.h"
#include "GbMutex.h"
#include <inttypes.h>
class RdbMem {
@ -24,6 +25,8 @@ class RdbMem {
void reset();
GbMutex& getLock() { return m_mtx; }
// . if a dump is not going on this uses the primary mem space
void *dupData(const char *data, int32_t dataSize);
@ -31,23 +34,20 @@ class RdbMem {
void *allocData(int32_t dataSize);
// how much mem is available?
int32_t getAvailMem() const {
// don't allow ptrs to equal each other...
if ( m_ptr1 == m_ptr2 ) return 0;
if ( m_ptr1 < m_ptr2 ) return m_ptr2 - m_ptr1 - 1;
return m_ptr1 - m_ptr2 - 1;
}
int32_t getAvailMem() const;
int32_t getTotalMem() const { return m_memSize; }
int32_t getUsedMem() const { return m_memSize - getAvailMem(); }
// used to determine when to dump
bool is90PercentFull() const { return m_is90PercentFull; }
bool is90PercentFull() const;
private:
friend class Rdb;
mutable GbMutex m_mtx;
// the primary mem
char *m_ptr1;
// the secondary mem