feat(newlib): Add lock function

Fixed some warnings due to the lack of macrodefinition.
This commit is contained in:
Dong Heng
2018-05-15 16:16:53 +08:00
parent 60c1e02e09
commit d7e96f9ea0
5 changed files with 50 additions and 45 deletions

View File

@ -157,6 +157,15 @@ void vPortEndScheduler( void ) PRIVILEGED_FUNCTION;
void vPortStoreTaskMPUSettings( xMPU_SETTINGS *xMPUSettings, const struct xMEMORY_REGION * const xRegions, StackType_t *pxBottomOfStack, uint32_t ulStackDepth ) PRIVILEGED_FUNCTION;
#endif
/*
* @brief check if current at interrupt context
*
* @param none
*
* @return 1 if at interrupt context or 0
*/
int xPortInIsrContext(void);
#ifdef __cplusplus
}
#endif

View File

@ -143,7 +143,6 @@ NVIC value of 255. */
#define configLIBRARY_KERNEL_INTERRUPT_PRIORITY 15
// add it to menuconfig later
#define CONFIG_FREERTOS_ENABLE_REENT
#ifdef CONFIG_FREERTOS_ENABLE_REENT
#define configUSE_NEWLIB_REENTRANT 1
#endif

View File

@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <reent.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
@ -20,8 +22,7 @@ struct _reent *_global_impure_ptr = &impure_data;
struct _reent *__getreent()
{
extern char _xt_isr_status;
#if configUSE_NEWLIB_REENTRANT == 1
/*
* Locking mutex(only mutex, not ISR mutex) at the following three
* state may cause OS death. So we use a extra _reent data instead
@ -32,7 +33,7 @@ struct _reent *__getreent()
* So we command that use ets_printf(ROM function) instead of "printf"
* at exception and system kernal critical state.
*/
if (_xt_isr_status
if (xPortInIsrContext()
|| !xTaskGetCurrentTaskHandle()
|| xTaskGetSchedulerState() != taskSCHEDULER_RUNNING)
return &impure_data;
@ -40,5 +41,6 @@ struct _reent *__getreent()
/*
* When scheduler starts, _global_impure_ptr = pxCurrentTCB->xNewLib_reent.
*/
#endif
return _global_impure_ptr;
}

View File

@ -323,6 +323,11 @@ uint16 IRAM_ATTR _xt_isr_handler(uint16 i)
return i & ~(1 << index);
}
int xPortInIsrContext(void)
{
return _xt_isr_status != 0;
}
void vApplicationStackOverflowHook(xTaskHandle xTask, signed char *pcTaskName)
{
os_printf("task [%s] stask overflow\n", pcTaskName);

View File

@ -14,32 +14,13 @@
#include <sys/lock.h>
#include <stdlib.h>
#include <sys/reent.h>
#include "esp_attr.h"
#include "soc/cpu.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/portmacro.h"
#include "freertos/task.h"
#include "freertos/portable.h"
#include <reent.h>
/* Notes on our newlib lock implementation:
*
* - Use FreeRTOS mutex semaphores as locks.
* - lock_t is int, but we store an xSemaphoreHandle there.
* - Locks are no-ops until the FreeRTOS scheduler is running.
* - Due to this, locks need to be lazily initialised the first time
* they are acquired. Initialisation/deinitialisation of locks is
* protected by lock_init_spinlock.
* - Race conditions around lazy initialisation (via lock_acquire) are
* protected against.
* - Anyone calling lock_close is reponsible for ensuring noone else
* is holding the lock at this time.
* - Race conditions between lock_close & lock_init (for the same lock)
* are the responsibility of the caller.
*/
#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"
static portMUX_TYPE lock_init_spinlock = portMUX_INITIALIZER_UNLOCKED;
#define portYIELD_FROM_ISR portYIELD
/* Initialize the given lock by allocating a new mutex semaphore
as the _lock_t value.
@ -47,11 +28,12 @@ static portMUX_TYPE lock_init_spinlock = portMUX_INITIALIZER_UNLOCKED;
Called by _lock_init*, also called by _lock_acquire* to lazily initialize locks that might have
been initialised (to zero only) before the RTOS scheduler started.
*/
static void IRAM_ATTR lock_init_generic(_lock_t *lock, uint8_t mutex_type) {
portENTER_CRITICAL(&lock_init_spinlock);
static void lock_init_generic(_lock_t *lock, uint8_t mutex_type) {
portENTER_CRITICAL();
if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
/* nothing to do until the scheduler is running */
portEXIT_CRITICAL(&lock_init_spinlock);
portEXIT_CRITICAL();
return;
}
@ -82,15 +64,15 @@ static void IRAM_ATTR lock_init_generic(_lock_t *lock, uint8_t mutex_type) {
}
*lock = (_lock_t)new_sem;
}
portEXIT_CRITICAL(&lock_init_spinlock);
portEXIT_CRITICAL();
}
void IRAM_ATTR _lock_init(_lock_t *lock) {
void _lock_init(_lock_t *lock) {
*lock = 0; // In case lock's memory is uninitialized
lock_init_generic(lock, queueQUEUE_TYPE_MUTEX);
}
void IRAM_ATTR _lock_init_recursive(_lock_t *lock) {
void _lock_init_recursive(_lock_t *lock) {
*lock = 0; // In case lock's memory is uninitialized
lock_init_generic(lock, queueQUEUE_TYPE_RECURSIVE_MUTEX);
}
@ -105,8 +87,8 @@ void IRAM_ATTR _lock_init_recursive(_lock_t *lock) {
re-initialised if it is used again. Caller has to avoid doing
this!
*/
void IRAM_ATTR _lock_close(_lock_t *lock) {
portENTER_CRITICAL(&lock_init_spinlock);
static void lock_close_generic(_lock_t *lock, uint8_t mutex_type) {
portENTER_CRITICAL();
if (*lock) {
xSemaphoreHandle h = (xSemaphoreHandle)(*lock);
#if (INCLUDE_xSemaphoreGetMutexHolder == 1)
@ -115,13 +97,21 @@ void IRAM_ATTR _lock_close(_lock_t *lock) {
vSemaphoreDelete(h);
*lock = 0;
}
portEXIT_CRITICAL(&lock_init_spinlock);
portEXIT_CRITICAL();
}
void _lock_close(_lock_t *lock) {
lock_close_generic(lock, queueQUEUE_TYPE_MUTEX);
}
void _lock_close_recursive(_lock_t *lock) {
lock_close_generic(lock, queueQUEUE_TYPE_RECURSIVE_MUTEX);
}
/* Acquire the mutex semaphore for lock. wait up to delay ticks.
mutex_type is queueQUEUE_TYPE_RECURSIVE_MUTEX or queueQUEUE_TYPE_MUTEX
*/
static int IRAM_ATTR lock_acquire_generic(_lock_t *lock, uint32_t delay, uint8_t mutex_type) {
static int lock_acquire_generic(_lock_t *lock, uint32_t delay, uint8_t mutex_type) {
xSemaphoreHandle h = (xSemaphoreHandle)(*lock);
if (!h) {
if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
@ -161,26 +151,26 @@ static int IRAM_ATTR lock_acquire_generic(_lock_t *lock, uint32_t delay, uint8_t
return (success == pdTRUE) ? 0 : -1;
}
void IRAM_ATTR _lock_acquire(_lock_t *lock) {
void _lock_acquire(_lock_t *lock) {
lock_acquire_generic(lock, portMAX_DELAY, queueQUEUE_TYPE_MUTEX);
}
void IRAM_ATTR _lock_acquire_recursive(_lock_t *lock) {
void _lock_acquire_recursive(_lock_t *lock) {
lock_acquire_generic(lock, portMAX_DELAY, queueQUEUE_TYPE_RECURSIVE_MUTEX);
}
int IRAM_ATTR _lock_try_acquire(_lock_t *lock) {
int _lock_try_acquire(_lock_t *lock) {
return lock_acquire_generic(lock, 0, queueQUEUE_TYPE_MUTEX);
}
int IRAM_ATTR _lock_try_acquire_recursive(_lock_t *lock) {
int _lock_try_acquire_recursive(_lock_t *lock) {
return lock_acquire_generic(lock, 0, queueQUEUE_TYPE_RECURSIVE_MUTEX);
}
/* Release the mutex semaphore for lock.
mutex_type is queueQUEUE_TYPE_RECURSIVE_MUTEX or queueQUEUE_TYPE_MUTEX
*/
static void IRAM_ATTR lock_release_generic(_lock_t *lock, uint8_t mutex_type) {
static void lock_release_generic(_lock_t *lock, uint8_t mutex_type) {
xSemaphoreHandle h = (xSemaphoreHandle)(*lock);
if (h == NULL) {
/* This is probably because the scheduler isn't running yet,
@ -207,10 +197,10 @@ static void IRAM_ATTR lock_release_generic(_lock_t *lock, uint8_t mutex_type) {
}
}
void IRAM_ATTR _lock_release(_lock_t *lock) {
void _lock_release(_lock_t *lock) {
lock_release_generic(lock, queueQUEUE_TYPE_MUTEX);
}
void IRAM_ATTR _lock_release_recursive(_lock_t *lock) {
void _lock_release_recursive(_lock_t *lock) {
lock_release_generic(lock, queueQUEUE_TYPE_RECURSIVE_MUTEX);
}