This commit is contained in:
Alexey Gerenkov 2017-08-28 18:41:32 +03:00 committed by Ivan Grokhotkov
parent bf8ff8c98b
commit 8859dab10c
4 changed files with 29 additions and 66 deletions

View File

@ -0,0 +1,16 @@
menu "PThreads"
config ESP32_PTHREAD_TASK_PRIO_DEFAULT
int "Default task priority"
range 0 255
default 5
help
Priority used to create new tasks with default pthread parameters.
config ESP32_PTHREAD_TASK_STACK_SIZE_DEFAULT
int "Default task stack size"
default 2048
help
Stack size used to create new tasks with default pthread parameters.
endmenu

View File

@ -8,31 +8,15 @@
#include "freertos/semphr.h"
#include "freertos/list.h"
#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE//CONFIG_LOG_DEFAULT_LEVEL
#define LOG_LOCAL_LEVEL CONFIG_LOG_DEFAULT_LEVEL//ESP_LOG_VERBOSE
#include "esp_log.h"
const static char *TAG = "esp_pthread";
#define ESP_PTHREAD_LOG( format, ... ) \
do { \
ets_printf(format, ##__VA_ARGS__); \
} while(0)
#define ESP_PTHREAD_LOG_LEV( _tag_, _L_, level, format, ... ) \
do { \
if (LOG_LOCAL_LEVEL >= level) { \
ESP_PTHREAD_LOG(LOG_FORMAT(_L_, format), esp_log_early_timestamp(), _tag_ , ##__VA_ARGS__); \
} \
} while(0)
#define ESP_PTHREAD_LOGE( _tag_, format, ... ) ESP_PTHREAD_LOG_LEV(_tag_, E, ESP_LOG_ERROR, format, ##__VA_ARGS__)
#define ESP_PTHREAD_LOGW( _tag_, format, ... ) ESP_PTHREAD_LOG_LEV(_tag_, W, ESP_LOG_WARN, format, ##__VA_ARGS__)
#define ESP_PTHREAD_LOGI( _tag_, format, ... ) ESP_PTHREAD_LOG_LEV(_tag_, I, ESP_LOG_INFO, format, ##__VA_ARGS__)
#define ESP_PTHREAD_LOGD( _tag_, format, ... ) ESP_PTHREAD_LOG_LEV(_tag_, D, ESP_LOG_DEBUG, format, ##__VA_ARGS__)
#define ESP_PTHREAD_LOGV( _tag_, format, ... ) ESP_PTHREAD_LOG_LEV(_tag_, V, ESP_LOG_VERBOSE, format, ##__VA_ARGS__)
#define ESP_PTHREAD_LOGO( _tag_, format, ... ) ESP_PTHREAD_LOG_LEV(_tag_, E, ESP_LOG_NONE, format, ##__VA_ARGS__)
#define PTHREAD_TASK_PRIO_DEFAULT 5
#define PTHREAD_TASK_STACK_SZ_DEFAULT (2048)//configMINIMAL_STACK_SIZE is not enough
#define ESP_PTHREAD_LOGE( _tag_, format, ... ) ESP_LOGE(_tag_, format, ##__VA_ARGS__)
#define ESP_PTHREAD_LOGW( _tag_, format, ... ) ESP_LOGW(_tag_, format, ##__VA_ARGS__)
#define ESP_PTHREAD_LOGI( _tag_, format, ... ) ESP_LOGI(_tag_, format, ##__VA_ARGS__)
#define ESP_PTHREAD_LOGD( _tag_, format, ... ) ESP_LOGD(_tag_, format, ##__VA_ARGS__)
#define ESP_PTHREAD_LOGV( _tag_, format, ... ) ESP_LOGV(_tag_, format, ##__VA_ARGS__)
#define PTHREAD_TASK_STATE_RUN 0
#define PTHREAD_TASK_STATE_EXIT 1
@ -47,7 +31,6 @@ typedef struct {
typedef struct {
void *(*func)(void *);
void *arg;
esp_pthread_t *pthread;
} esp_pthread_task_arg_t;
typedef struct {
@ -117,14 +100,14 @@ static void pthread_task_func(void *arg)
{
esp_pthread_task_arg_t *task_arg = (esp_pthread_task_arg_t *)arg;
ESP_PTHREAD_LOGV(TAG, "%s ENTER %p", __FUNCTION__, task_arg->pthread);
ESP_PTHREAD_LOGV(TAG, "%s ENTER %p", __FUNCTION__, task_arg->func);
// wait for start
xTaskNotifyWait(0, 0, NULL, portMAX_DELAY);
ESP_PTHREAD_LOGV(TAG, "%s START %p", __FUNCTION__, task_arg->pthread);
ESP_PTHREAD_LOGV(TAG, "%s START %p", __FUNCTION__, task_arg->func);
task_arg->func(task_arg->arg);
ESP_PTHREAD_LOGV(TAG, "%s END %p", __FUNCTION__, task_arg->pthread);
ESP_PTHREAD_LOGV(TAG, "%s END %p", __FUNCTION__, task_arg->func);
free(task_arg);
if (xSemaphoreTake(s_threads_mux, portMAX_DELAY) != pdTRUE) {
@ -143,7 +126,7 @@ static void pthread_task_func(void *arg)
// notify join
xTaskNotify(pthread->join_task, 0, eNoAction);
} else {
pthread->state = PTHREAD_TASK_STATE_EXIT;
pthread->state = PTHREAD_TASK_STATE_EXIT;
}
}
xSemaphoreGive(s_threads_mux);
@ -157,37 +140,12 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg)
{
TaskHandle_t xHandle = NULL;
// int priority; // Priority
// int stacksize; // Stack size
// int initial_state; // Initial state
// int cpu; // CPU affinity
// int res;
ESP_PTHREAD_LOGV(TAG, "%s", __FUNCTION__);
if (attr) {
ESP_PTHREAD_LOGE(TAG, "Attrs not supported!");
return EINVAL;
}
// if (attr) {
// stacksize = attr->stack_size;
// if (stacksize < PTHREAD_STACK_MIN) {
// errno = EINVAL;
// return EINVAL;
// }
// priority = attr->sched_priority;
// initial_state = attr->initial_state;
// cpu = attr->cpuset;
// } else {
// stacksize = CONFIG_LUA_RTOS_LUA_THREAD_STACK_SIZE;
// initial_state = PTHREAD_INITIAL_STATE_RUN;
// priority = CONFIG_LUA_RTOS_LUA_TASK_PRIORITY;
// if (portNUM_PROCESSORS > 0) {
// cpu = 0;
// } else {
// cpu = tskNO_AFFINITY;
// }
// }
esp_pthread_task_arg_t *task_arg = malloc(sizeof(esp_pthread_task_arg_t));
if (task_arg == NULL) {
ESP_PTHREAD_LOGE(TAG, "Failed to allocate task args!");
@ -205,8 +163,8 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
memset(pthread, 0, sizeof(esp_pthread_t));
task_arg->func = start_routine;
task_arg->arg = arg;
task_arg->pthread = pthread;
BaseType_t res = xTaskCreate(&pthread_task_func, "pthread", PTHREAD_TASK_STACK_SZ_DEFAULT, task_arg, PTHREAD_TASK_PRIO_DEFAULT, &xHandle);
BaseType_t res = xTaskCreate(&pthread_task_func, "pthread", CONFIG_ESP32_PTHREAD_TASK_STACK_SIZE_DEFAULT,
task_arg, CONFIG_ESP32_PTHREAD_TASK_PRIO_DEFAULT, &xHandle);
if(res != pdPASS) {
ESP_PTHREAD_LOGE(TAG, "Failed to create task!");
free(pthread);

View File

@ -1,11 +0,0 @@
#include "unity.h"
TEST_CASE("pthread C test 1", "[pthread]")
{
// int delay_ms = 50;
// const delay_test_arg_t args = { .delay_us = delay_ms * 1000, .method = 1 };
// xTaskCreatePinnedToCore(test_delay_task, "", 2048, (void*) &args, 3, NULL, 0);
// vTaskDelay(delay_ms / portTICK_PERIOD_MS + 1);
// xTaskCreatePinnedToCore(test_delay_task, "", 2048, (void*) &args, 3, NULL, 1);
// vTaskDelay(delay_ms / portTICK_PERIOD_MS + 1);
}

View File

@ -54,7 +54,7 @@ static void thread_main()
}
}
TEST_CASE("pthread CXX test 1", "[pthread]")
TEST_CASE("pthread CXX", "[pthread]")
{
global_sp.reset(new int(1));