📄 osapi_linux.cpp
字号:
*
* @retval
* This function is always successful.
*/
void OS_TaskDelayMsec(int milliseconds)
{
usleep(milliseconds*1000);
}
/**
* OS Task Priority Set function
*
* @param
* int taskId - task identifier
*
* @param
* int iPrio - task priority
*
* @retval
* OS_OK if successful.
* OS_FAILURE if not successful.
*/
OS_STATUS OS_TaskPrioritySet(int taskId, int iPrio)
{
OS_TASK *pthreadRecord;
/*
* Cast the given task identifier to a pointer to a task record
*/
pthreadRecord = (OS_TASK *)taskId;
/*
* Make sure the given priority value is valid
*/
if( (iPrio < 1) || (iPrio > 64) )
{
DBGPRINT(DBG_ON(DBG_ERROR), ("OS_TaskPrioritySet: Invalid priority value (%d)!\n", iPrio));
return OS_FAILURE;
}
/*
* Set the priority of the given thread
*/
/* <TBD> */
/*
* Return successfully
*/
return OS_OK;
}
/**
* OS Task Identify Self function.
*
* @param
* None.
*
* @retval
* int tHandle - handle of current task, if successful
* OS_FAILURE - if not successful
*/
ULONG OS_TaskIdSelf(void)
{
return((ULONG)pthread_self());
}
/**
* OS Task Lock function.
*
* @param
* None.
*
* @retval
* Returns FALSE on success, TRUE on failure.
*/
BOOLEAN OS_TaskLock(void)
{
if (pthread_mutex_lock(&global_mutex) != 0)
{
return (TRUE);
}
return (FALSE);
}
/**
* OS Task Unlock function.
*
* @param
* None.
*
* @retval
* Returns FALSE on success, TRUE on failure.
*/
BOOLEAN OS_TaskUnlock(void)
{
if (pthread_mutex_unlock(&global_mutex) != 0)
{
return (TRUE);
}
return (FALSE);
}
/**
* OS Task Name function.
*
* @param
* OS_TASK taskId - task identifier
*
* @retval
* char *strTaskName - task name, if successful
* NULL - if not successful
*/
char *OS_TaskName(int taskId)
{
printf("OS_TaskName: NOT IMPLEMENTED!\n");
return (char *)NULL;
}
/**
* OS Task Verify function.
*
* @param
* OS_TASK taskId - task identifier
*
* @retval
* OS_OK if successful.
* OS_FAILURE if not successful.
*/
OS_STATUS OS_TaskVerify(OS_TASK taskId)
{
printf("OS_TaskVerify: NOT IMPLEMENTED!\n");
return OS_FAILURE;
}
/**
* OS Task Name to Identifier function.
*
* @param
* char *strName - task name
*
* @retval
* int taskId - identifier of current task, if successful
* 0 - if not successful
*/
int OS_TaskNameToId(char *strName)
{
printf("OS_TaskNameToId: NOT IMPLEMENTED!\n");
return 0;
}
/**
* OS Task Show PC function.
*
* @param
* int taskId - task identifier
*
* @retval
* None.
*/
void OS_TaskShowPc(int taskId)
{
printf("OS_TaskShowPc: NOT IMPLEMENTED!\n");
}
/******************************************************************************
*******************************************************************************
** **
** Message queue function definitions **
** **
*******************************************************************************
******************************************************************************/
/**
* OS_MsgQCreate creates a message queue.
*
* @param int iDepth - queue depth
* @param int iMsgSize - message size, all messages placed into this queue must be of this size.
* @param int iOSOptions - operating system options (not used in this implementation)
*
* @retval
* OS_MSG_Q_ID mqId - pointer to the message box record, if successful
* 0 - if not successful
*/
OS_MSG_Q_ID OS_MsgQCreate(int iDepth, int iMsgSize, int iOSOptions)
{
OS_MBOX *pmsg = NULL;
/* Check the validity of the given message size */
if (iMsgSize <= 0)
{
DbgPrint(("%s: Invalid message size (%d)!\n", __FUNCTION__, iMsgSize));
goto error_handler;
}
/* Check the validity of the given depth */
if (iDepth <= 0)
{
DbgPrint(("%s: Invalid queue depth (%d)!\n", __FUNCTION__, iDepth));
goto error_handler;
}
/* Allocate Record */
pmsg = (OS_MBOX *)OS_MemAlloc(sizeof(OS_MBOX));
if (NULL == pmsg)
{
goto error_handler;
}
/* Allocate Buffers */
pmsg->pbBuffers = (BYTE *)OS_MemAlloc(iMsgSize * (iDepth + 1));
if (NULL == pmsg->pbBuffers)
{
goto error_handler;
}
/* Setup Head and Tail */
/* If Head == Tail Buffers are empty */
pmsg->ulHead = 0;
pmsg->ulTail = 0;
pmsg->ulDepth = (ULONG)iDepth;
pmsg->ulSize = (ULONG)iMsgSize;
/* Create the binary semaphore */
if (pthread_cond_init(&pmsg->condWaitEmpty, NULL) != 0)
{
DBGPRINT(DBG_ON(DBG_ERROR), ("OS_MsgQCreate: Could not init condWait"));
goto error_handler;
}
if (pthread_cond_init(&pmsg->condWaitFull, NULL) != 0)
{
DBGPRINT(DBG_ON(DBG_ERROR), ("OS_MsgQCreate: Could not init condWait"));
goto error_handler;
}
if (pthread_mutex_init(&pmsg->mutex, NULL) != 0)
{
DBGPRINT(DBG_ON(DBG_ERROR), ("OS_MsgQCreate: Could not init mutex"));
goto error_handler;
}
return ((OS_MSG_Q_ID)pmsg);
error_handler:
if (NULL != pmsg)
{
if (NULL != pmsg->pbBuffers)
{
OS_MemFree(pmsg->pbBuffers);
}
OS_MemFree(pmsg);
}
return (0);
}
/**
* OS Message Queue Delete function.
*
* @param
* OS_MSG_Q_ID mqId - message queue identifier
*
* @retval
* Returns OS_OK.
* This function is always successful.
*/
OS_STATUS OS_MsgQDelete(OS_MSG_Q_ID mqId)
{
OS_MBOX *pmsg = (OS_MBOX *)mqId;
if (mqId == 0)
{
DbgPrint(("%s: BAD POINTER\n", __FUNCTION__));
return (OS_FAILURE);
}
/* take the mutex to protect during cond destroy and to make sure noone else is holding the mutex */
pthread_mutex_lock(&pmsg->mutex);
/* Destroy the wait conditions */
if (pthread_cond_destroy(&pmsg->condWaitEmpty) != 0)
{
DBGPRINT(DBG_ON(DBG_ERROR), ("OS_MsgQDelete: Could not destroy condWait\n"));
}
if (pthread_cond_destroy(&pmsg->condWaitFull) != 0)
{
DBGPRINT(DBG_ON(DBG_ERROR), ("OS_MsgQDelete: Could not destroy condWait\n"));
}
/* you cannot destroy a mutex that is locked */
pthread_mutex_unlock(&pmsg->mutex);
if (pthread_mutex_destroy(&pmsg->mutex) != 0)
{
DBGPRINT(DBG_ON(DBG_ERROR), ("OS_MsgQDelete: Could not destroy mutex\n"));
}
/* DeaAllocate Buffers */
OS_MemFree(pmsg->pbBuffers);
/* Deallocate Record */
OS_MemFree(pmsg);
/* Return success */
return (OS_OK);
}
/**
* OS Message Queue Send function.
*
* @param OS_MSG_Q_ID mqId - message queue identifier
* @param char *strMsg - message pointer
* @param OS_UINT uiSize - message size
* @param int iTimeout - send timeout value, in ticks. (-1 = wait forever, 0 = no wait, > 1 = wait)
* @param int iPrio - message priority (not used)
*
* @retval
* OS_OK if successful
* OS_FAILURE if not successful
* OS_TIMEOUT if a timeout occurred
*/
OS_STATUS OS_MsgQSend(OS_MSG_Q_ID mqId, char *strMsg, ULONG ulSize, int iTimeout, int iPrio)
{
OS_STATUS iRet = OS_OK;
OS_MBOX *pmsg = (OS_MBOX *)mqId;
struct timeval tv;
struct timespec abstime;
/* Check the validity of message queue */
if (mqId == 0)
{
DbgPrint(("%s: BAD POINTER!\n", __FUNCTION__));
goto error_handler;
}
/* Check the validity of message */
if (strMsg == 0)
{
DbgPrint(("%s: BAD POINTER!\n", __FUNCTION__));
goto error_handler;
}
if (ulSize != pmsg->ulSize)
{
DbgPrint(("%s:%u - Error here\n", __FILE__, __LINE__));
goto error_handler;
}
/* Get Mutex */
if (pthread_mutex_lock(&pmsg->mutex) != 0)
{
DbgPrint(("%s: Failed to obtain lock\n",__FUNCTION__));
goto error_handler;
}
if ( (OS_WAIT_FOREVER != iTimeout) && (OS_NO_WAIT != iTimeout) )
{
/* get the current time */
if (gettimeofday(&tv, NULL) != 0)
{
DbgPrint(("%s: gettimeofday Failed!\n",__FUNCTION__));
pthread_mutex_unlock(&pmsg->mutex);
iRet = OS_FAILURE;
goto error_handler;
}
/* set our timeout */
abstime.tv_sec = tv.tv_sec + iTimeout/CLOCKS_PER_SEC;
abstime.tv_nsec = ( tv.tv_usec + (iTimeout%CLOCKS_PER_SEC) ) * 1000;
}
/* If no space, wait */
while ( pmsg->ulHead == ( (pmsg->ulTail + 1) % (pmsg->ulDepth + 1) ) )
{
if (OS_WAIT_FOREVER == iTimeout)
{
if (pthread_cond_wait(&pmsg->condWaitFull, &pmsg->mutex) != 0)
{
DBGPRINT(DBG_ON(DBG_ERROR), ("OS_MsgQSend: pthread_cond_wait ERROR\n"));
iRet = OS_FAILURE;
break;
}
}
else if (OS_NO_WAIT == iTimeout)
{
iRet = OS_TIMEOUT;
break;
}
else
{
iRet = pthread_cond_timedwait(&pmsg->condWaitFull, &pmsg->mutex, &abstime);
if (iRet != 0)
{
if (iRet == ETIMEDOUT)
{
iRet = OS_TIMEOUT;
break;
}
else
{
DBGPRINT(DBG_ON(DBG_ERROR), ("OS_MsgQSend: pthread_cond_timedwait ERROR = %s!\n", strerror(errno)));
iRet = OS_FAILURE;
break;
}
}
}
}
/* If we did not time out put it in the queue */
if (OS_OK == iRet)
{
/* Put it in the queue */
memcpy(pmsg->pbBuffers + (pmsg->ulTail * pmsg->ulSize ), strMsg, pmsg->ulSize);
pmsg->ulTail = ( (pmsg->ulTail + 1) % (pmsg->ulDepth + 1) );
/* Signal any waiters */
if (pthread_cond_signal(&pmsg->condWaitEmpty))
{
DbgPrint(("%s: Broadcast Failed\n",__FUNCTION__));
}
}
/* Release mutex */
if (pthread_mutex_unlock(&pmsg->mutex))
{
DBGPRINT(DBG_ON(DBG_ERROR), ("%s: Failed to release lock\n",__FUNCTION__));
goto error_handler;
}
/* Return success */
return (iRet);
error_handler:
/* Return failure */
return (OS_FAILURE);
}
/**
* OS Message Queue Receive function.
*
* @param mqId - message queue identifier
* @param *strMsg - message pointer, the location of where the received message is to be placed after it has been received
* @param uiSize - message size (not used)
* @param iTimeout - send timeout value, in ticks. (-1 = wait forever, 0 = no wait, > 1 = wait)
*
* @retval
* OS_OK if successful
* OS_FAILURE if not successful
* OS_TIMEOUT if a timeout occurred
*/
#if defined(NDEBUG)
OS_STATUS OS_MsgQReceive(OS_MSG_Q_ID mqId, char *strMsg, ULONG ulSize, int iTimeout)
#else
OS_STATUS OS_MsgQReceiveDbg(OS_MSG_Q_ID mqId, char *strMsg, ULONG ulSize, int iTimeout, char *srcFile, int srcLine)
#endif
{
/* Check the validity of message queue */
if (mqId == 0)
{
#if defined(NDEBUG)
DbgPrint(("%s: BAD POINTER.\n", __FUNCTION__));
#else
DbgPrint(("%s: BAD POINTER. Called by %s:%u\n", __FUNCTION__, srcFile, srcLine));
#endif
return (OS_FAILURE);
}
/* Receive message from the head by default */
return (OS_MsgQReceiveOption(mqId, strMsg, ulSize, iTimeout, OS_MSG_Q_HEAD) );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -