예제 #1
0
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
}
예제 #2
0
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
}
예제 #3
0
파일: mpthread.cpp 프로젝트: gitrider/wxsj2
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;
}
예제 #4
0
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;
}
예제 #5
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;
}
예제 #6
0
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;
}
예제 #7
0
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;
}
예제 #8
0
bool wxCriticalSection::TryEnter()
{
    return MPEnterCriticalRegion( (MPCriticalRegionID) m_critRegion, kDurationImmediate ) == noErr;
}
예제 #9
0
void wxCriticalSection::Enter()
{
    MPEnterCriticalRegion( (MPCriticalRegionID) m_critRegion, kDurationForever );
}
예제 #10
0
파일: safe.cpp 프로젝트: Karlan88/xray
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);
}