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

📄 ostype.c

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

/**********************************************************
 * FsmGetBinSem()
 * 
 * Descriptions:
 *        Get the semaphore -- blocked version
 *
 * Parameters:
 * 
 * 
 * Returned: 
 *
 *********************************************************/

uint32 FsmGetBinSem(HBSEM hBinSem)
{
#if (OS_TYPE == OS_NUCLEUS)

	STATUS status;

#ifdef DEBUG_FSM
	MonPrintf("\nEnter FsmGetBinSem() ---->>");
#endif

	if((status = NU_Obtain_Semaphore(hBinSem, NU_SUSPEND)) != NU_SUCCESS)
	{
		return ERR_SYSTEM;
	}

#ifdef DEBUG_FSM
	MonPrintf("\n---->> Exit FsmGetBinSem().");
#endif

	return ERR_NONE;

#else

	if(WaitForSingleObject(hBinSem, INFINITE) != WAIT_OBJECT_0)
		return ERR_SYSTEM;
	else
		return ERR_NONE;

#endif
}

/**********************************************************
 * FsmGetBinSemTimeout()
 * 
 * Descriptions:
 *        Wait the semaphore with time-out.
 *
 * Parameters:
 * 
 * 
 * Returned: 
 *
 *********************************************************/

uint32 FsmGetBinSemTimeout(HBSEM hBinSem, uint32 time_out)
{
#if (OS_TYPE == OS_NUCLEUS)

	STATUS status;

#ifdef DEBUG_FSM
	MonPrintf("\nEnter FsmGetBinSemTimeout() ---->>");
#endif


	if((time_out == NU_SUSPEND)
		|| (time_out == NU_NO_SUSPEND))
	{
		return ERR_PARAM;
	}

	status = NU_Obtain_Semaphore(hBinSem, time_out);

	if(status != NU_SUCCESS)
	{
		if(status == NU_TIMEOUT)
		{
			return ERR_TIMEOUT;
		}
		else
		{
			return ERR_SYSTEM;
		}
	}


#ifdef DEBUG_FSM
	MonPrintf("\n---->> Exit FsmGetBinSemTimeout().");
#endif

	return ERR_NONE;

#else

	DWORD		status;

	status = WaitForSingleObject(hBinSem, time_out);

	if(status == WAIT_OBJECT_0)
		return ERR_NONE;
	else if(status == WAIT_TIMEOUT)
		return ERR_TIMEOUT;
	else
		return ERR_SYSTEM;


#endif
}

/**********************************************************
 * FsmReleaseBinSem()
 * 
 * Descriptions:
 *        Release the semaphore.
 *
 * Parameters:
 * 
 * 
 * Returned: 
 *
 *********************************************************/

uint32 FsmReleaseBinSem(HBSEM hBinSem)
{
#if (OS_TYPE == OS_NUCLEUS)

	STATUS	status;

	char		name[NU_MAX_NAME];
	uint32		current_count;
	uint32		tasks_waiting;
	OPTION		suspend_type;
	NU_TASK *	first_task;

#ifdef DEBUG_FSM
	MonPrintf("\nEnter FsmReleaseBinSem() ---->>");
#endif

	status = NU_Semaphore_Information(hBinSem, 
									 name,
									 &current_count,
									 &suspend_type,
									 &tasks_waiting,
									 &first_task);

	if(status != NU_SUCCESS)
	{
#ifdef DEBUG_FSM
		MonPrintf("\nNU_Semaphore_Information() failed.");
#endif
		return ERR_SYSTEM;
	}

	if(current_count >= 1)
		return ERR_NONE;

	if((status = NU_Release_Semaphore(hBinSem)) != NU_SUCCESS)
	{
#ifdef DEBUG_FSM
		MonPrintf("\nNU_Release_Semaphore() failed.");
#endif
		return ERR_SYSTEM;
	}

#ifdef DEBUG_FSM
	MonPrintf("\n---->> Exit FsmReleaseBinSem().");
#endif

	return ERR_NONE;

#else

	if(ReleaseSemaphore(hBinSem, 1, NULL))
		return ERR_NONE;
	else
		return ERR_SYSTEM;

#endif
}


