From 7fa3b1bd2a4f1c192007e140475ab29ae0223f25 Mon Sep 17 00:00:00 2001 From: Jiafei Wen Date: Wed, 8 Apr 2015 18:02:42 -0700 Subject: [PATCH] Adding Mutex for Creating Context Adding lock for creating foreground context and background context, preventing any possible race condition. CRs-fixed: 820078 Change-Id: I7ace685622904ee2aa12f317540f9c04880acc70 --- core/LocDualContext.cpp | 22 ++++++++++++++++++++-- core/LocDualContext.h | 1 + 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/core/LocDualContext.cpp b/core/LocDualContext.cpp index 41ad4f0a..d68cb3bc 100644 --- a/core/LocDualContext.cpp +++ b/core/LocDualContext.cpp @@ -61,6 +61,8 @@ ContextBase* LocDualContext::mInjectContext = NULL; const char* LocDualContext::mLocationHalName = "Loc_hal_worker"; const char* LocDualContext::mLBSLibName = "liblbs_core.so"; +pthread_mutex_t LocDualContext::mGetLocContextMutex = PTHREAD_MUTEX_INITIALIZER; + const MsgTask* LocDualContext::getMsgTask(MsgTask::tCreate tCreator, const char* name) { @@ -84,22 +86,30 @@ const MsgTask* LocDualContext::getMsgTask(MsgTask::tAssociate tAssociate, ContextBase* LocDualContext::getLocFgContext(MsgTask::tCreate tCreator, const char* name) { + pthread_mutex_lock(&LocDualContext::mGetLocContextMutex); + LOC_LOGD("%s:%d]: querying ContextBase with tCreator", __func__, __LINE__); if (NULL == mFgContext) { + LOC_LOGD("%s:%d]: creating msgTask with tCreator", __func__, __LINE__); const MsgTask* msgTask = getMsgTask(tCreator, name); mFgContext = new LocDualContext(msgTask, mFgExclMask); } if(NULL == mInjectContext) { + LOC_LOGD("%s:%d]: mInjectContext is FgContext", __func__, __LINE__); mInjectContext = mFgContext; injectFeatureConfig(mInjectContext); } + pthread_mutex_unlock(&LocDualContext::mGetLocContextMutex); return mFgContext; } ContextBase* LocDualContext::getLocFgContext(MsgTask::tAssociate tAssociate, - const char* name) + const char* name) { + pthread_mutex_lock(&LocDualContext::mGetLocContextMutex); + LOC_LOGD("%s:%d]: querying ContextBase with tAssociate", __func__, __LINE__); if (NULL == mFgContext) { + LOC_LOGD("%s:%d]: creating msgTask with tAssociate", __func__, __LINE__); const MsgTask* msgTask = getMsgTask(tAssociate, name); mFgContext = new LocDualContext(msgTask, mFgExclMask); @@ -109,14 +119,17 @@ ContextBase* LocDualContext::getLocFgContext(MsgTask::tAssociate tAssociate, mInjectContext = mFgContext; injectFeatureConfig(mInjectContext); } + pthread_mutex_unlock(&LocDualContext::mGetLocContextMutex); return mFgContext; - } ContextBase* LocDualContext::getLocBgContext(MsgTask::tCreate tCreator, const char* name) { + pthread_mutex_lock(&LocDualContext::mGetLocContextMutex); + LOC_LOGD("%s:%d]: querying ContextBase with tCreator", __func__, __LINE__); if (NULL == mBgContext) { + LOC_LOGD("%s:%d]: creating msgTask with tCreator", __func__, __LINE__); const MsgTask* msgTask = getMsgTask(tCreator, name); mBgContext = new LocDualContext(msgTask, mBgExclMask); @@ -126,13 +139,17 @@ ContextBase* LocDualContext::getLocBgContext(MsgTask::tCreate tCreator, mInjectContext = mBgContext; injectFeatureConfig(mInjectContext); } + pthread_mutex_unlock(&LocDualContext::mGetLocContextMutex); return mBgContext; } ContextBase* LocDualContext::getLocBgContext(MsgTask::tAssociate tAssociate, const char* name) { + pthread_mutex_lock(&LocDualContext::mGetLocContextMutex); + LOC_LOGD("%s:%d]: querying ContextBase with tAssociate", __func__, __LINE__); if (NULL == mBgContext) { + LOC_LOGD("%s:%d]: creating msgTask with tAssociate", __func__, __LINE__); const MsgTask* msgTask = getMsgTask(tAssociate, name); mBgContext = new LocDualContext(msgTask, mBgExclMask); @@ -142,6 +159,7 @@ ContextBase* LocDualContext::getLocBgContext(MsgTask::tAssociate tAssociate, mInjectContext = mBgContext; injectFeatureConfig(mInjectContext); } + pthread_mutex_unlock(&LocDualContext::mGetLocContextMutex); return mBgContext; } diff --git a/core/LocDualContext.h b/core/LocDualContext.h index cdc4df3e..1e96ea56 100644 --- a/core/LocDualContext.h +++ b/core/LocDualContext.h @@ -45,6 +45,7 @@ class LocDualContext : public ContextBase { const char* name); static const MsgTask* getMsgTask(MsgTask::tAssociate tAssociate, const char* name); + static pthread_mutex_t mGetLocContextMutex; protected: LocDualContext(const MsgTask* msgTask,