b11df8a17b5e39d83ef82e05e98ca52c5125cf87
[mono.git] / mono / metadata / sgen-bridge.h
1 /*
2  * Copyright 2011 Novell, Inc.
3  * 
4  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
5  */
6
7 /*
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.
11  *
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
15  * finalization.
16  *
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 then removes any nodes corresponding to components which do not
21  * contain bridged objects.
22  *
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.
29  *
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.
36  *
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.
43  *
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.
47  */
48
49 #ifndef _MONO_SGEN_BRIDGE_H_
50 #define _MONO_SGEN_BRIDGE_H_
51
52 #include <mono/utils/mono-publib.h>
53
54 MONO_BEGIN_DECLS
55
56 enum {
57         SGEN_BRIDGE_VERSION = 4
58 };
59         
60 typedef enum {
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;
70
71 typedef struct {
72         mono_bool is_alive;     /* to be set by the cross reference callback */
73         int num_objs;
74         MonoObject *objs [MONO_ZERO_LEN_ARRAY];
75 } MonoGCBridgeSCC;
76
77 typedef struct {
78         int src_scc_index;
79         int dst_scc_index;
80 } MonoGCBridgeXRef;
81
82 typedef struct {
83         int bridge_version;
84         /*
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.
88          */
89         MonoGCBridgeObjectKind (*bridge_class_kind) (MonoClass *klass);
90         /*
91          * This is only called on objects for whose classes
92          * `bridge_class_kind()` returned `XXX_BRIDGE_CLASS`.
93          */
94         mono_bool (*is_bridge_object) (MonoObject *object);
95         void (*cross_references) (int num_sccs, MonoGCBridgeSCC **sccs, int num_xrefs, MonoGCBridgeXRef *xrefs);
96 } MonoGCBridgeCallbacks;
97
98 /*
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.
102  */
103 MONO_API void mono_gc_register_bridge_callbacks (MonoGCBridgeCallbacks *callbacks);
104
105 MONO_API void mono_gc_wait_for_bridge_processing (void);
106
107 MONO_END_DECLS
108
109 #endif