From 9bd833da6b67c0e9c2d90df3cfcb514d532273fd Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 19 Dec 2022 11:53:04 +0100 Subject: [PATCH] libnm: make NMRange ref/unref thread-safe Like for our other immutable/sealable types, make ref/unref thread safe. That is important, as the boxed types only increase the ref-count on copy. If ref/unref is not thread-safe, it means you cannot copy a boxed type, and operate on the copy on another thread. Fixes: 041e38b1514b ('libnm: add NMRange') --- src/libnm-core-impl/nm-setting.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libnm-core-impl/nm-setting.c b/src/libnm-core-impl/nm-setting.c index 70dbcf944b..5a46a1b439 100644 --- a/src/libnm-core-impl/nm-setting.c +++ b/src/libnm-core-impl/nm-setting.c @@ -4167,6 +4167,7 @@ nm_range_new(guint64 start, guint64 end) * @range: the #NMRange * * Increases the reference count of the object. + * This is thread-safe. * * Returns: the input argument @range object. * @@ -4177,9 +4178,9 @@ nm_range_ref(const NMRange *range) { g_return_val_if_fail(NM_IS_RANGE(range), NULL); - nm_assert(range->refcount < G_MAXUINT); + nm_assert(range->refcount < G_MAXINT); - ((NMRange *) range)->refcount++; + g_atomic_int_inc(&((NMRange *) range)->refcount); return (NMRange *) range; } @@ -4189,6 +4190,7 @@ nm_range_ref(const NMRange *range) * * Decreases the reference count of the object. If the reference count * reaches zero the object will be destroyed. + * This is thread-safe. * * Since: 1.42 **/ @@ -4197,10 +4199,8 @@ nm_range_unref(const NMRange *range) { g_return_if_fail(NM_IS_RANGE(range)); - nm_assert(range->refcount != 0); - - if (--((NMRange *) range)->refcount == 0) - g_slice_free(NMRange, (NMRange *) range); + if (g_atomic_int_dec_and_test(&((NMRange *) range)->refcount)) + nm_g_slice_free((NMRange *) range); } /**