-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/flockfile' into 'master'
feat(newlib): Adds flockfile/funlockfile for safe multi-line printing Closes IDF-2078 See merge request espressif/esp-idf!31009
- Loading branch information
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ set(srcs | |
"abort.c" | ||
"assert.c" | ||
"heap.c" | ||
"flockfile.c" | ||
"locks.c" | ||
"poll.c" | ||
"pthread.c" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |