/**
 * @file hv_vos_Mutex.c
 * @brief vos mutex source code
 * @details 
 * @author HiView SoC Software Team
 * @version 0.0.1
 * @date 20230301
 * @copyright Copyright(c),2023-3, Hiview Software. All rights reserved.
 * @par History: 
 * <table>
 * <tr><th>author                     <th>date           <th>description
 * <tr><td>HiView SoC Software Team   <td>20230301       <td>Create and first Version.
 * </table>
**/

#include "hv_vos_Comm.h"
#include "FreeRTOS.h"
#include "semphr.h"


struct _HV_VOS_MUTEX_S{
    QueueHandle_t handler;
};

HV_VOS_MUTEX_S *Hv_Vos_MutexInit(void)
{
    HV_VOS_MUTEX_S *self = NULL;
    self = HV_MALLOC_OBJECT(HV_VOS_MUTEX_S);
    self->handler = xSemaphoreCreateMutex(); 
    if (self->handler == NULL)
    {
        HV_FREE_OBJECT(self);
        self = NULL;
    }
    return self;
}


void Hv_Vos_MutexLock(HV_VOS_MUTEX_S *self)
{
    xSemaphoreTake(self->handler, portMAX_DELAY);
}


void Hv_Vos_MutexUnlock(HV_VOS_MUTEX_S *self)
{
    xSemaphoreGive( self->handler);
}

BOOL Hv_Vos_MutexTimedLock(HV_VOS_MUTEX_S *self, UINT32 timeoutMs)
{
    UINT32 ret = xSemaphoreTake(self->handler, pdMS_TO_TICKS(timeoutMs));
    return ret == pdPASS;
}

void Hv_Vos_MutexCleanup(HV_VOS_MUTEX_S *self)
{
    vSemaphoreDelete(self->handler);
    HV_FREE_OBJECT(self);
}

