diff options
author | Thomas Haller <thaller@redhat.com> | 2022-12-19 11:53:04 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2022-12-20 10:35:02 +0100 |
commit | 9bd833da6b67c0e9c2d90df3cfcb514d532273fd (patch) | |
tree | 60d14f35cb44559b0bfaae1752e384baddc683b5 | |
parent | 71454ae4cd14ef82ff3b9a43b6a57bd985c614a8 (diff) | |
download | NetworkManager-9bd833da6b67c0e9c2d90df3cfcb514d532273fd.tar.gz |
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')
-rw-r--r-- | src/libnm-core-impl/nm-setting.c | 12 |
1 files 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); } /** |