📄 hs_os.c
字号:
{
// NOT IMPLEMENTED YET
return HS_ERROR_FAILURE;
}
/***************************************************************************
* Function:
* HS_OS_SemaphoreDelete
*
* Description:
* 删除信号量
*
* Input:
* hSempHandle: 信号量句柄
*
* Output:
* 无
*
* Return:
* 其他 - 失败
* HS_NO_ERROR - 成功
*
* Others:
*
****************************************************************************/
HS_ErrorCode_e HS_OS_SemaphoreDelete(IN HANDLE hSempHandle)
{
OS_InnerSem_t *p_sem = NULL_POINTER(OS_InnerSem_t *);
S32 ret = 0;
p_sem = (OS_InnerSem_t *)hSempHandle;
if (p_sem EQUAL NULL_POINTER(OS_InnerSem_t *))
{
hs_debug_error(("Bad semaphore.\n"));
return HS_ERROR_FAILURE;
}
ret = sem_destroy(&p_sem->semHandle);
if(ret != 0)
{
hs_debug_error(("Semaphore Destroy Failed!\n"));
return HS_ERROR_FAILURE;
}
HS_FREE(p_sem);
return HS_NO_ERROR;
}
/***************************************************************************
* Function:
* HS_OS_SemaphoreDelete
*
* Description:
* 等待一个信号量,信号量P 操作
*
* Input:
* hSempHandle: 信号量句柄
* enmTimeoutFlagType: 等待超时类型
* TimeoutMs: 指定等待的时间,单位毫秒,
* 类型为HS_TIMEOUT_SETVALUE 时有效
*
* Output:
* 无
*
* Return:
* 其他 - 失败
* HS_NO_ERROR - 成功
*
* Others:
*
****************************************************************************/
HS_ErrorCode_e HS_OS_SemaphoreWait(
IN HANDLE hSempHandle,
IN HS_OS_TimeoutFlag_e enmTimeoutFlagType,
IN U32 TimeoutMs )
{
OS_InnerSem_t *p_sem = NULL_POINTER(OS_InnerSem_t *);
S32 value = 0;
S32 ret = 0;
S32 sysErrNo = 0;
p_sem = (OS_InnerSem_t *)hSempHandle;
if (p_sem EQUAL NULL_POINTER(OS_InnerSem_t *))
{
hs_debug_error(("Bad semaphore.\n"));
return HS_ERROR_FAILURE;
}
if(enmTimeoutFlagType == HS_TIMEOUT_IMMEDIATELY)
{
errno = 0;
ret = sem_trywait(&p_sem->semHandle);
sysErrNo = errno;
}
else if(enmTimeoutFlagType == HS_TIMEOUT_INFINITY)
{
errno = 0;
ret = sem_wait(&p_sem->semHandle);
sysErrNo = errno;
}
else if(enmTimeoutFlagType == HS_TIMEOUT_SETVALUE)
{
struct timespec tsTimeout;
struct timeval tvCurTime;
/* Acquire abs_timespec */
gettimeofday(&tvCurTime, NULL);
tsTimeout.tv_nsec = tvCurTime.tv_usec * 1000 + (TimeoutMs % 1000) * 1000 * 1000;
tsTimeout.tv_sec = tvCurTime.tv_sec + TimeoutMs / 1000 + tsTimeout.tv_nsec / 1000000000;
tsTimeout.tv_nsec = tsTimeout.tv_nsec % 1000000000;
errno = 0;
ret = sem_timedwait(&p_sem->semHandle, &tsTimeout);
sysErrNo = errno;
}
else
{
hs_debug_print(ERROR_LEVEL, "Invalid timeout type!\n");
return HS_ERROR_FAILURE;
}
if(ret < 0)
{
if(ETIMEDOUT == sysErrNo || EAGAIN == sysErrNo)
{
//hs_debug_print(INFO_LEVEL, "Wait semaphore timed out!\n");
return HS_ERROR_TIMEOUT;
}
else
{
hs_debug_print(ERROR_LEVEL, "Semaphore wait failed! errno = %u %s\n", sysErrNo, strerror(sysErrNo));
return HS_ERROR_FAILURE;
}
}
ret = sem_getvalue(&p_sem->semHandle, &value);
if(ret < 0)
{
hs_debug_error(("Update semaphore value failed\n"));
}
else
{
p_sem->curNum = value;
}
return HS_NO_ERROR;
}
/***************************************************************************
* Function:
* HS_OS_SemaphoreSignal
*
* Description:
* 释放一个信号量,信号量V 操作
*
* Input:
* hSempHandle: 信号量句柄
*
* Output:
* 无
*
* Return:
* 其他 - 失败
* HS_NO_ERROR - 成功
*
* Others:
*
****************************************************************************/
HS_ErrorCode_e HS_OS_SemaphoreSignal(IN HANDLE hSempHandle)
{
S32 ret = 0;
OS_InnerSem_t *p_sem = NULL_POINTER(OS_InnerSem_t *);
p_sem = (OS_InnerSem_t *)hSempHandle;
if (p_sem EQUAL NULL_POINTER(OS_InnerSem_t *))
{
hs_debug_error(("Bad semaphore.\n"));
return HS_ERROR_FAILURE;
}
if (p_sem->curNum EQUAL p_sem->totalNum)
{
return HS_NO_ERROR;
}
ret = sem_post(&p_sem->semHandle);
if(ret != 0)
{
hs_debug_print(ERROR_LEVEL, "Semaphore release failed!\n");
return HS_ERROR_FAILURE;
}
sem_getvalue(&p_sem->semHandle, &p_sem->curNum);
return HS_NO_ERROR;
}
/***************************************************************************
* Function:
* HS_OS_SemaphoreValue
*
* Description:
* 获取信号量剩余的个数
*
* Input:
* hSempHandle: 信号量句柄
*
* Output:
* value: 获取的信号量个数
*
* Return:
* 其他 - 失败
* HS_NO_ERROR - 成功
*
* Others:
*
****************************************************************************/
HS_ErrorCode_e HS_OS_SemaphoreValue(IN HANDLE hSempHandle, OUT S32 *pValue )
{
S32 ret = 0;
OS_InnerSem_t *p_sem = NULL_POINTER(OS_InnerSem_t *);
p_sem = (OS_InnerSem_t *)hSempHandle;
if (p_sem EQUAL NULL_POINTER(OS_InnerSem_t *))
{
hs_debug_error(("Bad semaphore.\n"));
return HS_ERROR_FAILURE;
}
ret = sem_getvalue(&p_sem->semHandle, pValue);
if(ret < 0)
{
hs_debug_print(ERROR_LEVEL, "Get semaphore value failed\n");
return HS_ERROR_FAILURE;
}
return HS_NO_ERROR;
}
/*******************************************************************
** 互斥量相关函数接口
*******************************************************************/
/***************************************************************************
* Function:
* HS_OS_MutexCreate
*
* Description:
* 创建互斥量
*
* Input:
* 无
*
* Output:
* phMutexHandle: 互斥量句柄
*
* Return:
* 其他 - 失败
* HS_NO_ERROR - 成功
*
* Others:
*
****************************************************************************/
HS_ErrorCode_e HS_OS_MutexCreate( OUT HANDLE *phMutexHandle)
{
S32 lRet;
pthread_mutex_t *mutex;
// pthread_mutexattr_t MutAttr;
mutex = hs_malloc(sizeof(pthread_mutex_t));
if (NULL == mutex)
{
hs_debug_print(ERROR_LEVEL, "Malloc failure while creating mutex!\n");
return HS_ERROR_FAILURE;
}
/*
pthread_mutexattr_init(&MutAttr);
lRet = pthread_mutexattr_settype(&MutAttr, PTHREAD_MUTEX_RECURSIVE_NP);
if (lRet == 0)
{
lRet = pthread_mutex_init(mutex, &MutAttr);
}
pthread_mutexattr_destroy(&MutAttr);
*/
lRet = pthread_mutex_init(mutex, NULL);
if (lRet != 0)
{
hs_debug_print(ERROR_LEVEL, "Mutex initialization failed!\n");
return HS_ERROR_FAILURE;
}
*phMutexHandle = (HANDLE)mutex;
return HS_NO_ERROR;
}
/***************************************************************************
* Function:
* HS_OS_MutexDelete
*
* Description:
* 销毁互斥量
*
* Input:
* hMutexHandle: 互斥量句柄
*
* Output:
* 无
*
* Return:
* 其他 - 失败
* HS_NO_ERROR - 成功
*
* Others:
*
****************************************************************************/
HS_ErrorCode_e HS_OS_MutexDelete (IN HANDLE hMutexHandle)
{
S32 lRet;
do
{
lRet = pthread_mutex_destroy((pthread_mutex_t *)hMutexHandle);
if (EBUSY == lRet)
{
usleep(30 * 1000); //sleep 30ms
}
}
while (EBUSY == lRet);
if (0 == lRet)
{
hs_free((void *)hMutexHandle);
}
else
{
hs_debug_print(ERROR_LEVEL, "Mutex destruction failed, Memory is NOT freed!\n");
return HS_ERROR_FAILURE;
}
return HS_NO_ERROR;
}
/***************************************************************************
* Function:
* HS_OS_MutexLock
*
* Description:
* 锁定互斥量
*
* Input:
* hMutexHandle: 互斥量句柄
* enmTimeoutFlagType: 等待超时的类型
* TimeoutMs: 指定等待的时间,当等待类型为
* HS_TIMEOUT_SETVALUE 时有效
* Output:
* 无
*
* Return:
* 其他 - 失败
* HS_NO_ERROR - 成功
*
* Others:
*
****************************************************************************/
HS_ErrorCode_e HS_OS_MutexLock (
IN HANDLE hMutexHandle,
IN HS_OS_TimeoutFlag_e enmTimeoutFlagType,
IN U32 TimeoutMs
)
{
S32 lRet;
if (HS_TIMEOUT_INFINITY == enmTimeoutFlagType)
{
lRet = pthread_mutex_lock(hMutexHandle);
}
else if (HS_TIMEOUT_IMMEDIATELY == enmTimeoutFlagType)
{
lRet = pthread_mutex_trylock(hMutexHandle);
}
else if (HS_TIMEOUT_SETVALUE == enmTimeoutFlagType)
{
struct timespec tsTimeout;
struct timeval tvCurTime;
/* Acquire abs_timespec */
gettimeofday(&tvCurTime, NULL);
tsTimeout.tv_nsec = tvCurTime.tv_usec * 1000 + (TimeoutMs % 1000) * 1000 * 1000;
tsTimeout.tv_sec = tvCurTime.tv_sec + TimeoutMs / 1000 + tsTimeout.tv_nsec / 1000000000;
tsTimeout.tv_nsec = tsTimeout.tv_nsec % 1000000000;
lRet = pthread_mutex_timedlock(hMutexHandle, &tsTimeout);
}
else
{
hs_debug_print(ERROR_LEVEL, "Invalid timeout type!\n");
return HS_ERROR_FAILURE;
}
if (0 != lRet)
{
if (ETIMEDOUT == lRet || EBUSY == lRet)
{
//hs_debug_print(INFO_LEVEL, "Mutex lock timed out! Detailed info: %u %s\n", lRet, strerror(lRet));
return HS_ERROR_TIMEOUT;
}
else if (EDEADLK == lRet)
{
hs_debug_print(INFO_LEVEL, "Mutex is already locked by this task! Detailed info: %u %s\n", lRet, strerror(lRet));
return HS_ERROR_MUTEX_LOCK;
}
else
{
hs_debug_print(ERROR_LEVEL, "Mutex lock failed! errno = %u %s\n", lRet, strerror(lRet));
return HS_ERROR_FAILURE;
}
}
return HS_NO_ERROR;
}
/***************************************************************************
* Function:
* HS_OS_MutexUnLock
*
* Description:
* 释放互斥量
*
* Input:
* hMutexHandle: 互斥量句柄
*
* Output:
* 无
*
* Return:
* 其他 - 失败
* HS_NO_ERROR - 成功
*
* Others:
*
****************************************************************************/
HS_ErrorCode_e HS_OS_MutexUnlock (IN HANDLE hMutexHandle)
{
S32 lRet;
lRet = pthread_mutex_unlock(hMutexHandle);
if (0 != lRet)
{
hs_debug_print(ERROR_LEVEL, "Mutex unlock failed!\n");
return HS_ERROR_FAILURE;
}
return HS_NO_ERROR;
}
/*******************************************************************
** 消息队列相关函数接口
*******************************************************************/
static S32 FindUnusedMQ(void)
{
U32 i;
for (i = 1; i < MAX_MQ_NUM + 1; i++)
{
if (s_amMQ[i].bUsed == FALSE)
{
return i;
}
}
return (-1);
}
HS_ErrorCode_e MQInit(void)
{
HS_ErrorCode_e errCode;
if (s_bMQInit)
{
return HS_ERROR_ALREADY_INITIALIZED;
}
//memset(s_amMQ, '\0', sizeof(s_amMQ));
errCode = HS_OS_MutexCreate(&s_hMQMutex);
if (HS_NO_ERROR != errCode || NULL == s_hMQMutex)
{
hs_debug_print(ERROR_LEVEL, "Unable to create mutex while initialising Message queue\n");
return HS_ERROR_FAILURE;
}
s_bMQInit = TRUE;
return HS_NO_ERROR;
}
/***************************************************************************
* Function:
* HS_OS_MsgQueueCreate
*
* Description:
* 创建一个消息队列
*
* Input:
* pucMsgQueueName: 消息队列名称
* maxMsgNums: 该队列可以容纳的最大消息个数
* maxMsgLength: 单个消息的最大长度
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -