refactor(core): reformat newlib and pthread with astyle

This commit is contained in:
Marius Vikhammer
2024-02-27 10:00:06 +08:00
parent b39f13d685
commit f2fe408b99
39 changed files with 442 additions and 460 deletions
+27 -28
View File
@@ -62,10 +62,9 @@ typedef struct {
static SemaphoreHandle_t s_threads_mux = NULL;
portMUX_TYPE pthread_lazy_init_lock = portMUX_INITIALIZER_UNLOCKED; // Used for mutexes and cond vars and rwlocks
static SLIST_HEAD(esp_thread_list_head, esp_pthread_entry) s_threads_list
= SLIST_HEAD_INITIALIZER(s_threads_list);
= SLIST_HEAD_INITIALIZER(s_threads_list);
static pthread_key_t s_pthread_cfg_key;
static int pthread_mutex_lock_internal(esp_pthread_mutex_t *mux, TickType_t tmo);
static void esp_pthread_cfg_key_destructor(void *value)
@@ -239,16 +238,16 @@ static UBaseType_t coreID_to_AffinityMask(BaseType_t core_id)
#endif
static BaseType_t pthread_create_freertos_task_with_caps(TaskFunction_t pxTaskCode,
const char * const pcName,
const configSTACK_DEPTH_TYPE usStackDepth,
void * const pvParameters,
UBaseType_t uxPriority,
BaseType_t core_id,
UBaseType_t uxStackMemoryCaps,
TaskHandle_t * const pxCreatedTask)
const char * const pcName,
const configSTACK_DEPTH_TYPE usStackDepth,
void * const pvParameters,
UBaseType_t uxPriority,
BaseType_t core_id,
UBaseType_t uxStackMemoryCaps,
TaskHandle_t * const pxCreatedTask)
{
#if CONFIG_SPIRAM
#if CONFIG_FREERTOS_SMP
#if CONFIG_FREERTOS_SMP
return prvTaskCreateDynamicAffinitySetWithCaps(pxTaskCode,
pcName,
usStackDepth,
@@ -257,7 +256,7 @@ static BaseType_t pthread_create_freertos_task_with_caps(TaskFunction_t pxTaskCo
coreID_to_AffinityMask(core_id),
uxStackMemoryCaps,
pxCreatedTask);
#else
#else
return prvTaskCreateDynamicPinnedToCoreWithCaps(pxTaskCode,
pcName,
usStackDepth,
@@ -266,7 +265,7 @@ static BaseType_t pthread_create_freertos_task_with_caps(TaskFunction_t pxTaskCo
core_id,
uxStackMemoryCaps,
pxCreatedTask);
#endif
#endif
#else
return xTaskCreatePinnedToCore(pxTaskCode,
pcName,
@@ -279,7 +278,7 @@ static BaseType_t pthread_create_freertos_task_with_caps(TaskFunction_t pxTaskCo
}
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg)
void *(*start_routine)(void *), void *arg)
{
TaskHandle_t xHandle = NULL;
@@ -360,13 +359,13 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
pthread->task_arg = task_arg;
BaseType_t res = pthread_create_freertos_task_with_caps(&pthread_task_func,
task_name,
stack_size,
task_arg,
prio,
core_id,
stack_alloc_caps,
&xHandle);
task_name,
stack_size,
task_arg,
prio,
core_id,
stack_alloc_caps,
&xHandle);
if (res != pdPASS) {
ESP_LOGE(TAG, "Failed to create task!");
@@ -549,7 +548,7 @@ int pthread_cancel(pthread_t thread)
return ENOSYS;
}
int sched_yield( void )
int sched_yield(void)
{
vTaskDelay(0);
return 0;
@@ -594,8 +593,8 @@ int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
static int mutexattr_check(const pthread_mutexattr_t *attr)
{
if (attr->type != PTHREAD_MUTEX_NORMAL &&
attr->type != PTHREAD_MUTEX_RECURSIVE &&
attr->type != PTHREAD_MUTEX_ERRORCHECK) {
attr->type != PTHREAD_MUTEX_RECURSIVE &&
attr->type != PTHREAD_MUTEX_ERRORCHECK) {
return EINVAL;
}
return 0;
@@ -686,7 +685,7 @@ static int pthread_mutex_lock_internal(esp_pthread_mutex_t *mux, TickType_t tmo)
}
if ((mux->type == PTHREAD_MUTEX_ERRORCHECK) &&
(xSemaphoreGetMutexHolder(mux->sem) == xTaskGetCurrentTaskHandle())) {
(xSemaphoreGetMutexHolder(mux->sem) == xTaskGetCurrentTaskHandle())) {
return EDEADLK;
}
@@ -740,8 +739,8 @@ int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *timeo
struct timespec currtime;
clock_gettime(CLOCK_REALTIME, &currtime);
TickType_t tmo = ((timeout->tv_sec - currtime.tv_sec)*1000 +
(timeout->tv_nsec - currtime.tv_nsec)/1000000)/portTICK_PERIOD_MS;
TickType_t tmo = ((timeout->tv_sec - currtime.tv_sec) * 1000 +
(timeout->tv_nsec - currtime.tv_nsec) / 1000000) / portTICK_PERIOD_MS;
res = pthread_mutex_lock_internal((esp_pthread_mutex_t *)*mutex, tmo);
if (res == EBUSY) {
@@ -775,8 +774,8 @@ int pthread_mutex_unlock(pthread_mutex_t *mutex)
}
if (((mux->type == PTHREAD_MUTEX_RECURSIVE) ||
(mux->type == PTHREAD_MUTEX_ERRORCHECK)) &&
(xSemaphoreGetMutexHolder(mux->sem) != xTaskGetCurrentTaskHandle())) {
(mux->type == PTHREAD_MUTEX_ERRORCHECK)) &&
(xSemaphoreGetMutexHolder(mux->sem) != xTaskGetCurrentTaskHandle())) {
return EPERM;
}
+1 -1
View File
@@ -127,7 +127,7 @@ int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut, const struc
timersub(&abs_time, &cur_time, &diff_time);
// Round up timeout microseconds to the next millisecond
timeout_msec = (diff_time.tv_sec * 1000) +
((diff_time.tv_usec + 1000 - 1) / 1000);
((diff_time.tv_usec + 1000 - 1) / 1000);
}
if (timeout_msec <= 0) {
+5 -5
View File
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -17,7 +17,7 @@
/* Sanity check to ensure that the number of FreeRTOS TLSPs is at least 1 */
#if (CONFIG_FREERTOS_THREAD_LOCAL_STORAGE_POINTERS < 1)
#error "CONFIG_FREERTOS_THREAD_LOCAL_STORAGE_POINTERS cannot be 0 for pthread TLS"
#error "CONFIG_FREERTOS_THREAD_LOCAL_STORAGE_POINTERS cannot be 0 for pthread TLS"
#endif
#define PTHREAD_TLS_INDEX 0
@@ -77,7 +77,7 @@ static key_entry_t *find_key(pthread_key_t key)
portENTER_CRITICAL(&s_keys_lock);
key_entry_t *result = NULL;;
SLIST_FOREACH(result, &s_keys, next) {
if(result->key == key) {
if (result->key == key) {
break;
}
}
@@ -171,7 +171,7 @@ static value_entry_t *find_value(const values_list_t *list, pthread_key_t key)
{
value_entry_t *result = NULL;;
SLIST_FOREACH(result, list, next) {
if(result->key == key) {
if (result->key == key) {
break;
}
}
@@ -186,7 +186,7 @@ void *pthread_getspecific(pthread_key_t key)
}
value_entry_t *entry = find_value(tls, key);
if(entry != NULL) {
if (entry != NULL) {
return entry->value;
}
return NULL;
+10 -10
View File
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -23,7 +23,6 @@
#include "esp_log.h"
const static char *TAG = "pthread_rw_lock";
/** pthread rw_mutex FreeRTOS wrapper */
typedef struct {
/**
@@ -46,8 +45,8 @@ typedef struct {
#define WRITER_QUEUE_SIZE 4
#define READER_QUEUE_SIZE 4
int pthread_rwlock_init (pthread_rwlock_t *rwlock,
const pthread_rwlockattr_t *attr)
int pthread_rwlock_init(pthread_rwlock_t *rwlock,
const pthread_rwlockattr_t *attr)
{
int result;
if (!rwlock) {
@@ -99,7 +98,7 @@ static int pthread_rwlock_init_if_static(pthread_rwlock_t *rwlock)
return res;
}
int pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
{
esp_pthread_rwlock_t *esp_rwlock;
@@ -158,7 +157,7 @@ static int checkrw_lock(pthread_rwlock_t *rwlock)
return 0;
}
int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
{
esp_pthread_rwlock_t *esp_rwlock;
int res;
@@ -191,7 +190,7 @@ int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
return 0;
}
int pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
{
esp_pthread_rwlock_t *esp_rwlock;
int res;
@@ -219,7 +218,7 @@ int pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
return res;
}
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
{
esp_pthread_rwlock_t *esp_rwlock;
int res;
@@ -247,7 +246,8 @@ int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
return 0;
}
int pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock) {
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
{
esp_pthread_rwlock_t *esp_rwlock;
int res;
@@ -276,7 +276,7 @@ int pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock) {
return res;
}
int pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
{
esp_pthread_rwlock_t *esp_rwlock;
int res;
+8 -8
View File
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -24,7 +24,7 @@ int sem_destroy(sem_t * semaphore)
return -1;
}
SemaphoreHandle_t freertos_semaphore = (SemaphoreHandle_t) *semaphore;
SemaphoreHandle_t freertos_semaphore = (SemaphoreHandle_t) * semaphore;
vSemaphoreDelete(freertos_semaphore);
return 0;
}
@@ -60,7 +60,7 @@ int sem_post(sem_t * semaphore)
return -1;
}
SemaphoreHandle_t freertos_semaphore = (SemaphoreHandle_t) *semaphore;
SemaphoreHandle_t freertos_semaphore = (SemaphoreHandle_t) * semaphore;
BaseType_t ret = xSemaphoreGive(freertos_semaphore);
if (ret == pdFALSE) {
@@ -96,7 +96,7 @@ int sem_timedwait(sem_t * restrict semaphore, const struct timespec *restrict ab
long timeout_msec;
// Round up timeout nanoseconds to the next millisecond
timeout_msec = (diff_time.tv_sec * 1000) +
((diff_time.tv_nsec + (1 * MIO) - 1) / (1 * MIO));
((diff_time.tv_nsec + (1 * MIO) - 1) / (1 * MIO));
// Round up milliseconds to the next tick
timeout_ticks = (timeout_msec + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS;
@@ -112,7 +112,7 @@ int sem_timedwait(sem_t * restrict semaphore, const struct timespec *restrict ab
timeout_ticks += 1;
}
SemaphoreHandle_t freertos_semaphore = (SemaphoreHandle_t) *semaphore;
SemaphoreHandle_t freertos_semaphore = (SemaphoreHandle_t) * semaphore;
BaseType_t sem_take_result;
sem_take_result = xSemaphoreTake(freertos_semaphore, timeout_ticks);
if (sem_take_result == pdFALSE) {
@@ -130,7 +130,7 @@ int sem_trywait(sem_t * semaphore)
return -1;
}
SemaphoreHandle_t freertos_semaphore = (SemaphoreHandle_t) *semaphore;
SemaphoreHandle_t freertos_semaphore = (SemaphoreHandle_t) * semaphore;
BaseType_t ret = xSemaphoreTake(freertos_semaphore, 0);
@@ -149,7 +149,7 @@ int sem_wait(sem_t * semaphore)
return -1;
}
SemaphoreHandle_t freertos_semaphore = (SemaphoreHandle_t) *semaphore;
SemaphoreHandle_t freertos_semaphore = (SemaphoreHandle_t) * semaphore;
// Only returns failure if block time expires, but we block indefinitely, hence not return code check
xSemaphoreTake(freertos_semaphore, portMAX_DELAY);
@@ -168,7 +168,7 @@ int sem_getvalue(sem_t *restrict semaphore, int *restrict sval)
return -1;
}
SemaphoreHandle_t freertos_semaphore = (SemaphoreHandle_t) *semaphore;
SemaphoreHandle_t freertos_semaphore = (SemaphoreHandle_t) * semaphore;
*sval = uxSemaphoreGetCount(freertos_semaphore);
return 0;
}
@@ -11,7 +11,6 @@
#include "unity_test_runner.h"
#include "esp_heap_caps.h"
// Some resources are lazy allocated (e.g. newlib locks), the threshold is left for that case
#define TEST_MEMORY_LEAK_THRESHOLD (-200)
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
@@ -24,10 +24,13 @@ static void waits(int idx, int timeout_ms)
std::unique_lock<std::mutex> lk(cv_m);
auto now = std::chrono::system_clock::now();
if(cv.wait_until(lk, now + std::chrono::milliseconds(timeout_ms), [](){return i == 1;}))
std::cout << "Thread " << idx << " finished waiting. i == " << i << '\n';
else
if (cv.wait_until(lk, now + std::chrono::milliseconds(timeout_ms), []() {
return i == 1;
}))
std::cout << "Thread " << idx << " finished waiting. i == " << i << '\n';
else {
std::cout << "Thread " << idx << " timed out. i == " << i << '\n';
}
}
static void signals(int signal_ms)
@@ -53,7 +56,6 @@ TEST_CASE("C++ condition_variable", "[std::condition_variable]")
std::cout << "All threads joined\n";
}
TEST_CASE("cxx: condition_variable can timeout", "[cxx]")
{
std::condition_variable cv;
@@ -76,19 +78,20 @@ TEST_CASE("cxx: condition_variable timeout never before deadline", "[cxx]")
std::unique_lock<std::mutex> lock(mutex);
for (int i = 0; i < 25; ++i) {
auto timeout = std::chrono::milliseconds(portTICK_PERIOD_MS * (i+1));
auto timeout = std::chrono::milliseconds(portTICK_PERIOD_MS * (i + 1));
auto deadline = SysClock::now() + timeout;
auto secs = std::chrono::time_point_cast<std::chrono::seconds>(deadline);
auto nsecs = std::chrono::duration_cast<std::chrono::nanoseconds>
(deadline - secs);
(deadline - secs);
struct timespec ts = {
.tv_sec = static_cast<time_t>(secs.time_since_epoch().count()),
.tv_nsec = static_cast<long>(nsecs.count())};
.tv_sec = static_cast<time_t>(secs.time_since_epoch().count()),
.tv_nsec = static_cast<long>(nsecs.count())
};
int rc = ::pthread_cond_timedwait(cond.native_handle(),
lock.mutex()->native_handle(), &ts);
auto status = (rc == ETIMEDOUT) ? std::cv_status::timeout :
std::cv_status::no_timeout;
std::cv_status::no_timeout;
auto end = SysClock::now();
auto extra = end - deadline;
auto extra_us = extra / std::chrono::microseconds(1);
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
@@ -12,17 +12,17 @@
TEST_CASE("C++ future", "[std::future]")
{
// future from a packaged_task
std::packaged_task<int()> task([]{ return 7; }); // wrap the function
std::packaged_task<int()> task([] { return 7; }); // wrap the function
std::future<int> f1 = task.get_future(); // get a future
std::thread t(std::move(task)); // launch on a thread
// future from an async()
std::future<int> f2 = std::async(std::launch::async, []{ return 8; });
std::future<int> f2 = std::async(std::launch::async, [] { return 8; });
// future from a promise
std::promise<int> p;
std::future<int> f3 = p.get_future();
std::thread( [&p]{ p.set_value_at_thread_exit(9); }).detach();
std::thread([&p] { p.set_value_at_thread_exit(9); }).detach();
std::cout << "Waiting..." << std::flush;
f1.wait();
@@ -232,7 +232,7 @@ static void test_mutex_lock_unlock(int mutex_type)
res = pthread_mutex_lock(&mutex);
if(mutex_type == PTHREAD_MUTEX_ERRORCHECK) {
if (mutex_type == PTHREAD_MUTEX_ERRORCHECK) {
TEST_ASSERT_EQUAL_INT(EDEADLK, res);
} else {
TEST_ASSERT_EQUAL_INT(0, res);
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
@@ -114,7 +114,7 @@ TEST_CASE("pthread local storage destructor in FreeRTOS task", "[thread-specific
TEST_ASSERT_EQUAL(0, pthread_key_create(&key, test_pthread_destructor));
xTaskCreate(task_test_pthread_destructor,
"ptdest", 8192, (void *)key, UNITY_FREERTOS_PRIORITY+1,
"ptdest", 8192, (void *)key, UNITY_FREERTOS_PRIORITY + 1,
NULL);
// Above task has higher priority to us, so should run immediately
@@ -146,14 +146,13 @@ static void *thread_stress_test(void *v_key)
pthread_setspecific(key, tls_value);
for(int i = 0; i < STRESS_NUMITER; i++) {
for (int i = 0; i < STRESS_NUMITER; i++) {
TEST_ASSERT_EQUAL_HEX32(pthread_getspecific(key), tls_value);
}
return NULL;
}
// This test case added to reproduce issues with unpinned tasks and TLS
TEST_CASE("pthread local storage stress test", "[thread-specific]")
{
@@ -169,7 +168,6 @@ TEST_CASE("pthread local storage stress test", "[thread-specific]")
}
}
#define NUM_KEYS 4 // number of keys used in repeat destructor test
#define NUM_REPEATS 17 // number of times we re-set a key to a non-NULL value to re-trigger destructor
@@ -179,7 +177,6 @@ typedef struct {
int last_idx; // index of last key where destructor was called
} destr_test_state_t;
static void s_test_repeat_destructor(void *vp_state);
static void *s_test_repeat_destructor_thread(void *vp_state);
@@ -202,7 +199,7 @@ TEST_CASE("pthread local storage 'repeat' destructor test", "[thread-specific]")
TEST_ASSERT_EQUAL(0, r);
r = pthread_join(thread, NULL);
TEST_ASSERT_EQUAL(0 ,r);
TEST_ASSERT_EQUAL(0, r);
// Cheating here to make sure compiler reads the value of 'count' from memory not from a register
//