gdbsupport: allow to specify dependencies between observers
authorMichael Weghorn <m.weghorn@posteo.de>
Tue, 27 Apr 2021 13:55:27 +0000 (09:55 -0400)
committerSimon Marchi <simon.marchi@polymtl.ca>
Tue, 27 Apr 2021 15:22:32 +0000 (11:22 -0400)
commit9a6e099f43a13efb0ee274002de689c2cb1b7e23
treeb403ea072bd3178cd60673deac400a29eaedd295
parent60cfa10c36668e3298bab35e9dc86624f3ddf61a
gdbsupport: allow to specify dependencies between observers

Previously, the observers attached to an observable were always notified
in the order in which they had been attached.  That order is not easily
controlled, because observers are typically attached in _initialize_*
functions, which are called in an undefined order.

However, an observer may require that another observer attached only
later is called before itself is.

Therefore, extend the 'observable' class to allow explicitly specifying
dependencies when attaching observers, by adding the possibility to
specify tokens for observers that it depends on.

To make sure dependencies are notified before observers depending on
them, the vector holding the observers is sorted in a way that
dependencies come before observers depending on them.  The current
implementation for sorting uses the depth-first search algorithm for
topological sorting as described at [1].

Extend the observable unit tests to cover this case as well.  Check that
this works for a few different orders in which the observers are
attached.

This newly introduced mechanism to explicitly specify dependencies will
be used in a follow-up commit.

[1] https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search

Tested on x86_64-linux (Debian testing).

gdb/ChangeLog:

* unittests/observable-selftests.c (dependency_test_counters):
New.
(observer_token0, observer_token1, observer_token2,
observer_token3, observer_token4, observer_token5): New.
(struct dependency_observer_data): New struct.
(observer_dependency_test_callback): New function.
(test_observers): New.
(run_dependency_test): New function.
(test_dependency): New.
(_initialize_observer_selftest): Register dependency test.

gdbsupport/ChangeLog:

* observable.h (class observable): Extend to allow specifying
dependencies between observers, keep vector holding observers
sorted so that dependencies are notified before observers
depending on them.

Change-Id: I5399def1eeb69ca99e28c9f1fdf321d78b530bdb
gdb/ChangeLog
gdb/unittests/observable-selftests.c
gdbsupport/ChangeLog
gdbsupport/observable.h