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

📄 fsmqueue.c

📁 norflash的文件系统。 用于中低端手机开发的参考
💻 C
📖 第 1 页 / 共 3 页
字号:

			FsmWriteQueue.LastP = NULL;
		}

		if (CurP->type == QUEUE_DATA_ITEM)
		{
			DevP = FsmDataItemDevObjP;
		}
		else
		{
			FdP = &FsmDfsFds[CurP->ItemFd.fd];
			DevP = FdP->FdHdr.DevObjP;
		}
		
		/* MODIFY ACCUMULATED COUNT */
		switch (CurP->cmd)
		{
		case CMD_CREATE:
			FsmWriteQueue.AccumCnt -= ACCUM_CNT_CREATE;
			break;
	
		case CMD_MKDIR:
			FsmWriteQueue.AccumCnt -= ACCUM_CNT_MKDIR;
			break;
	
		case CMD_APPEND:
			SectorSize = ((FsmFlashMediaT*)DevP->MediaObjP)->SectorSize;
			FsmWriteQueue.AccumCnt -= ACCUM_CNT_APPEND(CurP->offset, CurP->length, SectorSize);
			break;
	
		case CMD_MODIFY:
			FsmWriteQueue.AccumCnt -= ACCUM_CNT_MODIFY;
			break;
	
		case CMD_TRUNCATE:
			FsmWriteQueue.AccumCnt -= ACCUM_CNT_TRUNCATE;
			break;
	
		case CMD_DELETE:
			FsmWriteQueue.AccumCnt -= ACCUM_CNT_DELETE;
			break;

		case CMD_RMDIR:
			FsmWriteQueue.AccumCnt -= ACCUM_CNT_RMDIR;
			break;
	
		case CMD_RENAME:
			FsmWriteQueue.AccumCnt -= ACCUM_CNT_RENAME;
			break;
		}

		if (CurP->SemEvent != NULL)
		{
			FsmReleaseBinSem(CurP->SemEvent);
		}

		FsmFree((void *)CurP);

		if (FsmWriteQueue.FirstP == NULL)
		{
			FsmWriteQueue.AccumCnt = ACCUM_START_VALUE;
			FsmGetBinSemTry(FsmWriteQueue.SyncLock);
			FsmReleaseBinSem(FsmWriteQueue.WaitQueueEmptyLock);
		}
	}

	FsmReleaseMtxSem(FsmWriteQueue.QueueMtxLock);

	return ERR_NONE;
}

uint32 QueueReadData(
	uint8		type, 
	uint32		fd, 
	uint32		offset, 
	uint8 *		DataP, 
	uint32		length,
	uint32 *	read_size
	)
{
	uint32			status = ERR_NONE;
	uint32			len;
	FsmQueueItemT * CurP;

	if (QueueCreated == 0)
		return ERR_INIT;

	status = FsmGetMtxSem(FsmWriteQueue.QueueMtxLock);

	if (status != ERR_NONE)
	{
		return status;
	}

	CurP = FsmWriteQueue.FirstP;

	for ( ; CurP != NULL; CurP = CurP->NextP)
	{
		if ((CurP->ItemFd.fd != fd) || (CurP->type != type))
			continue;

		if ((CurP->cmd != CMD_APPEND) && (CurP->cmd != CMD_MODIFY))
			continue; 

		if ((CurP->offset <= offset) && ((CurP->offset + CurP->length) > offset))
		{
			if ((CurP->offset + CurP->length) >= (offset + length))
				len = length;
			else
				len = CurP->offset + CurP->length - offset;

			FsmMemoryMove(DataP, ((uint8 *)(CurP + 1)) + (offset - CurP->offset), len);

			if(len > (*read_size))
			{
				*read_size = len;
			}
		}
		else if ((CurP->offset >= offset) && (CurP->offset < (offset + length)))
		{
			if ((CurP->offset + CurP->length) >= (offset + length))
				len = offset + length - CurP->offset;
			else
				len = CurP->length;

			FsmMemoryMove(DataP + (CurP->offset - offset), (uint8 *)(CurP + 1), len);

			if((CurP->offset - offset) > (*read_size))
			{
#ifdef DEBUG_FSM
				MonPrintf("\n\n There is a bug in FSM. :( :( :( \n\n");
#endif /* DEBUG_FSM */
				/* There MUST be a bug if reaches here. */
				*read_size = 0;
				status = ERR_SYSTEM;
				break;
			}

			if((CurP->offset - offset) + len > (*read_size))
			{
				*read_size = (CurP->offset - offset) + len;
			}
		}
	}

	FsmReleaseMtxSem(FsmWriteQueue.QueueMtxLock);

	return status;
}

