Skip to content

Commit

Permalink
feat(newlib): Adds flockfile/funlockfile for safe multi-line printing
Browse files Browse the repository at this point in the history
for printf, vprintf, etc.

Closes #2565
  • Loading branch information
KonstantinKondrashov committed May 24, 2024
1 parent 60532e7 commit b0457ee
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions components/newlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(srcs
"abort.c"
"assert.c"
"heap.c"
"flockfile.c"
"locks.c"
"poll.c"
"pthread.c"
Expand Down
32 changes: 32 additions & 0 deletions components/newlib/flockfile.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdio.h>

#include <sys/lock.h>
#include <sys/reent.h>

void flockfile(FILE *fp)
{
if (fp && !(fp->_flags & __SSTR)) {
__lock_acquire_recursive(fp->_lock);
}
}

int ftrylockfile(FILE *fp)
{
if (fp && !(fp->_flags & __SSTR)) {
return __lock_try_acquire_recursive(fp->_lock);
}
return 0;
}

void funlockfile(FILE *fp)
{
if (fp && !(fp->_flags & __SSTR)) {
__lock_release_recursive(fp->_lock);
}
}
1 change: 1 addition & 0 deletions components/newlib/test_apps/newlib/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ idf_component_register(SRCS
"test_locks.c"
"test_misc.c"
"test_newlib.c"
"test_printf.c"
"test_setjmp.c"
"test_stdatomic.c"
"test_time.c"
Expand Down
60 changes: 60 additions & 0 deletions components/newlib/test_apps/newlib/main/test_printf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "unity.h"
#include "esp_log.h"
#include "sdkconfig.h"

#if !CONFIG_FREERTOS_UNICORE
static int my_printf(const char *fmt, ...)
{
va_list list;
va_start(list, fmt);
int result = vprintf(fmt, list);
va_end(list);
return result;
}

static void test_task(void* arg)
{
for (unsigned i = 0; i < 5; i++) {
printf("00000000%d\n", i);
}
SemaphoreHandle_t * p_done = (SemaphoreHandle_t *) arg;
xSemaphoreGive(*p_done);
vTaskDelay(1);
vTaskDelete(NULL);
}

TEST_CASE("Test flockfile/funlockfile", "[newlib]")
{
SemaphoreHandle_t done = xSemaphoreCreateBinary();
flockfile(stdout);
{
xTaskCreatePinnedToCore(&test_task, "test_task", 4096, &done, 5, NULL, 1);
// make sure test_task is already running and is actually blocked by flockfile
TEST_ASSERT_FALSE(xSemaphoreTake(done, 100 / portTICK_PERIOD_MS));
for (unsigned i = 0; i < 5; i++) {
my_printf(LOG_ANSI_COLOR_BOLD_BACKGROUND(LOG_ANSI_COLOR_BLACK, LOG_ANSI_COLOR_BG_RED) "I ");
my_printf("(%d) ", 737);
my_printf("[%s]: ", "TAG");
my_printf("%s %d ", "message ", i);
my_printf("%s\n", LOG_ANSI_COLOR_RESET);
}
// test_task is still blocked by flockfile
TEST_ASSERT_FALSE(xSemaphoreTake(done, 100 / portTICK_PERIOD_MS));
}
funlockfile(stdout);
// after funlockfile, test_task can print a msg and complete test_task
TEST_ASSERT_TRUE(xSemaphoreTake(done, portMAX_DELAY));
vSemaphoreDelete(done);
}
#endif // !CONFIG_FREERTOS_UNICORE

0 comments on commit b0457ee

Please sign in to comment.