/**
 * @file hal_mutex.c
 * @brief HAL 互斥锁实现（JieLi 平台）
 */

#include "hal_mutex.h"
#include "os/os_api.h"
#include <stdlib.h>

hal_mutex_t hal_mutex_create(void)
{
    OS_MUTEX *mutex = (OS_MUTEX *)zalloc(sizeof(OS_MUTEX));
    if (!mutex) {
        return NULL;
    }
    os_mutex_create(mutex);
    return (hal_mutex_t)mutex;
}

int hal_mutex_lock(hal_mutex_t mutex)
{
    if (!mutex) {
        return -1;
    }
    os_mutex_pend((OS_MUTEX *)mutex, 0);
    return 0;
}

int hal_mutex_unlock(hal_mutex_t mutex)
{
    if (!mutex) {
        return -1;
    }
    os_mutex_post((OS_MUTEX *)mutex);
    return 0;
}

void hal_mutex_destroy(hal_mutex_t mutex)
{
    if (!mutex) {
        return;
    }
    os_mutex_del((OS_MUTEX *)mutex, 0);
    free(mutex);
}
