Skip to content

Commit 4c7d9f9

Browse files
committed
Merge branch 'contrib/github_pr_14384' into 'master'
fix(pthread): Add esp_pthread function implementations for linux target (GitHub PR) Closes IDFGH-13485 See merge request espressif/esp-idf!33278
2 parents c62555d + f9e7305 commit 4c7d9f9

File tree

10 files changed

+213
-58
lines changed

10 files changed

+213
-58
lines changed

components/pthread/include/esp_pthread.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ esp_pthread_cfg_t esp_pthread_get_default_config(void);
6363
* @return
6464
* - ESP_OK if configuration was successfully set
6565
* - ESP_ERR_NO_MEM if out of memory
66+
* - ESP_ERR_INVALID_ARG if cfg is NULL
6667
* - ESP_ERR_INVALID_ARG if stack_size is less than PTHREAD_STACK_MIN
6768
* - ESP_ERR_INVALID_ARG if stack_alloc_caps does not include MALLOC_CAP_8BIT
6869
*/
@@ -79,6 +80,7 @@ esp_err_t esp_pthread_set_cfg(const esp_pthread_cfg_t *cfg);
7980
*
8081
* @return
8182
* - ESP_OK if the configuration was available
83+
* - ESP_ERR_INVALID_ARG if p is NULL
8284
* - ESP_ERR_NOT_FOUND if a configuration wasn't previously set
8385
*/
8486
esp_err_t esp_pthread_get_cfg(esp_pthread_cfg_t *p);

components/pthread/port/linux/pthread.c

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,25 @@
77
* pthread port for Linux build
88
*/
99

10+
#include <pthread.h>
11+
#include <sys/param.h>
12+
#include "sdkconfig.h"
1013
#include "esp_pthread.h"
14+
#include "esp_heap_caps.h"
15+
1116
#include <string.h>
17+
#include "freertos/FreeRTOS.h"
18+
19+
static pthread_key_t s_pthread_cfg_key;
20+
static void esp_pthread_cfg_key_destructor(void *value)
21+
{
22+
free(value);
23+
}
24+
25+
static int get_default_pthread_core(void)
26+
{
27+
return CONFIG_PTHREAD_TASK_CORE_DEFAULT == -1 ? tskNO_AFFINITY : CONFIG_PTHREAD_TASK_CORE_DEFAULT;
28+
}
1229

1330
/**
1431
* @brief Creates a default pthread configuration based
@@ -24,20 +41,72 @@ esp_pthread_cfg_t esp_pthread_get_default_config(void)
2441
.prio = CONFIG_PTHREAD_TASK_PRIO_DEFAULT,
2542
.inherit_cfg = false,
2643
.thread_name = NULL,
27-
.pin_to_core = 0,
28-
.stack_alloc_caps = 0,
44+
.pin_to_core = get_default_pthread_core(),
45+
.stack_alloc_caps = MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT,
2946
};
3047

3148
return cfg;
3249
}
3350

3451
esp_err_t esp_pthread_set_cfg(const esp_pthread_cfg_t *cfg)
3552
{
53+
// Not checking the stack size here since PTHREAD_STACK_MIN has two conflicting declarations on Linux
54+
55+
if (cfg == NULL) {
56+
return ESP_ERR_INVALID_ARG;
57+
}
58+
59+
// 0 is treated as default value, hence change caps to MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL in that case
60+
int heap_caps;
61+
if (cfg->stack_alloc_caps == 0) {
62+
heap_caps = MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL;
63+
} else {
64+
// Check that memory is 8-bit capable
65+
if (!(cfg->stack_alloc_caps & MALLOC_CAP_8BIT)) {
66+
return ESP_ERR_INVALID_ARG;
67+
}
68+
69+
heap_caps = cfg->stack_alloc_caps;
70+
}
71+
72+
/* If a value is already set, update that value */
73+
esp_pthread_cfg_t *p = pthread_getspecific(s_pthread_cfg_key);
74+
if (!p) {
75+
p = malloc(sizeof(esp_pthread_cfg_t));
76+
if (!p) {
77+
return ESP_ERR_NO_MEM;
78+
}
79+
}
80+
*p = *cfg;
81+
p->stack_alloc_caps = heap_caps;
82+
p->stack_size = MAX(p->stack_size, 0x4000); // make sure Linux minimal stack size is respected
83+
84+
int __attribute((unused)) res = pthread_setspecific(s_pthread_cfg_key, p);
85+
86+
assert(res == 0);
87+
3688
return ESP_OK;
3789
}
3890

