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
This commit is contained in:
Jiafei Wen 2015-04-08 18:02:42 -07:00 committed by Gerrit - the friendly Code Review server
parent e7fa3e15ae
commit 7fa3b1bd2a
2 changed files with 21 additions and 2 deletions

View file

@ -61,6 +61,8 @@ ContextBase* LocDualContext::mInjectContext = NULL;
const char* LocDualContext::mLocationHalName = "Loc_hal_worker"; const char* LocDualContext::mLocationHalName = "Loc_hal_worker";
const char* LocDualContext::mLBSLibName = "liblbs_core.so"; const char* LocDualContext::mLBSLibName = "liblbs_core.so";
pthread_mutex_t LocDualContext::mGetLocContextMutex = PTHREAD_MUTEX_INITIALIZER;
const MsgTask* LocDualContext::getMsgTask(MsgTask::tCreate tCreator, const MsgTask* LocDualContext::getMsgTask(MsgTask::tCreate tCreator,
const char* name) const char* name)
{ {
@ -84,22 +86,30 @@ const MsgTask* LocDualContext::getMsgTask(MsgTask::tAssociate tAssociate,
ContextBase* LocDualContext::getLocFgContext(MsgTask::tCreate tCreator, ContextBase* LocDualContext::getLocFgContext(MsgTask::tCreate tCreator,
const char* name) const char* name)
{ {
pthread_mutex_lock(&LocDualContext::mGetLocContextMutex);
LOC_LOGD("%s:%d]: querying ContextBase with tCreator", __func__, __LINE__);
if (NULL == mFgContext) { if (NULL == mFgContext) {
LOC_LOGD("%s:%d]: creating msgTask with tCreator", __func__, __LINE__);
const MsgTask* msgTask = getMsgTask(tCreator, name); const MsgTask* msgTask = getMsgTask(tCreator, name);
mFgContext = new LocDualContext(msgTask, mFgContext = new LocDualContext(msgTask,
mFgExclMask); mFgExclMask);
} }
if(NULL == mInjectContext) { if(NULL == mInjectContext) {
LOC_LOGD("%s:%d]: mInjectContext is FgContext", __func__, __LINE__);
mInjectContext = mFgContext; mInjectContext = mFgContext;
injectFeatureConfig(mInjectContext); injectFeatureConfig(mInjectContext);
} }
pthread_mutex_unlock(&LocDualContext::mGetLocContextMutex);
return mFgContext; return mFgContext;
} }
ContextBase* LocDualContext::getLocFgContext(MsgTask::tAssociate tAssociate, 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) { if (NULL == mFgContext) {
LOC_LOGD("%s:%d]: creating msgTask with tAssociate", __func__, __LINE__);
const MsgTask* msgTask = getMsgTask(tAssociate, name); const MsgTask* msgTask = getMsgTask(tAssociate, name);
mFgContext = new LocDualContext(msgTask, mFgContext = new LocDualContext(msgTask,
mFgExclMask); mFgExclMask);
@ -109,14 +119,17 @@ ContextBase* LocDualContext::getLocFgContext(MsgTask::tAssociate tAssociate,
mInjectContext = mFgContext; mInjectContext = mFgContext;
injectFeatureConfig(mInjectContext); injectFeatureConfig(mInjectContext);
} }
pthread_mutex_unlock(&LocDualContext::mGetLocContextMutex);
return mFgContext; return mFgContext;
} }
ContextBase* LocDualContext::getLocBgContext(MsgTask::tCreate tCreator, ContextBase* LocDualContext::getLocBgContext(MsgTask::tCreate tCreator,
const char* name) const char* name)
{ {
pthread_mutex_lock(&LocDualContext::mGetLocContextMutex);
LOC_LOGD("%s:%d]: querying ContextBase with tCreator", __func__, __LINE__);
if (NULL == mBgContext) { if (NULL == mBgContext) {
LOC_LOGD("%s:%d]: creating msgTask with tCreator", __func__, __LINE__);
const MsgTask* msgTask = getMsgTask(tCreator, name); const MsgTask* msgTask = getMsgTask(tCreator, name);
mBgContext = new LocDualContext(msgTask, mBgContext = new LocDualContext(msgTask,
mBgExclMask); mBgExclMask);
@ -126,13 +139,17 @@ ContextBase* LocDualContext::getLocBgContext(MsgTask::tCreate tCreator,
mInjectContext = mBgContext; mInjectContext = mBgContext;
injectFeatureConfig(mInjectContext); injectFeatureConfig(mInjectContext);
} }
pthread_mutex_unlock(&LocDualContext::mGetLocContextMutex);
return mBgContext; return mBgContext;
} }
ContextBase* LocDualContext::getLocBgContext(MsgTask::tAssociate tAssociate, ContextBase* LocDualContext::getLocBgContext(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 == mBgContext) { if (NULL == mBgContext) {
LOC_LOGD("%s:%d]: creating msgTask with tAssociate", __func__, __LINE__);
const MsgTask* msgTask = getMsgTask(tAssociate, name); const MsgTask* msgTask = getMsgTask(tAssociate, name);
mBgContext = new LocDualContext(msgTask, mBgContext = new LocDualContext(msgTask,
mBgExclMask); mBgExclMask);
@ -142,6 +159,7 @@ ContextBase* LocDualContext::getLocBgContext(MsgTask::tAssociate tAssociate,
mInjectContext = mBgContext; mInjectContext = mBgContext;
injectFeatureConfig(mInjectContext); injectFeatureConfig(mInjectContext);
} }
pthread_mutex_unlock(&LocDualContext::mGetLocContextMutex);
return mBgContext; return mBgContext;
} }

View file

@ -45,6 +45,7 @@ class LocDualContext : public ContextBase {
const char* name); const char* name);
static const MsgTask* getMsgTask(MsgTask::tAssociate tAssociate, static const MsgTask* getMsgTask(MsgTask::tAssociate tAssociate,
const char* name); const char* name);
static pthread_mutex_t mGetLocContextMutex;
protected: protected:
LocDualContext(const MsgTask* msgTask, LocDualContext(const MsgTask* msgTask,