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

📄 mbox.c

📁 samsung 9908DVD源代码,
💻 C
字号:
/**********************************************************************************
 * mbox.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_task.h"
#include "util\\mk_lib.h"
#include "kernel\\mk_mbox.h"
#include "kernel\\mk_ddi.h"

#ifdef _MK_MAILBOX

struct mk_msg_mailbox_struct *MK_pMsgMailboxListHead;
struct mk_msg_mailbox_struct *MK_pMsgMailboxListTail;

VOID
MK_MsgMailBoxInitialze(VOID)
{
	MK_pMsgMailboxListHead = MK_NULL;
	MK_pMsgMailboxListTail = MK_NULL;
}

STATUS
MK_CreateMsgMailBox(MK_MBOX *pMBox, CHAR *pName, VOID *pAddr, ULONG Size, 
					BOOLEAN Options)
{	
	INT	 Flags;

	if(pMBox->mb_Magic == MK_MSG_MAILBOX_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_CreateMsgMailBox() - Magic error!\n");
#endif
		return MK_ERROR;
	}
	
	Flags = MK_InterruptDisable();		/* Critical Region */

	pMBox->mb_Msg = (ULONG)pAddr;
	pMBox->mb_StartOfMsg = (ULONG)pAddr;

	pMBox->mb_MsgLength = 0;
	pMBox->mb_BufferSize = Size;
	pMBox->mb_pName = pName;

	MK_CreateSemaphore(&pMBox->mb_SendSemaphore, MK_NULL, 1, Options);

	MK_CreateSemaphore(&pMBox->mb_ReceiveSemaphore, MK_NULL, 0, Options);
	
	pMBox->mb_Magic = MK_MSG_MAILBOX_MAGIC;

	pMBox->mb_pNext = MK_NULL;
	pMBox->mb_pPrev = MK_NULL;
	if(MK_pMsgMailboxListHead == MK_NULL)
	{
		MK_pMsgMailboxListHead = pMBox;
		MK_pMsgMailboxListTail = pMBox;
	}
	else
	{
		pMBox->mb_pPrev = MK_pMsgMailboxListTail;
		MK_pMsgMailboxListTail->mb_pNext = pMBox;
		MK_pMsgMailboxListTail = pMBox;
	}

	MK_InterruptRestore(Flags);

	return MK_NO_ERROR;
}

ULONG
MK_GetMsgMailBoxMemorySize(ULONG Size)
{
	return Size;
}

STATUS
MK_DeleteMsgMailBox(MK_MBOX *pMBox)
{
	int Flags;

	if(pMBox->mb_Magic != MK_MSG_MAILBOX_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_DeleteMsgMailBox() - Magic Error! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
		return MK_RESOURCE_ERROR;
	}

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

	MK_DeleteSemaphore(&pMBox->mb_SendSemaphore);
	MK_DeleteSemaphore(&pMBox->mb_ReceiveSemaphore);
	pMBox->mb_Magic = 0;

	if(MK_pMsgMailboxListHead == MK_pMsgMailboxListTail)
	{
		MK_pMsgMailboxListHead = MK_NULL;
		MK_pMsgMailboxListTail = MK_NULL;
	}
	else
	{
		if(pMBox == MK_pMsgMailboxListHead)
		{
			MK_pMsgMailboxListHead = pMBox->mb_pNext;
			MK_pMsgMailboxListHead->mb_pPrev = MK_NULL;
		}
		else if(pMBox == MK_pMsgMailboxListTail)
		{
			MK_pMsgMailboxListTail = pMBox->mb_pPrev;
			MK_pMsgMailboxListTail->mb_pNext = MK_NULL;
		}
		else
		{
			pMBox->mb_pPrev->mb_pNext = pMBox->mb_pNext;
			pMBox->mb_pNext->mb_pPrev = pMBox->mb_pPrev;
		}
	}
	pMBox->mb_pNext = MK_NULL;
	pMBox->mb_pPrev = MK_NULL;

	MK_InterruptRestore(Flags);
	return MK_NO_ERROR;
}

