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.
This commit is contained in:
Emmanuele Bassi
2018-01-17 16:38:45 +00:00
parent 8bbc38b497
commit 9e5a53d576
8 changed files with 361 additions and 0 deletions

View File

@@ -119,6 +119,7 @@
<xi:include href="xml/gvariant.xml"/>
<xi:include href="gvariant-varargs.xml"/>
<xi:include href="gvariant-text.xml"/>
<xi:include href="xml/refcount.xml"/>
</chapter>
<chapter id="deprecated">

View File

@@ -3449,3 +3449,18 @@ g_hostname_is_ip_address
g_uuid_string_is_valid
g_uuid_string_random
</SECTION>
<SECTION>
<FILE>refcount</FILE>
grefcount
g_ref_count_init
g_ref_count_inc
g_ref_count_dec
g_ref_count_compare
<SUBSECTION>
gatomicrefcount
g_atomic_ref_count_init
g_atomic_ref_count_inc
g_atomic_ref_count_dec
g_atomic_ref_count_compare
</SECTION>