Re: [systemd-devel] [PATCH] __thread -- thread_local for C11 compat
On Sun, 15.12.13 16:19, Shawn Landden (sh...@churchofgit.com) wrote: to actually use this we would have to add this patch: diff --git a/configure.ac b/configure.ac index f85e86e..5ead127 100644 --- a/configure.ac +++ b/configure.ac @@ -113,6 +113,7 @@ AS_IF([test x$enable_address_sanitizer = xyes], [ ]) CC_CHECK_FLAGS_APPEND([with_cflags], [CFLAGS], [\ +-std=gnu11 \ -pipe \ -Wall \ -Wextra \ Hmm, why is this necessary? Lennart -- Lennart Poettering, Red Hat ___ systemd-devel mailing list systemd-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/systemd-devel
Re: [systemd-devel] [PATCH] __thread -- thread_local for C11 compat
On Sun, 15.12.13 16:24, Shawn Landden (sh...@churchofgit.com) wrote: Also make thread_local available w/o including threads.h. (as the latter hasn't been implemented, but this part is trivial) Thanks! Applied! --- src/libsystemd-bus/sd-bus.c | 4 ++-- src/libsystemd-bus/sd-event.c | 2 +- src/libsystemd-id128/sd-id128.c | 8 src/login/logind-inhibit.c | 2 +- src/shared/capability.c | 4 ++-- src/shared/cgroup-util.c| 2 +- src/shared/macro.h | 11 +++ src/shared/util.c | 6 +++--- src/shared/virt.c | 8 9 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c index 4eaceef..64cd663 100644 --- a/src/libsystemd-bus/sd-bus.c +++ b/src/libsystemd-bus/sd-bus.c @@ -2815,13 +2815,13 @@ static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus } _public_ int sd_bus_default_system(sd_bus **ret) { -static __thread sd_bus *default_system_bus = NULL; +static thread_local sd_bus *default_system_bus = NULL; return bus_default(sd_bus_open_system, default_system_bus, ret); } _public_ int sd_bus_default_user(sd_bus **ret) { -static __thread sd_bus *default_user_bus = NULL; +static thread_local sd_bus *default_user_bus = NULL; return bus_default(sd_bus_open_user, default_user_bus, ret); } diff --git a/src/libsystemd-bus/sd-event.c b/src/libsystemd-bus/sd-event.c index 06c84d7..727528b 100644 --- a/src/libsystemd-bus/sd-event.c +++ b/src/libsystemd-bus/sd-event.c @@ -2116,7 +2116,7 @@ _public_ int sd_event_get_now_monotonic(sd_event *e, uint64_t *usec) { _public_ int sd_event_default(sd_event **ret) { -static __thread sd_event *default_event = NULL; +static thread_local sd_event *default_event = NULL; sd_event *e; int r; diff --git a/src/libsystemd-id128/sd-id128.c b/src/libsystemd-id128/sd-id128.c index 07d2415..9ee40ab 100644 --- a/src/libsystemd-id128/sd-id128.c +++ b/src/libsystemd-id128/sd-id128.c @@ -104,8 +104,8 @@ static sd_id128_t make_v4_uuid(sd_id128_t id) { } _public_ int sd_id128_get_machine(sd_id128_t *ret) { -static __thread sd_id128_t saved_machine_id; -static __thread bool saved_machine_id_valid = false; +static thread_local sd_id128_t saved_machine_id; +static thread_local bool saved_machine_id_valid = false; _cleanup_close_ int fd = -1; char buf[33]; ssize_t k; @@ -153,8 +153,8 @@ _public_ int sd_id128_get_machine(sd_id128_t *ret) { } _public_ int sd_id128_get_boot(sd_id128_t *ret) { -static __thread sd_id128_t saved_boot_id; -static __thread bool saved_boot_id_valid = false; +static thread_local sd_id128_t saved_boot_id; +static thread_local bool saved_boot_id_valid = false; _cleanup_close_ int fd = -1; char buf[36]; ssize_t k; diff --git a/src/login/logind-inhibit.c b/src/login/logind-inhibit.c index ec6a722..042586d 100644 --- a/src/login/logind-inhibit.c +++ b/src/login/logind-inhibit.c @@ -413,7 +413,7 @@ bool manager_is_inhibited( } const char *inhibit_what_to_string(InhibitWhat w) { -static __thread char buffer[97]; +static thread_local char buffer[97]; char *p; if (w 0 || w = _INHIBIT_WHAT_MAX) diff --git a/src/shared/capability.c b/src/shared/capability.c index 3219520..f34f6ba 100644 --- a/src/shared/capability.c +++ b/src/shared/capability.c @@ -55,8 +55,8 @@ int have_effective_cap(int value) { } unsigned long cap_last_cap(void) { -static __thread unsigned long saved; -static __thread bool valid = false; +static thread_local unsigned long saved; +static thread_local bool valid = false; unsigned long p; if (valid) diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c index 2c2ffc5..309f65d 100644 --- a/src/shared/cgroup-util.c +++ b/src/shared/cgroup-util.c @@ -480,7 +480,7 @@ static int join_path(const char *controller, const char *path, const char *suffi int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) { const char *p; -static __thread bool good = false; +static thread_local bool good = false; assert(fs); diff --git a/src/shared/macro.h b/src/shared/macro.h index fd3762e..362d62b 100644 --- a/src/shared/macro.h +++ b/src/shared/macro.h @@ -27,6 +27,17 @@ #include sys/uio.h #include inttypes.h +#ifndef thread_local +/* don't break on glibc 2.16 that doesn't define __STDC_NO_THREADS__ + * see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53769 */ +# if __STDC_VERSION__ = 201112L !(defined(__STDC_NO_THREADS__) || \ +
Re: [systemd-devel] [PATCH] __thread -- thread_local for C11 compat
On Tue, Dec 17, 2013 at 12:46 PM, Lennart Poettering lenn...@poettering.net wrote: On Sun, 15.12.13 16:19, Shawn Landden (sh...@churchofgit.com) wrote: to actually use this we would have to add this patch: diff --git a/configure.ac b/configure.ac index f85e86e..5ead127 100644 --- a/configure.ac +++ b/configure.ac @@ -113,6 +113,7 @@ AS_IF([test x$enable_address_sanitizer = xyes], [ ]) CC_CHECK_FLAGS_APPEND([with_cflags], [CFLAGS], [\ +-std=gnu11 \ -pipe \ -Wall \ -Wextra \ Hmm, why is this necessary? It is not. Let us wait until we actually need gnu11 for something to switch to it. Lennart -- Lennart Poettering, Red Hat ___ systemd-devel mailing list systemd-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/systemd-devel ___ systemd-devel mailing list systemd-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/systemd-devel
[systemd-devel] [PATCH] __thread -- thread_local for C11 compat
also make thread_local available w/o including threads.h --- src/libsystemd-bus/sd-bus.c | 4 ++-- src/libsystemd-bus/sd-event.c | 2 +- src/libsystemd-id128/sd-id128.c | 8 src/login/logind-inhibit.c | 2 +- src/shared/capability.c | 4 ++-- src/shared/cgroup-util.c| 2 +- src/shared/macro.h | 8 src/shared/util.c | 6 +++--- src/shared/virt.c | 8 9 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c index 4eaceef..64cd663 100644 --- a/src/libsystemd-bus/sd-bus.c +++ b/src/libsystemd-bus/sd-bus.c @@ -2815,13 +2815,13 @@ static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus } _public_ int sd_bus_default_system(sd_bus **ret) { -static __thread sd_bus *default_system_bus = NULL; +static thread_local sd_bus *default_system_bus = NULL; return bus_default(sd_bus_open_system, default_system_bus, ret); } _public_ int sd_bus_default_user(sd_bus **ret) { -static __thread sd_bus *default_user_bus = NULL; +static thread_local sd_bus *default_user_bus = NULL; return bus_default(sd_bus_open_user, default_user_bus, ret); } diff --git a/src/libsystemd-bus/sd-event.c b/src/libsystemd-bus/sd-event.c index 06c84d7..727528b 100644 --- a/src/libsystemd-bus/sd-event.c +++ b/src/libsystemd-bus/sd-event.c @@ -2116,7 +2116,7 @@ _public_ int sd_event_get_now_monotonic(sd_event *e, uint64_t *usec) { _public_ int sd_event_default(sd_event **ret) { -static __thread sd_event *default_event = NULL; +static thread_local sd_event *default_event = NULL; sd_event *e; int r; diff --git a/src/libsystemd-id128/sd-id128.c b/src/libsystemd-id128/sd-id128.c index 07d2415..9ee40ab 100644 --- a/src/libsystemd-id128/sd-id128.c +++ b/src/libsystemd-id128/sd-id128.c @@ -104,8 +104,8 @@ static sd_id128_t make_v4_uuid(sd_id128_t id) { } _public_ int sd_id128_get_machine(sd_id128_t *ret) { -static __thread sd_id128_t saved_machine_id; -static __thread bool saved_machine_id_valid = false; +static thread_local sd_id128_t saved_machine_id; +static thread_local bool saved_machine_id_valid = false; _cleanup_close_ int fd = -1; char buf[33]; ssize_t k; @@ -153,8 +153,8 @@ _public_ int sd_id128_get_machine(sd_id128_t *ret) { } _public_ int sd_id128_get_boot(sd_id128_t *ret) { -static __thread sd_id128_t saved_boot_id; -static __thread bool saved_boot_id_valid = false; +static thread_local sd_id128_t saved_boot_id; +static thread_local bool saved_boot_id_valid = false; _cleanup_close_ int fd = -1; char buf[36]; ssize_t k; diff --git a/src/login/logind-inhibit.c b/src/login/logind-inhibit.c index ec6a722..042586d 100644 --- a/src/login/logind-inhibit.c +++ b/src/login/logind-inhibit.c @@ -413,7 +413,7 @@ bool manager_is_inhibited( } const char *inhibit_what_to_string(InhibitWhat w) { -static __thread char buffer[97]; +static thread_local char buffer[97]; char *p; if (w 0 || w = _INHIBIT_WHAT_MAX) diff --git a/src/shared/capability.c b/src/shared/capability.c index 3219520..f34f6ba 100644 --- a/src/shared/capability.c +++ b/src/shared/capability.c @@ -55,8 +55,8 @@ int have_effective_cap(int value) { } unsigned long cap_last_cap(void) { -static __thread unsigned long saved; -static __thread bool valid = false; +static thread_local unsigned long saved; +static thread_local bool valid = false; unsigned long p; if (valid) diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c index 2c2ffc5..309f65d 100644 --- a/src/shared/cgroup-util.c +++ b/src/shared/cgroup-util.c @@ -480,7 +480,7 @@ static int join_path(const char *controller, const char *path, const char *suffi int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) { const char *p; -static __thread bool good = false; +static thread_local bool good = false; assert(fs); diff --git a/src/shared/macro.h b/src/shared/macro.h index fd3762e..903e8ee 100644 --- a/src/shared/macro.h +++ b/src/shared/macro.h @@ -27,6 +27,14 @@ #include sys/uio.h #include inttypes.h +#if __STDC_VERSION__ = 201112L !defined(__STDC_NO_THREADS__) +#else +# define _Thread_local __thread +#endif + +/* make thread_local available w/o including threads.h */ +#define thread_local _Thread_local + #define _printf_(a,b) __attribute__ ((format (printf, a, b))) #define _alloc_(...) __attribute__ ((alloc_size(__VA_ARGS__))) #define _sentinel_ __attribute__ ((sentinel)) diff --git a/src/shared/util.c b/src/shared/util.c index 7c73074..0ce6f70 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -86,7 +86,7 @@ static volatile unsigned
[systemd-devel] [PATCH] __thread -- thread_local for C11 compat
also make thread_local available w/o including threads.h --- src/libsystemd-bus/sd-bus.c | 4 ++-- src/libsystemd-bus/sd-event.c | 2 +- src/libsystemd-id128/sd-id128.c | 8 src/login/logind-inhibit.c | 2 +- src/shared/capability.c | 4 ++-- src/shared/cgroup-util.c| 2 +- src/shared/macro.h | 10 ++ src/shared/util.c | 6 +++--- src/shared/virt.c | 8 9 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c index 4eaceef..64cd663 100644 --- a/src/libsystemd-bus/sd-bus.c +++ b/src/libsystemd-bus/sd-bus.c @@ -2815,13 +2815,13 @@ static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus } _public_ int sd_bus_default_system(sd_bus **ret) { -static __thread sd_bus *default_system_bus = NULL; +static thread_local sd_bus *default_system_bus = NULL; return bus_default(sd_bus_open_system, default_system_bus, ret); } _public_ int sd_bus_default_user(sd_bus **ret) { -static __thread sd_bus *default_user_bus = NULL; +static thread_local sd_bus *default_user_bus = NULL; return bus_default(sd_bus_open_user, default_user_bus, ret); } diff --git a/src/libsystemd-bus/sd-event.c b/src/libsystemd-bus/sd-event.c index 06c84d7..727528b 100644 --- a/src/libsystemd-bus/sd-event.c +++ b/src/libsystemd-bus/sd-event.c @@ -2116,7 +2116,7 @@ _public_ int sd_event_get_now_monotonic(sd_event *e, uint64_t *usec) { _public_ int sd_event_default(sd_event **ret) { -static __thread sd_event *default_event = NULL; +static thread_local sd_event *default_event = NULL; sd_event *e; int r; diff --git a/src/libsystemd-id128/sd-id128.c b/src/libsystemd-id128/sd-id128.c index 07d2415..9ee40ab 100644 --- a/src/libsystemd-id128/sd-id128.c +++ b/src/libsystemd-id128/sd-id128.c @@ -104,8 +104,8 @@ static sd_id128_t make_v4_uuid(sd_id128_t id) { } _public_ int sd_id128_get_machine(sd_id128_t *ret) { -static __thread sd_id128_t saved_machine_id; -static __thread bool saved_machine_id_valid = false; +static thread_local sd_id128_t saved_machine_id; +static thread_local bool saved_machine_id_valid = false; _cleanup_close_ int fd = -1; char buf[33]; ssize_t k; @@ -153,8 +153,8 @@ _public_ int sd_id128_get_machine(sd_id128_t *ret) { } _public_ int sd_id128_get_boot(sd_id128_t *ret) { -static __thread sd_id128_t saved_boot_id; -static __thread bool saved_boot_id_valid = false; +static thread_local sd_id128_t saved_boot_id; +static thread_local bool saved_boot_id_valid = false; _cleanup_close_ int fd = -1; char buf[36]; ssize_t k; diff --git a/src/login/logind-inhibit.c b/src/login/logind-inhibit.c index ec6a722..042586d 100644 --- a/src/login/logind-inhibit.c +++ b/src/login/logind-inhibit.c @@ -413,7 +413,7 @@ bool manager_is_inhibited( } const char *inhibit_what_to_string(InhibitWhat w) { -static __thread char buffer[97]; +static thread_local char buffer[97]; char *p; if (w 0 || w = _INHIBIT_WHAT_MAX) diff --git a/src/shared/capability.c b/src/shared/capability.c index 3219520..f34f6ba 100644 --- a/src/shared/capability.c +++ b/src/shared/capability.c @@ -55,8 +55,8 @@ int have_effective_cap(int value) { } unsigned long cap_last_cap(void) { -static __thread unsigned long saved; -static __thread bool valid = false; +static thread_local unsigned long saved; +static thread_local bool valid = false; unsigned long p; if (valid) diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c index 2c2ffc5..309f65d 100644 --- a/src/shared/cgroup-util.c +++ b/src/shared/cgroup-util.c @@ -480,7 +480,7 @@ static int join_path(const char *controller, const char *path, const char *suffi int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) { const char *p; -static __thread bool good = false; +static thread_local bool good = false; assert(fs); diff --git a/src/shared/macro.h b/src/shared/macro.h index fd3762e..c0597fa 100644 --- a/src/shared/macro.h +++ b/src/shared/macro.h @@ -27,6 +27,16 @@ #include sys/uio.h #include inttypes.h +#if __STDC_VERSION__ = 201112L !defined(__STDC_NO_THREADS__) +#else +# define _Thread_local __thread +#endif + +/* make thread_local available w/o including threads.h */ +#ifndef thread_local +# define thread_local _Thread_local +#endif + #define _printf_(a,b) __attribute__ ((format (printf, a, b))) #define _alloc_(...) __attribute__ ((alloc_size(__VA_ARGS__))) #define _sentinel_ __attribute__ ((sentinel)) diff --git a/src/shared/util.c b/src/shared/util.c index 7c73074..0ce6f70 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -86,7
Re: [systemd-devel] [PATCH] __thread -- thread_local for C11 compat
On Sun, Dec 15, 2013 at 1:56 PM, Lennart Poettering lenn...@poettering.net wrote: On Sun, 15.12.13 13:19, Shawn Landden (sh...@churchofgit.com) wrote: also make thread_local available w/o including threads.h Hmm, that looks a bit too early, no? Does gcc even support this? glibc gcc-4.9 supports _Thread_local http://gcc.gnu.org/gcc-4.9/changes.html switching -std=gnu99 to -std=gnu11 in Makefile I can compile systemd w/o the clause that defines _Thread_local as __thread and all tests pass. Perhaps we should add || (__GNUC__ == 4 __GNUC_MINOR__ = 9) to the check doesn't support threads.h afaics... I couldn't find anything in particular regarding C11 TLS support in gcc with google, any link you can recommend? Apparently C++11 support is somewhat more existant, but C11 support regarding threads appears to be very limited? My glibc certainly doesn't ship thread.h. Given that gcc __thread and C11 from C11 page 376: 3 The macros are thread_local which expands to _Thread_local; this part of threads.h is pretty simple thread_local appear to have different semantics regarding initialization I am also a bit afraid of just defining one to the other already at this point in time... --- src/libsystemd-bus/sd-bus.c | 4 ++-- src/libsystemd-bus/sd-event.c | 2 +- src/libsystemd-id128/sd-id128.c | 8 src/login/logind-inhibit.c | 2 +- src/shared/capability.c | 4 ++-- src/shared/cgroup-util.c| 2 +- src/shared/macro.h | 10 ++ src/shared/util.c | 6 +++--- src/shared/virt.c | 8 9 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c index 4eaceef..64cd663 100644 --- a/src/libsystemd-bus/sd-bus.c +++ b/src/libsystemd-bus/sd-bus.c @@ -2815,13 +2815,13 @@ static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus } _public_ int sd_bus_default_system(sd_bus **ret) { -static __thread sd_bus *default_system_bus = NULL; +static thread_local sd_bus *default_system_bus = NULL; return bus_default(sd_bus_open_system, default_system_bus, ret); } _public_ int sd_bus_default_user(sd_bus **ret) { -static __thread sd_bus *default_user_bus = NULL; +static thread_local sd_bus *default_user_bus = NULL; return bus_default(sd_bus_open_user, default_user_bus, ret); } diff --git a/src/libsystemd-bus/sd-event.c b/src/libsystemd-bus/sd-event.c index 06c84d7..727528b 100644 --- a/src/libsystemd-bus/sd-event.c +++ b/src/libsystemd-bus/sd-event.c @@ -2116,7 +2116,7 @@ _public_ int sd_event_get_now_monotonic(sd_event *e, uint64_t *usec) { _public_ int sd_event_default(sd_event **ret) { -static __thread sd_event *default_event = NULL; +static thread_local sd_event *default_event = NULL; sd_event *e; int r; diff --git a/src/libsystemd-id128/sd-id128.c b/src/libsystemd-id128/sd-id128.c index 07d2415..9ee40ab 100644 --- a/src/libsystemd-id128/sd-id128.c +++ b/src/libsystemd-id128/sd-id128.c @@ -104,8 +104,8 @@ static sd_id128_t make_v4_uuid(sd_id128_t id) { } _public_ int sd_id128_get_machine(sd_id128_t *ret) { -static __thread sd_id128_t saved_machine_id; -static __thread bool saved_machine_id_valid = false; +static thread_local sd_id128_t saved_machine_id; +static thread_local bool saved_machine_id_valid = false; _cleanup_close_ int fd = -1; char buf[33]; ssize_t k; @@ -153,8 +153,8 @@ _public_ int sd_id128_get_machine(sd_id128_t *ret) { } _public_ int sd_id128_get_boot(sd_id128_t *ret) { -static __thread sd_id128_t saved_boot_id; -static __thread bool saved_boot_id_valid = false; +static thread_local sd_id128_t saved_boot_id; +static thread_local bool saved_boot_id_valid = false; _cleanup_close_ int fd = -1; char buf[36]; ssize_t k; diff --git a/src/login/logind-inhibit.c b/src/login/logind-inhibit.c index ec6a722..042586d 100644 --- a/src/login/logind-inhibit.c +++ b/src/login/logind-inhibit.c @@ -413,7 +413,7 @@ bool manager_is_inhibited( } const char *inhibit_what_to_string(InhibitWhat w) { -static __thread char buffer[97]; +static thread_local char buffer[97]; char *p; if (w 0 || w = _INHIBIT_WHAT_MAX) diff --git a/src/shared/capability.c b/src/shared/capability.c index 3219520..f34f6ba 100644 --- a/src/shared/capability.c +++ b/src/shared/capability.c @@ -55,8 +55,8 @@ int have_effective_cap(int value) { } unsigned long cap_last_cap(void) { -static __thread unsigned long saved; -static __thread bool valid = false; +static thread_local unsigned long saved; +static thread_local bool valid = false; unsigned long p; if (valid) diff --git
Re: [systemd-devel] [PATCH] __thread -- thread_local for C11 compat
On Sun, 15.12.13 14:56, Shawn Landden (sh...@churchofgit.com) wrote: On Sun, Dec 15, 2013 at 1:56 PM, Lennart Poettering lenn...@poettering.net wrote: On Sun, 15.12.13 13:19, Shawn Landden (sh...@churchofgit.com) wrote: also make thread_local available w/o including threads.h Hmm, that looks a bit too early, no? Does gcc even support this? glibc gcc-4.9 supports _Thread_local http://gcc.gnu.org/gcc-4.9/changes.html Ah, nice. I figure that should be good enough then. (That said, my Fedora 20 doesn't have gcc 4.9 yet, so I can't test this...) switching -std=gnu99 to -std=gnu11 in Makefile I can compile systemd w/o the clause that defines _Thread_local as __thread and all tests pass. Perhaps we should add || (__GNUC__ == 4 __GNUC_MINOR__ = 9) to the check Hmm, checks for gcc versions are really only the last resorts. If there are feature test macros, we should use those, and should they break we an still resort to version checks. +#if __STDC_VERSION__ = 201112L !defined(__STDC_NO_THREADS__) +#else +# define _Thread_local __thread I don't really like the inverted if thing with nothing on the true branch and only a false branch I must say. Also, should we really take the deviation of filling in the c low-levelism _Thread_local here? I think I'd prefer this: #ifndef thread_local #if __STDC_VERSION__ = 201112L !defined(__STDC_NO_THREADS__) #define thread_local _Thread_local #else #define thread_local __thread #endif With that in place we stay away from defining our own low-level _Thread_local, instead we only define the high-level thread_local, and do so either to the C11 language feature or the gcc extension... If that looks OK (and is tested!) I'd be happy to merge such a patch. Lennart -- Lennart Poettering, Red Hat ___ systemd-devel mailing list systemd-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/systemd-devel
Re: [systemd-devel] [PATCH] __thread -- thread_local for C11 compat
On Sun, Dec 15, 2013 at 3:16 PM, Lennart Poettering lenn...@poettering.net wrote: On Sun, 15.12.13 14:56, Shawn Landden (sh...@churchofgit.com) wrote: On Sun, Dec 15, 2013 at 1:56 PM, Lennart Poettering lenn...@poettering.net wrote: On Sun, 15.12.13 13:19, Shawn Landden (sh...@churchofgit.com) wrote: also make thread_local available w/o including threads.h Hmm, that looks a bit too early, no? Does gcc even support this? glibc gcc-4.9 supports _Thread_local http://gcc.gnu.org/gcc-4.9/changes.html Ah, nice. I figure that should be good enough then. (That said, my Fedora 20 doesn't have gcc 4.9 yet, so I can't test this...) debian has gcc-snapshot in sid switching -std=gnu99 to -std=gnu11 in Makefile I can compile systemd w/o the clause that defines _Thread_local as __thread and all tests pass. Perhaps we should add || (__GNUC__ == 4 __GNUC_MINOR__ = 9) to the check Hmm, checks for gcc versions are really only the last resorts. If there are feature test macros, we should use those, and should they break we an still resort to version checks. +#if __STDC_VERSION__ = 201112L !defined(__STDC_NO_THREADS__) +#else +# define _Thread_local __thread I don't really like the inverted if thing with nothing on the true branch and only a false branch I must say. Also, should we really take the deviation of filling in the c low-levelism _Thread_local here? I think I'd prefer this: agreed #ifndef thread_local #if __STDC_VERSION__ = 201112L !defined(__STDC_NO_THREADS__) #define thread_local _Thread_local #else #define thread_local __thread #endif With that in place we stay away from defining our own low-level _Thread_local, instead we only define the high-level thread_local, and do so either to the C11 language feature or the gcc extension... If that looks OK (and is tested!) I'd be happy to merge such a patch. to actually use this we would have to add this patch: diff --git a/configure.ac b/configure.ac index f85e86e..5ead127 100644 --- a/configure.ac +++ b/configure.ac @@ -113,6 +113,7 @@ AS_IF([test x$enable_address_sanitizer = xyes], [ ]) CC_CHECK_FLAGS_APPEND([with_cflags], [CFLAGS], [\ +-std=gnu11 \ -pipe \ -Wall \ -Wextra \ ___ systemd-devel mailing list systemd-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/systemd-devel
[systemd-devel] [PATCH] __thread -- thread_local for C11 compat
Also make thread_local available w/o including threads.h. (as the latter hasn't been implemented, but this part is trivial) --- src/libsystemd-bus/sd-bus.c | 4 ++-- src/libsystemd-bus/sd-event.c | 2 +- src/libsystemd-id128/sd-id128.c | 8 src/login/logind-inhibit.c | 2 +- src/shared/capability.c | 4 ++-- src/shared/cgroup-util.c| 2 +- src/shared/macro.h | 11 +++ src/shared/util.c | 6 +++--- src/shared/virt.c | 8 9 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c index 4eaceef..64cd663 100644 --- a/src/libsystemd-bus/sd-bus.c +++ b/src/libsystemd-bus/sd-bus.c @@ -2815,13 +2815,13 @@ static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus } _public_ int sd_bus_default_system(sd_bus **ret) { -static __thread sd_bus *default_system_bus = NULL; +static thread_local sd_bus *default_system_bus = NULL; return bus_default(sd_bus_open_system, default_system_bus, ret); } _public_ int sd_bus_default_user(sd_bus **ret) { -static __thread sd_bus *default_user_bus = NULL; +static thread_local sd_bus *default_user_bus = NULL; return bus_default(sd_bus_open_user, default_user_bus, ret); } diff --git a/src/libsystemd-bus/sd-event.c b/src/libsystemd-bus/sd-event.c index 06c84d7..727528b 100644 --- a/src/libsystemd-bus/sd-event.c +++ b/src/libsystemd-bus/sd-event.c @@ -2116,7 +2116,7 @@ _public_ int sd_event_get_now_monotonic(sd_event *e, uint64_t *usec) { _public_ int sd_event_default(sd_event **ret) { -static __thread sd_event *default_event = NULL; +static thread_local sd_event *default_event = NULL; sd_event *e; int r; diff --git a/src/libsystemd-id128/sd-id128.c b/src/libsystemd-id128/sd-id128.c index 07d2415..9ee40ab 100644 --- a/src/libsystemd-id128/sd-id128.c +++ b/src/libsystemd-id128/sd-id128.c @@ -104,8 +104,8 @@ static sd_id128_t make_v4_uuid(sd_id128_t id) { } _public_ int sd_id128_get_machine(sd_id128_t *ret) { -static __thread sd_id128_t saved_machine_id; -static __thread bool saved_machine_id_valid = false; +static thread_local sd_id128_t saved_machine_id; +static thread_local bool saved_machine_id_valid = false; _cleanup_close_ int fd = -1; char buf[33]; ssize_t k; @@ -153,8 +153,8 @@ _public_ int sd_id128_get_machine(sd_id128_t *ret) { } _public_ int sd_id128_get_boot(sd_id128_t *ret) { -static __thread sd_id128_t saved_boot_id; -static __thread bool saved_boot_id_valid = false; +static thread_local sd_id128_t saved_boot_id; +static thread_local bool saved_boot_id_valid = false; _cleanup_close_ int fd = -1; char buf[36]; ssize_t k; diff --git a/src/login/logind-inhibit.c b/src/login/logind-inhibit.c index ec6a722..042586d 100644 --- a/src/login/logind-inhibit.c +++ b/src/login/logind-inhibit.c @@ -413,7 +413,7 @@ bool manager_is_inhibited( } const char *inhibit_what_to_string(InhibitWhat w) { -static __thread char buffer[97]; +static thread_local char buffer[97]; char *p; if (w 0 || w = _INHIBIT_WHAT_MAX) diff --git a/src/shared/capability.c b/src/shared/capability.c index 3219520..f34f6ba 100644 --- a/src/shared/capability.c +++ b/src/shared/capability.c @@ -55,8 +55,8 @@ int have_effective_cap(int value) { } unsigned long cap_last_cap(void) { -static __thread unsigned long saved; -static __thread bool valid = false; +static thread_local unsigned long saved; +static thread_local bool valid = false; unsigned long p; if (valid) diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c index 2c2ffc5..309f65d 100644 --- a/src/shared/cgroup-util.c +++ b/src/shared/cgroup-util.c @@ -480,7 +480,7 @@ static int join_path(const char *controller, const char *path, const char *suffi int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs) { const char *p; -static __thread bool good = false; +static thread_local bool good = false; assert(fs); diff --git a/src/shared/macro.h b/src/shared/macro.h index fd3762e..362d62b 100644 --- a/src/shared/macro.h +++ b/src/shared/macro.h @@ -27,6 +27,17 @@ #include sys/uio.h #include inttypes.h +#ifndef thread_local +/* don't break on glibc 2.16 that doesn't define __STDC_NO_THREADS__ + * see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53769 */ +# if __STDC_VERSION__ = 201112L !(defined(__STDC_NO_THREADS__) || \ + (defined(__GNU_LIBRARY__) __GLIBC__ == 2 __GLIBC_MINOR__ 16)) +# define thread_local _Thread_local +# else +# define thread_local __thread +# endif +#endif + #define _printf_(a,b) __attribute__ ((format