void CriticalSection::lock() { #if defined(_WIN32) EnterCriticalSection( &self->criticalSection ); #elif defined(__APPLE__) MPEnterCriticalRegion( self->criticalSection, kDurationForever ); #elif defined(__linux__) pthread_mutex_lock( &self->criticalSection ); #endif }
bool CriticalSection::tryLock() { #if defined(_WIN32) return ( TryEnterCriticalSection( &self->criticalSection ) != 0 ); #elif defined(__APPLE__) return ( MPEnterCriticalRegion( self->criticalSection, kDurationImmediate ) == noErr ); #elif defined(__linux__) return ( pthread_mutex_trylock( &self->criticalSection ) == 0 ); #endif }
wxMutexError wxMutexInternal::Lock() { wxCHECK_MSG( m_isOk , wxMUTEX_MISC_ERROR , wxT("Invalid Mutex") ) ; OSStatus err = MPEnterCriticalRegion( m_critRegion, kDurationForever); if ( err) { wxLogSysError(wxT("Could not lock mutex")); return wxMUTEX_MISC_ERROR; } return wxMUTEX_NO_ERROR; }
int CFSMutex::Lock(ULONG lTimeout) { #if defined (WIN32) FSUNUSED(lTimeout); ::EnterCriticalSection(&m_hMutex); #elif defined (UNIX) FSUNUSED(lTimeout); pthread_mutex_lock(&m_hMutex); #elif defined (MAC) MPEnterCriticalRegion(m_hMutex, (lTimeout == -1 ? kDurationForever : lTimeout)); #endif return 0; }
wxMutexError wxMutexInternal::TryLock() { wxCHECK_MSG( m_isOk , wxMUTEX_MISC_ERROR , wxT("Invalid Mutex") ) ; OSStatus err = MPEnterCriticalRegion( m_critRegion, kDurationImmediate); if (err != noErr) { if ( err == kMPTimeoutErr) return wxMUTEX_BUSY; wxLogSysError( wxT("Could not try lock mutex") ); return wxMUTEX_MISC_ERROR; } return wxMUTEX_NO_ERROR; }
OSStatus keyboard_handler(EventHandlerCallRef, EventRef event, void *) { unsigned char key; GetEventParameter(event,kEventParamKeyMacCharCodes,typeChar,0,sizeof(key),0,&key); MPEnterCriticalRegion(cr,kDurationForever); if (key == 27) { exit(0); } MPExitCriticalRegion(cr); return noErr; }
wxMutexError wxMutexInternal::Lock(unsigned long ms) { wxCHECK_MSG( m_isOk , wxMUTEX_MISC_ERROR , wxT("Invalid Mutex") ); OSStatus err = MPEnterCriticalRegion( m_critRegion, ms ); switch ( err ) { case noErr: break; case kMPTimeoutErr: wxASSERT_MSG( ms != kDurationForever, wxT("unexpected timeout") ); return wxMUTEX_TIMEOUT; default: wxLogSysError(wxT("Could not lock mutex")); return wxMUTEX_MISC_ERROR; } return wxMUTEX_NO_ERROR; }
bool wxCriticalSection::TryEnter() { return MPEnterCriticalRegion( (MPCriticalRegionID) m_critRegion, kDurationImmediate ) == noErr; }
void wxCriticalSection::Enter() { MPEnterCriticalRegion( (MPCriticalRegionID) m_critRegion, kDurationForever ); }
OSStatus safe_enter_critical_region(MPCriticalRegionID pCriticalRegionID, Duration lDuration, MPCriticalRegionID pCriticalRegionCriticalRegionID/* = kInvalidID*/) { if(pCriticalRegionCriticalRegionID != kInvalidID) { if(at_mp()) { // enter the critical region's critical region OSStatus lStatus = noErr; AbsoluteTime sExpiration; if(lDuration != kDurationImmediate && lDuration != kDurationForever) { sExpiration = AddDurationToAbsolute(lDuration, UpTime()); } lStatus = MPEnterCriticalRegion(pCriticalRegionCriticalRegionID, lDuration); assert(lStatus == noErr || lStatus == kMPTimeoutErr); if(lStatus == noErr) { // calculate a new duration if(lDuration != kDurationImmediate && lDuration != kDurationForever) { // check if we have any time left AbsoluteTime sUpTime(UpTime()); if(force_cast<uint64_t>(sExpiration) > force_cast<uint64_t>(sUpTime)) { // reset our duration to our remaining time lDuration = AbsoluteDeltaToDuration(sExpiration, sUpTime); } else { // no time left lDuration = kDurationImmediate; } } // if we entered the critical region, exit it again lStatus = MPExitCriticalRegion(pCriticalRegionCriticalRegionID); assert(lStatus == noErr); } else { // otherwise, give up return(lStatus); } } else { // if we're at system task time, try to enter the critical region's critical // region until we succeed. MP tasks will block on this until we let it go. OSStatus lStatus; do { lStatus = MPEnterCriticalRegion(pCriticalRegionCriticalRegionID, kDurationImmediate); } while(lStatus == kMPTimeoutErr); assert(lStatus == noErr); } } // try to enter the critical region function<OSStatus, Duration> oEnterCriticalRegion; oEnterCriticalRegion = bind(MPEnterCriticalRegion, pCriticalRegionID, _1); OSStatus lStatus = safe_wait(oEnterCriticalRegion, lDuration); // if we entered the critical region's critical region to get the critical region, // exit the critical region's critical region. if(pCriticalRegionCriticalRegionID != kInvalidID && at_mp() == false) { lStatus = MPExitCriticalRegion(pCriticalRegionCriticalRegionID); assert(lStatus == noErr); } return(lStatus); }