Skip to content

Commit 5bcddc0

Browse files
authored
Merge pull request #19002 from benpicco/cpu/native-read_preempt
cpu/native: use async read for stdio_read()
2 parents e3e6992 + 3ce2264 commit 5bcddc0

File tree

11 files changed

+36
-26
lines changed

11 files changed

+36
-26
lines changed

cpu/native/async_read.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ void native_async_read_cleanup(void) {
5252
native_unregister_interrupt(SIGIO);
5353

5454
for (int i = 0; i < _next_index; i++) {
55-
real_close(_fds[i].fd);
55+
/* don't close stdin */
56+
if (_fds[i].fd != STDIN_FILENO) {
57+
real_close(_fds[i].fd);
58+
}
5659
if (pollers[i].child_pid) {
5760
kill(pollers[i].child_pid, SIGKILL);
5861
}

cpu/native/include/async_read.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extern "C" {
3030
* @brief Maximum number of file descriptors
3131
*/
3232
#ifndef ASYNC_READ_NUMOF
33-
# define ASYNC_READ_NUMOF 2
33+
# define ASYNC_READ_NUMOF 8
3434
#endif
3535

3636
/**

cpu/native/startup.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,6 @@ static void _reset_handler(void)
465465
__attribute__((constructor)) static void startup(int argc, char **argv, char **envp)
466466
{
467467
_native_init_syscalls();
468-
/* initialize stdio as early as possible */
469-
early_init();
470468

471469
/* Passing argc, argv, and envp to init_fini handlers is a glibc
472470
* extension. If we are not running glibc, we parse /proc/self/cmdline
@@ -730,6 +728,9 @@ __attribute__((constructor)) static void startup(int argc, char **argv, char **e
730728

731729
native_register_interrupt(SIGUSR1, _reset_handler);
732730

731+
/* initialize stdio after signal setup */
732+
early_init();
733+
733734
puts("RIOT native hardware initialization complete.\n");
734735
irq_enable();
735736
kernel_init();

cpu/native/stdio_native/stdio_native.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,40 @@
99
/**
1010
* @file
1111
* @author Martine S. Lenders <[email protected]>
12+
* @author Benjamin Valentin <[email protected]>
1213
*/
1314

15+
#include "async_read.h"
1416
#include "kernel_defines.h"
1517
#include "native_internal.h"
1618

1719
#include "stdio_base.h"
1820

19-
void stdio_init(void)
20-
{
21+
static void _async_read_wrapper(int fd, void *arg) {
22+
uint8_t buf[STDIO_RX_BUFSIZE];
23+
24+
int res = real_read(fd, &buf, sizeof(buf));
25+
if (res > 0) {
26+
isrpipe_write(arg, buf, res);
27+
}
28+
29+
native_async_read_continue(fd);
2130
}
2231

23-
ssize_t stdio_read(void* buffer, size_t max_len)
32+
static void _init(void)
2433
{
25-
return real_read(STDIN_FILENO, buffer, max_len);
34+
native_async_read_setup();
35+
if (IS_USED(MODULE_STDIN)) {
36+
native_async_read_add_int_handler(STDIN_FILENO, &stdin_isrpipe,
37+
_async_read_wrapper);
38+
}
2639
}
2740

28-
ssize_t stdio_write(const void* buffer, size_t len)
41+
static ssize_t _write(const void* buffer, size_t len)
2942
{
3043
return real_write(STDOUT_FILENO, buffer, len);
3144
}
45+
46+
STDIO_PROVIDER(STDIO_NATIVE, _init, NULL, _write)
47+
48+
/** @} */

cpu/native/syscalls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ ssize_t _native_read(int fd, void *buf, size_t count)
166166
{
167167
ssize_t r;
168168

169-
if (fd == STDIN_FILENO) {
169+
if (fd == STDIN_FILENO && IS_USED(MODULE_STDIN)) {
170170
return stdio_read(buf, count);
171171
}
172172

examples/networking/gnrc/gnrc_border_router/Makefile.native.conf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ ZEP_PORT_BASE ?= 17754
44
ZEP_PORT_MAX := $(shell expr $(ZEP_PORT_BASE) + $(ZEP_DEVICES) - 1)
55

66
CFLAGS += -DSOCKET_ZEP_MAX=$(ZEP_DEVICES)
7-
CFLAGS += -DASYNC_READ_NUMOF=$(shell expr $(ZEP_DEVICES) + 1)
87
CFLAGS += -DCONFIG_DHCPV6_CLIENT_PFX_LEASE_MAX=$(ZEP_DEVICES)
98

109
# Set CFLAGS if not being set via Kconfig

makefiles/stdio.inc.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ STDIO_MODULES = \
1818
STDIO_LEGACY_MODULES = \
1919
ethos_stdio \
2020
stdio_ethos \
21-
stdio_native # requires #19002 \
2221
#
2322

2423
# select stdio_uart if no other stdio module is slected

sys/include/net/telnet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ int telnet_server_start(void);
7272
*
7373
* @return 0 on success, error otherwise
7474
*/
75-
int telnet_server_write(const void* buffer, size_t len);
75+
ssize_t telnet_server_write(const void* buffer, size_t len);
7676

7777
/**
7878
* @brief Read data from the telnet client, will block until data is available.

sys/net/application_layer/telnet/telnet_server.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ static void *telnet_thread(void *arg)
293293
return NULL;
294294
}
295295

296-
int telnet_server_write(const void* buffer, size_t len)
296+
ssize_t telnet_server_write(const void* buffer, size_t len)
297297
{
298298
if (connected) {
299299
int res = _write_buffer(buffer, len);

tests/pkg/lua_loader/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ USEMODULE += stdin
77

88
BOARD_WHITELIST += native native64 samr21-xpro
99

10+
# currently fails with a Floating point exception - see #495
11+
TEST_ON_CI_BLACKLIST += native
12+
1013
ifeq (,$(filter native native64,$(BOARD)))
1114
# This stack size is large enough to run Lua print() functions of
1215
# various lengths. Other functions untested.

0 commit comments

Comments
 (0)