📄 hostfunc.c
字号:
/*!
******************************************************************************
@Function HostPageableByteAlloc
@Description Allocate specified number of bytes of pageable memory
@Input ui32Bytes : number of bytes to allocate
@Return Pointer to linear address of allocated pageable memory if success,
returns NULL on failure
******************************************************************************/
IMG_PVOID HostPageableByteAlloc(IMG_UINT32 ui32Bytes)
{
/* Init heap if not done already */
if (hPageableHeap == NULL)
{
#define SYSTEM_MEM_HEAP_SIZE 393216
hPageableHeap = HeapCreate( HEAP_NO_SERIALIZE,
SYSTEM_MEM_HEAP_SIZE,
0); /* Maximum size is unsupported urghh? */
if (hPageableHeap == 0)
{
PVR_DPF((PVR_DBG_ERROR,"HostPageableByteAlloc: failed\n"));
return (NULL);
}
}
return (HeapAlloc(hPageableHeap, HEAP_ZERO_MEMORY, ui32Bytes));
}
/*!
******************************************************************************
@Function HostPageableByteFree
@Description Free memory allocated from HostPageableByteAlloc
@Input *pvBase : Reference to memory
@Return void
******************************************************************************/
void HostPageableByteFree(PVOID pvBase)
{
HeapFree(hPageableHeap, 0, pvBase);
return;
}
/*!
******************************************************************************
@Function HostOpenEvent
@Description Opens a named event allowing interprocess communication
@Input phEvent - pointer to OS dependent event structure
@Return error status
******************************************************************************/
PVRSRV_ERROR HostCreateEvent (PVRSRV_MUTEX_HANDLE *phEvent)
{
volatile IMG_UINT32 *pui32Access = (volatile IMG_UINT32 *)phEvent;
*pui32Access = (IMG_UINT32) CreateEvent(IMG_NULL, IMG_FALSE, IMG_FALSE, IMG_NULL);
if(*pui32Access == IMG_NULL)
return PVRSRV_ERROR_GENERIC;
else
return PVRSRV_OK;
}
/*!
******************************************************************************
@Function HostOpenEvent
@Description Opens a named event allowing interprocess communication
@Input phWaitEventHandle - pointer to OS dependent event structure
@Return error status
******************************************************************************/
PVRSRV_ERROR HostOpenEvent(PVRSRV_MUTEX_HANDLE *phWaitEventHandle)
{
volatile IMG_UINT32 *pui32Access = (volatile IMG_UINT32 *)phWaitEventHandle;
IMG_UINT16 ui16EventName[3];
/* Set up name from passed in Handle */
*((IMG_UINT32*)(ui16EventName)) = *pui32Access;
ui16EventName[2] = 0;/* Null Termination */
*pui32Access = (IMG_UINT32) OpenEvent(EVENT_ALL_ACCESS, IMG_FALSE, (LPCTSTR)ui16EventName);
if(!*pui32Access)
return PVRSRV_ERROR_GENERIC;
else
return PVRSRV_OK;
}
/*!
******************************************************************************
@Function HostSetEvent
@Description Signals an OS dependant event object
@Input hEvent - OS dependent event structure
@Return error status
******************************************************************************/
PVRSRV_ERROR HostSetEvent (PVRSRV_MUTEX_HANDLE hEvent)
{
if(!SetEvent((HANDLE) hEvent))
return PVRSRV_ERROR_GENERIC;
else
return PVRSRV_OK;
}
/*!
******************************************************************************
@Function HostReleaseEvent
@Description releases an OS dependant event object
@Input hEvent - OS dependent event structure
@Return error status
******************************************************************************/
PVRSRV_ERROR HostReleaseEvent (PVRSRV_MUTEX_HANDLE hEvent)
{
if(!CloseHandle((HANDLE) hEvent))
return PVRSRV_ERROR_GENERIC;
else
return PVRSRV_OK;
}
/*!
******************************************************************************
@Function HostCreateMutex
@Description creates a OS dependant mutex object
@Input phMutex - pointer to OS dependent mutex structure
@Return error status
******************************************************************************/
PVRSRV_ERROR HostCreateMutex (PVRSRV_MUTEX_HANDLE *phMutex)
{
/*
The ideal implementation may be `cheap semaphores provided by linux
For now we'll implement spin-locks using `atomic exchange' and Sleep(0)
*/
*phMutex = 0;
return PVRSRV_OK;
}
/*!
******************************************************************************
@Function HostDestroyMutex
@Description destroys an OS dependant mutex object
@Input phMutex - pointer to OS dependent mutex structure
@Return error status
******************************************************************************/
PVRSRV_ERROR HostDestroyMutex (PVRSRV_MUTEX_HANDLE *phMutex)
{
/*
The ideal implementation may be `cheap semaphores provided by linux
For now we'll implement spin-locks using `atomic exchange' and Sleep(0)
*/
/* nothing to do */
return PVRSRV_OK;
}
/*!
******************************************************************************
@Function HostAcquireMutex
@Description locks an OS dependant mutex object
@Input phMutex - pointer to OS dependent mutex structure
@Input bBlock - do we want to block?
@Return error status
******************************************************************************/
PVRSRV_ERROR HostAcquireMutex (PVRSRV_MUTEX_HANDLE *phMutex,
IMG_BOOL bBlock)
{
PVRSRV_ERROR eError;
IMG_UINT32 ui32LockVal;
IMG_UINT32 ui32Result;
IMG_UINT32 ui32Count = 0;
IMG_UINT32 ui32Ms = 0;
IMG_UINT32 *pui32Access = (IMG_UINT32 *)phMutex;
IMG_UINT32 i;
/*
The ideal implementation may be mutex objects
For now we'll implement spin-locks using `atomic exchange' and Sleep(0)
*/
/* get process id and use as lock id */
ui32LockVal = HostGetCurrentProcessID();
if (bBlock)
{
for(i=0; i<WAIT_TRY_COUNT; i++)
{
if(ui32Result = InterlockedTestExchange(pui32Access, 0, ui32LockVal))
{
/* wait some time */
HostWaitus(MAX_HW_TIME_US/WAIT_TRY_COUNT);
}
else
{
break;
}
}
}
else
{
/* try once or more*/
ui32Result = InterlockedTestExchange(pui32Access, 0, ui32LockVal);
}
if(ui32Result == 0)
{
eError = PVRSRV_OK;
}
else
{
#ifdef DEBUG
if (bBlock)
{
PVR_DPF((PVR_DBG_ERROR,"HostAcquireMutex: failed to acquire\n"));
}
else
{
PVR_DPF((PVR_DBG_MESSAGE,"HostAcquireMutex: failed to acquire\n"));
}
#endif
eError = PVRSRV_ERROR_GENERIC;
}
return eError;
}
/*!
******************************************************************************
@Function HostReleaseMutex
@Description Releases an OS dependant mutex object
@Input phMutex - pointer to OS dependent mutex structure
@Return error status
******************************************************************************/
PVRSRV_ERROR HostReleaseMutex (PVRSRV_MUTEX_HANDLE *phMutex)
{
IMG_UINT32 ui32LockVal;
volatile IMG_UINT32 *pui32Access = (volatile IMG_UINT32 *)phMutex;
PVRSRV_ERROR eError = PVRSRV_OK;
/*
The ideal implementation may be `cheap semaphores provided by linux
For now we'll implement spin-locks using `atomic exchange' and Sleep(0)
*/
ui32LockVal = HostGetCurrentProcessID();
if (*pui32Access != ui32LockVal)
{
/* This can happen Legally during resource tidy up */
PVR_DPF((PVR_DBG_ERROR,"HostReleaseMutex: Mutex is not locked with expected value"));
eError = PVRSRV_ERROR_GENERIC;
}
else
{
*pui32Access = 0;
}
return eError;
}
/*!
******************************************************************************
@Function HostCreateResource
@Description creates a OS dependant mutex object
@Input phResource - pointer to OS dependent resource
@Return error status
******************************************************************************/
PVRSRV_ERROR HostCreateResource (PVRSRV_RES_HANDLE *phResource)
{
*phResource = 0;
return PVRSRV_OK;
}
/*!
******************************************************************************
@Function HostDestroyResource
@Description destroys an OS dependant mutex object
@Input phResource - pointer to OS dependent resource
@Return error status
******************************************************************************/
PVRSRV_ERROR HostDestroyResource (PVRSRV_RES_HANDLE *phResource)
{
return PVRSRV_OK;
}
/*!
******************************************************************************
@Function HostLockResource
@Description locks an OS dependant Resource
@Input phResource - pointer to OS dependent Resource
@Input bBlock - do we want to block?
@Return error status
******************************************************************************/
PVRSRV_ERROR HostLockResource (PVRSRV_RES_HANDLE *phResource,
IMG_BOOL bBlock)
{
PVRSRV_ERROR eError;
IMG_UINT32 ui32LockVal;
IMG_UINT32 ui32Result;
IMG_UINT32 ui32Count = 0;
IMG_UINT32 ui32Ms = 0;
IMG_UINT32 *pui32Access = (IMG_UINT32 *)phResource;
IMG_UINT32 i;
/*
The ideal implementation may be kernel sync objects
For now we'll implement spin-locks using `atomic exchange' and Sleep(0)
*/
/* get process id and use as lock id */
ui32LockVal = HostGetCurrentProcessID();
if (bBlock)
{
for(i=0; i<WAIT_TRY_COUNT; i++)
{
if (ui32Result = InterlockedTestExchange(pui32Access, 0, ui32LockVal))
{
/* wait some time */
HostWaitus(MAX_HW_TIME_US/WAIT_TRY_COUNT);
}
else
{
break;
}
}
}
else
{
/* try once or more*/
ui32Result = InterlockedTestExchange(pui32Access, 0, ui32LockVal);
}
if(ui32Result == 0)
{
eError = PVRSRV_OK;
}
else
{
PVR_DPF((PVR_DBG_WARNING,"HostLockResource: failed to locl resource\n"));
eError = PVRSRV_ERROR_GENERIC;
}
return eError;
}
/*!
******************************************************************************
@Function HostUnlockResource
@Description unlocks an OS dependant resource
@Input phResource - pointer to OS dependent resource structure
@Return
******************************************************************************/
PVRSRV_ERROR HostUnlockResource (PVRSRV_RES_HANDLE *phResource)
{
IMG_UINT32 ui32LockVal;
volatile IMG_UINT32 *pui32Access = (volatile IMG_UINT32 *)phResource;
PVRSRV_ERROR eError = PVRSRV_OK;
ui32LockVal = HostGetCurrentProcessID();
if (*pui32Access != ui32LockVal)
{
/* This can happen Legally during resource tidy up */
PVR_DPF((PVR_DBG_ERROR,"HostUnlockResource: Resource is not locked with expected value"));
eError = PVRSRV_ERROR_GENERIC;
}
else
{
*pui32Access = 0;
}
return eError;
}
#ifdef DEBUG
/*!
******************************************************************************
@Function HostIsResourceLocked
@Description tests if resource is locked
@Input phResource - pointer to OS dependent mutex structure
@Return error status
******************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -