Skip to content

Commit

Permalink
sys/syscalls: add libc_gettimeofday
Browse files Browse the repository at this point in the history
Conditionally implement gettimeofday() if module is included, this
avoids including ztimer64 even when not needed
  • Loading branch information
fjmolinas committed Mar 24, 2022
1 parent feda38c commit 618c761
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 23 deletions.
1 change: 0 additions & 1 deletion cpu/native/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ config CPU_ARCH_NATIVE

# needed modules
select MODULE_PERIPH if TEST_KCONFIG
select MODULE_ZTIMER64_XTIMER_COMPAT if MODULE_ZTIMER_XTIMER_COMPAT

config CPU_CORE_NATIVE
bool
Expand Down
9 changes: 6 additions & 3 deletions cpu/native/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ ifneq (,$(filter socket_zep,$(USEMODULE)))
endif
endif

ifneq (,$(filter ztimer_xtimer_compat,$(USEMODULE)))
# requires 64bit for syscalls
USEMODULE += ztimer64_xtimer_compat
ifneq (,$(filter libc_gettimeofday,$(USEMODULE)))
USEMODULE += xtimer
ifneq (,$(filter ztimer_xtimer_compat,$(USEMODULE)))
# requires 64bit timestamps
USEMODULE += ztimer64_xtimer_compat
endif
endif

USEMODULE += periph
Expand Down
5 changes: 3 additions & 2 deletions cpu/native/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "xtimer.h"
#include "stdio_base.h"

#include "kernel_defines.h"
#include "native_internal.h"

#define ENABLE_DEBUG 0
Expand Down Expand Up @@ -474,10 +475,10 @@ int getpid(void)
return -1;
}

#ifdef MODULE_XTIMER
#if (IS_USED(MODULE_LIBC_GETTIMEOFDAY))
int _gettimeofday(struct timeval *tp, void *restrict tzp)
{
(void) tzp;
(void)tzp;
uint64_t now = xtimer_now_usec64();
tp->tv_sec = now / US_PER_SEC;
tp->tv_usec = now - tp->tv_sec;
Expand Down
4 changes: 4 additions & 0 deletions makefiles/pseudomodules.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ PSEUDOMODULES += log
PSEUDOMODULES += log_printfnoformat
PSEUDOMODULES += log_color
PSEUDOMODULES += lora
## @defgroup pseudomodule_libc_gettimeofday libc_gettimeofday
## @brief Includes implementation of gettimeofday()
##
PSEUDOMODULES += libc_gettimeofday

## @defgroup pseudomodule_mpu_stack_guard mpu_stack_guard
## @brief MPU based stack guard
Expand Down
8 changes: 8 additions & 0 deletions pkg/lua/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ FEATURES_BLACKLIST += arch_riscv
# - lua/liolib.c:671:38: error: '_IOFBF' undeclared (first use in this function)
# - lua/liolib.c:671:46: error: '_IOLBF' undeclared (first use in this function)
FEATURES_BLACKLIST += picolibc

ifneq (,$(filter newlib_syscalls_default,$(USEMODULE)))
USEMODULE += libc_gettimeofday
endif

ifneq (,$(filter native,$(CPU)))
USEMODULE += libc_gettimeofday
endif
8 changes: 8 additions & 0 deletions pkg/tinydtls/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ ifneq (,$(filter sock_dtls,$(USEMODULE)))
USEMODULE += tinydtls_sock_dtls
USEMODULE += ztimer_usec
endif

ifneq (,$(filter newlib_syscalls_default,$(USEMODULE)))
USEMODULE += libc_gettimeofday
endif

ifneq (,$(filter native,$(CPU)))
USEMODULE += libc_gettimeofday
endif
4 changes: 4 additions & 0 deletions pkg/wolfssl/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,9 @@ ifneq (,$(filter wolfcrypt_random,$(USEMODULE)))
USEMODULE += random
endif

ifneq (,$(filter newlib_syscalls_default,$(USEMODULE)))
USEMODULE += libc_gettimeofday
endif

# wolfssl is only supported by 32 bit architectures
FEATURES_REQUIRED += arch_32bit
8 changes: 7 additions & 1 deletion sys/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ config MODULE_NEWLIB
config MODULE_PICOLIBC
bool "Picolibc"
depends on HAS_PICOLIBC

endchoice

config MODULE_LIBC_GETTIMEOFDAY
bool
select MODULE_XTIMER
select MODULE_ZTIMER64_XTIMER_COMPAT if MODULE_ZTIMER_XTIMER_COMPAT
help
Support for gettimeofday()

rsource "Kconfig.newlib"
rsource "Kconfig.picolibc"

Expand Down
9 changes: 8 additions & 1 deletion sys/Kconfig.newlib
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ config MODULE_NEWLIB_SYSCALLS_DEFAULT
default y
depends on !HAVE_CUSTOM_NEWLIB_SYSCALLS
select MODULE_DIV
select MODULE_ZTIMER64_XTIMER_COMPAT if MODULE_ZTIMER_XTIMER_COMPAT
help
Default implementation of newlib system calls.

config MODULE_LIBC_GETTIMEOFDAY
bool
select MODULE_NEWLIB_SYSCALLS_DEFAULT if MODULE_NEWLIB
select MODULE_XTIMER
select MODULE_ZTIMER64_XTIMER_COMPAT if MODULE_ZTIMER_XTIMER_COMPAT
help
Support for gettimeofday()

endif # MODULE_NEWLIB

config HAVE_CUSTOM_NEWLIB_SYSCALLS
Expand Down
13 changes: 9 additions & 4 deletions sys/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,18 @@ ifneq (,$(filter newlib,$(USEMODULE)))
endif
ifneq (,$(filter newlib_syscalls_default,$(USEMODULE)))
USEMODULE += div
ifneq (,$(filter ztimer_xtimer_compat,$(USEMODULE)))
# requires 64bit timestamps when using xtimer
USEMODULE += ztimer64_xtimer_compat
ifneq (,$(filter libc_gettimeofday,$(USEMODULE)))
USEMODULE += xtimer
ifneq (,$(filter ztimer_xtimer_compat,$(USEMODULE)))
# requires 64bit timestamps
USEMODULE += ztimer64_xtimer_compat
endif
endif
endif
endif
endif



ifneq (,$(filter posix_select,$(USEMODULE)))
ifneq (,$(filter posix_sockets,$(USEMODULE)))
USEMODULE += sock_async
Expand Down
14 changes: 3 additions & 11 deletions sys/newlib_syscalls_default/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -630,24 +630,16 @@ int _kill(pid_t pid, int sig)
return -1;
}

#ifdef MODULE_XTIMER
#if (IS_USED(MODULE_LIBC_GETTIMEOFDAY))
int _gettimeofday_r(struct _reent *r, struct timeval *restrict tp, void *restrict tzp)
{
(void) r;
(void) tzp;
(void)tzp;
(void)r;
uint64_t now = xtimer_now_usec64();
tp->tv_sec = div_u64_by_1000000(now);
tp->tv_usec = now - (tp->tv_sec * US_PER_SEC);
return 0;
}
#else
int _gettimeofday_r(struct _reent *r, struct timeval *restrict tp, void *restrict tzp)
{
(void) tp;
(void) tzp;
r->_errno = ENOSYS;
return -1;
}
#endif

/**
Expand Down
1 change: 1 addition & 0 deletions tests/cpp11_mutex/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ include ../Makefile.tests_common

USEMODULE += cpp11-compat
USEMODULE += xtimer
USEMODULE += libc_gettimeofday

include $(RIOTBASE)/Makefile.include

0 comments on commit 618c761

Please sign in to comment.