📄 carbthrd.cpp
字号:
( bSkipParam2 || pMatch->m_pParam2 == param3 ) )
{
foundAMatch = TRUE;
break;
}
// copy it into the temp queue
::MPNotifyQueue(tempQ, param1, param2, param3);
}
if( foundAMatch )
{
// copy the found message to the out variable
pMsg->m_ulMessage = (UINT32)param1;
pMsg->m_pParam1 = param2;
pMsg->m_pParam2 = param3;
// if we aren't supposed to remove it, re-post it
if( !bRemoveMessage )
{
::MPNotifyQueue(tempQ, param1, param2, param3);
}
// now copy any remaining messages
while((osResult = ::MPWaitOnQueue(m_mpQueueID, ¶m1, ¶m2, ¶m3, kDurationImmediate)) == noErr)
{
::MPNotifyQueue(tempQ, param1, param2, param3);
}
retval = HXR_OK;
}
else
{
pMsg->m_ulMessage = 0;
retval = HXR_FAIL;
}
// now that the original queue is empty, throw it away and keep the copy that we made
::MPDeleteQueue(m_mpQueueID);
m_mpQueueID = tempQ;
m_pQueueMutex->Unlock();
return retval;
}
HX_RESULT
HXCarbonThread::DispatchMessage(HXThreadMessage* pMsg)
{
HX_RESULT retval = HXR_NOTIMPL;
HX_ASSERT(!"Unimplemented DispatchMessage!");
return retval;
}
// HXCarbonEvent
HXCarbonEvent::HXCarbonEvent(const char* pEventName, BOOL bManualReset)
: m_mpSemaphoreID(NULL)
, m_IsManuallyReset( bManualReset )
{
MPSemaphoreCount semaphoreMax = bManualReset ? INT_MAX : 1;
OSStatus osStatus = ::MPCreateSemaphore(semaphoreMax, 0, &m_mpSemaphoreID);
HX_ASSERT(osStatus == noErr);
}
HXCarbonEvent::~HXCarbonEvent()
{
OSStatus osStatus = ::MPDeleteSemaphore(m_mpSemaphoreID);
m_mpSemaphoreID = NULL;
HX_ASSERT(osStatus == noErr);
}
HX_RESULT
HXCarbonEvent::SignalEvent(void)
{
HX_RESULT retval = HXR_OK;
OSStatus osStatus = ::MPSignalSemaphore(m_mpSemaphoreID);
if (osStatus != noErr && osStatus != kMPInsufficientResourcesErr) retval = HXR_FAIL;
return retval;
}
HX_RESULT
HXCarbonEvent::ResetEvent(void)
{
HX_RESULT retval = HXR_OK;
OSStatus osStatus;
while(noErr == (osStatus = ::MPWaitOnSemaphore(m_mpSemaphoreID, kDurationImmediate)))
{
}
return retval;
}
void*
HXCarbonEvent::GetEventHandle(void)
{
return (void*)m_mpSemaphoreID;
}
HX_RESULT
HXCarbonEvent::Wait(UINT32 uTimeoutPeriod)
{
HX_RESULT retval = HXR_OK;
Duration timeout = ( uTimeoutPeriod == ALLFS ) ? kDurationForever : ( uTimeoutPeriod * kDurationMillisecond );
OSStatus osStatus = ::MPWaitOnSemaphore(m_mpSemaphoreID, timeout);
switch (osStatus)
{
case noErr:
if( m_IsManuallyReset )
{
// needs to go back into the raised state until Reset is explicitly called
::MPSignalSemaphore(m_mpSemaphoreID);
}
retval = HXR_OK;
break;
case kMPTimeoutErr:
retval = HXR_WOULD_BLOCK; // winthrd.cpp returns this if the wait times out
break;
default:
retval = HXR_FAIL;
break;
}
return retval;
}
// HXCarbonManualEvent
HXCarbonManualEvent::HXCarbonManualEvent(const char* pEventName)
: m_pMutex(NULL)
, m_bIsSignalled(FALSE)
, m_InternalSemaphoreID(NULL)
{
HXMutex::MakeMutex(m_pMutex);
OSStatus osStatus = ::MPCreateSemaphore(1, 0, &m_InternalSemaphoreID);
}
HXCarbonManualEvent::~HXCarbonManualEvent()
{
OSStatus osStatus = ::MPDeleteSemaphore(m_InternalSemaphoreID);
m_InternalSemaphoreID = NULL;
HX_DELETE(m_pMutex);
}
HX_RESULT
HXCarbonManualEvent::SignalEvent()
{
m_pMutex->Lock();
m_bIsSignalled = TRUE;
::MPSignalSemaphore(m_InternalSemaphoreID); // in case we're waiting.
m_pMutex->Unlock();
return HXR_OK;
}
HX_RESULT
HXCarbonManualEvent::ResetEvent()
{
m_pMutex->Lock();
m_bIsSignalled = FALSE;
::MPWaitOnSemaphore(m_InternalSemaphoreID, kDurationImmediate); // just clear it out...
m_pMutex->Unlock();
return HXR_OK;
}
HX_RESULT
HXCarbonManualEvent::Wait(UINT32 uTimeoutPeriod)
{
BOOL bDone = FALSE;
HX_RESULT retVal = HXR_OK;
while (!bDone)
{
m_pMutex->Lock();
BOOL bIsSignalled = m_bIsSignalled;
m_pMutex->Unlock();
if (bIsSignalled)
{
bDone = TRUE;
}
else
{
// xxxbobclark rely on MP semaphore
Duration timeout = ( uTimeoutPeriod == ALLFS ) ? kDurationForever : ( uTimeoutPeriod * kDurationMillisecond );
OSStatus osStatus = ::MPWaitOnSemaphore(m_InternalSemaphoreID, timeout);
bDone = TRUE;
if (osStatus == kMPTimeoutErr)
{
retVal = HXR_WOULD_BLOCK;
}
}
}
return retVal;
}
void*
HXCarbonManualEvent::GetEventHandle (void)
{
return (void*)this;
}
// HXCarbonMutex
HXCarbonMutex::HXCarbonMutex()
{
MPCreateCriticalRegion(&mCriticalRegion);
}
HXCarbonMutex::~HXCarbonMutex()
{
MPDeleteCriticalRegion(mCriticalRegion);
}
HX_RESULT
HXCarbonMutex::Lock(void)
{
MPEnterCriticalRegion(mCriticalRegion, kDurationForever);
return HXR_OK;
}
HX_RESULT
HXCarbonMutex::Unlock(void)
{
MPExitCriticalRegion(mCriticalRegion);
return HXR_OK;
}
HX_RESULT
HXCarbonMutex::Trylock(void)
{
HX_RESULT retval = HXR_OK;
OSStatus osStatus = MPEnterCriticalRegion(mCriticalRegion, kDurationImmediate);
if (osStatus != noErr)
{
retval = HXR_FAIL;
}
return retval;
}
//HXCarbonSemaphore
HXCarbonSemaphore::HXCarbonSemaphore( UINT32 unInitialCount /*=0*/)
:m_mpSemaphoreID(0)
{
OSStatus osStatus = ::MPCreateSemaphore(INT_MAX, unInitialCount, &m_mpSemaphoreID);
HX_ASSERT(osStatus == noErr);
}
HXCarbonSemaphore::~HXCarbonSemaphore()
{
MPDeleteSemaphore(m_mpSemaphoreID);
}
HX_RESULT HXCarbonSemaphore::Post()
{
HX_RESULT retval = HXR_OK;
OSStatus osStatus = MPSignalSemaphore(m_mpSemaphoreID);
if (osStatus != noErr)
{
retval = HXR_FAIL;
}
return retval;
}
HX_RESULT HXCarbonSemaphore::Wait()
{
HX_RESULT retval = HXR_OK;
OSStatus osStatus = MPWaitOnSemaphore(m_mpSemaphoreID, kDurationForever);
if (osStatus != noErr)
{
retval = HXR_FAIL;
}
return retval;
}
HX_RESULT HXCarbonSemaphore::TryWait()
{
HX_RESULT retval = HXR_OK;
OSStatus osStatus = MPWaitOnSemaphore(m_mpSemaphoreID, kDurationImmediate);
if (osStatus != noErr)
{
retval = HXR_FAIL;
}
return retval;
}
#if 0
// HXCarbonAsyncTimer
HXCarbonAsyncTimer::HXCarbonAsyncTimer(ULONG32 ulTimeOut, HXThread* pReceivingThread)
: m_ulTimeout(ulTimeOut)
, m_pReceivingThread(pReceivingThread)
, m_CarbonTimerUPP(NULL)
, m_CarbonTimerRef(NULL)
, m_msg(NULL)
{
m_msg = new HXThreadMessage(HXMSG_ASYNC_TIMER, (void*)m_ulTimeout, NULL, NULL);
m_CarbonTimerUPP = ::NewEventLoopTimerUPP((EventLoopTimerProcPtr)MyCarbonTimer);
::InstallEventLoopTimer(GetCurrentEventLoop(), 0, kEventDurationMillisecond *
m_ulTimeout, m_CarbonTimerUPP, this, &m_CarbonTimerRef);
}
HXCarbonAsyncTimer::~HXCarbonAsyncTimer()
{
if (m_CarbonTimerRef)
{
::RemoveEventLoopTimer(m_CarbonTimerRef);
m_CarbonTimerRef = NULL;
}
if (m_CarbonTimerUPP)
{
::DisposeEventLoopTimerUPP(m_CarbonTimerUPP);
m_CarbonTimerUPP = NULL;
}
delete m_msg;
}
/* static */
UINT32
HXCarbonAsyncTimer::SetTimer(ULONG32 ulTimeOut, HXThread* pReceivingThread)
{
volatile HXCarbonAsyncTimer* pAsyncTimer = new HXCarbonAsyncTimer(ulTimeOut, pReceivingThread);
return (UINT32) pAsyncTimer;
}
/* static */
void
HXCarbonAsyncTimer::MyCarbonTimer(EventLoopTimerRef, HXCarbonAsyncTimer* pAsyncTimer)
{
HX_ASSERT(pAsyncTimer);
HX_ASSERT(pAsyncTimer->m_pReceivingThread);
HXThreadMessage theMsg(HXMSG_ASYNC_TIMER, (void*)pAsyncTimer->m_ulTimeout, NULL, NULL);
pAsyncTimer->m_pReceivingThread->PostMessage(&theMsg);
}
#endif
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -