2 * Copyright 2011 Novell, Inc.
4 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
8 * The bridge is a mechanism for SGen to let clients override the death of some
9 * unreachable objects. We use it in monodroid to do garbage collection across
10 * the Mono and Java heaps.
12 * The client (Monodroid) can designate some objects as "bridged", which means
13 * that they participate in the bridge processing step once SGen considers them
14 * unreachable, i.e., dead. Bridged objects must be registered for
17 * When SGen is done marking, it puts together a list of all dead bridged
18 * objects. This is passed to the bridge processor, which does an analysis to
19 * simplify the graph: It replaces strongly-connected components with single
20 * nodes, and may remove nodes corresponding to components which do not contain
23 * The output of the SCC analysis is passed to the client's `cross_references()`
24 * callback. This consists of 2 arrays, an array of SCCs (MonoGCBridgeSCC),
25 * and an array of "xrefs" (edges between SCCs, MonoGCBridgeXRef). Edges are
26 * encoded as pairs of "API indices", ie indexes in the SCC array. The client
27 * is expected to set the `is_alive` flag on those strongly connected components
28 * that it wishes to be kept alive.
30 * In monodroid each bridged object has a corresponding Java mirror object. In
31 * the bridge callback it reifies the Mono object graph in the Java heap so that
32 * the full, combined object graph is now instantiated on the Java side. Then
33 * it triggers a Java GC, waits for it to finish, and checks which of the Java
34 * mirror objects are still alive. For those it sets the `is_alive` flag and
35 * returns from the callback.
37 * The SCC analysis is done while the world is stopped, but the callback is made
38 * with the world running again. Weak links to bridged objects and other
39 * objects reachable from them are kept until the callback returns, at which
40 * point all links to bridged objects that don't have `is_alive` set are nulled.
41 * Note that weak links to non-bridged objects reachable from bridged objects
42 * are not nulled. This might be considered a bug.
44 * There are three different implementations of the bridge processor, each of
45 * which implements 8 callbacks (see SgenBridgeProcessor). The implementations
46 * differ in the algorithm they use to compute the "simplified" SCC graph.
49 #ifndef _MONO_SGEN_BRIDGE_H_
50 #define _MONO_SGEN_BRIDGE_H_
52 #include <mono/utils/mono-publib.h>
57 SGEN_BRIDGE_VERSION = 5
61 /* Instances of this class should be scanned when computing the transitive dependency among bridges. E.g. List<object>*/
62 GC_BRIDGE_TRANSPARENT_CLASS,
63 /* Instances of this class should not be scanned when computing the transitive dependency among bridges. E.g. String*/
64 GC_BRIDGE_OPAQUE_CLASS,
65 /* Instances of this class should be bridged and have their dependency computed. */
66 GC_BRIDGE_TRANSPARENT_BRIDGE_CLASS,
67 /* Instances of this class should be bridged but no dependencies should not be calculated. */
68 GC_BRIDGE_OPAQUE_BRIDGE_CLASS,
69 } MonoGCBridgeObjectKind;
72 mono_bool is_alive; /* to be set by the cross reference callback */
74 MonoObject *objs [MONO_ZERO_LEN_ARRAY];
85 * Tells the runtime which classes to even consider when looking for
86 * bridged objects. If subclasses are to be considered as well, the
87 * subclass check must be done in the callback.
89 MonoGCBridgeObjectKind (*bridge_class_kind) (MonoClass *klass);
91 * This is only called on objects for whose classes
92 * `bridge_class_kind()` returned `XXX_BRIDGE_CLASS`.
94 mono_bool (*is_bridge_object) (MonoObject *object);
95 void (*cross_references) (int num_sccs, MonoGCBridgeSCC **sccs, int num_xrefs, MonoGCBridgeXRef *xrefs);
96 } MonoGCBridgeCallbacks;
99 * Note: This may be called at any time, but cannot be called concurrently
100 * with (during and on a separate thread from) sgen init. Callers are
101 * responsible for enforcing this.
103 MONO_API void mono_gc_register_bridge_callbacks (MonoGCBridgeCallbacks *callbacks);
105 MONO_API void mono_gc_wait_for_bridge_processing (void);