Skip to content

Commit 9a6e099

Browse files
michaelweghornsimark
authored andcommitted
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
1 parent 60cfa10 commit 9a6e099

File tree

4 files changed

+235
-16
lines changed

4 files changed

+235
-16
lines changed

gdb/ChangeLog

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
2021-04-27 Michael Weghorn <[email protected]>
2+
Simon Marchi <[email protected]>
3+
4+
* unittests/observable-selftests.c (dependency_test_counters):
5+
New.
6+
(observer_token0, observer_token1, observer_token2,
7+
observer_token3, observer_token4, observer_token5): New.
8+
(struct dependency_observer_data): New struct.
9+
(observer_dependency_test_callback): New function.
10+
(test_observers): New.
11+
(run_dependency_test): New function.
12+
(test_dependency): New.
13+
(_initialize_observer_selftest): Register dependency test.
14+
115
2021-04-26 Simon Marchi <[email protected]>
216

317
PR gdb/27773

gdb/unittests/observable-selftests.c

+113
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,60 @@ static int test_first_observer = 0;
3030
static int test_second_observer = 0;
3131
static int test_third_observer = 0;
3232

33+
/* Counters for observers used for dependency tests. */
34+
static std::vector<int> dependency_test_counters;
35+
36+
/* Tokens for observers used for dependency tests. */
37+
static gdb::observers::token observer_token0;
38+
static gdb::observers::token observer_token1;
39+
static gdb::observers::token observer_token2;
40+
static gdb::observers::token observer_token3;
41+
static gdb::observers::token observer_token4;
42+
static gdb::observers::token observer_token5;
43+
44+
/* Data for one observer used for checking that dependencies work as expected;
45+
dependencies are specified using their indices into the 'test_observers'
46+
vector here for simplicity and mapped to corresponding tokens later. */
47+
struct dependency_observer_data
48+
{
49+
gdb::observers::token *token;
50+
51+
/* Name of the observer to use on attach. */
52+
const char *name;
53+
54+
/* Indices of observers that this one directly depends on. */
55+
std::vector<int> direct_dependencies;
56+
57+
/* Indices for all dependencies, including transitive ones. */
58+
std::vector<int> all_dependencies;
59+
60+
/* Function to attach to the observable for this observer. */
61+
std::function<void (int)> callback;
62+
};
63+
64+
static void observer_dependency_test_callback (size_t index);
65+
66+
/* Data for observers to use for dependency tests, using some sample
67+
dependencies between the observers. */
68+
static std::vector<dependency_observer_data> test_observers = {
69+
{&observer_token0, "test0", {}, {},
70+
[] (int) { observer_dependency_test_callback (0); }},
71+
{&observer_token1, "test1", {0}, {0},
72+
[] (int) { observer_dependency_test_callback (1); }},
73+
{&observer_token2, "test2", {1}, {0, 1},
74+
[] (int) { observer_dependency_test_callback (2); }},
75+
{&observer_token3, "test3", {1}, {0, 1},
76+
[] (int) { observer_dependency_test_callback (3); }},
77+
{&observer_token4, "test4", {2, 3, 5}, {0, 1, 2, 3, 5},
78+
[] (int) { observer_dependency_test_callback (4); }},
79+
{&observer_token5, "test5", {0}, {0},
80+
[] (int) { observer_dependency_test_callback (5); }},
81+
{nullptr, "test6", {4}, {0, 1, 2, 3, 4, 5},
82+
[] (int) { observer_dependency_test_callback (6); }},
83+
{nullptr, "test7", {0}, {0},
84+
[] (int) { observer_dependency_test_callback (7); }},
85+
};
86+
3387
static void
3488
test_first_notification_function (int arg)
3589
{
@@ -63,6 +117,63 @@ notify_check_counters (int one, int two, int three)
63117
SELF_CHECK (three == test_third_observer);
64118
}
65119

