From 55abda30baed53a25b657fc061f4af30aa1fbd0d Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 26 Jan 2021 12:50:30 +0100 Subject: libndp: use thread local variables for static return arguments libndp should be thread safe. That doesn't mean, that one "struct ndp" can be used by multiple threads without locking. But it should be reasonably possible to use the library in a multi threaded scenario. Some API functions return values that are cached in static variables. That makes these function (and the entire library) not thread safe. Fix that by using gcc's __thread specifier for thread local storage. This is also supported by clang. Currently, it's not clear whether all compiler that libndp supports, support this. I expect that to be the case. Hence, the NDP_THREAD define does not try to workaround such (yet unknown) build environments. However, if the need arises, we can easily extend the NDP_THREAD define with some conditional compilation. Signed-off-by: Thomas Haller Signed-off-by: Jiri Pirko --- libndp/libndp.c | 6 +++--- libndp/ndp_private.h | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'libndp') diff --git a/libndp/libndp.c b/libndp/libndp.c index 283de77..a859ec7 100644 --- a/libndp/libndp.c +++ b/libndp/libndp.c @@ -1615,7 +1615,7 @@ uint32_t ndp_msg_opt_mtu(struct ndp_msg *msg, int offset) NDP_EXPORT struct in6_addr *ndp_msg_opt_route_prefix(struct ndp_msg *msg, int offset) { - static struct in6_addr prefix; + static NDP_THREAD struct in6_addr prefix; struct __nd_opt_route_info *ri = ndp_msg_payload_opts_offset(msg, offset); @@ -1721,7 +1721,7 @@ NDP_EXPORT struct in6_addr *ndp_msg_opt_rdnss_addr(struct ndp_msg *msg, int offset, int addr_index) { - static struct in6_addr addr; + static NDP_THREAD struct in6_addr addr; struct __nd_opt_rdnss *rdnss = ndp_msg_payload_opts_offset(msg, offset); size_t len = rdnss->nd_opt_rdnss_len << 3; /* convert to bytes */ @@ -1771,7 +1771,7 @@ char *ndp_msg_opt_dnssl_domain(struct ndp_msg *msg, int offset, int domain_index) { int i; - static char buf[256]; + static NDP_THREAD char buf[256]; struct __nd_opt_dnssl *dnssl = ndp_msg_payload_opts_offset(msg, offset); size_t len = dnssl->nd_opt_dnssl_len << 3; /* convert to bytes */ diff --git a/libndp/ndp_private.h b/libndp/ndp_private.h index 97adc9c..a75587f 100644 --- a/libndp/ndp_private.h +++ b/libndp/ndp_private.h @@ -27,6 +27,7 @@ #include "list.h" #define NDP_EXPORT __attribute__ ((visibility("default"))) +#define NDP_THREAD __thread /** * SECTION: ndp -- cgit v1.2.1