STATUS
MK_MsgMailBoxReset(MK_MBOX *pMBox)
{
	STATUS status;
	INT		Flags;

	if(pMBox->mb_Magic != MK_MSG_MAILBOX_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_MsgMailBoxReset() - Magic Error! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
		return MK_RESOURCE_ERROR;
	}

	Flags = MK_InterruptDisable();

	pMBox->mb_Msg = pMBox->mb_StartOfMsg;

	status = MK_SemaphoreReset(&pMBox->mb_SendSemaphore, 1);
	status = MK_SemaphoreReset(&pMBox->mb_ReceiveSemaphore, 0);

	MK_Schedule();

	MK_InterruptRestore( Flags );
	return status;
}

STATUS
MK_MsgMailBoxPend(MK_MBOX *pMBox, VOID **pMsg, ULONG BufSize, ULONG *pLength, 
				  LONG Ticks)
{
	STATUS Status;
	ULONG	MaxLength;
	INT Flags;

	if(pMBox->mb_Magic != MK_MSG_MAILBOX_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_MsgMailBoxPend() - Magic Error! MBox(%s)\n", pMBox->mb_pName);
#endif
		return MK_RESOURCE_ERROR;
	}

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

	Status = MK_SemaphorePend(&pMBox->mb_ReceiveSemaphore, Ticks);
	if( Status < 0)
	{
		MK_InterruptRestore( Flags );
		return Status;
	}

	/* Modified by hjahn(2003/03/03) */
#if 1
	MaxLength = (pMBox->mb_MsgLength <= BufSize) ? pMBox->mb_MsgLength : BufSize;
	MK_BlockCopy((CHAR *)*pMsg, (CHAR *)pMBox->mb_Msg, MaxLength);
	*pLength = MaxLength;
	Status = MK_SemaphorePost(&pMBox->mb_SendSemaphore);
#endif

	MK_InterruptRestore(Flags);
	return Status;
}

STATUS
MK_MsgMailBoxPost(MK_MBOX *pMBox, VOID *pMsg, ULONG Length, LONG Ticks)
{
	STATUS	Status;
	ULONG	MaxLength;
	INT		Flags;

	if(pMBox->mb_Magic != MK_MSG_MAILBOX_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_MsgMailBoxPost() - 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_MsgMailBoxPost() - Length must be great then zero! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
		return MK_ERROR;
	}

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

	Status = MK_SemaphorePend(&pMBox->mb_SendSemaphore, Ticks);
	if( Status < 0)
	{
		MK_InterruptRestore( Flags );
		return Status;
	}

	/* Modified by hjahn (2003/03/03) */
#if 1
	MaxLength = (Length <= pMBox->mb_BufferSize) ? Length : pMBox->mb_BufferSize;
	pMBox->mb_MsgLength = MaxLength;

	MK_BlockCopy((CHAR *)pMBox->mb_Msg, (CHAR *)pMsg, MaxLength);
#endif 

	Status = MK_SemaphorePost(&pMBox->mb_ReceiveSemaphore);

	MK_InterruptRestore(Flags);

	return Status;
}

#if 0
/* not implemented - only MK_MsgMailBoxPost() */
STATUS
MK_MsgMailBoxBroadcastPost(MK_MBOX *pMBox, VOID *pMsg, ULONG Length, LONG Ticks)
{
	STATUS	Status;
	int		Flags;

	if(pMBox->mb_Magic != MK_MSG_MAILBOX_MAGIC)
	{
#if MK_DEBUG_PRINT
		MK_Panic("MK_MsgMailBoxBroadPost() - 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_MsgMailBoxBroadcatPost() - Length must be great then zero! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
		return MK_ERROR;
	}

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


	MK_InterruptRestore(Flags);
	return Status;
}
#endif

#if _MK_DDI
STATUS
MK_MessageMailboxInformation(struct mk_ddi_struct *pDDI)
{
	MK_MBOX *pMBox;
	int Flags;

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

	for(pMBox = MK_pMsgMailboxListHead; pMBox != 0; pMBox = pMBox->mb_pNext)
	{
		MK_Fprintf(pDDI, "%s ", pMBox->mb_pName);
	}

	MK_Fprintf(pDDI,"\n");

	MK_InterruptRestore(Flags);

	return MK_NO_ERROR;
}
#endif	/* #if MK_DDI */

#endif /* #ifdef _MK_MAILBOX */


⌨️ 快捷键说明

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