/*******************************************************
 * FsmCreateMtxSem()
 * 
 * Descriptions: Create a Mutex semaphore.
 * 
 * Parameters:
 * 
 * Returned: NULL, Mutex semaphore Handle.
 *
 *******************************************************/

HMSEM FsmCreateMtxSem(uint8 state)
{
#if (OS_TYPE == OS_NUCLEUS)

	HMSEM		hMtxSem;
	STATUS		status;


#ifdef DEBUG_FSM
	MonPrintf("\nEnter FsmCreateMtxSem() ---->>");
#endif

	/* Allocate memory for semaphore */
	if((hMtxSem = (HMSEM)FsmTryMalloc(sizeof(MtxSemT))) == NULL)
	{
		return NULL;
	}

	if(state >= 1)
	{
		state = 1;
	}
	else
	{
		state = 0;
	}

	if((status = NU_Create_Semaphore(&(hMtxSem->BinSem), "", state, NU_FIFO)) != NU_SUCCESS)
	{
		FsmFree(hMtxSem);

#ifdef DEBUG_FSM
		MonPrintf("\nNU_Create_Semaphore() failed.");
#endif

		return NULL;
	}
	else
	{
		if(state)
		{
			hMtxSem->hOwnerTask = (HTASK)(-1);
			hMtxSem->OwnCount = 0;
		}
		else
		{
			hMtxSem->hOwnerTask = GET_CURRENT_TASK();
			hMtxSem->OwnCount = 1;
		}
	}


#ifdef DEBUG_FSM
	MonPrintf("\n---->> Exit FsmCreateMtxSem().");
#endif

	return hMtxSem;

#else

	if(state != 0)
		state = 0;
	else
		state = 1;

	return CreateMutex(NULL, state, NULL);

#endif
}


/*******************************************************
 * FsmDeleteMtxSem()
 * 
 * Descriptions: Delete the Mutex semaphore.
 * 
 * Parameters:
 * 
 * Returned: ERR_NONE, ERR_SYSTEM.
 *
 *******************************************************/

uint32 FsmDeleteMtxSem(HMSEM hMtxSem)
{
#if (OS_TYPE == OS_NUCLEUS)

	STATUS		status;

#ifdef DEBUG_FSM
	MonPrintf("\nEnter FsmDeleteMtxSem() ---->>");
#endif

	status = NU_Delete_Semaphore(&(hMtxSem->BinSem));

	if(status != NU_SUCCESS)
	{
#ifdef DEBUG_FSM
		MonPrintf("\nNU_Delete_Semaphore() failed.");
#endif
		return ERR_SYSTEM;
	}

	FsmFree(hMtxSem);

#ifdef DEBUG_FSM
	MonPrintf("\n---->> Exit FsmDeleteMtxSem().");
#endif

	return ERR_NONE;

#else

	CloseHandle(hMtxSem);

	return ERR_NONE;

#endif
}

/*******************************************************
 * FsmReleaseMtxSem()
 * 
 * Descriptions: Release the Mutex semaphore.
 * 
 * Parameters:
 * 
 * Returned: ERR_NONE, ERR_SYSTEM.
 *
 *******************************************************/

uint32 FsmReleaseMtxSem(HMSEM  hMtxSem)
{
#if (OS_TYPE == OS_NUCLEUS)

	uint32		status  = ERR_NONE;
	HTASK		hTask = GET_CURRENT_TASK();

#ifdef DEBUG_FSM
	MonPrintf("\nEnter FsmReleaseMtxSem() ---->>");
#endif

	if (hTask == hMtxSem->hOwnerTask)
	{
		hMtxSem->OwnCount--;

		if (hMtxSem->OwnCount == 0)
		{
			/* must clear the hTask field first. */
			hMtxSem->hOwnerTask = (HTASK)(-1);

			status = FsmReleaseBinSem(&(hMtxSem->BinSem));

			if(status != ERR_NONE)
			{
				/* restore the hTask field */
				hMtxSem->hOwnerTask = hTask;
				hMtxSem->OwnCount   = 1;
			}

#ifdef DEBUG_FSM
			MonPrintf("\n---->> Exit FsmReleaseMtxSem() = %d \n", status);
#endif

			return status;
		}
	}
	else
	{
		status = ERR_SYSTEM;
	}

#ifdef DEBUG_FSM
	MonPrintf("\n---->> Exit FsmReleaseMtxSem().");
#endif

	return status;

#else

	if(ReleaseMutex(hMtxSem))
		return ERR_NONE;
	else
		return ERR_SYSTEM;

#endif
}