/*#############################################################################
  ### QueueReadData
  ###
  ### DESCRIPTION:
  ###   This function reads data from the queue according to
  ###    these input parameters.
  ###   
  ### PARAMETERS:
  ###    IN:  
  ###         fd       file descriptor;
  ###         offset   Start point of read data.
  ###         DataP    Points to data object to store data.
  ###         length   Byte size of data object.
  ###         
  ###    OUT: 
  ###
  ### RETURNS:
  ###    Returns the following errors codes:
  ###         ERR_NONE
  ###         ERR_SYSTEM
  ###
 */
uint32 QueueReadFile(
	uint32		fd, 
	uint32		offset, 
	uint8 *		DataP, 
	uint32		length,
	uint32 *	read_size
	)
{
	return QueueReadData(
						QUEUE_FILE_DIR, 
						fd, 
						offset, 
						DataP, 
						length, 
						read_size);
}

/*#############################################################################
  ### QueueReadData
  ###
  ### DESCRIPTION:
  ###   This function reads data from the queue according to
  ###    these input parameters.
  ###   
  ### PARAMETERS:
  ###    IN:  
  ###         ItemType
  ###         ItemId
  ###         offset   Start point of read data.
  ###         DataP    Points to data object to store data.
  ###         length   Byte size of data object.
  ###         
  ###    OUT: 
  ###
  ### RETURNS:
  ###    Returns the following errors codes:
  ###         ERR_NONE
  ###         ERR_SYSTEM
  ###
 */
uint32 QueueReadDataItem(
	uint16		ItemType,
	uint16		ItemId, 
	uint32		offset, 
	uint8 *		DataP, 
	uint32		length,
	uint32 *	read_size
	)
{
	FsmItemFdT	itemfd;

	itemfd.Item.ItemType = ItemType;
	itemfd.Item.ItemId = ItemId;

	return QueueReadData(
						QUEUE_DATA_ITEM, 
						itemfd.fd, 
						offset, 
						DataP, 
						length, 
						read_size);
}

/*#############################################################################
  ### WaitQueueEmpty
  ###
  ### DESCRIPTION:
  ###   This function waits the queue empty.
  ###
  ### PARAMETERS:
  ###    IN:  
  ###         timeout   Timeout interval in milliseconds.
  ###                   The function returns if the interval elapses.
  ###                   If it is WAIT_TRY, the function returns immediately.
  ###                   If it is WAIT_FOREVER, the function don't return
  ###                   until queue is empty. 
  ###
  ###    OUT: 
  ###
  ### RETURNS:
  ###    Returns the following errors codes:
  ###         ERR_NONE
  ###         ERR_SYSTEM
  ###
 */
uint32 WaitQueueEmpty(uint32 timeout)
{
	uint32		ret;

	if (QueueCreated == 0)
		return ERR_INIT;

	ret = FsmGetMtxSem(FsmWriteQueue.QueueMtxLock);
	if (ret != ERR_NONE)
	{
		return ret;
	}

	if (FsmWriteQueue.FirstP != NULL)
	{
		FsmGetBinSemTry(FsmWriteQueue.WaitQueueEmptyLock);
		FsmReleaseMtxSem(FsmWriteQueue.QueueMtxLock);

		if (timeout == WAIT_TRY)
		{
			/* ret = ERR_PARAM; */
			ret = FsmGetBinSemTry(FsmWriteQueue.WaitQueueEmptyLock);
		}
		else if (timeout == WAIT_FOREVER)
		{
			ret = FsmGetBinSem(FsmWriteQueue.WaitQueueEmptyLock);
		}
		else
		{
			ret = FsmGetBinSemTimeout(FsmWriteQueue.WaitQueueEmptyLock, timeout);
		}

		if (ret != ERR_NONE)
			return ret;
	}
	else
	{
		FsmReleaseMtxSem(FsmWriteQueue.QueueMtxLock);
	}

	return ERR_NONE;
}


