2016-08-17 11:08:22 -04:00
|
|
|
/*
|
2022-07-22 04:14:37 -04:00
|
|
|
* SPDX-FileCopyrightText: 2001-2003 Swedish Institute of Computer Science
|
2016-08-17 11:08:22 -04:00
|
|
|
*
|
2022-07-22 04:14:37 -04:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2016-08-17 11:08:22 -04:00
|
|
|
*
|
2024-05-09 07:09:59 -04:00
|
|
|
* SPDX-FileContributor: 2018-2024 Espressif Systems (Shanghai) CO LTD
|
2016-08-17 11:08:22 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* lwIP includes. */
|
|
|
|
|
2017-10-04 00:52:19 -04:00
|
|
|
#include <pthread.h>
|
2022-07-22 04:14:37 -04:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/semphr.h"
|
|
|
|
#include "freertos/queue.h"
|
2016-08-17 11:08:22 -04:00
|
|
|
#include "lwip/debug.h"
|
|
|
|
#include "lwip/def.h"
|
|
|
|
#include "lwip/sys.h"
|
|
|
|
#include "lwip/mem.h"
|
2016-11-28 05:36:14 -05:00
|
|
|
#include "lwip/stats.h"
|
2022-04-28 11:04:07 -04:00
|
|
|
#include "arch/sys_arch.h"
|
|
|
|
#include "arch/vfs_lwip.h"
|
2017-03-20 04:30:35 -04:00
|
|
|
#include "esp_log.h"
|
2019-10-15 17:01:05 -04:00
|
|
|
#include "esp_compiler.h"
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
static const char* TAG = "lwip_arch";
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2017-03-20 04:30:35 -04:00
|
|
|
static sys_mutex_t g_lwip_protect_mutex = NULL;
|
2016-12-15 01:37:21 -05:00
|
|
|
|
2017-10-04 00:52:19 -04:00
|
|
|
static pthread_key_t sys_thread_sem_key;
|
|
|
|
static void sys_thread_sem_free(void* data);
|
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
#if !LWIP_COMPAT_MUTEX
|
2019-09-29 06:10:48 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create a new mutex
|
|
|
|
*
|
|
|
|
* @param pxMutex pointer of the mutex to create
|
|
|
|
* @return ERR_OK on success, ERR_MEM when out of memory
|
|
|
|
*/
|
2016-08-17 11:08:22 -04:00
|
|
|
err_t
|
|
|
|
sys_mutex_new(sys_mutex_t *pxMutex)
|
|
|
|
{
|
|
|
|
*pxMutex = xSemaphoreCreateMutex();
|
2019-09-29 06:10:48 -04:00
|
|
|
if (*pxMutex == NULL) {
|
|
|
|
LWIP_DEBUGF(ESP_THREAD_SAFE_DEBUG, ("sys_mutex_new: out of mem\r\n"));
|
|
|
|
return ERR_MEM;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2016-10-27 02:11:01 -04:00
|
|
|
LWIP_DEBUGF(ESP_THREAD_SAFE_DEBUG, ("sys_mutex_new: m=%p\n", *pxMutex));
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
return ERR_OK;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
/**
|
|
|
|
* @brief Lock a mutex
|
|
|
|
*
|
|
|
|
* @param pxMutex pointer of mutex to lock
|
|
|
|
*/
|
|
|
|
void
|
2016-08-17 11:08:22 -04:00
|
|
|
sys_mutex_lock(sys_mutex_t *pxMutex)
|
|
|
|
{
|
2019-09-29 06:10:48 -04:00
|
|
|
BaseType_t ret = xSemaphoreTake(*pxMutex, portMAX_DELAY);
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
LWIP_ASSERT("failed to take the mutex", ret == pdTRUE);
|
2021-02-12 00:01:05 -05:00
|
|
|
(void)ret;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
/**
|
|
|
|
* @brief Unlock a mutex
|
|
|
|
*
|
|
|
|
* @param pxMutex pointer of mutex to unlock
|
|
|
|
*/
|
|
|
|
void
|
2016-08-17 11:08:22 -04:00
|
|
|
sys_mutex_unlock(sys_mutex_t *pxMutex)
|
|
|
|
{
|
2019-09-29 06:10:48 -04:00
|
|
|
BaseType_t ret = xSemaphoreGive(*pxMutex);
|
|
|
|
|
|
|
|
LWIP_ASSERT("failed to give the mutex", ret == pdTRUE);
|
2021-02-12 00:01:05 -05:00
|
|
|
(void)ret;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
/**
|
|
|
|
* @brief Delete a mutex
|
|
|
|
*
|
|
|
|
* @param pxMutex pointer of mutex to delete
|
|
|
|
*/
|
2016-08-17 11:08:22 -04:00
|
|
|
void
|
|
|
|
sys_mutex_free(sys_mutex_t *pxMutex)
|
|
|
|
{
|
2016-10-27 02:11:01 -04:00
|
|
|
LWIP_DEBUGF(ESP_THREAD_SAFE_DEBUG, ("sys_mutex_free: m=%p\n", *pxMutex));
|
2019-09-29 06:10:48 -04:00
|
|
|
vSemaphoreDelete(*pxMutex);
|
|
|
|
*pxMutex = NULL;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
#endif /* !LWIP_COMPAT_MUTEX */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Creates a new semaphore
|
|
|
|
*
|
|
|
|
* @param sem pointer of the semaphore
|
|
|
|
* @param count initial state of the semaphore
|
|
|
|
* @return err_t
|
|
|
|
*/
|
2016-08-17 11:08:22 -04:00
|
|
|
err_t
|
|
|
|
sys_sem_new(sys_sem_t *sem, u8_t count)
|
|
|
|
{
|
2019-09-29 06:10:48 -04:00
|
|
|
LWIP_ASSERT("initial_count invalid (neither 0 nor 1)",
|
|
|
|
(count == 0) || (count == 1));
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
*sem = xSemaphoreCreateBinary();
|
|
|
|
if (*sem == NULL) {
|
|
|
|
LWIP_DEBUGF(ESP_THREAD_SAFE_DEBUG, ("sys_sem_new: out of mem\r\n"));
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
if (count == 1) {
|
|
|
|
BaseType_t ret = xSemaphoreGive(*sem);
|
|
|
|
LWIP_ASSERT("sys_sem_new: initial give failed", ret == pdTRUE);
|
2021-02-12 00:01:05 -05:00
|
|
|
(void)ret;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
return ERR_OK;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
/**
|
|
|
|
* @brief Signals a semaphore
|
|
|
|
*
|
|
|
|
* @param sem pointer of the semaphore
|
|
|
|
*/
|
|
|
|
void
|
2016-08-17 11:08:22 -04:00
|
|
|
sys_sem_signal(sys_sem_t *sem)
|
|
|
|
{
|
2019-09-29 06:10:48 -04:00
|
|
|
BaseType_t ret = xSemaphoreGive(*sem);
|
|
|
|
/* queue full is OK, this is a signal only... */
|
|
|
|
LWIP_ASSERT("sys_sem_signal: sane return value",
|
|
|
|
(ret == pdTRUE) || (ret == errQUEUE_FULL));
|
2021-02-12 00:01:05 -05:00
|
|
|
(void)ret;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2018-05-03 04:41:10 -04:00
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
// Signals a semaphore (from ISR)
|
2019-09-29 06:10:48 -04:00
|
|
|
int
|
|
|
|
sys_sem_signal_isr(sys_sem_t *sem)
|
2018-05-03 04:41:10 -04:00
|
|
|
{
|
|
|
|
BaseType_t woken = pdFALSE;
|
|
|
|
xSemaphoreGiveFromISR(*sem, &woken);
|
|
|
|
return woken == pdTRUE;
|
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
/**
|
|
|
|
* @brief Wait for a semaphore to be signaled
|
|
|
|
*
|
|
|
|
* @param sem pointer of the semaphore
|
2022-01-24 23:23:59 -05:00
|
|
|
* @param timeout if zero, will wait infinitely, or will wait at least for milliseconds specified by this argument
|
2019-09-29 06:10:48 -04:00
|
|
|
* @return SYS_ARCH_TIMEOUT when timeout, 0 otherwise
|
|
|
|
*/
|
|
|
|
u32_t
|
2016-08-17 11:08:22 -04:00
|
|
|
sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
|
|
|
|
{
|
2019-09-29 06:10:48 -04:00
|
|
|
BaseType_t ret;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
if (!timeout) {
|
|
|
|
/* wait infinite */
|
|
|
|
ret = xSemaphoreTake(*sem, portMAX_DELAY);
|
|
|
|
LWIP_ASSERT("taking semaphore failed", ret == pdTRUE);
|
|
|
|
} else {
|
2022-01-24 23:23:59 -05:00
|
|
|
/* Round up the number of ticks.
|
|
|
|
* Not only we need to round up the number of ticks, but we also need to add 1.
|
|
|
|
* Indeed, this function shall wait for AT LEAST timeout, but on FreeRTOS,
|
|
|
|
* if we specify a timeout of 1 tick to `xSemaphoreTake`, it will take AT MOST
|
|
|
|
* 1 tick before triggering a timeout. Thus, we need to pass 2 ticks as a timeout
|
|
|
|
* to `xSemaphoreTake`. */
|
|
|
|
TickType_t timeout_ticks = ((timeout + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS) + 1;
|
2019-09-29 06:10:48 -04:00
|
|
|
ret = xSemaphoreTake(*sem, timeout_ticks);
|
|
|
|
if (ret == errQUEUE_EMPTY) {
|
|
|
|
/* timed out */
|
|
|
|
return SYS_ARCH_TIMEOUT;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
2019-09-29 06:10:48 -04:00
|
|
|
LWIP_ASSERT("taking semaphore failed", ret == pdTRUE);
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
return 0;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
/**
|
|
|
|
* @brief Delete a semaphore
|
|
|
|
*
|
|
|
|
* @param sem pointer of the semaphore to delete
|
|
|
|
*/
|
2016-08-17 11:08:22 -04:00
|
|
|
void
|
|
|
|
sys_sem_free(sys_sem_t *sem)
|
|
|
|
{
|
|
|
|
vSemaphoreDelete(*sem);
|
2019-09-29 06:10:48 -04:00
|
|
|
*sem = NULL;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
/**
|
|
|
|
* @brief Create an empty mailbox.
|
|
|
|
*
|
|
|
|
* @param mbox pointer of the mailbox
|
|
|
|
* @param size size of the mailbox
|
|
|
|
* @return ERR_OK on success, ERR_MEM when out of memory
|
|
|
|
*/
|
2016-08-17 11:08:22 -04:00
|
|
|
err_t
|
|
|
|
sys_mbox_new(sys_mbox_t *mbox, int size)
|
|
|
|
{
|
2017-09-30 03:28:41 -04:00
|
|
|
*mbox = mem_malloc(sizeof(struct sys_mbox_s));
|
2016-08-17 11:08:22 -04:00
|
|
|
if (*mbox == NULL){
|
2016-10-27 02:11:01 -04:00
|
|
|
LWIP_DEBUGF(ESP_THREAD_SAFE_DEBUG, ("fail to new *mbox\n"));
|
2016-08-17 11:08:22 -04:00
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*mbox)->os_mbox = xQueueCreate(size, sizeof(void *));
|
|
|
|
|
|
|
|
if ((*mbox)->os_mbox == NULL) {
|
2019-09-29 06:10:48 -04:00
|
|
|
LWIP_DEBUGF(ESP_THREAD_SAFE_DEBUG, ("fail to new (*mbox)->os_mbox\n"));
|
2016-08-17 11:08:22 -04:00
|
|
|
free(*mbox);
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
|
2018-11-06 21:52:33 -05:00
|
|
|
LWIP_DEBUGF(ESP_THREAD_SAFE_DEBUG, ("new *mbox ok mbox=%p os_mbox=%p\n", *mbox, (*mbox)->os_mbox));
|
2016-08-17 11:08:22 -04:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
/**
|
|
|
|
* @brief Send message to mailbox
|
|
|
|
*
|
|
|
|
* @param mbox pointer of the mailbox
|
|
|
|
* @param msg pointer of the message to send
|
|
|
|
*/
|
|
|
|
void
|
2016-08-17 11:08:22 -04:00
|
|
|
sys_mbox_post(sys_mbox_t *mbox, void *msg)
|
|
|
|
{
|
2019-09-29 06:10:48 -04:00
|
|
|
BaseType_t ret = xQueueSendToBack((*mbox)->os_mbox, &msg, portMAX_DELAY);
|
|
|
|
LWIP_ASSERT("mbox post failed", ret == pdTRUE);
|
2021-02-12 00:01:05 -05:00
|
|
|
(void)ret;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
/**
|
|
|
|
* @brief Try to post a message to mailbox
|
|
|
|
*
|
|
|
|
* @param mbox pointer of the mailbox
|
|
|
|
* @param msg pointer of the message to send
|
|
|
|
* @return ERR_OK on success, ERR_MEM when mailbox is full
|
|
|
|
*/
|
|
|
|
err_t
|
2016-08-17 11:08:22 -04:00
|
|
|
sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
|
|
|
|
{
|
|
|
|
err_t xReturn;
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
if (xQueueSend((*mbox)->os_mbox, &msg, 0) == pdTRUE) {
|
2016-08-17 11:08:22 -04:00
|
|
|
xReturn = ERR_OK;
|
|
|
|
} else {
|
2016-10-27 02:11:01 -04:00
|
|
|
LWIP_DEBUGF(ESP_THREAD_SAFE_DEBUG, ("trypost mbox=%p fail\n", (*mbox)->os_mbox));
|
2016-08-17 11:08:22 -04:00
|
|
|
xReturn = ERR_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return xReturn;
|
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
/**
|
|
|
|
* @brief Try to post a message to mailbox from ISR
|
|
|
|
*
|
|
|
|
* @param mbox pointer of the mailbox
|
|
|
|
* @param msg pointer of the message to send
|
|
|
|
* @return ERR_OK on success
|
|
|
|
* ERR_MEM when mailbox is full
|
|
|
|
* ERR_NEED_SCHED when high priority task wakes up
|
|
|
|
*/
|
|
|
|
err_t
|
|
|
|
sys_mbox_trypost_fromisr(sys_mbox_t *mbox, void *msg)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2019-09-29 06:10:48 -04:00
|
|
|
BaseType_t ret;
|
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
ret = xQueueSendFromISR((*mbox)->os_mbox, &msg, &xHigherPriorityTaskWoken);
|
|
|
|
if (ret == pdTRUE) {
|
|
|
|
if (xHigherPriorityTaskWoken == pdTRUE) {
|
|
|
|
return ERR_NEED_SCHED;
|
|
|
|
}
|
|
|
|
return ERR_OK;
|
|
|
|
} else {
|
|
|
|
LWIP_ASSERT("mbox trypost failed", ret == errQUEUE_FULL);
|
|
|
|
return ERR_MEM;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
2019-09-29 06:10:48 -04:00
|
|
|
}
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
/**
|
|
|
|
* @brief Fetch message from mailbox
|
|
|
|
*
|
|
|
|
* @param mbox pointer of mailbox
|
|
|
|
* @param msg pointer of the received message, could be NULL to indicate the message should be dropped
|
|
|
|
* @param timeout if zero, will wait infinitely; or will wait milliseconds specify by this argument
|
|
|
|
* @return SYS_ARCH_TIMEOUT when timeout, 0 otherwise
|
|
|
|
*/
|
|
|
|
u32_t
|
|
|
|
sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
|
|
|
|
{
|
|
|
|
BaseType_t ret;
|
|
|
|
void *msg_dummy;
|
|
|
|
|
|
|
|
if (msg == NULL) {
|
|
|
|
msg = &msg_dummy;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2018-11-06 21:52:33 -05:00
|
|
|
if (timeout == 0) {
|
2019-09-29 06:10:48 -04:00
|
|
|
/* wait infinite */
|
|
|
|
ret = xQueueReceive((*mbox)->os_mbox, &(*msg), portMAX_DELAY);
|
|
|
|
LWIP_ASSERT("mbox fetch failed", ret == pdTRUE);
|
2018-11-06 21:52:33 -05:00
|
|
|
} else {
|
2022-02-08 04:39:38 -05:00
|
|
|
TickType_t timeout_ticks = timeout / portTICK_PERIOD_MS;
|
2019-09-29 06:10:48 -04:00
|
|
|
ret = xQueueReceive((*mbox)->os_mbox, &(*msg), timeout_ticks);
|
|
|
|
if (ret == errQUEUE_EMPTY) {
|
|
|
|
/* timed out */
|
|
|
|
*msg = NULL;
|
|
|
|
return SYS_ARCH_TIMEOUT;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
2019-09-29 06:10:48 -04:00
|
|
|
LWIP_ASSERT("mbox fetch failed", ret == pdTRUE);
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
return 0;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
/**
|
|
|
|
* @brief try to fetch message from mailbox
|
|
|
|
*
|
|
|
|
* @param mbox pointer of mailbox
|
|
|
|
* @param msg pointer of the received message
|
|
|
|
* @return SYS_MBOX_EMPTY if mailbox is empty, 1 otherwise
|
|
|
|
*/
|
2016-08-17 11:08:22 -04:00
|
|
|
u32_t
|
|
|
|
sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
|
|
|
|
{
|
2019-09-29 06:10:48 -04:00
|
|
|
BaseType_t ret;
|
|
|
|
void *msg_dummy;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
|
|
|
if (msg == NULL) {
|
2019-09-29 06:10:48 -04:00
|
|
|
msg = &msg_dummy;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
2019-09-29 06:10:48 -04:00
|
|
|
ret = xQueueReceive((*mbox)->os_mbox, &(*msg), 0);
|
|
|
|
if (ret == errQUEUE_EMPTY) {
|
|
|
|
*msg = NULL;
|
|
|
|
return SYS_MBOX_EMPTY;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
2019-09-29 06:10:48 -04:00
|
|
|
LWIP_ASSERT("mbox fetch failed", ret == pdTRUE);
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
return 0;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
/**
|
|
|
|
* @brief Delete a mailbox
|
|
|
|
*
|
|
|
|
* @param mbox pointer of the mailbox to delete
|
|
|
|
*/
|
2016-08-17 11:08:22 -04:00
|
|
|
void
|
|
|
|
sys_mbox_free(sys_mbox_t *mbox)
|
|
|
|
{
|
2019-09-29 06:10:48 -04:00
|
|
|
if ((NULL == mbox) || (NULL == *mbox)) {
|
|
|
|
return;
|
2018-11-06 21:52:33 -05:00
|
|
|
}
|
2019-09-29 06:10:48 -04:00
|
|
|
UBaseType_t msgs_waiting = uxQueueMessagesWaiting((*mbox)->os_mbox);
|
|
|
|
LWIP_ASSERT("mbox quence not empty", msgs_waiting == 0);
|
|
|
|
|
2016-08-17 11:08:22 -04:00
|
|
|
vQueueDelete((*mbox)->os_mbox);
|
|
|
|
free(*mbox);
|
|
|
|
*mbox = NULL;
|
2021-02-12 00:01:05 -05:00
|
|
|
|
|
|
|
(void)msgs_waiting;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
/**
|
|
|
|
* @brief Create a new thread
|
|
|
|
*
|
|
|
|
* @param name thread name
|
|
|
|
* @param thread thread function
|
|
|
|
* @param arg thread arguments
|
|
|
|
* @param stacksize stacksize of the thread
|
|
|
|
* @param prio priority of the thread
|
|
|
|
* @return thread ID
|
|
|
|
*/
|
2016-08-17 11:08:22 -04:00
|
|
|
sys_thread_t
|
|
|
|
sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio)
|
|
|
|
{
|
2019-09-29 06:10:48 -04:00
|
|
|
TaskHandle_t rtos_task;
|
|
|
|
BaseType_t ret;
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
/* LwIP's lwip_thread_fn matches FreeRTOS' TaskFunction_t, so we can pass the
|
|
|
|
thread function without adaption here. */
|
|
|
|
ret = xTaskCreatePinnedToCore(thread, name, stacksize, arg, prio, &rtos_task,
|
2019-04-29 08:30:13 -04:00
|
|
|
CONFIG_LWIP_TCPIP_TASK_AFFINITY);
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2023-04-05 10:45:45 -04:00
|
|
|
LWIP_DEBUGF(TCPIP_DEBUG, ("new lwip task : %" U32_F ", prio:%d,stack:%d\n",
|
2022-10-27 13:07:07 -04:00
|
|
|
(u32_t)rtos_task, prio, stacksize));
|
2022-03-17 07:20:28 -04:00
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
if (ret != pdTRUE) {
|
2016-08-17 11:08:22 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-08-01 05:33:43 -04:00
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
return (sys_thread_t)rtos_task;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
/**
|
|
|
|
* @brief Initialize the sys_arch layer
|
|
|
|
*
|
|
|
|
*/
|
2016-08-17 11:08:22 -04:00
|
|
|
void
|
|
|
|
sys_init(void)
|
|
|
|
{
|
2020-02-15 00:14:08 -05:00
|
|
|
if (!g_lwip_protect_mutex) {
|
|
|
|
if (ERR_OK != sys_mutex_new(&g_lwip_protect_mutex)) {
|
2023-06-08 14:56:11 -04:00
|
|
|
ESP_LOGE(TAG, "sys_init: failed to init lwip protect mutex");
|
2020-02-15 00:14:08 -05:00
|
|
|
}
|
2019-09-29 06:10:48 -04:00
|
|
|
}
|
2017-10-04 00:52:19 -04:00
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
// Create the pthreads key for the per-thread semaphore storage
|
|
|
|
pthread_key_create(&sys_thread_sem_key, sys_thread_sem_free);
|
2017-10-04 00:52:19 -04:00
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
esp_vfs_lwip_sockets_register();
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
/**
|
|
|
|
* @brief Get system ticks
|
|
|
|
*
|
|
|
|
* @return system tick counts
|
|
|
|
*/
|
2017-01-20 06:05:38 -05:00
|
|
|
u32_t
|
|
|
|
sys_jiffies(void)
|
|
|
|
{
|
|
|
|
return xTaskGetTickCount();
|
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
/**
|
2024-05-09 07:09:59 -04:00
|
|
|
* @brief Get current time, in milliseconds
|
2019-09-29 06:10:48 -04:00
|
|
|
*
|
|
|
|
* @return current time
|
|
|
|
*/
|
2016-08-17 11:08:22 -04:00
|
|
|
u32_t
|
|
|
|
sys_now(void)
|
|
|
|
{
|
2019-09-29 06:10:48 -04:00
|
|
|
return xTaskGetTickCount() * portTICK_PERIOD_MS;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
/**
|
|
|
|
* @brief Protect critical region
|
|
|
|
*
|
|
|
|
* @note This function is only called during very short critical regions.
|
|
|
|
*
|
|
|
|
* @return previous protection level
|
|
|
|
*/
|
2016-08-17 11:08:22 -04:00
|
|
|
sys_prot_t
|
|
|
|
sys_arch_protect(void)
|
|
|
|
{
|
2020-02-15 00:14:08 -05:00
|
|
|
if (unlikely(!g_lwip_protect_mutex)) {
|
|
|
|
sys_mutex_new(&g_lwip_protect_mutex);
|
|
|
|
}
|
2017-03-20 04:30:35 -04:00
|
|
|
sys_mutex_lock(&g_lwip_protect_mutex);
|
2016-08-17 11:08:22 -04:00
|
|
|
return (sys_prot_t) 1;
|
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
/**
|
|
|
|
* @brief Unprotect critical region
|
|
|
|
*
|
|
|
|
* @param pval protection level
|
|
|
|
*/
|
2016-08-17 11:08:22 -04:00
|
|
|
void
|
|
|
|
sys_arch_unprotect(sys_prot_t pval)
|
|
|
|
{
|
2019-09-29 06:10:48 -04:00
|
|
|
LWIP_UNUSED_ARG(pval);
|
2017-03-20 04:30:35 -04:00
|
|
|
sys_mutex_unlock(&g_lwip_protect_mutex);
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2017-10-04 00:52:19 -04:00
|
|
|
/*
|
2019-09-29 06:10:48 -04:00
|
|
|
* get per thread semaphore
|
2016-08-19 05:23:04 -04:00
|
|
|
*/
|
2019-09-29 06:10:48 -04:00
|
|
|
sys_sem_t*
|
|
|
|
sys_thread_sem_get(void)
|
2016-08-19 05:23:04 -04:00
|
|
|
{
|
2017-10-04 00:52:19 -04:00
|
|
|
sys_sem_t *sem = pthread_getspecific(sys_thread_sem_key);
|
|
|
|
|
|
|
|
if (!sem) {
|
|
|
|
sem = sys_thread_sem_init();
|
2016-08-19 05:23:04 -04:00
|
|
|
}
|
2016-10-27 02:11:01 -04:00
|
|
|
LWIP_DEBUGF(ESP_THREAD_SAFE_DEBUG, ("sem_get s=%p\n", sem));
|
2016-08-19 05:23:04 -04:00
|
|
|
return sem;
|
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
static void
|
|
|
|
sys_thread_sem_free(void* data) // destructor for TLS semaphore
|
2016-08-24 04:33:30 -04:00
|
|
|
{
|
|
|
|
sys_sem_t *sem = (sys_sem_t*)(data);
|
|
|
|
|
|
|
|
if (sem && *sem){
|
2017-10-04 00:52:19 -04:00
|
|
|
LWIP_DEBUGF(ESP_THREAD_SAFE_DEBUG, ("sem del, sem=%p\n", *sem));
|
2016-08-24 04:33:30 -04:00
|
|
|
vSemaphoreDelete(*sem);
|
|
|
|
}
|
|
|
|
|
2017-10-04 00:52:19 -04:00
|
|
|
if (sem) {
|
|
|
|
LWIP_DEBUGF(ESP_THREAD_SAFE_DEBUG, ("sem pointer del, sem_p=%p\n", sem));
|
2016-08-24 04:33:30 -04:00
|
|
|
free(sem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
sys_sem_t*
|
|
|
|
sys_thread_sem_init(void)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2017-09-30 03:28:41 -04:00
|
|
|
sys_sem_t *sem = (sys_sem_t*)mem_malloc(sizeof(sys_sem_t*));
|
2016-08-19 05:23:04 -04:00
|
|
|
|
|
|
|
if (!sem){
|
2017-03-20 04:30:35 -04:00
|
|
|
ESP_LOGE(TAG, "thread_sem_init: out of memory");
|
2016-08-19 05:23:04 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*sem = xSemaphoreCreateBinary();
|
|
|
|
if (!(*sem)){
|
|
|
|
free(sem);
|
2017-03-20 04:30:35 -04:00
|
|
|
ESP_LOGE(TAG, "thread_sem_init: out of memory");
|
2016-08-19 05:23:04 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-04 00:52:19 -04:00
|
|
|
pthread_setspecific(sys_thread_sem_key, sem);
|
2016-08-17 09:05:29 -04:00
|
|
|
return sem;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
void
|
|
|
|
sys_thread_sem_deinit(void)
|
2016-08-19 05:23:04 -04:00
|
|
|
{
|
2017-10-04 00:52:19 -04:00
|
|
|
sys_sem_t *sem = pthread_getspecific(sys_thread_sem_key);
|
|
|
|
if (sem != NULL) {
|
|
|
|
sys_thread_sem_free(sem);
|
|
|
|
pthread_setspecific(sys_thread_sem_key, NULL);
|
|
|
|
}
|
2016-08-19 05:23:04 -04:00
|
|
|
}
|
|
|
|
|
2019-09-29 06:10:48 -04:00
|
|
|
void
|
|
|
|
sys_delay_ms(uint32_t ms)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2016-12-21 20:42:21 -05:00
|
|
|
vTaskDelay(ms / portTICK_PERIOD_MS);
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
2022-10-27 13:07:07 -04:00
|
|
|
|
|
|
|
bool
|
|
|
|
sys_thread_tcpip(sys_thread_core_lock_t type)
|
|
|
|
{
|
|
|
|
static sys_thread_t lwip_task = NULL;
|
|
|
|
#if LWIP_TCPIP_CORE_LOCKING
|
|
|
|
static sys_thread_t core_lock_holder = NULL;
|
|
|
|
#endif
|
|
|
|
switch (type) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case LWIP_CORE_IS_TCPIP_INITIALIZED:
|
|
|
|
return lwip_task != NULL;
|
|
|
|
case LWIP_CORE_MARK_TCPIP_TASK:
|
|
|
|
LWIP_ASSERT("LWIP_CORE_MARK_TCPIP_TASK: lwip_task == NULL", (lwip_task == NULL));
|
|
|
|
lwip_task = (sys_thread_t) xTaskGetCurrentTaskHandle();
|
|
|
|
return true;
|
|
|
|
#if LWIP_TCPIP_CORE_LOCKING
|
|
|
|
case LWIP_CORE_LOCK_QUERY_HOLDER:
|
|
|
|
return lwip_task ? core_lock_holder == (sys_thread_t) xTaskGetCurrentTaskHandle() : true;
|
|
|
|
case LWIP_CORE_LOCK_MARK_HOLDER:
|
|
|
|
core_lock_holder = (sys_thread_t) xTaskGetCurrentTaskHandle();
|
|
|
|
return true;
|
|
|
|
case LWIP_CORE_LOCK_UNMARK_HOLDER:
|
|
|
|
core_lock_holder = NULL;
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
case LWIP_CORE_LOCK_QUERY_HOLDER:
|
|
|
|
return lwip_task == NULL || lwip_task == (sys_thread_t) xTaskGetCurrentTaskHandle();
|
|
|
|
#endif /* LWIP_TCPIP_CORE_LOCKING */
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|