/********************************************************
 * FsmGetMtxSemTry()
 * 
 * Descriptions: Try to get the Mutex semaphore. --non-blocked
 * 
 * Parameters:
 * 
 * 
 * Returned: ERR_NONE, ERR_SYSTEM.
 *
 *******************************************************/

uint32 FsmGetMtxSemTry(HMSEM hMtxSem)
{
#if (OS_TYPE == OS_NUCLEUS)

	uint32		status;
	HTASK		hTask = GET_CURRENT_TASK();


#ifdef DEBUG_FSM
	MonPrintf("\nEnter FsmGetMtxSemTry() ---->>");
#endif

	if (hTask == hMtxSem->hOwnerTask)
	{
		hMtxSem->OwnCount++;

		status  = ERR_NONE;
	}
	else
	{
		/* Try to get the binary semaphore first. */
		if ((status = FsmGetBinSemTry(&(hMtxSem->BinSem))) == ERR_NONE)
		{
			/* own the semaphore. */
			hMtxSem->hOwnerTask = hTask;
			hMtxSem->OwnCount  = 1;
		}
		else
		{

#ifdef DEBUG_FSM
			MonPrintf("\nSEM_BIN_TRY_WAIT() failed.");
#endif

			status = ERR_SYSTEM;
		}
	}

#ifdef DEBUG_FSM
	MonPrintf("\n---->> Exit FsmGetMtxSemTry().");
#endif

	return status;

#else

	if(WaitForSingleObject(hMtxSem, 0) != WAIT_OBJECT_0)
		return ERR_SYSTEM;
	else
		return ERR_NONE;

#endif
}

/********************************************************
 * FsmGetMtxSem()
 * 
 * Descriptions: Get the Mutex semaphore.
 * 
 * Parameters:
 * 
 * 
 * Returned: ERR_NONE, ERR_SYSTEM.
 *
 *******************************************************/

uint32 FsmGetMtxSem(HMSEM hMtxSem)
{
#if (OS_TYPE == OS_NUCLEUS)

	uint32		status;
	HTASK		hTask = GET_CURRENT_TASK();

#ifdef DEBUG_FSM
	MonPrintf("\nEnter FsmGetMtxSem() ---->>");
#endif

	if (hTask == hMtxSem->hOwnerTask)
	{
		hMtxSem->OwnCount++; 

		status  = ERR_NONE;
	}
	else
	{
		/* Get the binary semaphore first. */
		if ((status = FsmGetBinSem(&(hMtxSem->BinSem))) == ERR_NONE)
		{
			hMtxSem->hOwnerTask = hTask;
			hMtxSem->OwnCount   = 1;
		}
		else
		{

#ifdef DEBUG_FSM
			MonPrintf("\nSEM_BIN_WAIT() failed.");
#endif

			status = ERR_SYSTEM;
		}
	}
	
#ifdef DEBUG_FSM
	MonPrintf("\n---->> Exit FsmGetMtxSem().");
#endif

	return status;

#else

	if(WaitForSingleObject(hMtxSem, INFINITE) != WAIT_OBJECT_0)
		return ERR_SYSTEM;
	else
		return ERR_NONE;

#endif
}



/********************************************************
 * FsmCreateTask()
 * 
 * Descriptions: Create Task.
 * 
 * Parameters:
 * 
 * 
 * Returned: NULL, pointer.
 *
 *******************************************************/