3991
esp_err_t esp_pthread_get_cfg(esp_pthread_cfg_t *p)
4092
{
93+
if (p == NULL) {
94+
return ESP_ERR_INVALID_ARG;
95+
}
96+
97+
esp_pthread_cfg_t *cfg = pthread_getspecific(s_pthread_cfg_key);
98+
if (cfg) {
99+
*p = *cfg;
100+
return ESP_OK;
101+
}
41102
memset(p, 0, sizeof(*p));
42103
return ESP_ERR_NOT_FOUND;
43104
}
105+
106+
__attribute__((constructor)) esp_err_t esp_pthread_init(void)
107+
{
108+
if (pthread_key_create(&s_pthread_cfg_key, esp_pthread_cfg_key_destructor) != 0) {
109+
return ESP_ERR_NO_MEM;
110+
}
111+
return ESP_OK;
112+
}

components/pthread/pthread.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ static void pthread_delete(esp_pthread_t *pthread)
142142
/* Call this function to configure pthread stacks in Pthreads */
143143
esp_err_t esp_pthread_set_cfg(const esp_pthread_cfg_t *cfg)
144144
{
145+
if (cfg == NULL) {
146+
return ESP_ERR_INVALID_ARG;
147+
}
148+
145149
if (cfg->stack_size < PTHREAD_STACK_MIN) {
146150
return ESP_ERR_INVALID_ARG;
147151
}
@@ -174,12 +178,16 @@ esp_err_t esp_pthread_set_cfg(const esp_pthread_cfg_t *cfg)
174178
p->stack_alloc_caps = heap_caps;
175179
pthread_setspecific(s_pthread_cfg_key, p);
176180

177-
return 0;
181+
return ESP_OK;
178182
ESP_COMPILER_DIAGNOSTIC_POP("-Wanalyzer-malloc-leak")
179183
}
180184

181185
esp_err_t esp_pthread_get_cfg(esp_pthread_cfg_t *p)
182186
{
187+
if (p == NULL) {
188+
return ESP_ERR_INVALID_ARG;
189+
}
190+
183191
ESP_RETURN_ON_ERROR(lazy_init_pthread_cfg_key(), TAG, "Failed to initialize pthread key");
184192

185193
esp_pthread_cfg_t *cfg = pthread_getspecific(s_pthread_cfg_key);

components/pthread/test_apps/.build-test-rules.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ components/pthread/test_apps/pthread_psram_tests:
44
enable:
55
- if: IDF_TARGET in ["esp32"]
66
reason: PSRAM only available on ESP32, S2, S3; code is fairly generic
7+
8+
components/pthread/test_apps/pthread_unity_tests:
9+
enable:
10+
- if: IDF_TARGET in ["esp32", "esp32c2", "esp32c3", "esp32c5", "esp32c6", "esp32c61", "esp32h2", "esp32p4", "esp32s2", "esp32s3", "linux"]
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 |
2-
| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- |
1+
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | Linux |
2+
| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | ----- |
Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
set(sources "test_app_main.c"
2-
"test_pthread.c"
3-
"test_pthread_cond_var.c"
4-
"test_pthread_local_storage.c"
5-
"test_pthread_cxx.cpp"
6-
"test_pthread_rwlock.c"
7-
"test_pthread_semaphore.c")
1+
idf_build_get_property(target IDF_TARGET)
2+
3+
set(sources "test_app_main.c" "test_esp_pthread.c")
4+
set(priv_requires "pthread" "unity")
5+
6+
if(NOT ${target} STREQUAL "linux")
7+
list(APPEND sources "test_pthread.c"
8+
"test_pthread_cond_var.c"
9+
"test_pthread_local_storage.c"
10+
"test_pthread_cxx.cpp"
11+
"test_pthread_rwlock.c"
12+
"test_pthread_semaphore.c")
13+
list(APPEND priv_requires "esp_timer" "test_utils")
14+
endif()
815

916
idf_component_register(SRCS ${sources}
1017
INCLUDE_DIRS "."
11-
REQUIRES pthread esp_timer test_utils
18+
REQUIRES ${priv_requires}
1219
WHOLE_ARCHIVE)

components/pthread/test_apps/pthread_unity_tests/main/test_app_main.c

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,35 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
#include <stdio.h>
78
#include <errno.h>
89
#include "freertos/FreeRTOS.h"
910
#include "freertos/task.h"
1011
#include "unity.h"
1112
#include "unity_test_runner.h"
12-
#include "esp_heap_caps.h"
13+
#include "unity_test_utils_memory.h"
1314

1415
// Some resources are lazy allocated (e.g. newlib locks), the threshold is left for that case
1516
#define TEST_MEMORY_LEAK_THRESHOLD (-200)
1617

17-
static size_t before_free_8bit;
18-
static size_t before_free_32bit;
19-
20-
static void check_leak(size_t before_free, size_t after_free, const char *type)
21-
{
22-
ssize_t delta = after_free - before_free;
23-
printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta);
24-
TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak");
25-
}
26-
2718
void setUp(void)
2819
{
29-
before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
30-
before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
20+
unity_utils_set_leak_level(TEST_MEMORY_LEAK_THRESHOLD);
21+
unity_utils_record_free_mem();
3122
errno = 0;
3223
}
3324

