Commit Graph

8 Commits

Author SHA1 Message Date
Nishal Kulkarni
8cba1f4c17 grefcount: Optimise g_atomic_ref_count_dec
Currently, `g_atomic_ref_count_dec ()` does two operations.
We try to get this down to one operation.

Replace `g_atomic_int_dec_and_test ()` with `g_atomic_int_add ()`
Passing -1 as value.

Note: changes current behaviour, checks are now done after decrement.
2021-05-11 15:08:20 +05:30
Nishal Kulkarni
827b4ec855 grefcount: Optimise g_atomic_ref_count_inc
Currently, `g_atomic_ref_count_inc ()` does three operations.
We try to get this down to one operation.

Fixed by replacing `g_atomic_int_inc ()` with `g_atomic_int_add ()`
which increments by 1 and returns old value

Note: changes current behaviour, checks are now done after increment.

Closes: #1583
2021-05-10 15:45:00 +05:30
Philip Withnall
23277fbcad Merge branch 'wip/clarify-ref-count-api' into 'master'
refcount: Clarify when the ref count ends up undefined

See merge request GNOME/glib!2041
2021-04-13 13:41:19 +00:00
Dor Askayo
eb6ca282f0 grefcount: Clarify that the initial reference count is 1 2021-04-09 13:15:30 +03:00
Jonas Ådahl
21cc809158 refcount: Clarify when the ref count ends up undefined
Calling g_atomic_ref_count_dec() or g_ref_count_dec() and the reference
count reaches zero results in different side effects depending on
whether the reference count is atomic or not.

The intended side effect when this happens is undefined, i.e. one should
not rely on the reference count actually reaching zero, or staying 1, or
becoming something else, and one should treat the grefcount /
gatomicrefcount to be unusable until reinitialized.

This wasn't documented, so add a paragraph about this.
2021-04-09 11:13:36 +02:00
Tomasz Miąsko
4fde2b273d gatomicrefcount: Make g_atomic_ref_count_init non-atomic
Programmer needs to ensure that initializations happens before other
operations on gatomicrefcount as otherwise they could access
uninitialized memory, so there is no practical use case for making
initialization atomic.
2018-11-08 16:04:51 +01:00
Emmanuele Bassi
827c208cbf Use macros for refcount types API
If we're using GCC we can use __extension__ to inline the grefcount and
gatomicrefcount API, and avoid the function call.

These macros are only enabled if G_DISABLE_CHECKS is defined, as they
remove critical warnings when the reference counters achieve saturation.
2018-06-11 14:59:39 +01:00
Emmanuele Bassi
9e5a53d576 Add reference counting types
We have a common pattern for reference counting in GLib, but we always
implement it with ad hoc code. This is a good chance at trying to
standardise the implementation and make it public, so that other code
using GLib can take advantage of shared behaviour and semantics.

Instead of simply taking an integer variable, we should create type
aliases, to immediately distinguish the reference counting semantics of
the code; we can handle mixing atomic reference counting with a
non-atomic type (and vice versa) by using differently signed values for
the atomic and non-atomic cases.

The gatomicrefcount type is modelled on the Linux kernel refcount_t
type; the grefcount type is added to let single-threaded code bases to
avoid paying the price of atomic memory barriers on reference counting
operations.
2018-06-11 14:59:39 +01:00