vfs: add default initializer for vfs_eventfd

This commit is contained in:
Jiacheng Guo 2021-03-08 16:58:11 +08:00
parent 40035edcac
commit 0d986de363
4 changed files with 21 additions and 50 deletions

View File

@ -26,9 +26,13 @@ extern "C" {
#endif
typedef struct {
size_t eventfd_max_num;
size_t max_fds;
} esp_vfs_eventfd_config_t;
#define ESP_VFS_EVENTD_CONFIG_DEFAULT() (esp_vfs_eventfd_config_t) { \
.max_fds = 5, \
};
/**
* @brief Registers the event vfs.
*

View File

@ -24,9 +24,7 @@
TEST_CASE("eventfd create and close", "[vfs][eventfd]")
{
esp_vfs_eventfd_config_t config = {
.eventfd_max_num = 5,
};
esp_vfs_eventfd_config_t config = ESP_VFS_EVENTD_CONFIG_DEFAULT();
TEST_ESP_OK(esp_vfs_eventfd_register(&config));
int fd = eventfd(0, 0);
TEST_ASSERT_GREATER_OR_EQUAL(0, fd);
@ -40,9 +38,7 @@ TEST_CASE("eventfd create and close", "[vfs][eventfd]")
TEST_CASE("eventfd reject unknown flags", "[vfs][eventfd]")
{
esp_vfs_eventfd_config_t config = {
.eventfd_max_num = 5,
};
esp_vfs_eventfd_config_t config = ESP_VFS_EVENTD_CONFIG_DEFAULT();
TEST_ESP_OK(esp_vfs_eventfd_register(&config));
int fd = eventfd(0, 1);
@ -57,9 +53,7 @@ TEST_CASE("eventfd reject unknown flags", "[vfs][eventfd]")
TEST_CASE("eventfd read", "[vfs][eventfd]")
{
esp_vfs_eventfd_config_t config = {
.eventfd_max_num = 5,
};
esp_vfs_eventfd_config_t config = ESP_VFS_EVENTD_CONFIG_DEFAULT();
TEST_ESP_OK(esp_vfs_eventfd_register(&config));
unsigned int initval = 123;
@ -77,9 +71,7 @@ TEST_CASE("eventfd read", "[vfs][eventfd]")
TEST_CASE("eventfd read invalid size", "[vfs][eventfd]")
{
esp_vfs_eventfd_config_t config = {
.eventfd_max_num = 5,
};
esp_vfs_eventfd_config_t config = ESP_VFS_EVENTD_CONFIG_DEFAULT();
TEST_ESP_OK(esp_vfs_eventfd_register(&config));
int fd = eventfd(0, 0);
@ -94,9 +86,7 @@ TEST_CASE("eventfd read invalid size", "[vfs][eventfd]")
TEST_CASE("eventfd write invalid size", "[vfs][eventfd]")
{
esp_vfs_eventfd_config_t config = {
.eventfd_max_num = 5,
};
esp_vfs_eventfd_config_t config = ESP_VFS_EVENTD_CONFIG_DEFAULT();
TEST_ESP_OK(esp_vfs_eventfd_register(&config));
int fd = eventfd(0, 0);
@ -111,9 +101,7 @@ TEST_CASE("eventfd write invalid size", "[vfs][eventfd]")
TEST_CASE("eventfd write then read", "[vfs][eventfd]")
{
esp_vfs_eventfd_config_t config = {
.eventfd_max_num = 5,
};
esp_vfs_eventfd_config_t config = ESP_VFS_EVENTD_CONFIG_DEFAULT();
TEST_ESP_OK(esp_vfs_eventfd_register(&config));
int fd = eventfd(0, 0);
@ -135,9 +123,7 @@ TEST_CASE("eventfd write then read", "[vfs][eventfd]")
TEST_CASE("eventfd instant select", "[vfs][eventfd]")
{
esp_vfs_eventfd_config_t config = {
.eventfd_max_num = 5,
};
esp_vfs_eventfd_config_t config = ESP_VFS_EVENTD_CONFIG_DEFAULT();
TEST_ESP_OK(esp_vfs_eventfd_register(&config));
int fd = eventfd(0, 0);
@ -153,7 +139,6 @@ TEST_CASE("eventfd instant select", "[vfs][eventfd]")
FD_ZERO(&write_fds);
FD_ZERO(&error_fds);
FD_SET(fd, &read_fds);
printf("fd %d\n", fd);
int ret = select(fd + 1, &read_fds, &write_fds, &error_fds, &zero_time);
TEST_ASSERT_EQUAL(0, ret);
TEST_ASSERT(!FD_ISSET(fd, &read_fds));
@ -191,9 +176,7 @@ static void signal_task(void *arg)
TEST_CASE("eventfd signal from task", "[vfs][eventfd]")
{
esp_vfs_eventfd_config_t config = {
.eventfd_max_num = 5,
};
esp_vfs_eventfd_config_t config = ESP_VFS_EVENTD_CONFIG_DEFAULT();
TEST_ESP_OK(esp_vfs_eventfd_register(&config));
int fd0 = eventfd(0, 0);
@ -215,7 +198,6 @@ TEST_CASE("eventfd signal from task", "[vfs][eventfd]")
zero_time.tv_usec = 0;
int ret = select(max_fd + 1, &read_fds, NULL, NULL, &wait_time);
printf("Frist select returned\n");
TEST_ASSERT_EQUAL(1, ret);
TEST_ASSERT(FD_ISSET(fd0, &read_fds));
@ -246,9 +228,7 @@ static void IRAM_ATTR eventfd_select_test_isr(void *arg)
TEST_CASE("eventfd signal from ISR", "[vfs][eventfd]")
{
esp_vfs_eventfd_config_t config = {
.eventfd_max_num = 5,
};
esp_vfs_eventfd_config_t config = ESP_VFS_EVENTD_CONFIG_DEFAULT();
TEST_ESP_OK(esp_vfs_eventfd_register(&config));
int fd = eventfd(0, EFD_SUPPORT_ISR);
@ -299,9 +279,7 @@ static void close_task(void *arg)
TEST_CASE("eventfd select closed fd", "[vfs][eventfd]")
{
esp_vfs_eventfd_config_t config = {
.eventfd_max_num = 5,
};
esp_vfs_eventfd_config_t config = ESP_VFS_EVENTD_CONFIG_DEFAULT();
TEST_ESP_OK(esp_vfs_eventfd_register(&config));
int fd = eventfd(0, 0);
@ -350,9 +328,7 @@ static void select_task(void *arg)
TEST_CASE("eventfd multiple selects", "[vfs][eventfd]")
{
esp_vfs_eventfd_config_t config = {
.eventfd_max_num = 5,
};
esp_vfs_eventfd_config_t config = ESP_VFS_EVENTD_CONFIG_DEFAULT();
TEST_ESP_OK(esp_vfs_eventfd_register(&config));
int fd = eventfd(0, 0);
@ -380,4 +356,3 @@ TEST_CASE("eventfd multiple selects", "[vfs][eventfd]")
TEST_ASSERT_EQUAL(0, close(fd));
TEST_ESP_OK(esp_vfs_eventfd_unregister());
}

View File

@ -41,7 +41,7 @@ typedef struct event_select_args_t {
esp_vfs_select_sem_t signal_sem;
struct event_select_args_t *prev_in_fd;
struct event_select_args_t *next_in_fd;
struct event_select_args_t *next_in_args;
struct event_select_args_t *next_in_args; // a linked list for all pending select args for one select call
} event_select_args_t;
typedef struct {
@ -49,7 +49,7 @@ typedef struct {
bool support_isr;
volatile bool is_set;
volatile uint64_t value;
event_select_args_t *select_args;
event_select_args_t *select_args; // a double-linked list for all pending select args with this fd
_lock_t lock;
spinlock_t data_spin_lock; // only for event fds that support ISR.
} event_context_t;
@ -336,14 +336,14 @@ static int event_close(int fd)
esp_err_t esp_vfs_eventfd_register(const esp_vfs_eventfd_config_t *config)
{
if (config == NULL || config->eventfd_max_num >= MAX_FDS) {
if (config == NULL || config->max_fds >= MAX_FDS) {
return ESP_ERR_INVALID_ARG;
}
if (s_eventfd_vfs_id != -1) {
return ESP_ERR_INVALID_STATE;
}
s_event_size = config->eventfd_max_num;
s_event_size = config->max_fds;
s_events = (event_context_t *)calloc(s_event_size, sizeof(event_context_t));
for (size_t i = 0; i < s_event_size; i++) {
_lock_init_recursive(&s_events[i].lock);
@ -362,12 +362,6 @@ esp_err_t esp_vfs_eventfd_register(const esp_vfs_eventfd_config_t *config)
.access = NULL,
.start_select = &event_start_select,
.end_select = &event_end_select,
#ifdef CONFIG_SUPPORT_TERMIOS
.tcsetattr = NULL,
.tcgetattr = NULL,
.tcdrain = NULL,
.tcflush = NULL,
#endif // CONFIG_SUPPORT_TERMIOS
};
return esp_vfs_register_with_id(&vfs, NULL, &s_eventfd_vfs_id);
}

View File

@ -90,9 +90,7 @@ static void worker_task(void *arg)
static void collector_task(void *arg)
{
esp_vfs_eventfd_config_t config = {
.eventfd_max_num = 2,
};
esp_vfs_eventfd_config_t config = ESP_VFS_EVENTD_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_vfs_eventfd_register(&config));
s_timer_fd = eventfd(0, EFD_SUPPORT_ISR);