Skip to content

Commit

Permalink
fix up issues detect by Xcode Thread Sanitizer
Browse files Browse the repository at this point in the history
  • Loading branch information
戴黎明 committed Apr 15, 2020
1 parent f21eba3 commit 72700df
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
42 changes: 21 additions & 21 deletions comm/mtaf_ptrbuffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ void PtrBuffer::Write(const void *_pBuffer, size_t _nLen, off_t _nPos) {
MTAF_ASSERT(NULL != _pBuffer);
MTAF_ASSERT(0 <= _nPos);
MTAF_ASSERT((unsigned int)_nPos <= Length());
size_t copylen = (size_t)min(_nLen, max_length_ - _nPos);
length_ = (size_t)max(length_, copylen + _nPos);
size_t copylen = (size_t)min(_nLen, max_length_.load() - _nPos);
length_.store((size_t)max(length_.load(), copylen + _nPos));
memcpy((unsigned char *)Ptr() + _nPos, _pBuffer, copylen);

if (length_ < max_length_ - 4) {
if (length_.load() < max_length_.load() - 4) {
memset((unsigned char *)Ptr() + _nPos + copylen, 0, sizeof(char) * 3);
memset((unsigned char *)Ptr() + _nPos + copylen + 3, '\r', sizeof(char));
}
Expand All @@ -96,35 +96,35 @@ size_t PtrBuffer::Read(void *_pBuffer, size_t _nLen, off_t _nPos) const {
void PtrBuffer::Seek(off_t _nOffset, TSeek _eOrigin) {
switch (_eOrigin) {
case kSeekStart:
pos_ = _nOffset;
pos_.store(_nOffset);
break;

case kSeekCur:
pos_ += _nOffset;
pos_.store(pos_.load() + _nOffset);
break;

case kSeekEnd:
pos_ = length_ + _nOffset;
pos_.store(length_.load() + _nOffset);
break;

default:
MTAF_ASSERT(false);
break;
}

if (pos_ < 0)
pos_ = 0;
if (pos_.load() < 0)
pos_.store(0);

if ((unsigned int)pos_ > length_)
pos_ = length_;
if ((unsigned int)pos_.load() > length_.load())
pos_.store(length_.load());
}

void PtrBuffer::Length(off_t _nPos, size_t _nLenght) {
MTAF_ASSERT(0 <= _nPos);
MTAF_ASSERT((size_t)_nPos <= _nLenght);
MTAF_ASSERT(_nLenght <= MaxLength());

length_ = max_length_ < _nLenght ? max_length_ : _nLenght;
length_.store(max_length_.load() < _nLenght ? max_length_.load() : _nLenght);
Seek(_nPos, kSeekStart);
}

Expand All @@ -145,26 +145,26 @@ const void *PtrBuffer::PosPtr() const {
}

off_t PtrBuffer::Pos() const {
return pos_;
return pos_.load();
}

size_t PtrBuffer::PosLength() const {
return (size_t)(length_ - pos_);
return (size_t)(length_.load() - pos_.load());
}

size_t PtrBuffer::Length() const {
return length_;
return length_.load();
}

size_t PtrBuffer::MaxLength() const {
return max_length_;
return max_length_.load();
}

void PtrBuffer::Attach(void *_pBuffer, size_t _nLen, size_t _maxlen) {
Reset();
parray_ = (unsigned char *)_pBuffer;
length_ = _nLen;
max_length_ = _maxlen;
length_.store(_nLen);
max_length_.store(_maxlen);
}

void PtrBuffer::Attach(void *_pBuffer, size_t _nLen) {
Expand All @@ -173,7 +173,7 @@ void PtrBuffer::Attach(void *_pBuffer, size_t _nLen) {

void PtrBuffer::Reset() {
parray_ = NULL;
pos_ = 0;
length_ = 0;
max_length_ = 0;
pos_.store(0);
length_.store(0);
max_length_.store(0);
}
7 changes: 4 additions & 3 deletions comm/mtaf_ptrbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <string.h>
#include <sys/types.h>
#include <atomic>

namespace MTAppenderFile {
class PtrBuffer;
Expand Down Expand Up @@ -86,9 +87,9 @@ class MTAppenderFile::PtrBuffer

private:
unsigned char *parray_;
off_t pos_;
size_t length_;
size_t max_length_;
std::atomic<off_t> pos_;
std::atomic_size_t length_;
std::atomic_size_t max_length_;
};

extern const MTAppenderFile::PtrBuffer KNullPtrBuffer;
Expand Down
20 changes: 10 additions & 10 deletions loglib/mtaf_appender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct mtaf_log_appender_t {

Mutex sg_mutex_log_file;

volatile bool sg_log_close;
std::atomic_bool sg_log_close;

mtaf_mmapped_file *sg_mmap_file;

Expand Down Expand Up @@ -123,7 +123,7 @@ mtaf_log_appender *mtaf_log_appender_create(bool use_share_thread) {
return NULL;
}

mlog->sg_log_close = true;
mlog->sg_log_close.store(true);
mlog->sg_log_buff = NULL;
mlog->sg_logfile = NULL;
mlog->sg_openfiletime = 0;
Expand Down Expand Up @@ -170,7 +170,7 @@ void mtaf_log_appender_open(mtaf_log_appender *mlog, mtaf_append_mode _mode, con
printf("mt_appender_open : MTLogAppender is NULL\n");
return;
}
if (!mlog->sg_log_close) {
if (!mlog->sg_log_close.load()) {
printf("appender has already been opened. _dir:%s _name:%s\n", filedir, filename);
return;
}
Expand Down Expand Up @@ -205,7 +205,7 @@ void mtaf_log_appender_open(mtaf_log_appender *mlog, mtaf_append_mode _mode, con
ScopedLock lock(mlog->sg_mutex_log_file);
mlog->sg_logdir = filedir;
mlog->sg_logfilename = filename;
mlog->sg_log_close = false;
mlog->sg_log_close.store(false);
mtaf_log_appender_setmode(mlog, _mode);
lock.unlock();

Expand Down Expand Up @@ -334,7 +334,7 @@ static void __async_log_shared_thread_operation() {

for (auto it = shared_appenders()->begin(); it != shared_appenders()->end(); it++) {
mtaf_log_appender *mlog = *it;
if (mlog->sg_log_close) {
if (mlog->sg_log_close.load()) {
continue;
}

Expand All @@ -356,7 +356,7 @@ static void __async_log_thread_operation(void *arg) {

__log_thread_task(mlog);

if (mlog->sg_log_close) {
if (mlog->sg_log_close.load()) {
break;
}

Expand Down Expand Up @@ -404,7 +404,7 @@ static void __appender_async(mtaf_log_appender *mlog, const mtaf_log_info *_info

for (auto it = shared_appenders()->begin(); it != shared_appenders()->end(); it++) {
mtaf_log_appender *mlog = *it;
if (mlog->sg_log_close)
if (mlog->sg_log_close.load())
continue;

if (mlog->sg_log_buff->GetData().Length() >= mlog->sg_bufferblock_length * 1 / 3 || (NULL != _info && LOG_LEVEL_FATAL == _info->level)) {
Expand All @@ -415,7 +415,7 @@ static void __appender_async(mtaf_log_appender *mlog, const mtaf_log_info *_info
}

void mtaf_log_appender_append(mtaf_log_appender *mlog, const char *log) {
if (mlog->sg_log_close) return;
if (mlog->sg_log_close.load()) return;

if (mlog->sg_consolelog_open) mtaf_console_log(log);

Expand All @@ -426,7 +426,7 @@ void mtaf_log_appender_append(mtaf_log_appender *mlog, const char *log) {
}

void mtaf_log_appender_append_ex(mtaf_log_appender *mlog, const char *log, const mtaf_log_info *mLogInfo) {
if (mlog->sg_log_close) return;
if (mlog->sg_log_close.load()) return;

if (mlog->sg_consolelog_open) mtaf_console_log(mLogInfo, log);

Expand All @@ -437,7 +437,7 @@ void mtaf_log_appender_append_ex(mtaf_log_appender *mlog, const char *log, const
}

void mtaf_log_appender_close(mtaf_log_appender *mlog) {
if (mlog->sg_log_close) return;
if (mlog->sg_log_close.load()) return;

mlog->sg_log_close = true;

Expand Down

0 comments on commit 72700df

Please sign in to comment.