From 195b406ac0df36a061682656c562bf8544993fd2 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 21 Aug 2020 13:33:35 +0200 Subject: [PATCH] platform: extend nm_platform_kernel_support_get() and use atomic operations to access result Add nm_platform_kernel_support_get_full() to allow fetching the support state without setting it to the compile time default. Also, use g_atomic_int_get() to access _nm_platform_kernel_support_state values. We should not access static variables without synchronization. Better get it correct in any case than fast. --- src/platform/nm-platform.h | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index f089a72783..8c9abb631a 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -986,21 +986,31 @@ _nm_platform_kernel_support_detected (NMPlatformKernelSupportType type) nm_assert ( _NM_INT_NOT_NEGATIVE (type) && type < G_N_ELEMENTS (_nm_platform_kernel_support_state)); - return G_LIKELY (_nm_platform_kernel_support_state[type] != 0); + return G_LIKELY (g_atomic_int_get (&_nm_platform_kernel_support_state[type]) != 0); +} + +static inline NMTernary +nm_platform_kernel_support_get_full (NMPlatformKernelSupportType type, + gboolean init_if_not_set) +{ + int v; + + nm_assert ( _NM_INT_NOT_NEGATIVE (type) + && type < G_N_ELEMENTS (_nm_platform_kernel_support_state)); + + v = g_atomic_int_get (&_nm_platform_kernel_support_state[type]); + if (G_UNLIKELY (v == 0)) { + if (!init_if_not_set) + return NM_TERNARY_DEFAULT; + v = _nm_platform_kernel_support_init (type, 0); + } + return (v >= 0); } static inline gboolean nm_platform_kernel_support_get (NMPlatformKernelSupportType type) { - int v; - - nm_assert (_NM_INT_NOT_NEGATIVE (type) - && type < G_N_ELEMENTS (_nm_platform_kernel_support_state)); - - v = _nm_platform_kernel_support_state[type]; - if (G_UNLIKELY (v == 0)) - v = _nm_platform_kernel_support_init (type, 0); - return (v >= 0); + return nm_platform_kernel_support_get_full (type, TRUE) != NM_TERNARY_FALSE; } /*****************************************************************************/