📄 hxsched.cpp
字号:
/* ***** BEGIN LICENSE BLOCK *****
* Version: RCSL 1.0/RPSL 1.0
*
* Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
*
* The contents of this file, and the files included with this file, are
* subject to the current version of the RealNetworks Public Source License
* Version 1.0 (the "RPSL") available at
* http://www.helixcommunity.org/content/rpsl unless you have licensed
* the file under the RealNetworks Community Source License Version 1.0
* (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
* in which case the RCSL will apply. You may also obtain the license terms
* directly from RealNetworks. You may not use this file except in
* compliance with the RPSL or, if you have a valid RCSL with RealNetworks
* applicable to this file, the RCSL. Please see the applicable RPSL or
* RCSL for the rights, obligations and limitations governing use of the
* contents of the file.
*
* This file is part of the Helix DNA Technology. RealNetworks is the
* developer of the Original Code and owns the copyrights in the portions
* it created.
*
* This file, and the files included with this file, is distributed and made
* available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
*
* Technology Compatibility Kit Test Suite(s) Location:
* http://www.helixcommunity.org/content/tck
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** */
#include "hxcom.h"
#include "hxtypes.h"
#include "hxresult.h"
#ifdef _WINDOWS
#include <windows.h>
#endif
#include "timeval.h"
#include "clientpq.h"
#include "ihxpckts.h"
#include "hxfiles.h"
#include "hxengin.h"
#include "hxcore.h"
#include "hxprefs.h"
#include "timeline.h"
#include "hxtick.h"
#ifdef _MACINTOSH
#include "hx_moreprocesses.h"
#endif
#if defined(_WIN32) || defined(THREADS_SUPPORTED)
#include "casyntim.h"
#endif /*_WIN32*/
#include "hxthread.h"
#include "hxsched.h"
#include "hxheap.h"
#ifdef _DEBUG
#undef HX_THIS_FILE
static const char HX_THIS_FILE[] = __FILE__;
#endif
#if defined(_WINDOWS) && !defined(_WIN32) /* WIN16 */
#define MINIMUM_GRANULARITY 55
#elif defined (_MACINTOSH) /* MACINTOSH */
#define MINIMUM_GRANULARITY 20
#elif defined (_UNIX)
#define MINIMUM_GRANULARITY 20
#else /* ELSE */
#define MINIMUM_GRANULARITY 20
#endif
#define MINIMUM_DIFFERENCE 5
// HXScheduler...
HXScheduler::HXScheduler(IUnknown* pContext) :
m_lRefCount (0)
,m_pScheduler(0)
,m_pInterruptTimeScheduler(0)
,m_pID(0)
,m_pContext(pContext)
,m_bLocked(FALSE)
,m_bUseDeferredTask(TRUE)
#if defined(_WIN32) || defined(THREADS_SUPPORTED)
,m_pAsyncTimer(0)
#endif
,m_pTimeline(0)
,m_ulCurrentGranularity(0)
,m_pCoreMutex(NULL)
,m_ulLastUpdateTime(0)
,m_bIsInterruptEnabled(FALSE)
,m_ulSystemNextDueTime(0)
,m_ulInterruptNextDueTime(0)
,m_headTime(0)
,m_interruptHeadTime(0)
,m_bImmediatesPending(0)
{
m_pID = new CHXID(100);
m_pScheduler = new ClientPQ(m_pID);
m_pInterruptTimeScheduler = new ClientPQ(m_pID);
(void) gettimeofday(&m_CurrentTimeVal, 0);
m_ulLastUpdateTime = HX_GET_TICKCOUNT();
}
HXScheduler::~HXScheduler()
{
StopScheduler();
HX_DELETE(m_pScheduler);
HX_DELETE(m_pInterruptTimeScheduler);
HX_DELETE(m_pID);
#if defined(_WIN32) || defined(THREADS_SUPPORTED)
HX_DELETE(m_pAsyncTimer);
#endif
HX_DELETE(m_pTimeline);
}
/*
* IUnknown methods
*/
/////////////////////////////////////////////////////////////////////////
// Method:
// IUnknown::QueryInterface
// Purpose:
// Implement this to export the interfaces supported by your
// object.
//
STDMETHODIMP HXScheduler::QueryInterface(REFIID riid, void** ppvObj)
{
#ifdef _MACINTOSH
if (IsEqualIID(riid, IID_IHXInterruptState) && m_pContext)
{
return m_pContext->QueryInterface(riid, ppvObj);
}
#endif
QInterfaceList qiList[] =
{
{ GET_IIDHANDLE(IID_IHXScheduler), (IHXScheduler*)this },
{ GET_IIDHANDLE(IID_IUnknown), (IUnknown*)(IHXScheduler*)this },
};
return ::QIFind(qiList, QILISTSIZE(qiList), riid, ppvObj);
}
/////////////////////////////////////////////////////////////////////////
// Method:
// IUnknown::AddRef
// Purpose:
// Everyone usually implements this the same... feel free to use
// this implementation.
//
STDMETHODIMP_(ULONG32) HXScheduler::AddRef()
{
return InterlockedIncrement(&m_lRefCount);
}
/////////////////////////////////////////////////////////////////////////
// Method:
// IUnknown::Release
// Purpose:
// Everyone usually implements this the same... feel free to use
// this implementation.
//
STDMETHODIMP_(ULONG32) HXScheduler::Release()
{
if (InterlockedDecrement(&m_lRefCount) > 0)
{
return m_lRefCount;
}
delete this;
return 0;
}
/*
* HXScheduler methods
*/
/************************************************************************
* Method:
* IHXScheduler::Enter
* Purpose:
* enter objects in the service queue
*/
STDMETHODIMP_(CallbackHandle)
HXScheduler::RelativeEnter(IHXCallback* pCallback, ULONG32 ulTime)
{
/*
* A RelativeEnter() of 0 ms is a special case that needs to be
* AbsoluteEnter() of 0
*/
if (ulTime == 0)
{
HXTimeval rVal;
rVal.tv_sec = rVal.tv_usec = 0;
return AbsoluteEnter(pCallback, rVal);
}
UINT32 usecs = 0;
UINT32 secs = 0;
Timeval lTime;
// handle the possible overflow of UINT32 when
// converting from milli-second to micro-second
if (ulTime > 4000000)
{
secs = ulTime / 1000;
usecs = (ulTime % 1000) * 1000;
}
else
{
secs = 0;
usecs = ulTime * 1000;
if (usecs >= 1000000)
{
secs = usecs / 1000000;
usecs = usecs % 1000000;
}
}
lTime.tv_sec = secs;
lTime.tv_usec = usecs;
Timeval now;
now.tv_sec = m_CurrentTimeVal.tv_sec;
now.tv_usec = m_CurrentTimeVal.tv_usec;
now += lTime;
if (pCallback)
{
IHXInterruptSafe* pInterruptSafeCB = NULL;
if(HXR_OK == pCallback->QueryInterface(IID_IHXInterruptSafe,
(void**) &pInterruptSafeCB))
{
if (pInterruptSafeCB && pInterruptSafeCB->IsInterruptSafe())
{
CallbackHandle hCallback = (CallbackHandle) m_pInterruptTimeScheduler->
enter(now, pCallback);
pInterruptSafeCB->Release();
return hCallback;
}
pInterruptSafeCB->Release();
}
}
return (CallbackHandle) m_pScheduler->enter(now, pCallback);
}
/************************************************************************
* Method:
* IHXScheduler::AbsoluteEnter
* Purpose:
* enter objects in the service queue at absolute time
*/
STDMETHODIMP_(CallbackHandle)
HXScheduler::AbsoluteEnter(IHXCallback* pCallback, HXTimeval tVal)
{
Timeval lTime;
lTime.tv_sec = tVal.tv_sec;
lTime.tv_usec = tVal.tv_usec;
if (pCallback)
{
IHXInterruptSafe* pInterruptSafeCB = NULL;
if(HXR_OK == pCallback->QueryInterface(IID_IHXInterruptSafe,
(void**) &pInterruptSafeCB))
{
if (pInterruptSafeCB && pInterruptSafeCB->IsInterruptSafe())
{
CallbackHandle hCallback = (CallbackHandle) m_pInterruptTimeScheduler->
enter(lTime, pCallback);
pInterruptSafeCB->Release();
return hCallback;
}
pInterruptSafeCB->Release();
}
}
return (CallbackHandle) m_pScheduler->enter(lTime, pCallback);
}
/************************************************************************
* Method:
* IHXScheduler::Remove
* Purpose:
* remove objects from the service queue
*/
STDMETHODIMP HXScheduler::Remove(CallbackHandle Handle)
{
if (m_pInterruptTimeScheduler->removeifexists(Handle))
{
return HXR_OK;
}
m_pScheduler->remove(Handle);
return HXR_OK;
}
/************************************************************************
* Method:
* IHXScheduler::GetCurrentSchedulerTime
* Purpose:
* gives the current time in the timeline of the scheduler...
*/
STDMETHODIMP_(HXTimeval) HXScheduler::GetCurrentSchedulerTime(void)
{
HXTimeval hxTimeval;
hxTimeval.tv_sec = m_CurrentTimeVal.tv_sec;
hxTimeval.tv_usec = m_CurrentTimeVal.tv_usec;
return hxTimeval;
}
/************************************************************************
* Method:
* OnTimeSync
* Purpose:
* TBD
*
*/
HX_RESULT HXScheduler::OnTimeSync(BOOL bAtInterrupt)
{
HX_RESULT theErr = HXR_OK;
if (m_pCoreMutex)
{
m_pCoreMutex->Lock();
}
if (!m_bLocked)
{
m_bLocked = TRUE;
theErr = ExecuteCurrentFunctions (bAtInterrupt);
m_bLocked = FALSE;
}
if (m_pCoreMutex)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -