Commit Graph

29390 Commits

Author SHA1 Message Date
Thomas Haller
fe0347bc64 gobject: fix race clearing weak locations for resurrection during g_object_unref()
dispose() can resurrect an object and/or register a weak-ref. After
returning from dispose(), we must check again. And we must do so in a
race-free manner, where we check that we have no more weak-locations
and the ref-count is one.

In fact, if _object_unref_clear_weak_locations() determines that the
ref-count is 1, it must also decrement the ref-count to zero while
holding the weak_locations_lock. This prevents g_weak_ref_set() to
still register a weak-pointer after the reference count dropped to zero.

Also add an assertion to g_weak_ref_set(), that the object is still
alive. The assertion is useful to finding bugs, but the change really
makes it impossible that a wrongly used g_weak_ref_set() can still
resurrect the object after finalization starts.

The final

    g_datalist_id_set_data (&object->qdata, quark_weak_locations, NULL);

during finalization is no longer necessary and dropped.
2023-12-28 09:36:56 +01:00
Thomas Haller
aaf4ccb901 gobject: avoid calling g_weak_ref_set() when intializing GWeakRef to NULL
If object is NULL, g_weak_ref_set() really does nothing. However,
it will pointlessly take a lock, before deciding to do nothing.
Avoid that.
2023-12-28 09:36:56 +01:00
Thomas Haller
de5e26e2ed gobject: remove unused quark_weak_refs from gobject
We never set any data for quark_weak_refs. It's unused, drop it.

Also, fail a g_critical() assertion, if the GWeakRef is unexpectedly not
registered in the object. That really shouldn't happen.
2023-12-28 09:36:56 +01:00
Thomas Haller
9ae43169cf gobject: fix race in toggle ref during g_object_ref()
Previously:

   1. old_val = atomic_add(&object->ref_count);
   2. if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object)) { toggle_notify() }

As old_val was 1, you might think that no other thread can have a valid
reference to object. However, that's not the case. For one, GWeakRef can
be used to create another strong reference. More easily, the single
reference can be shared between multiple threads (as long as the code
takes care that the object lives long enough).

That means, another thread can easily add and drop references (including
toggle references). All between step 1 and 2.

A race here might be hard to hit, and the effect might not be obviously
bad. However, consider old_val is 1 due to a normal reference, and
another thread adds a toggle ref between step 1. and 2. Then we would
notify a toggle from 1->2, although a newly added toggle ref is expected
to always start with a normal and a toggle reference. The first toggle
notification is expected to notify about the loss of other references, not
about getting a second reference.

To handle this properly, when we increase the reference count from 1 to
2, we must do so under a lock and check for the toggle notification.

As we now correctly track the toggle behavior, we can also assert in
toggle_refs_get_notify_unlocked() that n_toggle_refs agrees with the
number of references, that is, that the user did always match
g_object_add_toggle_ref() with g_object_remove_toggle_ref().

The downside is here too, that there is now a case (when increasing the
reference count from 1 to 2)  where we need to take the global lock.
That performance problem should be addresses by using per-object locks
instead of a global lock.
2023-12-28 09:36:56 +01:00
Thomas Haller
408dc69186 gobject: fix race in g_object_unref() related to toggle references
The previous g_object_unref() was racy. There were three places where we
decremented the ref count, but still accessed the object afterwards
(while assuming that somebody else might still hold a reference). For
example:

      if (!g_atomic_int_compare_and_exchange_full ((int *) &object->ref_count,
                                                   old_ref, old_ref - 1,
                                                   &old_ref))
        continue;

      TRACE (GOBJECT_OBJECT_UNREF (object, G_TYPE_FROM_INSTANCE (object), old_ref));

      /* if we went from 2->1 we need to notify toggle refs if any */
      if (old_ref == 2 && OBJECT_HAS_TOGGLE_REF (object))
        {
          /* The last ref being held in this case is owned by the toggle_ref */
          toggle_refs_notify (object, TRUE);
        }

After we decrement the reference count (and gave up our reference), we
are only allowed to access object if we know we have the only possible
reference to it. In particular, if old_ref is larger than 1, then
somebody else holds references and races against destroying object.
The object might be a dangling pointer already.

This is slightly complicated due to toggle references and clearing of
weak-locations.

For toggle references, we must take a lock on the mutex. Luckily, that
is only necessary, when the current reference count is exactly 2.

Note that we emit the TRACE() after the ref count was already decreased.
If another thread unrefs the object, inside the TRACE() we might have a
dangling pointer. That would only be fixable, by emitting the TRACE()
before the actual unref (which has its own problems). This problem
already existed previously.

The change to the test is necessary and correct. Before this patch,
g_object_unref() would call dispose() and decrement the reference count
right after.
In the test case at gobject/tests/reference.c:1108, the reference count
after dispose and decrement is 1. Then it thaws the queue notification,
which emits a property changed signal. The test then proceeds to
reference the object again and notifying the toggle reference.
Previously, the toggle reference was notified 3 times.
After this change, the property changed signal is emitted before
decreasing the reference count. Taking a reference then does not cause
an additional toggle on+off, so in total only one toggle happens.
That accounts for the change in the test. The new behavior is
correct.
2023-12-28 09:36:56 +01:00
Thomas Haller
7292726931 gobject: reformat g_object_ref()/g_object_unref() with clang-format
The indentation level in g_object_unref() is wrong. Fix it by reformatting
the function with clang-format. That makes follow up patches easier to adhere
a consistent style.

No other changes.
2023-12-28 09:36:56 +01:00
Thomas Haller
b2a68d6ec4 gobject: factor out toggle_refs_get_notify_unlocked() function
This is the part of toggle_refs_notify(), for which we usually hold the
lock. It will be used next.
2023-12-28 09:32:49 +01:00
Thomas Haller
2276099236 gobject: remove data when last toggle ref is removed
Toggle refs are seldom used, and when they are, it makes mostly sense that
there is only one of them. Thus, when removing the last toggle ref, also
remove the associated data.
2023-12-28 08:39:41 +01:00
Thomas Haller
4da4220c8b gobject: remove unnecessary "object" field from ToggleRefStack
We always have at hand the object pointer that we care about.
Tracking it also in ToggleRefStack is redundant and unnecessary.

Drop the field.
2023-12-28 08:39:41 +01:00
Emmanuele Bassi
eddd04add1 Merge branch 'ebassi/fix-gir-location-for-docs' into 'main'
build: Tell gi-docgen where to find the GIR files

See merge request GNOME/glib!3792
2023-12-27 16:25:54 +00:00
Emmanuele Bassi
0db7395ef9 build: Tell gi-docgen where to find the GIR files
Now that all files are located under top_srcdir/introspection, we need
to ensure that gi-docgen can find them.
2023-12-27 16:00:06 +00:00
Michael Catanzaro
686ab492cf Merge branch 'g_get_num_processors-affinity-fix' into 'main'
Account for cpu affinity in g_get_num_processors

Closes #1010

See merge request GNOME/glib!3784
2023-12-22 16:11:46 +00:00
Kryggird
09de26185e Account for cpu affinity in g_get_num_processors 2023-12-22 16:11:45 +00:00
Philip Withnall
e597b189c3
2.79.0
Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2023-12-22 15:37:24 +00:00
Philip Withnall
994bf21d9d gdatetime: Fix title of documentation comment
Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2023-12-22 14:48:30 +00:00
Philip Withnall
ec3fb9a48c Merge branch 'glib-gir-sources' into 'main'
gobject: Make GLib-2.0 gir build depend on GObject dependency

See merge request GNOME/glib!3772
2023-12-22 14:45:37 +00:00
Philip Withnall
b546299fb9 Merge branch '3155-girepository-version' into 'main'
girepository: Re-number GIR file from 2.0 to 3.0

See merge request GNOME/glib!3786
2023-12-22 12:43:06 +00:00
Philip Withnall
a35c6f764d girepository: Re-number GIR file from 2.0 to 3.0
The library shipped by gobject-introspection.git was
`libgirepository-1.0.so`, but for some reason (accident?), it was
accompanied by `GIRepository-2.0.gir`. That’s been the case for the last
6 years.

In moving libgirepository to glib.git, we’ve bumped the version to
`libgirepository-2.0.so`, and have changed the API.

In order to avoid a collision between the new `GIRepository-2.0.gir` and
the old `GIRepository-2.0.gir`, we can either:
 * Rename the basename of the library (confusing).
 * Re-version the whole thing to 3.0 (would mean it’s completely out of
   sync with the rest of glib.git, and would lead to build system
   misery).
 * Re-version only the GIR file (a bit confusing, but hopefully less
   confusing).

So I’ve done the final option: glib.git now ships
`libgirepository-2.0.so` and `GIRepository-3.0.gir`. This avoids
collisions with what’s shipped by gobject-introspection.git, while
hopefully still making some sense.

We considered using version number 2.1 rather than 3.0, but decided
against it because that makes it look like it’s compatible with version
2.0, which it isn’t.

Note that none of these changes touch the
`${prefix}/lib/girepository-1.0` and `${prefix}/share/gir-1.0`
directories. The version numbers in those refer to the versions of the
GIR and typelib file formats, which have not changed.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3155
2023-12-22 12:21:57 +00:00
Philip Withnall
2473268574 Merge branch 'gtypemodule-fix-mem-leaks' into 'main'
gtypemodule: Add assertions in finalize()

See merge request GNOME/glib!3779
2023-12-22 12:15:54 +00:00
Philip Withnall
d55d40a603 Merge branch 'mcatanzaro/#3203' into 'main'
gapplication: ensure app ID is set when sending notification

Closes #3203

See merge request GNOME/glib!3785
2023-12-21 19:50:16 +00:00
Emmanuele Bassi
cda15952dd Merge branch '3037-install-docs' into 'main'
docs: Install the gi-docgen docs

See merge request GNOME/glib!3787
2023-12-21 18:23:53 +00:00
Gaël Bonithon
55e2d9f6a7 gtypemodule: Add assertions in finalize() 2023-12-21 18:25:36 +01:00
Philip Withnall
4c23a17752 Merge branch '3037-man-rst2man' into 'main'
docs: Port the man pages from DocBook to reStructuredText

Closes #3037

See merge request GNOME/glib!3775
2023-12-21 17:04:33 +00:00
Philip Withnall
d2dbfba208 docs: Install the gi-docgen docs
They are now installed to (e.g.)
`${prefix}/share/doc/glib-2.0/{glib,gmodule,gobject,gio}/index.html`.

We might want to drop one level of nesting out of that, but for the
moment I thought I’d keep it in so we can disambiguate by installed
major version.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3037
2023-12-21 17:00:06 +00:00
Michael Catanzaro
c2fab14030 gapplication: ensure app ID is set when sending notification
Otherwise, we hit a bunch of criticals later on. Also, document this
requirement to ensure there is no ambiguity.

Fixes #3203
2023-12-21 10:44:25 -06:00
Philip Withnall
020ebe42f9 build: Change default for -Dman-pages from disabled to auto
Previously, `-Dman=false` was the default, because the generated man
pages were shipped in the distribution tarball already, so the option
actually mostly controlled whether to *re*build them.

The generated pages are no longer shipped in the tarball (and probably
haven’t been since the port to Meson, though I haven’t checked), so it
makes sense to change the default to encourage building the man pages if
the right tooling (`rst2man`) is available.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2023-12-21 16:13:03 +00:00
Marco Trevisan
419777b0f1 Merge branch 'gir-use-gstrv' into 'main'
girepository: Return enumerated versions and search paths as a GStrv

See merge request GNOME/glib!3773
2023-12-21 14:37:07 +00:00
Philip Withnall
031e65808d docs: Port the man pages from DocBook to reStructuredText
So they are consistent with the way we’re building man pages in other
projects, and because some people are allergic to XML.

This changes the build-time dependencies from `xsltproc` to `rst2man`,
and also takes the opportunity to change the `-Dman` Meson option from a
boolean to a feature (so you should use `-Dman-pages={enabled,disabled}`
now, rather than `-Dman={true,false}`).

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3037
2023-12-21 13:34:48 +00:00
Philip Withnall
72dfdd3a6e Merge branch 'th/notify-queue' into 'main'
[th/notify-queue] some optimization around g_object_freeze_notify()/g_object_thaw_notify()

See merge request GNOME/glib!3762
2023-12-21 13:30:40 +00:00
Marco Trevisan (Treviño)
8e26e7aee2 girepository: Clarify docstring about which kind of strings are formatted 2023-12-21 14:14:27 +01:00
Marco Trevisan (Treviño)
3ec0192970 girepository: Use newer API to create null-terminated ptr array for dependencies
And add a test for this.
2023-12-21 14:14:27 +01:00
Marco Trevisan (Treviño)
2c00c7c924 girepository: Use an array to iterate over and return the search paths
We used to store the search paths into a GSList but this is not
efficient for various reasons, so replace this with an array so that we
can replace return just a GStrv in the public API.
2023-12-21 14:14:27 +01:00
Philip Withnall
833d3fb6cf Merge branch 'file-uri-cleanup' into 'main'
glocalvfs: Remove unnecessary and buggy code

See merge request GNOME/glib!3776
2023-12-21 12:42:27 +00:00
Philip Withnall
385641572d Merge branch '3037-docs-ci' into 'main'
ci: Build docs artifacts for deployment to docs.gtk.org

See merge request GNOME/glib!3768
2023-12-21 12:32:34 +00:00
Michael Catanzaro
af6bf99495 Merge branch 'meson-size_t-check' into 'main'
Fix detecting size_t size when `-Wmissing-prototypes` is in CFLAGS

See merge request GNOME/glib!3777
2023-12-21 12:20:21 +00:00
Philip Withnall
a7de4e3786 Merge branch 'typos' into 'main'
docs: fix a typo

See merge request GNOME/glib!3782
2023-12-21 12:18:48 +00:00
Yegor Yefremov
b969e8fd58 docs: fix a typo
Fix a typo found with codespell.

