From 14461e7217cc51dc4dd4c0f9ea97b93ccc5d1e5f Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 19 Feb 2020 15:28:33 +0100 Subject: [PATCH] shared: add NM_MORE_ASSERT_ONCE() macro --- shared/nm-glib-aux/nm-macros-internal.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/shared/nm-glib-aux/nm-macros-internal.h b/shared/nm-glib-aux/nm-macros-internal.h index 52591040e2..3cb11a973f 100644 --- a/shared/nm-glib-aux/nm-macros-internal.h +++ b/shared/nm-glib-aux/nm-macros-internal.h @@ -1023,6 +1023,24 @@ nm_str_realloc (char *str) #define nm_assert_not_reached() G_STMT_START { ; } G_STMT_END #endif +/* Usage: + * + * if (NM_MORE_ASSERT_ONCE (5)) { extra_check (); } + * + * This will only run the check once, and only if NM_MORE_ASSERT is >= than + * more_assert_level. + */ +#define NM_MORE_ASSERT_ONCE(more_assert_level) \ + ( (NM_MORE_ASSERTS >= (more_assert_level)) \ + && ({ \ + static volatile int _assert_once = 0; \ + \ + G_STATIC_ASSERT_EXPR ((more_assert_level) >= 0); \ + \ + G_UNLIKELY ( _assert_once == 0 \ + && g_atomic_int_compare_and_exchange (&_assert_once, 0, 1)); \ + })) + /*****************************************************************************/ #define NM_GOBJECT_PROPERTIES_DEFINE_BASE(...) \