HFSMTASK FsmCreateTask(
	char *		Name, 
	uint32		Priority, 
	uint32		stack_size, 
	uint32		task_entry
	)
{
#if (OS_TYPE == OS_NUCLEUS)

	HFSMTASK	pTaskInfo;
	STATUS		status;

#ifdef DEBUG_FSM
	MonPrintf("\nEnter FsmCreateTask() ---->>");
#endif

	pTaskInfo = FsmTryMalloc(sizeof(FsmTaskInfoT));

	if(pTaskInfo == NULL)
		return NULL;

	pTaskInfo->pStack = FsmTryMalloc(stack_size);

	if(pTaskInfo->pStack == NULL)
	{
		FsmFree(pTaskInfo);
		return NULL;
	}

	status = NU_Create_Task(&pTaskInfo->TaskCB,
	                        Name,
							(TASK_ENTRY)task_entry,
							0,
							NULL,
							pTaskInfo->pStack,
							stack_size,
							Priority,
							0,
							NU_PREEMPT,
							NU_START);

	if(status != NU_SUCCESS)
	{
		FsmFree(pTaskInfo->pStack);
		FsmFree(pTaskInfo);

#ifdef DEBUG_FSM
		MonPrintf("\nNU_Create_Task() failed.");
#endif

		return NULL;
	}

#ifdef DEBUG_FSM
	MonPrintf("\n---->> Exit FsmCreateTask().");
#endif

	return pTaskInfo;

#else

	DWORD	threadId;

	return CreateThread(NULL, 
						stack_size, 
						(LPTHREAD_START_ROUTINE)task_entry,
						NULL, 
						0,
						&threadId
						);

#endif
}


/********************************************************
 * FsmDeleteTask()
 * 
 * Descriptions:
 * 
 * Parameters:
 * 
 * 
 * Returned: ERR_NONE, ERR_SYSTEM.
 *
 *******************************************************/

uint32 FsmDeleteTask(HFSMTASK pTaskInfo)
{
#if (OS_TYPE == OS_NUCLEUS)

	STATUS		status;

#ifdef DEBUG_FSM
	MonPrintf("\nEnter FsmDeleteTask() ---->>");
#endif

	status = NU_Terminate_Task(&pTaskInfo->TaskCB);

	if(status != NU_SUCCESS)
	{
#ifdef DEBUG_FSM
		MonPrintf("\nNU_Terminate_Task() failed.");
#endif
		return ERR_SYSTEM;
	}

	status = NU_Delete_Task(&pTaskInfo->TaskCB);

	if(status != NU_SUCCESS)
	{
#ifdef DEBUG_FSM
		MonPrintf("\nNU_Delete_Task() failed.");
#endif
		return ERR_SYSTEM;
	}

	FsmFree(pTaskInfo->pStack);
	FsmFree(pTaskInfo);

#ifdef DEBUG_FSM
	MonPrintf("\n---->> Exit FsmDeleteTask().");
#endif

	return ERR_NONE;

#else

	TerminateThread(pTaskInfo, 0);
	CloseHandle(pTaskInfo);

	return ERR_NONE;

#endif
}


/*****************************************************************************
* $Log: Ostype.c $
* Revision 1.3  2004/03/17 12:58:49  zgy
* Revision 1.11  2004/03/16 16:00:19  jjs
* Revision 1.10  2003/11/05 10:52:56  zgy
* Revision 1.9  2003/10/15 18:09:29  jjs
* Revision 1.8  2003/09/20 18:11:34  jjs
* Revision 1.7  2003/09/20 17:23:14  jjs
* Revision 1.6  2003/09/15 16:24:15  jjs
* Fix a bug in FsmCreateMtxSem when in WINDOWS mode.
* Revision 1.5  2003/09/14 16:55:59  jjs
* Revision 1.4  2003/09/12 17:22:57  jjs
* Revision 1.3  2003/09/12 11:36:07  jjs
* Correct the parameters when call to TerminateThread.
* Revision 1.2  2003/09/09 19:35:02  jjs
* Added WINDOWS OS support for testing.
* Revision 1.1  2003/09/09 15:08:13  jjs
* Initial revision
*****************************************************************************/


⌨️ 快捷键说明

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