2018-02-04 17:06:45 -05:00
|
|
|
/*
|
|
|
|
Test for thread local storage support.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
2020-12-23 19:55:10 -05:00
|
|
|
#include <stdlib.h>
|
2018-02-04 17:06:45 -05:00
|
|
|
#include <esp_types.h>
|
|
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "unity.h"
|
2018-10-25 00:52:32 -04:00
|
|
|
#include "test_utils.h"
|
2018-02-04 17:06:45 -05:00
|
|
|
#include "sdkconfig.h"
|
|
|
|
|
2020-12-23 19:55:10 -05:00
|
|
|
#if defined(__XTENSA__)
|
|
|
|
#define GET_THREADPTR(tp_dest) do { asm volatile ("rur.threadptr %0":"=r"(tp_dest)); } while(0)
|
|
|
|
#elif defined (__riscv)
|
|
|
|
#define GET_THREADPTR(tp_dest) do { register uint32_t _tp asm("tp"); tp_dest = _tp; } while(0)
|
|
|
|
#endif
|
|
|
|
|
2020-01-02 01:25:33 -05:00
|
|
|
|
2018-02-04 17:06:45 -05:00
|
|
|
static __thread int tl_test_var1;
|
|
|
|
static __thread uint8_t tl_test_var2 = 55;
|
|
|
|
static __thread uint16_t tl_test_var3 = 44;
|
|
|
|
static __thread uint8_t tl_test_arr_var[10];
|
|
|
|
static __thread struct test_tls_var {
|
|
|
|
int f32;
|
|
|
|
uint8_t f8;
|
|
|
|
uint16_t f16;
|
|
|
|
uint8_t farr[10];
|
|
|
|
} tl_test_struct_var;
|
|
|
|
|
|
|
|
static void task_test_tls(void *arg)
|
|
|
|
{
|
|
|
|
bool *running = (bool *)arg;
|
2020-12-23 19:55:10 -05:00
|
|
|
uint32_t tp = (uint32_t) -1;
|
2018-02-04 17:06:45 -05:00
|
|
|
int test_var1_old = 0;
|
|
|
|
uint8_t test_var2_old = 0;
|
|
|
|
uint16_t test_var3_old = 0;
|
|
|
|
int f32_old = 0;
|
|
|
|
uint8_t f8_old = 0;
|
|
|
|
uint16_t f16_old = 0;
|
2020-12-23 19:55:10 -05:00
|
|
|
srand((int) xTaskGetCurrentTaskHandle());
|
|
|
|
int step = (rand() % 10) + 1;
|
2018-02-04 17:06:45 -05:00
|
|
|
|
2020-12-23 19:55:10 -05:00
|
|
|
GET_THREADPTR(tp);
|
2018-02-04 17:06:45 -05:00
|
|
|
for (int i = 0; i < 5; i++) {
|
2020-12-23 19:55:10 -05:00
|
|
|
printf("Task[%x]: var = 0x%x 0x%x step=%d\n", tp, tl_test_var1, tl_test_var2, step);
|
2018-02-04 17:06:45 -05:00
|
|
|
if (i == 0) {
|
|
|
|
TEST_ASSERT_EQUAL(0, tl_test_var1);
|
|
|
|
TEST_ASSERT_EQUAL(55, tl_test_var2);
|
|
|
|
TEST_ASSERT_EQUAL(44, tl_test_var3);
|
|
|
|
for (int k = 0; k < sizeof(tl_test_arr_var); k++) {
|
|
|
|
TEST_ASSERT_EQUAL(0, tl_test_arr_var[k]);
|
|
|
|
}
|
|
|
|
TEST_ASSERT_EQUAL(0, tl_test_struct_var.f32);
|
|
|
|
TEST_ASSERT_EQUAL(0, tl_test_struct_var.f8);
|
|
|
|
TEST_ASSERT_EQUAL(0, tl_test_struct_var.f16);
|
|
|
|
for (int k = 0; k < sizeof(tl_test_struct_var.farr); k++) {
|
|
|
|
TEST_ASSERT_EQUAL(0, tl_test_struct_var.farr[k]);
|
|
|
|
}
|
|
|
|
} else {
|
2020-12-23 19:55:10 -05:00
|
|
|
TEST_ASSERT_EQUAL(test_var1_old + step, tl_test_var1);
|
|
|
|
TEST_ASSERT_EQUAL(test_var2_old + step, tl_test_var2);
|
|
|
|
TEST_ASSERT_EQUAL(test_var3_old + step, tl_test_var3);
|
2018-02-04 17:06:45 -05:00
|
|
|
for (int k = 0; k < sizeof(tl_test_arr_var); k++) {
|
2020-12-23 19:55:10 -05:00
|
|
|
TEST_ASSERT_EQUAL((i - 1) * step, tl_test_arr_var[k]);
|
2018-02-04 17:06:45 -05:00
|
|
|
}
|
2020-12-23 19:55:10 -05:00
|
|
|
TEST_ASSERT_EQUAL(f32_old + step, tl_test_struct_var.f32);
|
|
|
|
TEST_ASSERT_EQUAL(f8_old + step, tl_test_struct_var.f8);
|
|
|
|
TEST_ASSERT_EQUAL(f16_old + step, tl_test_struct_var.f16);
|
2018-02-04 17:06:45 -05:00
|
|
|
for (int k = 0; k < sizeof(tl_test_struct_var.farr); k++) {
|
2020-12-23 19:55:10 -05:00
|
|
|
TEST_ASSERT_EQUAL((i - 1) * step, tl_test_struct_var.farr[k]);
|
2018-02-04 17:06:45 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-23 19:55:10 -05:00
|
|
|
|
2018-02-04 17:06:45 -05:00
|
|
|
test_var1_old = tl_test_var1;
|
|
|
|
test_var2_old = tl_test_var2;
|
|
|
|
test_var3_old = tl_test_var3;
|
|
|
|
f32_old = tl_test_struct_var.f32;
|
|
|
|
f8_old = tl_test_struct_var.f8;
|
|
|
|
f16_old = tl_test_struct_var.f16;
|
2020-12-23 19:55:10 -05:00
|
|
|
tl_test_var1 += step;
|
|
|
|
tl_test_var2 += step;
|
|
|
|
tl_test_var3 += step;
|
|
|
|
memset(tl_test_arr_var, i * step, sizeof(tl_test_arr_var));
|
|
|
|
tl_test_struct_var.f32 += step;
|
|
|
|
tl_test_struct_var.f8 += step;
|
|
|
|
tl_test_struct_var.f16 += step;
|
|
|
|
memset(tl_test_struct_var.farr, i * step, sizeof(tl_test_struct_var.farr));
|
2018-02-04 17:06:45 -05:00
|
|
|
vTaskDelay(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (running) {
|
|
|
|
*running = false;
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 01:25:33 -05:00
|
|
|
TEST_CASE("TLS test", "[freertos]")
|
2018-02-04 17:06:45 -05:00
|
|
|
{
|
2018-07-17 01:39:40 -04:00
|
|
|
const size_t stack_size = 3072;
|
|
|
|
StackType_t s_stack[stack_size]; /* with 8KB test task stack (default) this test still has ~3KB headroom */
|
2018-02-04 17:06:45 -05:00
|
|
|
StaticTask_t s_task;
|
|
|
|
bool running[2] = {true, true};
|
|
|
|
#if CONFIG_FREERTOS_UNICORE == 0
|
|
|
|
int other_core = 1;
|
|
|
|
#else
|
|
|
|
int other_core = 0;
|
|
|
|
#endif
|
|
|
|
|
2018-07-17 01:39:40 -04:00
|
|
|
xTaskCreatePinnedToCore((TaskFunction_t)&task_test_tls, "task_test_tls", stack_size, &running[0],
|
|
|
|
UNITY_FREERTOS_PRIORITY, NULL, 0);
|
|
|
|
xTaskCreateStaticPinnedToCore((TaskFunction_t)&task_test_tls, "task_test_tls", stack_size, &running[1],
|
2020-12-23 19:55:10 -05:00
|
|
|
UNITY_FREERTOS_PRIORITY, s_stack, &s_task, other_core);
|
2018-02-04 17:06:45 -05:00
|
|
|
while (running[0] || running[1]) {
|
|
|
|
vTaskDelay(10);
|
|
|
|
}
|
2018-07-17 01:12:44 -04:00
|
|
|
vTaskDelay(10); /* Make sure idle task can clean up s_task, before it goes out of scope */
|
2018-02-04 17:06:45 -05:00
|
|
|
}
|