Signed-off-by: Yegor Yefremov <yegorslists@googlemail.com>
2023-12-21 07:45:04 +01:00
Thomas Haller
42bd9627a5 gobject: use g_malloc() allocator for GObjectNotifyQueue struct
The GSlice allocator is deprecated. Use g_new0() instead.
2023-12-21 07:26:50 +01:00
Thomas Haller
630d8da211 gobject: remove unused "conditional" argument from g_object_notify_queue_freeze() 2023-12-21 07:26:50 +01:00
Thomas Haller
94c735a2aa gobject: don't freeze to queue notify event in g_object_notify_by_spec_internal()
Previously:

- if the object is currently not frozen, we called
  g_object_notify_queue_freeze() once. Afterwards dispatch the event
  directly. This is probably the common case, and requires one
  notify_lock lock.

- if the object is currently frozen, we call
  g_object_notify_queue_freeze(), g_object_notify_queue_add().
  g_object_notify_queue_thaw().
  This required taking the notify_lock three times.

- if the object is currently not frozen and in_init, then we called
  g_object_notify_queue_freeze(), g_object_notify_queue_freeze(),
  g_object_notify_queue_add(). This also required to take
  the lock three times. There is another thaw at the end of
  object initialization.

That was because we first call g_object_notify_queue_freeze() to see
whether we are frozen. And depending on that, queue the event (and thaw
again).

Instead, g_object_notify_queue_add() can do the check and queueing in
one step. There is no need to call a freeze() to (conditionally) enqueue
a notification. Now only one lock is taken in all cases.

Also, g_object_notify_queue_freeze() and g_object_notify_queue_thaw()
both call g_datalist_id_get_data() (which also take a bit lock). As the
thaw is no longer necessary, the second lock is also saved.
2023-12-21 07:26:50 +01:00
Thomas Haller
a2b467624b gobject: add g_object_notify_queue_create_queue_frozen() helper
This will be used next, and is a separate commit to do a trivial thing
first.
2023-12-21 07:26:50 +01:00
Thomas Haller
9bee14ef3e gobject: avoid taking reference during g_object_thaw_notify()
Before dispatching signals (and calling out to user code), we want to
take a reference and ensure that the object stays alive.

However, a thaw may not decrease the freeze_count to zero, or there may
be no properties to notify. Avoid taking a reference in those cases.
2023-12-21 07:26:50 +01:00
Thomas Haller
95c2150d61 gobject: avoid additional freeze+thaw in g_object_thaw_notify()
It's easy to avoid.
2023-12-21 07:26:50 +01:00
Thomas Haller
f2fafdfe06 gobject: drop ref/unref from g_object_freeze_notify()
This was done since the beginning (commit e773d7dba6 ('fixed dealing
with collection/lcopy of NULL values.'). But it's not clear, why we
would need to take a reference on the calling object.

Freeze does not emit any signals/callbacks and does not call back to the
user. It just sets up some internal state.

This doesn't require to take a reference. The caller must hold a valid
reference to being with, but if that's given, there is no need to
acquire another reference.
2023-12-21 07:26:50 +01:00
Thomas Haller
28331deae2 gobject: adjust assertion for ref-count in g_object_freeze_notify()
g_atomic_int_get() returns a signed int. While we don't expect this to be ever
negative, a negative value would also indicate a bug. Adjust the check to assert
against negative ref-count too.
2023-12-21 07:26:50 +01:00
Marco Trevisan (Treviño)
a8588b803e girepository/tests: Add search paths tests
Add some more tests to ensure search paths behavior follows the expected
one.
2023-12-21 03:02:27 +01:00
Marco Trevisan (Treviño)
2fbe6ca53e cleanup: Add missing final ending line to meson girrepository files 2023-12-21 03:02:27 +01:00
Colomban Wendling
7e9f2dadc6 Fix detecting size_t size when -Wmissing-prototypes is in CFLAGS
As this test includes `-Werror`, we need to be extra careful in which
warnings this code could trigger.
2023-12-20 21:41:33 +01:00
Marco Trevisan (Treviño)
b82d28141d build: Ignore ASAN link order errors when generating typelib files
Without this when compiling GLib with address sanitizer enabled, we'd
end up failing with this error:

  ==375535==ASan runtime does not come first in initial library list;
  you should either link runtime to your application or manually preload
  it with LD_PRELOAD.

Now, given that addressing the fix implies doing more radical changes,
it's just fine here to ignore ASAN to work on tools we use for building
gir files.
2023-12-20 21:35:53 +01:00
Marco Trevisan (Treviño)
083322c0db build: Factorize some common values in gi generation 2023-12-20 21:35:53 +01:00