⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msgqv.c

📁 samsung 9908DVD源代码,
💻 C
字号:
/**********************************************************************************
 * msgQV.c
 * coded by hspark@ce.cnu.ac.kr
 * date : 2001/06/23
 * modified by hjahn@ce.cnu.ac.kr
 * date : 2003/03/03
 **********************************************************************************/

#include "kernel\\mk_sys.h"
#include "kernel\\mk_msgQV.h"
#include "kernel\\mk_task.h"
#include "util\\mk_lib.h"

#ifdef _MK_MSGQUEUE

VOID
MK_VariableMsgQueueInitialze(VOID)
{
}

STATUS
MK_CreateVariableMsgQueue(MK_VARIABLE_QUEUE *pQueue, CHAR *pName, VOID *pAddr, 
						  long MemorySize, ULONG MaxSize, BOOLEAN Options)
{
	STATUS	Status;
	INT		Flags;

	if(pQueue->vq_Magic == MK_MSGQV_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_CreateVariableMsgQueue() - Magic error!\n");
#endif
		return MK_ERROR;
	}
	
	Flags = MK_InterruptDisable();

	pQueue->vq_pMsgListHead = MK_NULL;
	pQueue->vq_pMsgListTail = MK_NULL;
	pQueue->vq_MaxSize = MaxSize;
	
	Status = MK_CreateHeapManager(&pQueue->vq_Heap, MK_NULL, pAddr, MemorySize, 
		sizeof(MK_VARIABLE_QUEUE_MSG), Options);
	
	if(Status < 0)
	{
#if MK_DEBUG_PRINT
		MK_InfoPrintf(MK_TASK_WARNING, "MK_CreateVariableMsgQueue() - Heap Manager is not created! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
		MK_InterruptRestore( Flags );
		return Status;
	}
		
	Status = MK_CreateSemaphore(&pQueue->vq_ReceiveSemaphore, MK_NULL, 0, Options);
		
	if(Status < 0)
	{
#if MK_DEBUG_PRINT
		MK_InfoPrintf(MK_TASK_WARNING, "MK_CreateVariableMsgQueue() - Heap Manager is not created! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
		MK_InterruptRestore( Flags );
		return Status;
	}
	
	pQueue->vq_Magic = MK_MSGQV_MAGIC;

	MK_InterruptRestore( Flags );
	return MK_NO_ERROR;
}

STATUS
MK_DeleteVariableMsgQueue(MK_VARIABLE_QUEUE *pQueue)
{
	INT	Flags;

	if(pQueue->vq_Magic != MK_MSGQV_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_DeleteVariableMsgQueue() - Magic error! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
		return MK_RESOURCE_ERROR;
	}

	Flags = MK_InterruptDisable();

	MK_DeleteHeapManager(&pQueue->vq_Heap);
	MK_DeleteSemaphore(&pQueue->vq_ReceiveSemaphore);

	pQueue->vq_Magic = 0;
	
	MK_InterruptRestore( Flags );
	return MK_NO_ERROR;
}

ULONG
MK_GetVariableMsgQueueMaxSize(MK_VARIABLE_QUEUE *pQueue)
{
	if(pQueue->vq_Magic != MK_MSGQV_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_GetVariableMsgQueueMaxSize() - Magic error! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
		return 0;
	}

	return pQueue->vq_MaxSize;
}

ULONG
MK_GetVariableMsgQueueAvailableSize(MK_VARIABLE_QUEUE *pQueue)
{
	if(pQueue->vq_Magic != MK_MSGQV_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_GetVariableMsgQueueAvailableSize() - Magic error! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
		return 0;
	}

	return MK_GetHeapMaxMemorySize(&pQueue->vq_Heap)-sizeof(MK_VARIABLE_QUEUE_MSG);
}

INT 
MK_GetVariableMsgQueueMsgCount(MK_VARIABLE_QUEUE *pQueue)
{
	if(pQueue->vq_Magic != MK_MSGQV_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_GetVariableMsgQueueNsgCount() - Magic error! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
		return 0;
	}

	return MK_GetSemaphoreCount(&pQueue->vq_ReceiveSemaphore);
}

STATUS
MK_VariableMsgQueuePend(MK_VARIABLE_QUEUE *pQueue, CHAR **pMsg, ULONG BufSize,
						ULONG *pLength, LONG Ticks)
{
	MK_VARIABLE_QUEUE_MSG *pQueueMsg;
	STATUS	Status;
	INT		Flags;

	if(pQueue->vq_Magic != MK_MSGQV_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_VariableMsgQueuePend() - Magic error! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
		return MK_RESOURCE_ERROR;
	}

	Flags = MK_InterruptDisable();			/* Critical Region */

	Status = MK_SemaphorePend(&pQueue->vq_ReceiveSemaphore, Ticks);
	if( Status < 0)
	{
		*(CHAR *)(*pMsg) = 0;
		*pLength = 0;
#if MK_DEBUG_PRINT
		MK_InfoPrintf(MK_TASK_ERROR, "MK_VariableMsgQueuePend() - Receive semaphorePend is magic error or timeout! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif		
		MK_InterruptRestore(Flags);
		return Status;
	}
	if(pQueue->vq_pMsgListHead == pQueue->vq_pMsgListTail)
	{
		pQueueMsg = pQueue->vq_pMsgListHead;
		pQueue->vq_pMsgListHead = MK_NULL;
		pQueue->vq_pMsgListTail = MK_NULL;
		pQueueMsg->vm_pNext = MK_NULL;
	}
	else
	{
		pQueueMsg = pQueue->vq_pMsgListHead;
		pQueue->vq_pMsgListHead = pQueueMsg->vm_pNext;
		pQueueMsg->vm_pNext = MK_NULL;
	}

	*pLength = (pQueueMsg->vm_Length <= BufSize) ? pQueueMsg->vm_Length : BufSize;
	MK_BlockCopy((CHAR *)(*pMsg), (CHAR *)pQueueMsg->vm_StartOfMsg, *pLength);
	Status = MK_FreeMemory((VOID *)pQueueMsg);

	MK_InterruptRestore(Flags);
	return Status;
}

STATUS
MK_VariableMsgQueuePost(MK_VARIABLE_QUEUE *pQueue, CHAR *pMsg, ULONG Length, 
						LONG Ticks)
{
	MK_VARIABLE_QUEUE_MSG *pQueueMsg;
	STATUS	Status;
	VOID	*pAddr;
	UINT	MaxLength;
	INT		Flags;

	if(pQueue->vq_Magic != MK_MSGQV_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_VariableMsgQueuePost() - Magic error! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
		return MK_RESOURCE_ERROR;
	}
	if(Length ==0)
	{
#if MK_DEBUG_PRINT
		MK_InfoPrintf(MK_TASK_WARNING, "MK_VariableMsgQueuePost() - Length must be great than zero! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
		return MK_ERROR;
	}

	Flags = MK_InterruptDisable();		/* Critical Region */

	MaxLength = ( Length <= pQueue->vq_MaxSize ) ? Length : pQueue->vq_MaxSize;
	Status = MK_GetMemory(&pQueue->vq_Heap, &pAddr, MaxLength+sizeof(MK_VARIABLE_QUEUE_MSG), Ticks);
	if( Status < 0)
	{
#if MK_DEBUG_PRINT
		MK_InfoPrintf(MK_TASK_WARNING, "MK_VariableMsgQueuePost() - Msg is not allocated memory by MK_GetMemory()! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif		
		MK_InterruptRestore(Flags);
		return Status;
	}

	pQueueMsg = (MK_VARIABLE_QUEUE_MSG *)pAddr;
	pQueueMsg->vm_StartOfMsg = (ULONG)pQueueMsg + sizeof(MK_VARIABLE_QUEUE_MSG);

	pQueueMsg->vm_Length = MaxLength;
	
	pQueueMsg->vm_pNext = MK_NULL;

	MK_BlockCopy((char *)pQueueMsg->vm_StartOfMsg, (char *)pMsg, MaxLength);
	
	if(pQueue->vq_pMsgListHead == MK_NULL)
	{
		pQueue->vq_pMsgListHead = pQueueMsg;
		pQueue->vq_pMsgListTail = pQueueMsg;
	}
	else
	{
		pQueue->vq_pMsgListTail->vm_pNext = pQueueMsg;
		pQueue->vq_pMsgListTail = pQueueMsg;
		pQueueMsg->vm_pNext = MK_NULL;
	}

	Status = MK_SemaphorePost(&pQueue->vq_ReceiveSemaphore); 

	MK_InterruptRestore(Flags);
	return Status;
}

STATUS
MK_VariableMsgQueueFrontPost(MK_VARIABLE_QUEUE *pQueue, CHAR *pMsg, ULONG Length, 
						LONG Ticks)
{
	MK_VARIABLE_QUEUE_MSG *pQueueMsg;
	STATUS	Status;
	VOID	*pAddr;
	UINT	MaxLength;
	INT		Flags;

	if(pQueue->vq_Magic != MK_MSGQV_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_VariableMsgQueuePost() - Magic error! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
		return MK_RESOURCE_ERROR;
	}
	if(Length ==0)
	{
#if MK_DEBUG_PRINT
		MK_InfoPrintf(MK_TASK_WARNING, "MK_VariableMsgQueuePost() - Length must be great than zero! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
		return MK_ERROR;
	}

	Flags = MK_InterruptDisable();		/* Critical Region */

	Status = MK_GetMemory(&pQueue->vq_Heap, &pAddr, Length, Ticks);
	if( Status < 0)
	{
#if MK_DEBUG_PRINT
		MK_InfoPrintf(MK_TASK_WARNING, "MK_VariableMsgQueuePost() - Msg is not allocated memory by MK_GetMemory()! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
		MK_InterruptRestore(Flags);
		return Status;
	}

	pQueueMsg = (MK_VARIABLE_QUEUE_MSG *)pAddr;
	pQueueMsg->vm_StartOfMsg = (ULONG)pQueueMsg + sizeof(MK_VARIABLE_QUEUE_MSG);

	MaxLength = ( Length <= pQueue->vq_MaxSize ) ? Length : pQueue->vq_MaxSize;
	pQueueMsg->vm_Length = MaxLength;
	
	pQueueMsg->vm_pNext = MK_NULL;

	MK_BlockCopy((CHAR *)pQueueMsg->vm_StartOfMsg, (CHAR *)pMsg, MaxLength);
	
	if(pQueue->vq_pMsgListHead == MK_NULL)
	{
		pQueue->vq_pMsgListHead = pQueueMsg;
		pQueue->vq_pMsgListTail = pQueueMsg;
	}
	else
	{
		pQueueMsg->vm_pNext = pQueue->vq_pMsgListHead;
		pQueue->vq_pMsgListHead = pQueueMsg;
	}

	Status = MK_SemaphorePost(&pQueue->vq_ReceiveSemaphore); 

	MK_InterruptRestore(Flags);
	return Status;
}

#if 1
/* not implemented - only MK_VariableMsgQueuePost() */
STATUS
MK_VariableMsgQueueBroadcastPost(MK_VARIABLE_QUEUE *pQueue, CHAR *pMsg, ULONG Length, 
						LONG Ticks)
{
	MK_VARIABLE_QUEUE_MSG *pQueueMsg;
	STATUS	Status;
	INT		Flags;

	if(pQueue->vq_Magic != MK_MSGQV_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_VariableMsgQueuePost() - Magic error! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
		return MK_RESOURCE_ERROR;
	}
	if(Length ==0)
	{
#if MK_DEBUG_PRINT
		MK_InfoPrintf(MK_TASK_WARNING, "MK_VariableMsgQueuePost() - Length must be great than zero! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
		return MK_ERROR;
	}

	Flags = MK_InterruptDisable();		/* Critical Region */


	MK_InterruptRestore(Flags);
	return Status;
}
#endif

STATUS
MK_VariableMsgQueueReset(MK_VARIABLE_QUEUE *pQueue)
{
	STATUS status;
	INT Flags;

	Flags = MK_InterruptDisable();

	pQueue->vq_pMsgListHead = MK_NULL;
	pQueue->vq_pMsgListTail = MK_NULL;

	status = MK_ResetHeapManager(&pQueue->vq_Heap);
	status = MK_SemaphoreReset(&pQueue->vq_ReceiveSemaphore,  0);

	MK_Schedule();

	MK_InterruptRestore( Flags );
	return status;
}

#endif /* #ifdef _MK_MSGQUEUE */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -