diff options
author | Hoa V. DINH <dinh.viet.hoa@gmail.com> | 2016-03-03 22:45:19 -0800 |
---|---|---|
committer | Hoa V. DINH <dinh.viet.hoa@gmail.com> | 2016-03-03 22:45:19 -0800 |
commit | a55c8f370bd663ea1d31fed230987711aa1cf176 (patch) | |
tree | 567a995e2f0565d24aebff0fab959d3c95045670 /src/core/pop | |
parent | 886aae2ab94c74a4d4c644c446f67d37a333c1f9 (diff) |
Thread safety on logger
Diffstat (limited to 'src/core/pop')
-rw-r--r-- | src/core/pop/MCPOPSession.cpp | 37 | ||||
-rw-r--r-- | src/core/pop/MCPOPSession.h | 8 |
2 files changed, 40 insertions, 5 deletions
diff --git a/src/core/pop/MCPOPSession.cpp b/src/core/pop/MCPOPSession.cpp index ab8975a6..77452515 100644 --- a/src/core/pop/MCPOPSession.cpp +++ b/src/core/pop/MCPOPSession.cpp @@ -36,6 +36,7 @@ void POPSession::init() mProgressCallback = NULL; mState = STATE_DISCONNECTED; mConnectionLogger = NULL; + pthread_mutex_init(&mConnectionLoggerLock, NULL); } POPSession::POPSession() @@ -45,6 +46,7 @@ POPSession::POPSession() POPSession::~POPSession() { + pthread_mutex_destroy(&mConnectionLoggerLock); MC_SAFE_RELEASE(mHostname); MC_SAFE_RELEASE(mUsername); MC_SAFE_RELEASE(mPassword); @@ -155,20 +157,24 @@ void POPSession::body_progress(size_t current, size_t maximum, void * context) static void logger(mailpop3 * pop3, int log_type, const char * buffer, size_t size, void * context) { POPSession * session = (POPSession *) context; - - if (session->connectionLogger() == NULL) + session->lockConnectionLogger(); + + if (session->connectionLoggerNoLock() == NULL) { + session->unlockConnectionLogger(); return; + } ConnectionLogType type = getConnectionType(log_type); bool isBuffer = isBufferFromLogType(log_type); if (isBuffer) { Data * data = Data::dataWithBytes(buffer, (unsigned int) size); - session->connectionLogger()->log(session, type, data); + session->connectionLoggerNoLock()->log(session, type, data); } else { - session->connectionLogger()->log(session, type, NULL); + session->connectionLoggerNoLock()->log(session, type, NULL); } + session->unlockConnectionLogger(); } void POPSession::setup() @@ -592,12 +598,35 @@ void POPSession::noop(ErrorCode * pError) } } +void POPSession::lockConnectionLogger() +{ + pthread_mutex_lock(&mConnectionLoggerLock); +} + +void POPSession::unlockConnectionLogger() +{ + pthread_mutex_unlock(&mConnectionLoggerLock); +} + void POPSession::setConnectionLogger(ConnectionLogger * logger) { + lockConnectionLogger(); mConnectionLogger = logger; + unlockConnectionLogger(); } ConnectionLogger * POPSession::connectionLogger() { + ConnectionLogger * result; + + lockConnectionLogger(); + result = connectionLoggerNoLock(); + unlockConnectionLogger(); + + return result; +} + +ConnectionLogger * POPSession::connectionLoggerNoLock() +{ return mConnectionLogger; } diff --git a/src/core/pop/MCPOPSession.h b/src/core/pop/MCPOPSession.h index eda323c0..f32f9b18 100644 --- a/src/core/pop/MCPOPSession.h +++ b/src/core/pop/MCPOPSession.h @@ -64,7 +64,12 @@ namespace mailcore { virtual void setConnectionLogger(ConnectionLogger * logger); virtual ConnectionLogger * connectionLogger(); - + + public: // private + virtual void lockConnectionLogger(); + virtual void unlockConnectionLogger(); + virtual ConnectionLogger * connectionLoggerNoLock(); + private: String * mHostname; unsigned int mPort; @@ -81,6 +86,7 @@ namespace mailcore { int mState; ConnectionLogger * mConnectionLogger; + pthread_mutex_t mConnectionLoggerLock; void init(); void bodyProgress(unsigned int current, unsigned int maximum); |