esp-idf/components/freertos/esp_additions/FreeRTOSSimulator_wrappers.c
Darian Leung 66499f17a5 freertos: Refactor component structure
This commit refactors the "freertos" component's structure as follows:

- "FreeRTOSConfig.h" related files moved to "./config" directory
- Refactored CMakeLists.txt file in preparation for v10.5.1 upgrade
    - Grouped list appends based on component organization
    - Removed some unecessarily public "include_dirs"
- Removed FreeRTOS-openocd.c
    - uxTopUsedPriority has been added back to tasks.c since v10.4.2
    - Thus the workaround in FreeRTOS-openocd.c is no longer needed and can
      be removed.
2023-05-08 18:40:03 +08:00

52 lines
1.7 KiB
C

/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <pthread.h>
#include "esp_err.h"
#include "errno.h"
/** This module addresses the FreeRTOS simulator's coexistence with linux system calls from user apps.
* It's only included when building without lwIP, so we need to use linux system's select() which would receive
* EINTR event on every FreeRTOS interrupt; we workaround this problem by wrapping select()
* to bypass and silence these events.
*/
extern int __real_select (int fd, fd_set * rfds, fd_set * wfds, fd_set *efds, struct timeval *tval);
static inline int64_t get_us(void)
{
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
return spec.tv_nsec / 1000 + spec.tv_sec * 1000000;
}
int __wrap_select (int fd, fd_set * rfds, fd_set * wfds, fd_set *efds, struct timeval *tval)
{
int ret;
struct timeval *tv = tval;
int64_t start = 0;
int64_t timeout_us = 0;
if (tv != NULL) {
start = get_us();
timeout_us = tval->tv_sec * 1000000 + tval->tv_usec;
struct timeval timeval_local = { .tv_sec = tval->tv_sec, .tv_usec = tval->tv_usec };
tv = &timeval_local; // this (tv != NULL) indicates that we should handle timeouts
}
while ((ret = __real_select(fd, rfds, wfds, efds, tv)) < 0 && errno == EINTR) {
if (tv != NULL) {
int64_t now = get_us();
timeout_us -= now - start;
if (timeout_us < 0) {
errno = 0;
ret = 0;
break;
}
start = now;
tv->tv_usec = timeout_us % 1000000;
tv->tv_sec = timeout_us / 1000000;
}
}
return ret;
}