Merge pull request #1708 from alexanderkyte/always_use_imt
[mono.git] / mono / metadata / sgen-workers.c
1 /*
2  * sgen-workers.c: Worker threads for parallel and concurrent GC.
3  *
4  * Copyright 2001-2003 Ximian, Inc
5  * Copyright 2003-2010 Novell, Inc.
6  * Copyright (C) 2012 Xamarin Inc
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License 2.0 as published by the Free Software Foundation;
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License 2.0 along with this library; if not, write to the Free
19  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include "config.h"
23 #ifdef HAVE_SGEN_GC
24
25 #include "metadata/sgen-gc.h"
26 #include "metadata/sgen-workers.h"
27 #include "metadata/sgen-thread-pool.h"
28 #include "utils/mono-counters.h"
29
30 static int workers_num;
31 static WorkerData *workers_data;
32
33 static SgenSectionGrayQueue workers_distribute_gray_queue;
34 static gboolean workers_distribute_gray_queue_inited;
35
36 enum {
37         STATE_NOT_WORKING,
38         STATE_WORKING,
39         STATE_NURSERY_COLLECTION
40 } WorkersStateName;
41
42 typedef gint32 State;
43
44 static volatile State workers_state;
45
46 static guint64 stat_workers_num_finished;
47
48 static gboolean
49 set_state (State old_state, State new_state)
50 {
51         if (old_state == STATE_NURSERY_COLLECTION)
52                 SGEN_ASSERT (0, new_state != STATE_NOT_WORKING, "Can't go from nursery collection to not working");
53
54         return InterlockedCompareExchange (&workers_state, new_state, old_state) == old_state;
55 }
56
57 static void
58 assert_not_working (State state)
59 {
60         SGEN_ASSERT (0, state == STATE_NOT_WORKING, "Can only signal enqueue work when in no work state");
61 }
62
63 static void
64 assert_working (State state)
65 {
66         SGEN_ASSERT (0, state == STATE_WORKING, "A worker can't wait without being in working state");
67 }
68
69 static void
70 assert_nursery_collection (State state)
71 {
72         SGEN_ASSERT (0, state == STATE_NURSERY_COLLECTION, "Must be in the nursery collection state");
73 }
74
75 static void
76 assert_working_or_nursery_collection (State state)
77 {
78         if (state != STATE_WORKING)
79                 assert_nursery_collection (state);
80 }
81
82 static void
83 workers_signal_enqueue_work (gboolean from_nursery_collection)
84 {
85         State old_state = workers_state;
86         gboolean did_set_state;
87
88         if (from_nursery_collection)
89                 assert_nursery_collection (old_state);
90         else
91                 assert_not_working (old_state);
92
93         did_set_state = set_state (old_state, STATE_WORKING);
94         SGEN_ASSERT (0, did_set_state, "Nobody else should be mutating the state");
95
96         sgen_thread_pool_idle_signal ();
97 }
98
99 static void
100 workers_signal_enqueue_work_if_necessary (void)
101 {
102         if (workers_state == STATE_NOT_WORKING)
103                 workers_signal_enqueue_work (FALSE);
104 }
105
106 void
107 sgen_workers_ensure_awake (void)
108 {
109         SGEN_ASSERT (0, workers_state != STATE_NURSERY_COLLECTION, "Can't wake workers during nursery collection");
110         workers_signal_enqueue_work_if_necessary ();
111 }
112
113 static void
114 worker_finish (void)
115 {
116         State old_state;
117
118         ++stat_workers_num_finished;
119
120         do {
121                 old_state = workers_state;
122
123                 assert_working_or_nursery_collection (old_state);
124                 if (old_state == STATE_NURSERY_COLLECTION)
125                         return;
126
127                 /* We are the last thread to go to sleep. */
128         } while (!set_state (old_state, STATE_NOT_WORKING));
129 }
130
131 static gboolean
132 collection_needs_workers (void)
133 {
134         return sgen_collection_is_concurrent ();
135 }
136
137 void
138 sgen_workers_enqueue_job (SgenThreadPoolJob *job)
139 {
140         if (!collection_needs_workers ()) {
141                 job->func (NULL, job);
142                 sgen_thread_pool_job_free (job);
143                 return;
144         }
145
146         sgen_thread_pool_job_enqueue (job);
147 }
148
149 void
150 sgen_workers_wait_for_jobs_finished (void)
151 {
152         sgen_thread_pool_wait_for_all_jobs ();
153 }
154
155 void
156 sgen_workers_signal_start_nursery_collection_and_wait (void)
157 {
158         State old_state;
159
160         do {
161                 old_state = workers_state;
162
163                 if (old_state != STATE_NOT_WORKING)
164                         assert_working (old_state);
165         } while (!set_state (old_state, STATE_NURSERY_COLLECTION));
166
167         sgen_thread_pool_idle_wait ();
168
169         assert_nursery_collection (workers_state);
170 }
171
172 void
173 sgen_workers_signal_finish_nursery_collection (void)
174 {
175         assert_nursery_collection (workers_state);
176         workers_signal_enqueue_work (TRUE);
177 }
178
179 static gboolean
180 workers_get_work (WorkerData *data)
181 {
182         SgenMajorCollector *major;
183
184         g_assert (sgen_gray_object_queue_is_empty (&data->private_gray_queue));
185
186         /* If we're concurrent, steal from the workers distribute gray queue. */
187         major = sgen_get_major_collector ();
188         if (major->is_concurrent) {
189                 GrayQueueSection *section = sgen_section_gray_queue_dequeue (&workers_distribute_gray_queue);
190                 if (section) {
191                         sgen_gray_object_enqueue_section (&data->private_gray_queue, section);
192                         return TRUE;
193                 }
194         }
195
196         /* Nobody to steal from */
197         g_assert (sgen_gray_object_queue_is_empty (&data->private_gray_queue));
198         return FALSE;
199 }
200
201 static void
202 concurrent_enqueue_check (char *obj)
203 {
204         g_assert (sgen_concurrent_collection_in_progress ());
205         g_assert (!sgen_ptr_in_nursery (obj));
206         g_assert (SGEN_LOAD_VTABLE (obj));
207 }
208
209 static void
210 init_private_gray_queue (WorkerData *data)
211 {
212         sgen_gray_object_queue_init (&data->private_gray_queue,
213                         sgen_get_major_collector ()->is_concurrent ? concurrent_enqueue_check : NULL);
214 }
215
216 static void
217 thread_pool_init_func (void *data_untyped)
218 {
219         WorkerData *data = data_untyped;
220         SgenMajorCollector *major = sgen_get_major_collector ();
221
222         mono_thread_info_register_small_id ();
223
224         if (!major->is_concurrent)
225                 return;
226
227         init_private_gray_queue (data);
228 }
229
230 static gboolean
231 marker_idle_func (void *data_untyped)
232 {
233         WorkerData *data = data_untyped;
234         SgenMajorCollector *major = sgen_get_major_collector ();
235
236         if (workers_state != STATE_WORKING)
237                 return FALSE;
238
239         SGEN_ASSERT (0, sgen_get_current_collection_generation () != GENERATION_NURSERY, "Why are we doing work while there's a nursery collection happening?");
240
241         if (!sgen_gray_object_queue_is_empty (&data->private_gray_queue) || workers_get_work (data)) {
242                 SgenObjectOperations *ops = sgen_concurrent_collection_in_progress ()
243                         ? &major->major_concurrent_ops
244                         : &major->major_ops;
245                 ScanCopyContext ctx = { ops->scan_object, NULL, &data->private_gray_queue };
246
247                 SGEN_ASSERT (0, !sgen_gray_object_queue_is_empty (&data->private_gray_queue), "How is our gray queue empty if we just got work?");
248
249                 sgen_drain_gray_stack (32, ctx);
250
251                 return TRUE;
252         }
253
254         worker_finish ();
255
256         return FALSE;
257 }
258
259 static void
260 init_distribute_gray_queue (void)
261 {
262         if (workers_distribute_gray_queue_inited) {
263                 g_assert (sgen_section_gray_queue_is_empty (&workers_distribute_gray_queue));
264                 g_assert (workers_distribute_gray_queue.locked);
265                 return;
266         }
267
268         sgen_section_gray_queue_init (&workers_distribute_gray_queue, TRUE,
269                         sgen_get_major_collector ()->is_concurrent ? concurrent_enqueue_check : NULL);
270         workers_distribute_gray_queue_inited = TRUE;
271 }
272
273 void
274 sgen_workers_init_distribute_gray_queue (void)
275 {
276         SGEN_ASSERT (0, sgen_get_major_collector ()->is_concurrent && collection_needs_workers (),
277                         "Why should we init the distribute gray queue if we don't need it?");
278         init_distribute_gray_queue ();
279 }
280
281 void
282 sgen_workers_init (int num_workers)
283 {
284         int i;
285         void *workers_data_ptrs [num_workers];
286
287         if (!sgen_get_major_collector ()->is_concurrent) {
288                 sgen_thread_pool_init (num_workers, thread_pool_init_func, NULL, NULL);
289                 return;
290         }
291
292         //g_print ("initing %d workers\n", num_workers);
293
294         workers_num = num_workers;
295
296         workers_data = sgen_alloc_internal_dynamic (sizeof (WorkerData) * num_workers, INTERNAL_MEM_WORKER_DATA, TRUE);
297         memset (workers_data, 0, sizeof (WorkerData) * num_workers);
298
299         init_distribute_gray_queue ();
300
301         for (i = 0; i < workers_num; ++i)
302                 workers_data_ptrs [i] = &workers_data [i];
303
304         sgen_thread_pool_init (num_workers, thread_pool_init_func, marker_idle_func, workers_data_ptrs);
305
306         mono_counters_register ("# workers finished", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_workers_num_finished);
307 }
308
309 void
310 sgen_workers_start_all_workers (void)
311 {
312         if (!collection_needs_workers ())
313                 return;
314
315         workers_signal_enqueue_work (FALSE);
316 }
317
318 void
319 sgen_workers_join (void)
320 {
321         int i;
322
323         if (!collection_needs_workers ())
324                 return;
325
326         sgen_thread_pool_wait_for_all_jobs ();
327
328         for (;;) {
329                 SGEN_ASSERT (0, workers_state != STATE_NURSERY_COLLECTION, "Can't be in nursery collection when joining");
330                 sgen_thread_pool_idle_wait ();
331                 assert_not_working (workers_state);
332
333                 /*
334                  * Checking whether there is still work left and, if not, going to sleep,
335                  * are two separate actions that are not performed atomically by the
336                  * workers.  Therefore there's a race condition where work can be added
337                  * after they've checked for work, and before they've gone to sleep.
338                  */
339                 if (sgen_section_gray_queue_is_empty (&workers_distribute_gray_queue))
340                         break;
341
342                 workers_signal_enqueue_work (FALSE);
343         }
344
345         /* At this point all the workers have stopped. */
346
347         g_assert (sgen_section_gray_queue_is_empty (&workers_distribute_gray_queue));
348         for (i = 0; i < workers_num; ++i)
349                 g_assert (sgen_gray_object_queue_is_empty (&workers_data [i].private_gray_queue));
350 }
351
352 gboolean
353 sgen_workers_all_done (void)
354 {
355         return workers_state == STATE_NOT_WORKING;
356 }
357
358 gboolean
359 sgen_workers_are_working (void)
360 {
361         return workers_state == STATE_WORKING;
362 }
363
364 void
365 sgen_workers_wait (void)
366 {
367         sgen_thread_pool_idle_wait ();
368         SGEN_ASSERT (0, sgen_workers_all_done (), "Why are the workers not done after we wait for them?");
369 }
370
371 SgenSectionGrayQueue*
372 sgen_workers_get_distribute_section_gray_queue (void)
373 {
374         return &workers_distribute_gray_queue;
375 }
376
377 #endif