uint32 WaitEmpty(uint8 type, uint32 fd, uint32 timeout)
{
	uint32				ret;
	FsmQueueItemT *		CurP;
	FsmQueueItemT *		TargetP;

	if (QueueCreated == 0)
		return ERR_INIT;

	ret = FsmGetMtxSem(FsmWriteQueue.QueueMtxLock);
	if (ret != ERR_NONE)
	{
		return ret;
	}

	TargetP = NULL;

	for ( CurP = FsmWriteQueue.FirstP; CurP != NULL; CurP = CurP->NextP)
	{
		if ((CurP->ItemFd.fd == fd) && (CurP->type == type))
		{
			TargetP = CurP;
		}
	}

	if (TargetP != NULL)
	{
		if (TargetP->SemEvent == NULL)
		{
			HBSEM		sem_bin;

			sem_bin = FsmCreateBinSem(0);

			TargetP->SemEvent = sem_bin;

			if (TargetP->SemEvent == NULL)
			{
				FsmReleaseMtxSem(FsmWriteQueue.QueueMtxLock);
				return ERR_SYSTEM;
			}
			else
			{
				FsmReleaseMtxSem(FsmWriteQueue.QueueMtxLock);

				if(timeout == WAIT_TRY)
				{
					/* ret = ERR_PARAM; */
					ret = FsmGetBinSemTry(sem_bin);
				}
				else if(timeout == WAIT_FOREVER)
				{
					ret = FsmGetBinSem(sem_bin);
				}
				else
				{
					ret = FsmGetBinSemTimeout(sem_bin, timeout);
				}

				FsmDeleteBinSem(sem_bin);

				return ret;
			}
		}
		else
		{
#ifdef DEBUG_FSM
			MonPrintf("\nHi, someone has already waited on me!");
#endif

			FsmReleaseMtxSem(FsmWriteQueue.QueueMtxLock);

			return ERR_SYSTEM;
		}
	}

	FsmReleaseMtxSem(FsmWriteQueue.QueueMtxLock);

	return ERR_NONE;
}

/*#############################################################################
  ### WaitFdEmpty
  ###
  ### DESCRIPTION:
  ###   This function waits these queue items empty whose memeber 'fd'
  ###   value is equal to input parameter 'fd' value.
  ###
  ### PARAMETERS:
  ###    IN:  
  ###         fd        File descriptor.
  ###         timeout   Timeout interval in milliseconds.
  ###                   The function returns if the interval elapses.
  ###                   If it is WAIT_TRY, the function returns immediately.
  ###                   If it is WAIT_FOREVER, the function don't return
  ###                   until queue is empty. 
  ###         
  ###    OUT: 
  ###
  ### RETURNS:
  ###    Returns the following errors codes:
  ###         ERR_NONE
  ###         ERR_SYSTEM
  ###
 */
uint32 WaitFdEmpty(uint32 fd, uint32 timeout)
{
	return WaitEmpty(QUEUE_FILE_DIR, fd, timeout);
}

/*#############################################################################
  ### WaitTypeIdEmpty
  ###
  ### DESCRIPTION:
  ###   This function waits these queue items empty whose memeber
  ###   'ItemType' and 'ItemId' value are equal to input parameter
  ###   'ItemType' and 'ItemId' value.
  ###
  ### PARAMETERS:
  ###    IN:  
  ###         ItemType
  ###         ItemId
  ###         timeout   Timeout interval in milliseconds.
  ###                   The function returns if the interval elapses.
  ###                   If it is WAIT_TRY, the function returns immediately.
  ###                   If it is WAIT_FOREVER, the function don't return
  ###                   until queue is empty. 
  ###         
  ###    OUT: 
  ###
  ### RETURNS:
  ###    Returns the following errors codes:
  ###         ERR_NONE
  ###         ERR_SYSTEM
  ###
 */
uint32 WaitTypeIdEmpty(uint16 ItemType, uint16 ItemId, uint32 timeout)
{
	FsmItemFdT	itemfd;

	itemfd.Item.ItemType = ItemType;
	itemfd.Item.ItemId = ItemId;
	
	return WaitEmpty(QUEUE_DATA_ITEM, itemfd.fd, timeout);
}

/*#############################################################################
  ### QueueGetDataItemLength
  ###
  ### DESCRIPTION:
  ###   This function gets the current length of data item.
  ###
  ### PARAMETERS:
  ###    IN:  
  ###         ItemType
  ###         ItemId
  ###         
  ###    OUT: 
  ###
  ### RETURNS:
  ###    Returns the following errors codes:
  ###         ERR_NONE
  ###         ERR_NOTEXIST
  ###
 */
