[coop handles] Add some memory fences (#3617)
[mono.git] / mono / metadata / handle.c
1 /*
2  * handle.c: Handle to object in native code
3  *
4  * Authors:
5  *  - Ludovic Henry <ludovic@xamarin.com>
6  *  - Aleksey Klieger <aleksey.klieger@xamarin.com>
7  *  - Rodrigo Kumpera <kumpera@xamarin.com>
8  *
9  * Copyright 2016 Dot net foundation.
10  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
11  */
12
13 #include <config.h>
14 #include <glib.h>
15
16 #include <mono/metadata/handle.h>
17 #include <mono/metadata/object-internals.h>
18 #include <mono/metadata/gc-internals.h>
19 #include <mono/utils/atomic.h>
20 #include <mono/utils/mono-lazy-init.h>
21 #include <mono/utils/mono-threads.h>
22 /* TODO (missing pieces)
23
24 Add counters for:
25         number of stack marks
26         stack marks per icall
27         mix/max/avg size of stack marks
28         handle stack wastage
29
30 Actually do something in mono_handle_verify
31
32 Shrink the handles stack in mono_handle_stack_scan
33 Properly report it to the profiler.
34 Add a boehm implementation
35
36 TODO (things to explore):
37
38 There's no convenient way to wrap the object allocation function.
39 Right now we do this:
40         MonoCultureInfoHandle culture = MONO_HANDLE_NEW (MonoCultureInfo, mono_object_new_checked (domain, klass, &error));
41
42 Maybe what we need is a round of cleanup around all exposed types in the runtime to unify all helpers under the same hoof.
43 Combine: MonoDefaults, GENERATE_GET_CLASS_WITH_CACHE, TYPED_HANDLE_DECL and friends.
44         This would solve the age old issue of making it clear which types are optional and tell that to the linker.
45         We could then generate neat type safe wrappers.
46 */
47
48 /*
49  * NOTE: Async suspend
50  * 
51  * If we are running with cooperative GC, all the handle stack
52  * manipulation will complete before a GC thread scans the handle
53  * stack. If we are using async suspend, however, a thread may be
54  * trying to allocate a new handle, or unwind the handle stack when
55  * the GC stops the world.
56  *
57  * In particular, we need to ensure that if the mutator thread is
58  * suspended while manipulating the handle stack, the stack is in a
59  * good enough state to be scanned.  In particular, the size of each
60  * chunk should be updated before an object is written into the
61  * handle, and chunks to be scanned (between bottom and top) should
62  * always be valid.
63  *
64  * Note that the handle stack is scanned PRECISELY (see
65  * sgen_client_scan_thread_data ()).  That means there should not be
66  * stale objects scanned.  So when we manipulate the size of a chunk,
67  * wemust ensure that the newly scannable slot is either null or
68  * points to a valid value.
69  */
70
71 const MonoObjectHandle mono_null_value_handle = NULL;
72
73 #define THIS_IS_AN_OK_NUMBER_OF_HANDLES 100
74
75 /* Actual handles implementation */
76 MonoRawHandle
77 mono_handle_new (MonoObject *object)
78 {
79         MonoThreadInfo *info = mono_thread_info_current ();
80         HandleStack *handles = (HandleStack *)info->handle_stack;
81         HandleChunk *top = handles->top;
82
83 retry:
84         if (G_LIKELY (top->size < OBJECTS_PER_HANDLES_CHUNK)) {
85                 int idx = top->size;
86                 /* can be interrupted anywhere here, so:
87                  * 1. make sure the new slot is null
88                  * 2. make the new slot scannable (increment size)
89                  * 3. put a valid object in there
90                  *
91                  * (have to do 1 then 3 so that if we're interrupted
92                  * between 1 and 2, the object is still live)
93                  */
94                 top->objects [idx] = NULL;
95                 mono_memory_write_barrier ();
96                 top->size++;
97                 mono_memory_write_barrier ();
98                 MonoObject **h = &top->objects [idx];
99                 *h = object;
100                 return h;
101         }
102         if (G_LIKELY (top->next)) {
103                 top->next->size = 0;
104                 /* make sure size == 0 is visible to a GC thread before it sees the new top */
105                 mono_memory_write_barrier ();
106                 top = top->next;
107                 handles->top = top;
108                 goto retry;
109         }
110         HandleChunk *new_chunk = g_new (HandleChunk, 1);
111         new_chunk->size = 0;
112         new_chunk->prev = top;
113         new_chunk->next = NULL;
114         /* make sure size == 0 before new chunk is visible */
115         mono_memory_write_barrier ();
116         top->next = new_chunk;
117         handles->top = new_chunk;
118         goto retry;
119 }
120
121
122
123 HandleStack*
124 mono_handle_stack_alloc (void)
125 {
126         HandleStack *stack = g_new (HandleStack, 1);
127         HandleChunk *chunk = g_new (HandleChunk, 1);
128
129         chunk->size = 0;
130         chunk->prev = chunk->next = NULL;
131         mono_memory_write_barrier ();
132         stack->top = stack->bottom = chunk;
133         return stack;
134 }
135
136 void
137 mono_handle_stack_free (HandleStack *stack)
138 {
139         if (!stack)
140                 return;
141         HandleChunk *c = stack->bottom;
142         stack->top = stack->bottom = NULL;
143         mono_memory_write_barrier ();
144         while (c) {
145                 HandleChunk *next = c->next;
146                 g_free (c);
147                 c = next;
148         }
149         g_free (c);
150         g_free (stack);
151 }
152
153 void
154 mono_handle_stack_scan (HandleStack *stack, GcScanFunc func, gpointer gc_data)
155 {
156         /* if we're running, we know the world is stopped.
157          */
158         HandleChunk *cur = stack->bottom;
159         HandleChunk *last = stack->top;
160
161         if (!cur)
162                 return;
163
164         while (cur) {
165                 int i;
166                 for (i = 0; i < cur->size; ++i)
167                         func ((gpointer*)&cur->objects [i], gc_data);
168                 if (cur == last)
169                         break;
170                 cur = cur->next;
171         }
172 }
173
174 void
175 mono_stack_mark_record_size (MonoThreadInfo *info, HandleStackMark *stackmark, const char *func_name)
176 {
177         HandleStack *handles = (HandleStack *)info->handle_stack;
178         HandleChunk *cur = stackmark->chunk;
179         int size = -stackmark->size; //discard the starting point of the stack
180         while (cur) {
181                 size += cur->size;
182                 if (cur == handles->top)
183                         break;
184                 cur = cur->next;
185         }
186
187         if (size > THIS_IS_AN_OK_NUMBER_OF_HANDLES)
188                 printf ("%s USED %d handles\n", func_name, size);
189 }
190
191 /*
192  * Pop the stack until @stackmark and make @value the top value.
193  *
194  * @return the new handle for what @value points to 
195  */
196 MonoRawHandle
197 mono_stack_mark_pop_value (MonoThreadInfo *info, HandleStackMark *stackmark, MonoRawHandle value)
198 {
199         g_error ("impl me");
200 }
201
202 /* Temporary place for some of the handle enabled wrapper functions*/
203
204 MonoStringHandle
205 mono_string_new_handle (MonoDomain *domain, const char *data, MonoError *error)
206 {
207         return MONO_HANDLE_NEW (MonoString, mono_string_new_checked (domain, data, error));
208 }
209
210 MonoArrayHandle
211 mono_array_new_handle (MonoDomain *domain, MonoClass *eclass, uintptr_t n, MonoError *error)
212 {
213         return MONO_HANDLE_NEW (MonoArray, mono_array_new_checked (domain, eclass, n, error));
214 }
215
216 #ifdef ENABLE_CHECKED_BUILD
217 /* Checked build helpers */
218 void
219 mono_handle_verify (MonoRawHandle raw_handle)
220 {
221         
222 }
223 #endif