Files
kunlun/common/os_shim/freertos/src/os_lock.c

145 lines
3.9 KiB
C
Raw Normal View History

2024-09-28 14:24:04 +08:00
/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
be copied by any method or incorporated into another program without
the express written consent of Aerospace C.Power. This Information or any portion
thereof remains the property of Aerospace C.Power. The Information contained herein
is believed to be accurate and Aerospace C.Power assumes no responsibility or
liability for its use in any way and conveys no license or title under
any patent or copyright and makes no representation or warranty that this
Information is free from patent or copyright infringement.
****************************************************************************/
/* Free RTOS includes */
#include "FreeRTOS.h"
#include "semphr.h"
/* os shim includes */
#include "os_types.h"
#include "os_mem.h"
#include "os_lock.h"
/* common includes */
#include "iot_config.h"
#include "iot_utils.h"
#include "iot_dbglog_api.h"
typedef struct _os_mutex {
SemaphoreHandle_t mutex;
} os_mutex_t;
typedef struct _os_sem {
SemaphoreHandle_t sem;
} os_sem_t;
#if (PLC_STATISTICS_ENABLE)
#include "iot_io_api.h"
#include "iot_dbglog_parser.h"
#include "iot_module.h"
module_id_t mid_lock[MAX_MID_NUM] = {0};
uint32_t lock_count = 0;
#endif
os_mutex_h os_create_mutex(module_id_t module_id)
{
os_mutex_t *tmp = NULL;
tmp = os_mem_malloc(module_id, sizeof(tmp));
if (tmp) {
tmp->mutex = xSemaphoreCreateRecursiveMutex();
if (tmp->mutex == NULL) {
os_mem_free(tmp);
tmp = NULL;
}
}
#if (PLC_STATISTICS_ENABLE)
if (tmp) {
++mid_lock[module_id];
++lock_count;
for (module_id_t i = 0; i < MAX_MID_NUM; ++i) {
if (mid_lock[i]) {
iot_printf("ststic:lock %d[%s], lock cnt = %d, total = %d\n",
i, iot_get_mid_str(i), mid_lock[i], lock_count);
iot_dbglog_input(IOT_STATISTICS_MID, DBGLOG_ERR, OS_LOCK_USAGE1,
3, i, mid_lock[i], lock_count);
}
}
iot_printf("ststic:lock total cnt = %d--------------\n", lock_count);
iot_dbglog_input(IOT_STATISTICS_MID, DBGLOG_ERR, OS_LOCK_USAGE2,
1, lock_count);
}
#endif //PLC_STATISTICS_ENABLE
return tmp;
}
void os_acquire_mutex(os_mutex_h mutex)
{
xSemaphoreTakeRecursive(((os_mutex_t*)mutex)->mutex, portMAX_DELAY);
}
void os_release_mutex(os_mutex_h mutex)
{
xSemaphoreGiveRecursive(((os_mutex_t*)mutex)->mutex);
}
bool_t os_try_acquire_mutex(os_mutex_h mutex)
{
BaseType_t result;
result = xSemaphoreTakeRecursive(((os_mutex_t*)mutex)->mutex, 0);
return result == pdPASS ? true : false;
}
void os_delete_mutex(os_mutex_h mutex)
{
vSemaphoreDelete(((os_mutex_t*)mutex)->mutex);
os_mem_free(mutex);
}
os_sem_h os_create_sem(module_id_t module_id, uint32_t max_count,
uint32_t init_count)
{
os_sem_t *tmp = os_mem_malloc(module_id, sizeof(tmp));
if (tmp) {
tmp->sem = xSemaphoreCreateCounting( max_count, init_count );
if (tmp->sem == NULL) {
os_mem_free(tmp);
tmp = NULL;
}
}
return tmp;
}
bool_t os_pend_sem(os_sem_h semaphore, uint32_t timeout)
{
if (timeout != portMAX_DELAY) {
timeout = pdMS_TO_TICKS(timeout);
if (timeout != portMAX_DELAY) {
timeout--;
}
}
BaseType_t result = xSemaphoreTake(((os_sem_t*)semaphore)->sem, timeout);
return result == pdTRUE ? true : false;
}
bool_t os_post_sem(os_sem_h semaphore)
{
BaseType_t result = xSemaphoreGive(((os_sem_t*)semaphore)->sem);
return result == pdTRUE ? true : false;
}
void os_delete_sem(os_sem_h semaphore)
{
vSemaphoreDelete(((os_sem_t*)semaphore)->sem);
os_mem_free(semaphore);
}