120+
/* Function for each observer to run when being notified during the dependency
121+
tests. Verify that the observer's dependencies have been notified before the
122+
observer itself by checking their counters, then increase the observer's own
123+
counter. */
124+
static void
125+
observer_dependency_test_callback (size_t index)
126+
{
127+
/* Check that dependencies have already been notified. */
128+
for (int i : test_observers[index].all_dependencies)
129+
SELF_CHECK (dependency_test_counters[i] == 1);
130+
131+
/* Increase own counter. */
132+
dependency_test_counters[index]++;
133+
}
134+
135+
/* Run a dependency test by attaching the observers in the specified order
136+
then notifying them. */
137+
static void
138+
run_dependency_test (std::vector<int> insertion_order)
139+
{
140+
gdb::observers::observable<int> dependency_test_notification
141+
("dependency_test_notification");
142+
143+
/* Reset counters. */
144+
dependency_test_counters = std::vector<int> (test_observers.size (), 0);
145+
146+
/* Attach all observers in the given order, specifying dependencies. */
147+
for (int i : insertion_order)
148+
{
149+
const dependency_observer_data &o = test_observers[i];
150+
151+
/* Get tokens for dependencies using their indices. */
152+
std::vector<const gdb::observers::token *> dependency_tokens;
153+
for (int index : o.all_dependencies)
154+
dependency_tokens.emplace_back (test_observers[index].token);
155+
156+
if (o.token != nullptr)
157+
dependency_test_notification.attach
158+
(o.callback, *o.token, o.name, dependency_tokens);
159+
else
160+
dependency_test_notification.attach (o.callback, o.name,
161+
dependency_tokens);
162+
}
163+
164+
/* Notify observers, they check that their dependencies were notified. */
165+
dependency_test_notification.notify (1);
166+
}
167+
168+
static void
169+
test_dependency ()
170+
{
171+
/* Run dependency tests with different insertion orders. */
172+
run_dependency_test ({0, 1, 2, 3, 4, 5, 6, 7});
173+
run_dependency_test ({7, 6, 5, 4, 3, 2, 1, 0});
174+
run_dependency_test ({0, 3, 2, 1, 7, 6, 4, 5});
175+
}
176+
66177
static void
67178
run_tests ()
68179
{
@@ -133,4 +244,6 @@ _initialize_observer_selftest ()
133244
{
134245
selftests::register_test ("gdb::observers",
135246
selftests::observers::run_tests);
247+
selftests::register_test ("gdb::observers dependency",
248+
selftests::observers::test_dependency);
136249
}

gdbsupport/ChangeLog

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
2021-04-27 Michael Weghorn <[email protected]>
2+
Simon Marchi <[email protected]>
3+
4+
* observable.h (class observable): Extend to allow specifying
5+
dependencies between observers, keep vector holding observers
6+
sorted so that dependencies are notified before observers
7+
depending on them.
8+
19
2021-04-24 Simon Marchi <[email protected]>
210

311
* observable.h (observer_debug_printf,

gdbsupport/observable.h

+100-16
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,15 @@ class observable
7171
private:
7272
struct observer
7373
{
74-
observer (const struct token *token, func_type func, const char *name)
75-
: token (token), func (func), name (name)
74+
observer (const struct token *token, func_type func, const char *name,
75+
const std::vector<const struct token *> &dependencies)
76+
: token (token), func (func), name (name), dependencies (dependencies)
7677
{}
7778

7879
const struct token *token;
7980
func_type func;
8081
const char *name;
82+
std::vector<const struct token *> dependencies;
8183
};
8284

8385
public:
@@ -88,30 +90,34 @@ class observable
8890

8991
DISABLE_COPY_AND_ASSIGN (observable);
9092

91-
/* Attach F as an observer to this observable. F cannot be
92-
detached.
93+
/* Attach F as an observer to this observable. F cannot be detached or
94+
specified as a dependency.
95+
96+
DEPENDENCIES is a list of tokens of observers to be notified before this
97+
one.
9398
9499
NAME is the name of the observer, used for debug output purposes. Its
95100
lifetime must be at least as long as the observer is attached. */
96-
void attach (const func_type &f, const char *name)
101+
void attach (const func_type &f, const char *name,
102+
const std::vector<const struct token *> &dependencies = {})
97103
{
98-
observer_debug_printf ("Attaching observable %s to observer %s",
99-
name, m_name);
100-
101-
m_observers.emplace_back (nullptr, f, name);
104+
attach (f, nullptr, name, dependencies);
102105
}
103106

104-
/* Attach F as an observer to this observable. T is a reference to
105-
a token that can be used to later remove F.
107+
/* Attach F as an observer to this observable.
108+
109+
T is a reference to a token that can be used to later remove F or specify F
110+
as a dependency of another observer.
111+
112+
DEPENDENCIES is a list of tokens of observers to be notified before this
113+
one.
106114
107115
NAME is the name of the observer, used for debug output purposes. Its
108116
lifetime must be at least as long as the observer is attached. */
109-
void attach (const func_type &f, const token &t, const char *name)
117+
void attach (const func_type &f, const token &t, const char *name,
118+
const std::vector<const struct token *> &dependencies = {})
110119
{
111-
observer_debug_printf ("Attaching observable %s to observer %s",
112-
name, m_name);
113-
114-
m_observers.emplace_back (&t, f, name);
120+
attach (f, &t, name, dependencies);
115121
}
116122

117123
/* Remove observers associated with T from this observable. T is
@@ -149,6 +155,84 @@ class observable
149155

150156
std::vector<observer> m_observers;
151157
const char *m_name;
158+
159+
/* Use for sorting algorithm, to indicate which observer we have visited. */
160+
enum class visit_state
161+
{
162+
NOT_VISITED,
163+
VISITING,
164+
VISITED,
165+
};
166+
167+
/* Helper method for topological sort using depth-first search algorithm.
168+
169+
Visit all dependencies of observer at INDEX in M_OBSERVERS (later referred
170+
to as "the observer"). Then append the observer to SORTED_OBSERVERS.
171+
172+
If the observer is already visited, do nothing. */
173+
void visit_for_sorting (std::vector<observer> &sorted_observers,
174+
std::vector<visit_state> &visit_states, int index)
175+
{
176+
if (visit_states[index] == visit_state::VISITED)
177+
return;
178+
179+
/* If we are already visiting this observer, it means there's a cycle. */
180+
gdb_assert (visit_states[index] != visit_state::VISITING);
181+
182+
visit_states[index] = visit_state::VISITING;
183+
184+
/* For each dependency of this observer... */
185+
for (const token *dep : m_observers[index].dependencies)
186+
{
187+
/* ... find the observer that has token DEP. If found, visit it. */
188+
auto it_dep
189+
= std::find_if (m_observers.begin (), m_observers.end (),
190+
[&] (observer o) { return o.token == dep; });
191+
if (it_dep != m_observers.end ())
192+
{
193+
int i = std::distance (m_observers.begin (), it_dep);
194+
visit_for_sorting (sorted_observers, visit_states, i);
195+
}
196+
}
197+
198+
visit_states[index] = visit_state::VISITED;
199+
sorted_observers.push_back (m_observers[index]);
200+
}
201+
202+
/* Sort the observers, so that dependencies come before observers
203+
depending on them.
204+
205+
Uses depth-first search algorithm for topological sorting, see
206+
https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search . */
207+
void sort_observers ()
208+
{
209+
std::vector<observer> sorted_observers;
210+
std::vector<visit_state> visit_states (m_observers.size (),
211+
visit_state::NOT_VISITED);
212+
213+
for (size_t i = 0; i < m_observers.size (); i++)
214+
visit_for_sorting (sorted_observers, visit_states, i);
215+
216+
m_observers = std::move (sorted_observers);
217+
}
218+
219+
void attach (const func_type &f, const token *t, const char *name,
220+
const std::vector<const struct token *> &dependencies)
221+
{
222+
223+
observer_debug_printf ("Attaching observable %s to observer %s",
224+
name, m_name);
225+
226+
m_observers.emplace_back (t, f, name, dependencies);
227+
228+
/* The observer has been inserted at the end of the vector, so it will be
229+
after any of its potential dependencies attached earlier. If the
230+
observer has a token, it means that other observers can specify it as
231+
a dependency, so sorting is necessary to ensure those will be after the
232+
newly inserted observer afterwards. */
233+
if (t != nullptr)
234+
sort_observers ();
235+
};
152236
};
153237

154238
} /* namespace observers */

0 commit comments

Comments
 (0)