3425
void tearDown(void)
3526
{
27+
#ifndef CONFIG_IDF_TARGET_LINUX // on Linux, we don't check for memory leaks with memory utils
3628
// Add a short delay of 200ms to allow the idle task to free remaining memory
3729
vTaskDelay(pdMS_TO_TICKS(200));
38-
size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
39-
size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
40-
check_leak(before_free_8bit, after_free_8bit, "8BIT");
41-
check_leak(before_free_32bit, after_free_32bit, "32BIT");
30+
#endif // CONFIG_IDF_TARGET_LINUX
31+
32+
unity_utils_evaluate_leaks();
4233
}
4334

4435
void app_main(void)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Unlicense OR CC0-1.0
5+
*/
6+
7+
#include <pthread.h>
8+
#include "sdkconfig.h"
9+
#include "esp_pthread.h"
10+
#include "esp_heap_caps.h"
11+
#include "unity.h"
12+
13+
TEST_CASE("esp_pthread_get_default_config creates correct stack memory capabilities", "[cfg]")
14+
{
15+
esp_pthread_cfg_t default_config = esp_pthread_get_default_config();
16+
17+
// The default must always be internal, 8-bit accessible RAM
18+
TEST_ASSERT_EQUAL_HEX(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL, default_config.stack_alloc_caps);
19+
}
20+
21+
TEST_CASE("null pointers are rejected", "[cfg]")
22+
{
23+
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_pthread_set_cfg(NULL));
24+
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_pthread_get_cfg(NULL));
25+
}
26+
27+
TEST_CASE("wrong heap caps are rejected", "[cfg]")
28+
{
29+
esp_pthread_cfg_t default_config = esp_pthread_get_default_config();
30+
TEST_ASSERT_EQUAL(ESP_OK, esp_pthread_set_cfg(&default_config)); // make sure we have saved a known value
31+
32+
// Test the rejection of wrong values
33+
default_config.stack_alloc_caps = MALLOC_CAP_32BIT;
34+
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_pthread_set_cfg(&default_config));
35+
36+
default_config.stack_alloc_caps = MALLOC_CAP_32BIT | MALLOC_CAP_INTERNAL;
37+
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_pthread_set_cfg(&default_config));
38+
39+
// check that saved values are unaltered
40+
esp_pthread_cfg_t retrieved_config;
41+
TEST_ASSERT_EQUAL(ESP_OK, esp_pthread_get_cfg(&retrieved_config));
42+
TEST_ASSERT_EQUAL_HEX(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL, retrieved_config.stack_alloc_caps);
43+
}
44+
45+
// On Linux, we silently adjust the stack size since pthread on Linux
46+
// requires a minimum stack size of 0x4000.
47+
#if !CONFIG_IDF_TARGET_LINUX
48+
TEST_CASE("invalid stack size is rejected", "[cfg]")
49+
{
50+
esp_pthread_cfg_t default_config = esp_pthread_get_default_config();
51+
TEST_ASSERT_EQUAL(ESP_OK, esp_pthread_set_cfg(&default_config)); // make sure we have saved a known value
52+
53+
// Test the rejection of wrong values
54+
default_config.stack_size = PTHREAD_STACK_MIN - 1;
55+
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_pthread_set_cfg(&default_config));
56+
57+
// check that saved values are unaltered
58+
esp_pthread_cfg_t retrieved_config;
59+
TEST_ASSERT_EQUAL(ESP_OK, esp_pthread_get_cfg(&retrieved_config));
60+
TEST_ASSERT_EQUAL(CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT, retrieved_config.stack_size);
61+
}
62+
#endif // !CONFIG_IDF_TARGET_LINUX
63+
64+
TEST_CASE("correct memory is accepted", "[cfg]")
65+
{
66+
esp_pthread_cfg_t default_config = esp_pthread_get_default_config();
67+
68+
default_config.stack_alloc_caps = MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL;
69+
TEST_ASSERT_EQUAL(ESP_OK, esp_pthread_set_cfg(&default_config));
70+
}
71+
72+
TEST_CASE("configuration is preserved inside pthread", "[cfg]")
73+
{
74+
esp_pthread_cfg_t saved_config;
75+
esp_pthread_cfg_t retrieved_config;
76+
saved_config.stack_size = PTHREAD_STACK_MIN;
77+
saved_config.prio = 5;
78+
saved_config.inherit_cfg = true;
79+
saved_config.thread_name = "test_esp_pthread";
80+
saved_config.pin_to_core = 0;
81+
saved_config.stack_alloc_caps = MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL;
82+
83+
TEST_ASSERT_EQUAL(ESP_OK, esp_pthread_set_cfg(&saved_config));
84+
TEST_ASSERT_EQUAL(ESP_OK, esp_pthread_get_cfg(&retrieved_config));
85+
86+
TEST_ASSERT_EQUAL(saved_config.stack_size, retrieved_config.stack_size);
87+
TEST_ASSERT_EQUAL(saved_config.prio, retrieved_config.prio);
88+
TEST_ASSERT_EQUAL(saved_config.inherit_cfg, retrieved_config.inherit_cfg);
89+
TEST_ASSERT_EQUAL(saved_config.thread_name, retrieved_config.thread_name);
90+
TEST_ASSERT_EQUAL(saved_config.pin_to_core, retrieved_config.pin_to_core);
91+
TEST_ASSERT_EQUAL(saved_config.stack_alloc_caps, retrieved_config.stack_alloc_caps);
92+
93+
esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
94+
TEST_ASSERT_EQUAL(ESP_OK, esp_pthread_set_cfg(&cfg));
95+
}

