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,