esp32s3: Update libc stubs to properly acquire/release locks.
Avoid using static mutex and recursive mutex as the resource to be acquired/release. Instead, create a specific lock for each call if it does not exist.
This commit is contained in:
parent
86bb721d80
commit
44e2e9011f
@ -24,3 +24,5 @@ ifeq ($(CONFIG_WS2812_NON_SPI_DRIVER),y)
|
|||||||
CHIP_CSRCS += esp_ws2812.c
|
CHIP_CSRCS += esp_ws2812.c
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
INCLUDES += ${INCDIR_PREFIX}$(ARCH_SRCDIR)$(DELIM)common$(DELIM)espressif$(DELIM)platform_include
|
||||||
|
213
arch/xtensa/src/common/espressif/platform_include/sys/lock.h
Normal file
213
arch/xtensa/src/common/espressif/platform_include/sys/lock.h
Normal file
@ -0,0 +1,213 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* arch/xtensa/src/common/espressif/platform_include/sys/lock.h
|
||||||
|
*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership. The
|
||||||
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance with the
|
||||||
|
* License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include_next <sys/lock.h>
|
||||||
|
|
||||||
|
#ifdef _RETARGETABLE_LOCKING
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Type Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* Actual platfrom-specific definition of struct __lock.
|
||||||
|
* The size here should be sufficient for a NuttX mutex and recursive mutex.
|
||||||
|
* This is checked by a static assertion in <chip>_libc_stubs.c
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct __lock
|
||||||
|
{
|
||||||
|
int reserved[4];
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef _LOCK_T _lock_t;
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Function Prototypes
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: _lock_init
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Allocate lock related resources.
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* plock - pointer to user defined lock object
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* None
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
void _lock_init(_lock_t *plock);
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: _lock_init_recursive
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Allocate recursive lock related resources.
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* plock - pointer to user defined lock object
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* None
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
void _lock_init_recursive(_lock_t *plock);
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: _lock_close
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Free lock related resources.
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* plock - pointer to user defined lock object
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* None
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
void _lock_close(_lock_t *plock);
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: _lock_close_recursive
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Free recursive lock related resources.
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* plock - pointer to user defined lock object
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* None
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
void _lock_close_recursive(_lock_t *plock);
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: _lock_acquire
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Acquire lock immediately after the lock object is available.
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* plock - pointer to user defined lock object
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* None
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
void _lock_acquire(_lock_t *plock);
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: _lock_acquire_recursive
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Acquire recursive lock immediately after the lock object is available.
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* plock - pointer to user defined lock object
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* None
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
void _lock_acquire_recursive(_lock_t *plock);
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: _lock_try_acquire
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Acquire lock if the lock object is available.
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* plock - pointer to user defined lock object
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* Zero for success and non-zero to indicate that the lock cannot be
|
||||||
|
* acquired
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int _lock_try_acquire(_lock_t *plock);
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: _lock_try_acquire_recursive
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Acquire recursive lock if the lock object is available.
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* plock - pointer to user defined lock object
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* Zero for success and non-zero to indicate that the lock cannot be
|
||||||
|
* acquired
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int _lock_try_acquire_recursive(_lock_t *plock);
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: _lock_release
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Relinquish the lock ownership.
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* plock - pointer to user defined lock object
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* None
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
void _lock_release(_lock_t *plock);
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: _lock_release_recursive
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Relinquish the recursive lock ownership.
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* plock - pointer to user defined lock object
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* None
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
void _lock_release_recursive(_lock_t *plock);
|
||||||
|
|
||||||
|
#endif // _RETARGETABLE_LOCKING
|
@ -207,6 +207,7 @@ chip/$(ESP_HAL_3RDPARTY_REPO):
|
|||||||
# Silent preprocessor warnings
|
# Silent preprocessor warnings
|
||||||
|
|
||||||
CFLAGS += -Wno-undef -Wno-unused-variable
|
CFLAGS += -Wno-undef -Wno-unused-variable
|
||||||
|
CFLAGS += ${DEFINE_PREFIX}_RETARGETABLE_LOCKING
|
||||||
|
|
||||||
# Files that require the HAL recipe
|
# Files that require the HAL recipe
|
||||||
|
|
||||||
|
@ -42,17 +42,12 @@
|
|||||||
* Pre-processor Definitions
|
* Pre-processor Definitions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#define _lock_t int
|
|
||||||
|
|
||||||
#define ROM_MUTEX_MAGIC 0xbb10c433
|
#define ROM_MUTEX_MAGIC 0xbb10c433
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Types
|
* Private Types
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static mutex_t g_nxlock_common;
|
|
||||||
static mutex_t g_nxlock_recursive;
|
|
||||||
|
|
||||||
/* Forward declaration */
|
/* Forward declaration */
|
||||||
|
|
||||||
struct _reent;
|
struct _reent;
|
||||||
@ -173,64 +168,108 @@ void _raise_r(struct _reent *r)
|
|||||||
|
|
||||||
void _lock_init(_lock_t *lock)
|
void _lock_init(_lock_t *lock)
|
||||||
{
|
{
|
||||||
nxmutex_init(&g_nxlock_common);
|
mutex_t *mutex = (mutex_t *)kmm_malloc(sizeof(mutex_t));
|
||||||
nxsem_get_value(&g_nxlock_common.sem, lock);
|
|
||||||
|
nxmutex_init(mutex);
|
||||||
|
|
||||||
|
*lock = (_lock_t)mutex;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _lock_init_recursive(_lock_t *lock)
|
void _lock_init_recursive(_lock_t *lock)
|
||||||
{
|
{
|
||||||
nxmutex_init(&g_nxlock_recursive);
|
rmutex_t *rmutex = (rmutex_t *)kmm_malloc(sizeof(rmutex_t));
|
||||||
nxsem_get_value(&g_nxlock_recursive.sem, lock);
|
|
||||||
|
nxrmutex_init(rmutex);
|
||||||
|
|
||||||
|
*lock = (_lock_t)rmutex;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _lock_close(_lock_t *lock)
|
void _lock_close(_lock_t *lock)
|
||||||
{
|
{
|
||||||
nxmutex_destroy(&g_nxlock_common);
|
mutex_t *mutex = (mutex_t *)(*lock);
|
||||||
|
|
||||||
|
nxmutex_destroy(mutex);
|
||||||
|
kmm_free(*lock);
|
||||||
*lock = 0;
|
*lock = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _lock_close_recursive(_lock_t *lock)
|
void _lock_close_recursive(_lock_t *lock)
|
||||||
{
|
{
|
||||||
nxmutex_destroy(&g_nxlock_recursive);
|
rmutex_t *rmutex = (rmutex_t *)(*lock);
|
||||||
|
|
||||||
|
nxrmutex_destroy(rmutex);
|
||||||
|
kmm_free(*lock);
|
||||||
*lock = 0;
|
*lock = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _lock_acquire(_lock_t *lock)
|
void _lock_acquire(_lock_t *lock)
|
||||||
{
|
{
|
||||||
nxmutex_lock(&g_nxlock_common);
|
if ((*lock) == NULL)
|
||||||
nxsem_get_value(&g_nxlock_common.sem, lock);
|
{
|
||||||
|
mutex_t *mutex = (mutex_t *)kmm_malloc(sizeof(mutex_t));
|
||||||
|
|
||||||
|
nxmutex_init(mutex);
|
||||||
|
|
||||||
|
*lock = (_lock_t)mutex;
|
||||||
|
}
|
||||||
|
|
||||||
|
nxmutex_lock((mutex_t *)(*lock));
|
||||||
}
|
}
|
||||||
|
|
||||||
void _lock_acquire_recursive(_lock_t *lock)
|
void _lock_acquire_recursive(_lock_t *lock)
|
||||||
{
|
{
|
||||||
nxmutex_lock(&g_nxlock_recursive);
|
if ((*lock) == NULL)
|
||||||
nxsem_get_value(&g_nxlock_recursive.sem, lock);
|
{
|
||||||
|
rmutex_t *rmutex = (rmutex_t *)kmm_malloc(sizeof(rmutex_t));
|
||||||
|
|
||||||
|
nxrmutex_init(rmutex);
|
||||||
|
|
||||||
|
*lock = (_lock_t)rmutex;
|
||||||
|
}
|
||||||
|
|
||||||
|
nxrmutex_lock((rmutex_t *)(*lock));
|
||||||
}
|
}
|
||||||
|
|
||||||
int _lock_try_acquire(_lock_t *lock)
|
int _lock_try_acquire(_lock_t *lock)
|
||||||
{
|
{
|
||||||
nxmutex_trylock(&g_nxlock_common);
|
if ((*lock) == NULL)
|
||||||
nxsem_get_value(&g_nxlock_common.sem, lock);
|
{
|
||||||
return 0;
|
mutex_t *mutex = (mutex_t *)kmm_malloc(sizeof(mutex_t));
|
||||||
|
|
||||||
|
nxmutex_init(mutex);
|
||||||
|
|
||||||
|
*lock = (_lock_t)mutex;
|
||||||
|
}
|
||||||
|
|
||||||
|
return nxmutex_trylock((mutex_t *)(*lock));
|
||||||
}
|
}
|
||||||
|
|
||||||
int _lock_try_acquire_recursive(_lock_t *lock)
|
int _lock_try_acquire_recursive(_lock_t *lock)
|
||||||
{
|
{
|
||||||
nxmutex_trylock(&g_nxlock_recursive);
|
if ((*lock) == NULL)
|
||||||
nxsem_get_value(&g_nxlock_recursive.sem, lock);
|
{
|
||||||
return 0;
|
rmutex_t *rmutex = (rmutex_t *)kmm_malloc(sizeof(rmutex_t));
|
||||||
|
|
||||||
|
nxrmutex_init(rmutex);
|
||||||
|
|
||||||
|
*lock = (_lock_t)rmutex;
|
||||||
|
}
|
||||||
|
|
||||||
|
return nxrmutex_trylock((rmutex_t *)(*lock));
|
||||||
}
|
}
|
||||||
|
|
||||||
void _lock_release(_lock_t *lock)
|
void _lock_release(_lock_t *lock)
|
||||||
{
|
{
|
||||||
nxmutex_unlock(&g_nxlock_common);
|
mutex_t *mutex = (mutex_t *)(*lock);
|
||||||
nxsem_get_value(&g_nxlock_common.sem, lock);
|
|
||||||
|
nxmutex_unlock(mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _lock_release_recursive(_lock_t *lock)
|
void _lock_release_recursive(_lock_t *lock)
|
||||||
{
|
{
|
||||||
nxmutex_unlock(&g_nxlock_recursive);
|
rmutex_t *rmutex = (rmutex_t *)(*lock);
|
||||||
nxsem_get_value(&g_nxlock_recursive.sem, lock);
|
|
||||||
|
nxrmutex_unlock(rmutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void __retarget_lock_init(_lock_t *lock)
|
void __retarget_lock_init(_lock_t *lock)
|
||||||
@ -376,6 +415,8 @@ static const struct syscall_stub_table g_stub_table =
|
|||||||
|
|
||||||
void esp_setup_syscall_table(void)
|
void esp_setup_syscall_table(void)
|
||||||
{
|
{
|
||||||
|
static_assert(sizeof(struct __lock) >= sizeof(mutex_t));
|
||||||
|
|
||||||
syscall_table_ptr = (struct syscall_stub_table *)&g_stub_table;
|
syscall_table_ptr = (struct syscall_stub_table *)&g_stub_table;
|
||||||
|
|
||||||
/* Newlib 3.3.0 is used in ROM, built with _RETARGETABLE_LOCKING.
|
/* Newlib 3.3.0 is used in ROM, built with _RETARGETABLE_LOCKING.
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <sys/lock.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <sys/times.h>
|
#include <sys/times.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@ -38,8 +39,6 @@
|
|||||||
|
|
||||||
#include <nuttx/mutex.h>
|
#include <nuttx/mutex.h>
|
||||||
|
|
||||||
#define _lock_t int
|
|
||||||
|
|
||||||
/* Forward declaration */
|
/* Forward declaration */
|
||||||
|
|
||||||
struct _reent;
|
struct _reent;
|
||||||
|
Loading…
Reference in New Issue
Block a user