components/pthread/test_apps/pthread_unity_tests/main/test_pthread.c

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,6 @@
1313

1414
#include "unity.h"
1515

16-
TEST_CASE("esp_pthread_get_default_config creates correct stack memory capabilities", "[set_cfg]")
17-
{
18-
esp_pthread_cfg_t default_config = esp_pthread_get_default_config();
19-
20-
// The default must always be internal, 8-bit accessible RAM
21-
TEST_ASSERT_EQUAL_HEX(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL, default_config.stack_alloc_caps);
22-
}
23-
24-
TEST_CASE("wrong heap caps are rejected", "[set_cfg]")
25-
{
26-
esp_pthread_cfg_t default_config = esp_pthread_get_default_config();
27-
28-
default_config.stack_alloc_caps = MALLOC_CAP_32BIT;
29-
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_pthread_set_cfg(&default_config));
30-
31-
default_config.stack_alloc_caps = MALLOC_CAP_32BIT | MALLOC_CAP_INTERNAL;
32-
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_pthread_set_cfg(&default_config));
33-
}
34-
35-
TEST_CASE("correct memory is accepted", "[set_cfg]")
36-
{
37-
esp_pthread_cfg_t default_config = esp_pthread_get_default_config();
38-
39-
default_config.stack_alloc_caps = MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL;
40-
TEST_ASSERT_EQUAL(ESP_OK, esp_pthread_set_cfg(&default_config));
41-
}
42-
4316
static void *compute_square(void *arg)
4417
{
4518
int *num = (int *) arg;

components/pthread/test_apps/pthread_unity_tests/pytest_pthread_unity_tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,9 @@ def test_pthread_qemu(dut: Dut) -> None:
6262
for case in dut.test_menu:
6363
if 'qemu-ignore' not in case.groups and case.type == 'normal':
6464
dut._run_normal_case(case, timeout=75)
65+
66+
67+
@pytest.mark.linux
68+
@pytest.mark.host_test
69+
def test_pthread_linux(dut: Dut) -> None:
70+
dut.run_all_single_board_cases(timeout=120)

0 commit comments

Comments
 (0)