uint32 QueueGetDataItemLength(uint16 ItemType, uint16 ItemId, uint32 * LengthP)
{
	uint32				ret;
	FsmQueueItemT *		CurP;
	FsmItemFdT			itemfd;
	uint8				type = QUEUE_DATA_ITEM;
	uint32				MaxLength = 0;
	uint8				IsExisted = 0;
	
	itemfd.Item.ItemType = ItemType;
	itemfd.Item.ItemId = ItemId;

	if (QueueCreated == 0)
		return ERR_INIT;

	ret = FsmGetMtxSem(FsmWriteQueue.QueueMtxLock);
	if (ret != ERR_NONE)
	{
		return ret;
	}

	for ( CurP = FsmWriteQueue.FirstP; CurP != NULL; CurP = CurP->NextP)
	{
		if ((CurP->ItemFd.fd == itemfd.fd) && 
			(CurP->type == type) &&
			((CurP->cmd ==  CMD_CREATE) ||
			(CurP->cmd ==  CMD_APPEND)))
		{
			if ((CurP->length + CurP->offset) >= MaxLength)
			{
				MaxLength = CurP->length + CurP->offset;
				IsExisted = 1;
			}
		}
	}

	FsmReleaseMtxSem(FsmWriteQueue.QueueMtxLock);

	if (IsExisted)
	{
		*LengthP = MaxLength;
		return ERR_NONE;
	}
	else
	{
		*LengthP = 0;
		return ERR_NOTEXIST;
	}
}

/*****************************************************************************
* $Log: FsmQueue.c $
* Revision 1.3  2004/03/17 12:57:30  zgy
* Revision 1.30  2004/03/16 15:54:35  jjs
* Revision 1.29  2003/11/05 13:55:23  zgy
* Revision 1.28  2003/11/05 10:24:49  zgy
* Revision 1.27  2003/10/24 14:18:29  zgy
* Revision 1.26  2003/10/24 14:09:28  zgy
* append func: QueueGetDataItemLength.
* Revision 1.25  2003/10/22 12:10:04  jjs
* Revision 1.24  2003/10/16 10:56:06  jjs
* Revision 1.23  2003/10/10 13:22:02  zgy
* Add api func: FsmReadFile. FsmReadDataItem.
* Revision 1.22  2003/10/09 13:19:29  zgy
* add function WaitTypeIdEmpty.
* Revision 1.21  2003/10/09 10:18:26  zgy
* Modify QueueAddDataItem
* Revision 1.20  2003/10/08 15:12:50  zgy
* modify QueueAddDataItem.
* Revision 1.19  2003/10/08 12:01:43  jjs
* Revision 1.18  2003/09/27 15:14:54  zgy
* Revision 1.17  2003/09/27 14:44:31  zgy
* Modify QueueAddDataItem Input parameter
* Revision 1.16  2003/09/27 14:22:21  zgy
* Add queue api function: QueueAdd.
* Modify QueueAddDataItem function.
* Revision 1.15  2003/09/20 17:58:48  jjs
* Revision 1.14  2003/09/18 22:04:02  jjs
* Fix a memory leak in the QueueAddFileDir for CMD_TRANCATE.
* Revision 1.13  2003/09/18 14:50:27  zgy
* Revision 1.12  2003/09/16 21:50:23  jjs
* Revision 1.11  2003/09/16 17:58:53  zgy
* Revision 1.10  2003/09/16 15:51:06  zgy
* add error check
* Revision 1.9  2003/09/15 16:19:13  zgy
* Modify GetAccumSize function.
* Revision 1.8  2003/09/15 13:02:19  zgy
* Modify QueuePeek, WaitFdEmpty, WaitQueueEmpty function.
* Revision 1.7  2003/09/14 17:18:15  jjs
* Revision 1.6  2003/09/12 15:50:13  zgy
* Revision 1.5  2003/09/11 17:22:16  jjs
* Set the WaitQueueEmptyLock initial state to signaled.
* Revision 1.4  2003/09/11 15:56:50  jjs
* Added some sanity check code and some comments.
* Revision 1.2  2003/09/09 16:33:02  zgy
* Revision 1.1  2003/09/09 15:06:58  zgy
* Initial revision
*****************************************************************************/

⌨️ 快捷键说明

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