diff options
author | Thomas Haller <thaller@redhat.com> | 2021-01-26 12:50:30 +0100 |
---|---|---|
committer | Jiri Pirko <jiri@nvidia.com> | 2021-01-27 09:10:20 +0100 |
commit | 55abda30baed53a25b657fc061f4af30aa1fbd0d (patch) | |
tree | aaf6810dda7e8221cf008e410aaa524543b9c199 | |
parent | 778d7b3cb4874e176332c2acdf02c2b172f9fa96 (diff) | |
download | libndp-55abda30baed53a25b657fc061f4af30aa1fbd0d.tar.gz |
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 <thaller@redhat.com>
Signed-off-by: Jiri Pirko <jiri@nvidia.com>
-rw-r--r-- | libndp/libndp.c | 6 | ||||
-rw-r--r-- | libndp/ndp_private.h | 1 |
2 files changed, 4 insertions, 3 deletions
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 |