?? sem.c
字號:
/**********************************************************************************
* sem.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_sem.h"
#include "kernel\\mk_task.h"
#include "util\\mk_lib.h"
#include "kernel\\mk_ddi.h"
struct mk_semaphore_struct *MK_pSemaphoreListHead;
struct mk_semaphore_struct *MK_pSemaphoreListTail;
VOID
MK_SemaphoreInitialize(VOID)
{
MK_pSemaphoreListHead = MK_NULL;
MK_pSemaphoreListTail = MK_NULL;
}
STATUS
MK_CreateSemaphore(MK_SEMAPHORE *pSemaphore, CHAR *pName, INT Count, BOOLEAN Options)
{
int Flags;
if(pSemaphore->s_Magic == MK_SEMAPHORE_MAGIC)
{
#if MK_DEBUG_PRINT
MK_Panic("MK_CreateSemaphore() - Magic error!\n");
#endif
return MK_ERROR;
}
if( Count < 0 )
{
#if MK_DEBUG_PRINT
MK_InfoPrintf(MK_TASK_WARNING, "MK_CreateSemaphore() - Count must be great equal zero! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
return MK_ERROR;
}
Flags = MK_InterruptDisable(); /* Critical Region */
pSemaphore->s_pName = pName;
pSemaphore->s_Count = Count;
MK_CreatePendingList(&pSemaphore->s_PendingList, Options);
pSemaphore->s_Magic = MK_SEMAPHORE_MAGIC;
pSemaphore->s_Next = MK_NULL;
pSemaphore->s_Prev = MK_NULL;
if(MK_pSemaphoreListHead == MK_NULL)
{
MK_pSemaphoreListHead = pSemaphore;
MK_pSemaphoreListTail = pSemaphore;
}
else
{
pSemaphore->s_Prev = MK_pSemaphoreListTail;
MK_pSemaphoreListTail->s_Next = pSemaphore;
MK_pSemaphoreListTail = pSemaphore;
}
MK_InterruptRestore(Flags);
return MK_NO_ERROR;
}
INT
MK_GetSemaphoreCount(MK_SEMAPHORE *pSemaphore)
{
if(pSemaphore->s_Magic != MK_SEMAPHORE_MAGIC)
{
#if MK_DEBUG_PRINT
MK_Panic("MK_GetSemaphoreCont() - Magic error! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
return 0;
}
return pSemaphore->s_Count;
}
int
MK_DeleteSemaphore(MK_SEMAPHORE *pSemaphore)
{
int Count;
int Flags;
if(pSemaphore->s_Magic != MK_SEMAPHORE_MAGIC)
{
#if MK_DEBUG_PRINT
MK_Panic("MK_DeleteSemaphore() - Magic error! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
return MK_RESOURCE_ERROR;
}
Flags = MK_InterruptDisable(); /* Critical Region */
Count = MK_ClearPendingList(&pSemaphore->s_PendingList);
MK_DeletePendingList(&pSemaphore->s_PendingList); //Modified
if(MK_pSemaphoreListHead == MK_pSemaphoreListTail)
{
MK_pSemaphoreListHead = MK_NULL;
MK_pSemaphoreListTail = MK_NULL;
}
else
{
if(pSemaphore == MK_pSemaphoreListHead)
{
MK_pSemaphoreListHead = pSemaphore->s_Next;
MK_pSemaphoreListHead->s_Prev = MK_NULL;
}
else if(pSemaphore == MK_pSemaphoreListTail)
{
MK_pSemaphoreListTail = pSemaphore->s_Prev;
MK_pSemaphoreListTail->s_Next = MK_NULL;
}
else
{
pSemaphore->s_Prev->s_Next = pSemaphore->s_Next;
pSemaphore->s_Next->s_Prev = pSemaphore->s_Prev;
}
}
pSemaphore->s_Next = MK_NULL;
pSemaphore->s_Prev = MK_NULL;
/* Clear the semaphore count */
pSemaphore->s_Count = 0;
/* Clear the magic value of semaphore */
pSemaphore->s_Magic = 0;
MK_InterruptRestore(Flags);
return Count;
}
STATUS
MK_SemaphorePend(MK_SEMAPHORE *pSemaphore, long Ticks)
{
MK_TASK *pTask;
int Flags;
if(pSemaphore->s_Magic != MK_SEMAPHORE_MAGIC)
{
#if MK_DEBUG_PRINT
MK_Panic("MK_SemaphorePend() - Magic error! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
return MK_RESOURCE_ERROR;
}
Flags = MK_InterruptDisable(); /* Critical Region */
if(--pSemaphore->s_Count < 0)
{
if(Ticks == 0)
{
pSemaphore->s_Count++;
#if MK_DEBUG_PRINT
// MK_InfoPrintf(MK_TASK_WARNING, "MK_SemaphorePend() - Semaphore is timeout(0)! %s, Task(%s)\n", pSemaphore->s_pName, MK_GetCurrentTask()->t_pName);
#endif
MK_InterruptRestore(Flags);
return MK_TIMEOUT;
}
/* Suspension is selected. */
pTask = MK_GetCurrentTask();
/* Delete task from ReadyList */
MK_DeleteTaskFromReadyList(pTask);
/* Insert task to PendingList */
MK_InsertTaskToPendingList(&pSemaphore->s_PendingList, pTask);
/* If task has timeout, insert task to delayedlist */
if(Ticks > 0)
{
MK_InsertTaskToDelayedList(pTask, Ticks);
}
MK_Schedule();
/* When this Task is released */
if( (pTask->t_Status & MK_TASK_FORCED))
{
pTask->t_Status &= ~MK_TASK_FORCED;
#if MK_DEBUG_PRINT
MK_InfoPrintf(MK_TASK_WARNING, "MK_SemaphorePend() - Semaphore is deleted or reset by force! %s, Task(%s)\n", pSemaphore->s_pName, MK_GetCurrentTask()->t_pName);
#endif
MK_InterruptRestore(Flags);
return MK_RESOURCE_ERROR;
}
if(pTask->t_Status & MK_TASK_PENDING) /* released by TIMEOUT */
{
MK_DeleteTaskFromPendingList(&pSemaphore->s_PendingList, pTask);
pSemaphore->s_Count++;
#if MK_DEBUG_PRINT
// MK_InfoPrintf(MK_TASK_WARNING, "MK_SemaphorePend() - Semaphore is timeout! %s, Task(%s)\n", pSemaphore->s_pName, MK_GetCurrentTask()->t_pName);
#endif
MK_InterruptRestore(Flags);
return MK_TIMEOUT;
}
else
{
MK_InterruptRestore(Flags);
return MK_NO_ERROR;
}
}
else /* Resource Available */
{
MK_InterruptRestore(Flags);
return MK_NO_ERROR;
}
}
STATUS
MK_SemaphorePost(MK_SEMAPHORE *pSemaphore)
{
MK_TASK *pTask;
int Flags;
if(pSemaphore->s_Magic != MK_SEMAPHORE_MAGIC)
{
#if MK_DEBUG_PRINT
MK_Panic("MK_SemaphorePost() - Magic error! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
return MK_RESOURCE_ERROR;
}
Flags = MK_InterruptDisable();
/* Determine if another task is waiting on the semaphore. */
if(pSemaphore->s_Count++ < 0)
{
/* Obtain and remove the first suspended block from the list. */
pTask = MK_GetFirstPendingTask(&pSemaphore->s_PendingList);
MK_DeleteTaskFromPendingList(&pSemaphore->s_PendingList, pTask);
if(pTask->t_Status & MK_TASK_DELAYED)
{
MK_DeleteTaskFromDelayedList(pTask);
}
/* Insert the suspended task to ReadyList */
MK_InsertTaskToReadyList(pTask);
MK_Schedule();
}
MK_InterruptRestore(Flags);
return MK_NO_ERROR;
}
MK_TASK *
MK_GetSemaphorePendingTask(MK_SEMAPHORE *pSemaphore)
{
return MK_GetFirstPendingTask(&pSemaphore->s_PendingList);
}
STATUS
MK_SemaphoreReset(MK_SEMAPHORE *pSemaphore, LONG Count)
{
int Flags;
if(pSemaphore->s_Magic != MK_SEMAPHORE_MAGIC)
{
#if MK_DEBUG_PRINT
MK_Panic("MK_SemaphoreReset() - Magic error! Task(%s)\n", MK_GetCurrentTask()->t_pName);
#endif
return MK_RESOURCE_ERROR;
}
Flags = MK_InterruptDisable(); /* Critical Region */
MK_ClearPendingList(&pSemaphore->s_PendingList);
pSemaphore->s_Count = Count;
MK_InterruptRestore(Flags);
return MK_NO_ERROR;
}
#if _MK_DDI
STATUS
MK_SemaphoreInformation(struct mk_ddi_struct *pDDI)
{
MK_SEMAPHORE *pSemaphore;
INT Flags;
Flags = MK_InterruptDisable(); /* Critical Region */
for(pSemaphore = MK_pSemaphoreListHead; pSemaphore != MK_NULL;
pSemaphore = pSemaphore->s_Next)
{
if(pSemaphore->s_Magic != MK_SEMAPHORE_MAGIC)
{
MK_KernelPanic(("MK_SemaphoreInformation() - Magic error! Task(%s)\n", MK_GetCurrentTask()->t_pName));
return MK_RESOURCE_ERROR;
}
MK_Fprintf(pDDI, "%s ", pSemaphore->s_pName);
MK_Fprintf(pDDI, "%d ", pSemaphore->s_Count);
}
MK_Fprintf(pDDI,"\n");
MK_InterruptRestore(Flags);
return MK_NO_ERROR;
}
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -