// Get a pointer to the singleton instance. static T * getPtr () { // first check if it was created. If the mutex would be placed first, a performance hit would occur, since // all operations would create and release a lock. if (! objectPtr_) { // try to lock #if GUSLIB_FLAG_MULTITHREAD guslib::GScopedLock myLock (creationMutex_); #endif // While the lock tried to take control, another thread may have already finished instantiating the object, // so a new check should be done to ensure safety (double-checked locking pattern). if (! objectPtr_) { // Should create the object as volatile to prevent some compiler optimizations that may lead to the lines being // executed internally in a slightly different order than the one given here. // (see also: http://aszt.inf.elte.hu/~gsd/klagenfurt/material/ch03s05.html) volatile T * volatileptr = new T (); objectPtr_ = (T*) volatileptr; // If possible, use the std::atexit #ifndef ATEXIT_FIXED std::atexit (& destroy); #endif// ATEXIT_FIXED } } return objectPtr_; }//getptr
void Controller::GetUpdate() { MMThreadGuard myLock(lock_); { string propName; Purge(); Send("CSS?"); do { ReceiveOneLine(); } while (0 != buf_string_.compare(0, 3, "CSS", 0, 3)); globalState_ = false; //Record intensities and first LED on for (unsigned int i = 0; i < 3; i++) { //Read the intensity channelIntensities_[i] = atol(buf_string_.substr(6 + i * 6, 3).c_str()); string t = buf_string_.substr(4 + i * 6, 1); channelSelection_[i] = buf_string_.substr(4 + i * 6, 1) == "S" ? 1 : 0; propName = g_Keyword_Intensity; propName.push_back('A' + (char)i); intensityUpdated_[i] = true;; UpdateProperty(propName.c_str()); propName = g_Keyword_Selection; propName.push_back('A' + (char)i); selectionUpdated_[i] = true; UpdateProperty(propName.c_str()); globalState_ |= buf_string_.substr(5 + i * 6, 1) == "N"; globalStateUpdated_ = true; } } }
void V4RCamNode::showCameraImage() { timeval lastShownFrame = timeLastFrame_; double lastDuration = durationLastFrame_; cv::Mat img; int key = -1; do { if((lastShownFrame.tv_sec != timeLastFrame_.tv_sec) || (lastShownFrame.tv_usec != timeLastFrame_.tv_usec)) { img.create(height_, width_, CV_8UC3); lastShownFrame = timeLastFrame_; std::stringstream ss; ss << 1000.0 / ((durationLastFrame_ + lastDuration) / 2.0); lastDuration = durationLastFrame_; Lock myLock(mutexImage_); Pyuv422tobgr24(pVideoIn_->framebuffer, img.data, width_, height_); cv::putText(img, ss.str(), cv::Point(10, 10), cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar::all(0)); cv::imshow("Camera", img); } key = cv::waitKey(50); } while((key == -1) && show_camera_image_); cv::destroyWindow("Camera"); cv::waitKey(10); if(key != -1) { pVideoIn_->signalquit = 0; } }
void GBaseEngine::showLoadingScreen( bool loading ) { guslib::GScopedLock myLock( loadingCmdWriteMutex ); GTRACE(3, "Engine::" << (loading? "showing": "hiding")<<" loading screen."); try { if( loaderOverlay ) { if( loading ) { loaderOverlay->show(); } else { loaderOverlay->hide(); } } else { GTRACE(1, "NULL Overlay!"); } } catch( ... ) { GTRACE(1, "ERROR showing/hiding loading screen!"); } }
int Controller::OnChannelWave(MM::PropertyBase* pProp, MM::ActionType eAct, long channel) { string wavelength; if (eAct == MM::BeforeGet && waveUpdated_[channel]) { pProp->Set(channelWave_[channel].c_str()); waveUpdated_[channel] = false; } else if (eAct == MM::AfterSet) { pProp->Get(wavelength); //LOAD:<wavelength> loads that particular wavelength in the channel stringstream msg; msg << "LOAD:" << wavelength; MMThreadGuard myLock(lock_); Purge(); Send(msg.str()); do { ReceiveOneLine(); } while (buf_string_.size() == 0); } return HandleErrors(); }
//************************************************************ //********功能:向空闲线程栈中添加一个线程对象指针 //********参数:线程对象指针 //********返回值: //************************************************************ void CIdleThreadStack::push(CRealThread* thread) { if (thread != NULL) { CLock myLock(m_mutex); m_ThreadStack.push(thread); } }
USING_PRIVATE_NAMESPACE void RefCntObj::addRef() { ScopeLock myLock(lock); refCnt += 1; }
void CSocketInterface::StoreReceivedMessage(std::string &aMessage) { std::string message(aMessage.begin(), aMessage.end()); Lock myLock(m_receiveBufMutex, true); m_receiveBuffer.push_back(message); }
//************************************************************ //********功能:弹出空闲线程栈中的一个线程对象指针 //********参数: //********返回值:线程对象指针 //************************************************************ CRealThread* CIdleThreadStack::pop() { CLock myLock(m_mutex); if (m_ThreadStack.empty())return NULL; CRealThread* res = m_ThreadStack.top(); m_ThreadStack.pop(); return res; }
//************************************************************ //********功能:向活动线程链表中添加一个线程对象指针 //********参数:线程对象指针 //********返回值: //************************************************************ void CActiveThreadList::addThread(CRealThread* thread) { if (thread) { CLock myLock(m_mutex); m_ActiveThread.push_back(thread); } }
//************************************************************ //********功能:移除一个线程对象指针 //********参数:无 //********返回值: //************************************************************ void CActiveThreadList::removeThread(CRealThread* thread) { if (thread) { CLock myLock(m_mutex); m_ActiveThread.remove(thread); } }
void CSocketInterface::GetMessageToSend(std::string &aMessage) { Lock myLock(m_sendBufMutex, true); aMessage = m_sendBuffer.front(); m_sendBuffer.pop_front(); }
int boostThreadLocksTest::firstFunction(boostThreadLocksTest *pBoostThreadLocksTest) { std::cout<<"Entering "<<boost::this_thread::get_id()<<" "<<"firstFunction"<<std::endl; boost::upgrade_lock<boost::shared_mutex> myLock(pBoostThreadLocksTest->myMutex); pBoostThreadLocksTest->secondFunction(pBoostThreadLocksTest, myLock); std::cout<<"Returned From Call "<<boost::this_thread::get_id()<<" "<<"firstFunction"<<std::endl; std::cout<<"Returning from "<<boost::this_thread::get_id()<<" "<<"firstFunction"<<std::endl; return(0); }
bool GBaseEngine::renderCompleteScene() { // This is basically a copy of the standard OGRE root function "renderOneFrame". // What it does in addition to the aforementioned function is to add a mutex lock for // loading and rendering operations. // The function should be called from the rendering thread! if( ! frameListener ) { return false; } guslib::Timer myTimer; myTimer.reset(); GTRACE(5, "Engine: render complete scene() entered."); GTRACE(6, "GBaseEngine::renderCompleteScene() getting mutex(loadAndRenderMutex)"); guslib::GScopedLock myLock( loadAndRenderMutex ); GTRACE(6, "GBaseEngine::renderCompleteScene() got mutex(loadAndRenderMutex)"); if( isExiting() ) return false; // Trigger the FrameListener's "frameStarted" function to render the 3D scene. if(!root->_fireFrameStarted()) { GTRACE(1, "GBaseEngine::renderCompleteScene received error from _fireFrameStarted"); return false; } if( isExiting() ) return false; try { if( ! root->_updateAllRenderTargets() ) { GTRACE(1, "GBaseEngine::renderCompleteScene received error from _updateAllRenderTargets"); return false; } } catch( Exception & e ) { GTRACE(1, "Exception caught! GBaseEngine::renderCompleteScene(): "<<e.getFullDescription()); } if( isExiting() ) return false; // Trigger the FrameListener's "frameEnded" function. bool returnValue = root->_fireFrameEnded(); GTRACE(5, "Engine: render complete scene() exiting. Render took: "<< myTimer.elapsed()<< " time units."); GTRACE(6, "GBaseEngine::renderCompleteScene() releasing mutex(loadAndRenderMutex)"); return returnValue; }
unsigned long genUID::getUID() { #ifdef THREADED // Construction of myLock acquires lock on uidAccessM tbb::mutex::scoped_lock myLock(*uidAccessM); #endif return ++curUID; // Destruction of myLock releases lock on uidAccessM }
//************************************************************ //********功能:清空一个活动线程链表 //********参数:无 //********返回值: //************************************************************ void CActiveThreadList::clear() { CLock myLock(m_mutex); while (!m_ActiveThread.empty()) { CRealThread* p = m_ActiveThread.front(); m_ActiveThread.pop_front(); delete p; } }
//************************************************************ //********功能:清空一个空闲线程栈 //********参数:无 //********返回值: //************************************************************ void CIdleThreadStack::clear() { CLock myLock(m_mutex); while (!m_ThreadStack.empty()) { CRealThread* res = m_ThreadStack.top(); m_ThreadStack.pop(); delete res; } }
char* CharBuf::getChars() { PLSingleLock myLock(&fSemaphore, TRUE); fBuf[fIndex] = 0; char* s = new char[fIndex + 1]; strcpy_s(s, fIndex + 1, fBuf); fIndex = 0; return s; }
void GBaseEngine::shutDown() { guslib::GScopedLock myLock( exitCmdWriteMutex ); GTRACE(3, "engine received cmd to shutdown"); GTRACE(4, "forwarding cmd to state mgr"); GBaseStateManager::getPtr()->setShutdown(); exitCmdReceived = true; //TODO:XXX: consider the possibility of having to remove the resource groups. //Note that if you try to remove the groups during rendering, the application may freeze. //(this side effect occured randomly while deleting the General group) }
int HTTPCounter::getNextCounter() { MutexLock myLock(m_mutex); m_counter++; if (m_counter > m_maxValue) { m_counter = 0; } return m_counter; }
UnicodeString StringConverter::fromUnicodeLE(const char* buffer, int bufferSize) { boost::mutex::scoped_lock myLock(*unicodeConverterLeMutex_); UErrorCode error = U_ZERO_ERROR; UnicodeString tmp(buffer, bufferSize, getUnicodeConverterLE(), error); if (U_FAILURE(error)) { tmp = UnicodeString("##FLUOERROR"); // set error string LOG_WARN << "Unable to convert from utf-16-le string" << std::endl; } UnicodeString ret(tmp.getTerminatedBuffer()); return ret; }
void Controller::SetIntensity(long intensity, long index) { stringstream msg; msg << "C" << string(1, 'A' + (char)index) << "I" << intensity; { MMThreadGuard myLock(lock_); Purge(); Send(msg.str()); ReceiveOneLine(); } }
///////////////////////////////////////////// // Property Generators ///////////////////////////////////////////// void Controller::GeneratePropertyLockPod() { CPropertyAction* pAct = new CPropertyAction(this, &Controller::OnLockPod); CreateProperty(g_Keyword_PodLock, "0", MM::Integer, false, pAct); AddAllowedValue(g_Keyword_PodLock, "0"); AddAllowedValue(g_Keyword_PodLock, "1"); MMThreadGuard myLock(lock_); Purge(); Send("PORT:P=ON"); ReceiveOneLine(); }
void GLoaderState::deleteInstance() { if( objectPtr ) { guslib::GScopedLock myLock( creationMutex ); //recheck to see if the object was not deleted while trying to obtain the lock. if( objectPtr ) { delete objectPtr; objectPtr = NULL; } } }
// Destroy the singleton instance. Again, try a double checked lock. static void destroy () { if (objectPtr_) { #if GUSLIB_FLAG_MULTITHREAD guslib::GScopedLock myLock (creationMutex_); #endif if (objectPtr_) { delete objectPtr_; objectPtr_ = 0; // NULL } } }//destroy
GLoaderState * GLoaderState::getInstancePtr( GBaseEngine * enginePtr ) { if( !objectPtr ) { guslib::GScopedLock myLock( creationMutex ); //recheck to see if the object was not constructed while trying to obtain the lock. if( !objectPtr ) { volatile GLoaderState * tempPtr = new GLoaderState(enginePtr); objectPtr = (GLoaderState*)tempPtr; } } return objectPtr; }
void GResourceManagerUtil::processUnloadRequests() { GTRACE(6, "processUnloadRequests getting mutex (loadingMutex)"); guslib::GScopedLock myLock(loadingMutex); GTRACE(5, "Processing unload requests..."); for( std::vector<GOgreRenderable*>::iterator it = resToUnload.begin(); it != resToUnload.end(); ++it ) { (*it)->unload(); delete *it; } resToUnload.clear(); GTRACE(5, "Processed unload requests..."); }
void GBaseEngine::loadOverlays() { GTRACE(4, "GBaseEngine loading Overlays..."); GTRACE(5, "GBaseEngine: loadOverlays getting lock (loadAndRenderMutex)"); guslib::GScopedLock myLock( loadAndRenderMutex ); GTRACE(5, "GBaseEngine: loadOverlays got mutex (loadAndRenderMutex)"); overlay = OverlayManager::getSingletonPtr()->getByName(c_mainMenuOverlay); //Post init - hide templates and copy them. //overlay->show(); //std::string gsCopyrightNotice = std::string( GUS_COPYRIGHT_NOTICE ) + std::string( GUS_APP_VERSION ); //OverlayManager::getSingleton().getOverlayElement("GS/TAMenuCredits")-> // setCaption(gsCopyrightNotice); //overlay->hide(); }
void RefCntObj::removeRef() { bool shouldDelete; { // // The nested scope here is used to ensure that we unlock the lock // before we delete the object containing it. // ScopeLock myLock(lock); shouldDelete = (--refCnt == 0); } if (shouldDelete) delete this; }
Log::ModuleId Log::addModule(const std::string& name) throw() { Log::ModuleId module = 0; am_status_t status = AM_SUCCESS; if (!initialized && (status = initialize()) != AM_SUCCESS) { Log::log(Log::ALL_MODULES, Log::LOG_ERROR, "Log::addModule(): Error initializing log."); } else { ScopeLock myLock(*lockPtr); module = pAddModule(name); } return module; }