[coop] Add missing coop checks for WFSO and WFMO.
[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 /*
37  * Allowed transitions:
38  *
39  * | from \ to          | NOT WORKING | WORKING | WORK ENQUEUED | NURSERY COLLECTION |
40  * |--------------------+-------------+---------+---------------+--------------------|
41  * | NOT WORKING        | -           | -       | main          | main               |
42  * | WORKING            | worker      | -       | main          | main               |
43  * | WORK ENQUEUED      | -           | worker  | -             | main               |
44  * | NURSERY COLLECTION | -           | -       | main          | -                  |
45  *
46  * The WORK ENQUEUED state guarantees that the worker thread will inspect the queue again at
47  * least once.  Only after looking at the queue will it go back to WORKING, and then,
48  * eventually, to NOT WORKING.  After enqueuing work the main thread transitions the state
49  * to WORK ENQUEUED.  Signalling the worker thread to wake up is only necessary if the old
50  * state was NOT WORKING.
51  */
52
53 enum {
54         STATE_NOT_WORKING,
55         STATE_WORKING,
56         STATE_WORK_ENQUEUED,
57         STATE_NURSERY_COLLECTION
58 } WorkersStateName;
59
60 typedef gint32 State;
61
62 static volatile State workers_state;
63
64 static SgenObjectOperations * volatile idle_func_object_ops;
65
66 static guint64 stat_workers_num_finished;
67
68 static gboolean
69 set_state (State old_state, State new_state)
70 {
71         SGEN_ASSERT (0, old_state != new_state, "Why are we transitioning to the same state?");
72         if (new_state == STATE_NOT_WORKING)
73                 SGEN_ASSERT (0, old_state == STATE_WORKING, "We can only transition to NOT WORKING from WORKING");
74         else if (new_state == STATE_WORKING)
75                 SGEN_ASSERT (0, old_state == STATE_WORK_ENQUEUED, "We can only transition to WORKING from WORK ENQUEUED");
76         if (new_state == STATE_NOT_WORKING || new_state == STATE_WORKING)
77                 SGEN_ASSERT (6, sgen_thread_pool_is_thread_pool_thread (mono_native_thread_id_get ()), "Only the worker thread is allowed to transition to NOT_WORKING or WORKING");
78
79         return InterlockedCompareExchange (&workers_state, new_state, old_state) == old_state;
80 }
81
82 static void
83 assert_nursery_collection (State state)
84 {
85         SGEN_ASSERT (0, state == STATE_NURSERY_COLLECTION, "Must be in the nursery collection state");
86 }
87
88 static gboolean
89 state_is_working_or_enqueued (State state)
90 {
91         return state == STATE_WORKING || state == STATE_WORK_ENQUEUED;
92 }
93
94 static void
95 workers_signal_enqueue_work (gboolean from_nursery_collection)
96 {
97         State old_state;
98         gboolean did_set_state;
99
100         do {
101                 old_state = workers_state;
102
103                 if (from_nursery_collection)
104                         assert_nursery_collection (old_state);
105                 else
106                         SGEN_ASSERT (0, old_state != STATE_NURSERY_COLLECTION, "If we're not in a nursery collection, how come the state is NURSERY COLLECTION?");
107
108                 if (old_state == STATE_WORK_ENQUEUED)
109                         break;
110
111                 did_set_state = set_state (old_state, STATE_WORK_ENQUEUED);
112                 if (from_nursery_collection)
113                         SGEN_ASSERT (0, did_set_state, "Nobody else should be mutating the state");
114         } while (!did_set_state);
115
116         if (!state_is_working_or_enqueued (old_state))
117                 sgen_thread_pool_idle_signal ();
118 }
119
120 void
121 sgen_workers_ensure_awake (void)
122 {
123         SGEN_ASSERT (0, workers_state != STATE_NURSERY_COLLECTION, "Can't wake workers during nursery collection");
124         workers_signal_enqueue_work (FALSE);
125 }
126
127 static void
128 worker_try_finish (void)
129 {
130         State old_state;
131
132         ++stat_workers_num_finished;
133
134         do {
135                 old_state = workers_state;
136
137                 SGEN_ASSERT (0, old_state != STATE_NOT_WORKING, "How did we get from doing idle work to NOT WORKING without setting it ourselves?");
138                 if (old_state == STATE_NURSERY_COLLECTION)
139                         return;
140                 if (old_state == STATE_WORK_ENQUEUED)
141                         return;
142                 SGEN_ASSERT (0, old_state == STATE_WORKING, "What other possibility is there?");
143
144                 /* We are the last thread to go to sleep. */
145         } while (!set_state (old_state, STATE_NOT_WORKING));
146 }
147
148 static gboolean
149 collection_needs_workers (void)
150 {
151         return sgen_collection_is_concurrent ();
152 }
153
154 void
155 sgen_workers_enqueue_job (SgenThreadPoolJob *job)
156 {
157         if (!collection_needs_workers ()) {
158                 job->func (NULL, job);
159                 sgen_thread_pool_job_free (job);
160                 return;
161         }
162
163         sgen_thread_pool_job_enqueue (job);
164 }
165
166 void
167 sgen_workers_wait_for_jobs_finished (void)
168 {
169         sgen_thread_pool_wait_for_all_jobs ();
170         /*
171          * If the idle task was never triggered or it finished before the last job did and
172          * then didn't get triggered again, we might end up in the situation of having
173          * something in the gray queue yet the idle task not working.  The easiest way to
174          * make sure this doesn't stay that way is to just trigger it again after all jobs
175          * have finished.
176          */
177         sgen_workers_ensure_awake ();
178 }
179
180 void
181 sgen_workers_signal_start_nursery_collection_and_wait (void)
182 {
183         State old_state;
184
185         do {
186                 old_state = workers_state;
187
188                 if (old_state != STATE_NOT_WORKING)
189                         SGEN_ASSERT (0, old_state != STATE_NURSERY_COLLECTION, "Why are we transitioning to NURSERY COLLECTION when we're already there?");
190         } while (!set_state (old_state, STATE_NURSERY_COLLECTION));
191
192         sgen_thread_pool_idle_wait ();
193
194         assert_nursery_collection (workers_state);
195 }
196
197 void
198 sgen_workers_signal_finish_nursery_collection (void)
199 {
200         assert_nursery_collection (workers_state);
201         workers_signal_enqueue_work (TRUE);
202 }
203
204 static gboolean
205 workers_get_work (WorkerData *data)
206 {
207         SgenMajorCollector *major;
208
209         g_assert (sgen_gray_object_queue_is_empty (&data->private_gray_queue));
210
211         /* If we're concurrent, steal from the workers distribute gray queue. */
212         major = sgen_get_major_collector ();
213         if (major->is_concurrent) {
214                 GrayQueueSection *section = sgen_section_gray_queue_dequeue (&workers_distribute_gray_queue);
215                 if (section) {
216                         sgen_gray_object_enqueue_section (&data->private_gray_queue, section);
217                         return TRUE;
218                 }
219         }
220
221         /* Nobody to steal from */
222         g_assert (sgen_gray_object_queue_is_empty (&data->private_gray_queue));
223         return FALSE;
224 }
225
226 static void
227 concurrent_enqueue_check (char *obj)
228 {
229         g_assert (sgen_concurrent_collection_in_progress ());
230         g_assert (!sgen_ptr_in_nursery (obj));
231         g_assert (SGEN_LOAD_VTABLE (obj));
232 }
233
234 static void
235 init_private_gray_queue (WorkerData *data)
236 {
237         sgen_gray_object_queue_init (&data->private_gray_queue,
238                         sgen_get_major_collector ()->is_concurrent ? concurrent_enqueue_check : NULL);
239 }
240
241 static void
242 thread_pool_init_func (void *data_untyped)
243 {
244         WorkerData *data = data_untyped;
245         SgenMajorCollector *major = sgen_get_major_collector ();
246
247         mono_thread_info_register_small_id ();
248
249         if (!major->is_concurrent)
250                 return;
251
252         init_private_gray_queue (data);
253 }
254
255 static gboolean
256 continue_idle_func (void)
257 {
258         return state_is_working_or_enqueued (workers_state);
259 }
260
261 static void
262 marker_idle_func (void *data_untyped)
263 {
264         WorkerData *data = data_untyped;
265
266         if (!continue_idle_func ())
267                 return;
268
269         SGEN_ASSERT (0, sgen_concurrent_collection_in_progress (), "The worker should only mark in concurrent collections.");
270         SGEN_ASSERT (0, sgen_get_current_collection_generation () != GENERATION_NURSERY, "Why are we doing work while there's a nursery collection happening?");
271
272         if (workers_state == STATE_WORK_ENQUEUED) {
273                 set_state (STATE_WORK_ENQUEUED, STATE_WORKING);
274                 SGEN_ASSERT (0, workers_state != STATE_NOT_WORKING, "How did we get from WORK ENQUEUED to NOT WORKING?");
275         }
276
277         if (!sgen_gray_object_queue_is_empty (&data->private_gray_queue) || workers_get_work (data)) {
278                 ScanCopyContext ctx = CONTEXT_FROM_OBJECT_OPERATIONS (idle_func_object_ops, &data->private_gray_queue);
279
280                 SGEN_ASSERT (0, !sgen_gray_object_queue_is_empty (&data->private_gray_queue), "How is our gray queue empty if we just got work?");
281
282                 sgen_drain_gray_stack (32, ctx);
283         } else {
284                 worker_try_finish ();
285         }
286 }
287
288 static void
289 init_distribute_gray_queue (void)
290 {
291         if (workers_distribute_gray_queue_inited) {
292                 g_assert (sgen_section_gray_queue_is_empty (&workers_distribute_gray_queue));
293                 g_assert (workers_distribute_gray_queue.locked);
294                 return;
295         }
296
297         sgen_section_gray_queue_init (&workers_distribute_gray_queue, TRUE,
298                         sgen_get_major_collector ()->is_concurrent ? concurrent_enqueue_check : NULL);
299         workers_distribute_gray_queue_inited = TRUE;
300 }
301
302 void
303 sgen_workers_init_distribute_gray_queue (void)
304 {
305         SGEN_ASSERT (0, sgen_get_major_collector ()->is_concurrent && collection_needs_workers (),
306                         "Why should we init the distribute gray queue if we don't need it?");
307         init_distribute_gray_queue ();
308 }
309
310 void
311 sgen_workers_init (int num_workers)
312 {
313         int i;
314         void *workers_data_ptrs [num_workers];
315
316         if (!sgen_get_major_collector ()->is_concurrent) {
317                 sgen_thread_pool_init (num_workers, thread_pool_init_func, NULL, NULL, NULL);
318                 return;
319         }
320
321         //g_print ("initing %d workers\n", num_workers);
322
323         workers_num = num_workers;
324
325         workers_data = sgen_alloc_internal_dynamic (sizeof (WorkerData) * num_workers, INTERNAL_MEM_WORKER_DATA, TRUE);
326         memset (workers_data, 0, sizeof (WorkerData) * num_workers);
327
328         init_distribute_gray_queue ();
329
330         for (i = 0; i < workers_num; ++i)
331                 workers_data_ptrs [i] = &workers_data [i];
332
333         sgen_thread_pool_init (num_workers, thread_pool_init_func, marker_idle_func, continue_idle_func, workers_data_ptrs);
334
335         mono_counters_register ("# workers finished", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_workers_num_finished);
336 }
337
338 void
339 sgen_workers_start_all_workers (SgenObjectOperations *object_ops)
340 {
341         if (!collection_needs_workers ())
342                 return;
343
344         idle_func_object_ops = object_ops;
345         mono_memory_write_barrier ();
346
347         workers_signal_enqueue_work (FALSE);
348 }
349
350 void
351 sgen_workers_join (void)
352 {
353         int i;
354
355         SGEN_ASSERT (0, workers_state != STATE_NURSERY_COLLECTION, "Can't be in nursery collection when joining");
356
357         if (!collection_needs_workers ())
358                 return;
359
360         sgen_thread_pool_wait_for_all_jobs ();
361         sgen_thread_pool_idle_wait ();
362         SGEN_ASSERT (0, workers_state == STATE_NOT_WORKING, "Can only signal enqueue work when in no work state");
363
364         /* At this point all the workers have stopped. */
365
366         SGEN_ASSERT (0, sgen_section_gray_queue_is_empty (&workers_distribute_gray_queue), "Why is there still work left to do?");
367         for (i = 0; i < workers_num; ++i)
368                 SGEN_ASSERT (0, sgen_gray_object_queue_is_empty (&workers_data [i].private_gray_queue), "Why is there still work left to do?");
369 }
370
371 gboolean
372 sgen_workers_all_done (void)
373 {
374         return workers_state == STATE_NOT_WORKING;
375 }
376
377 /* Must only be used for debugging */
378 gboolean
379 sgen_workers_are_working (void)
380 {
381         return state_is_working_or_enqueued (workers_state);
382 }
383
384 void
385 sgen_workers_wait (void)
386 {
387         sgen_thread_pool_idle_wait ();
388         SGEN_ASSERT (0, sgen_workers_all_done (), "Why are the workers not done after we wait for them?");
389 }
390
391 SgenSectionGrayQueue*
392 sgen_workers_get_distribute_section_gray_queue (void)
393 {
394         return &workers_distribute_gray_queue;
395 }
396
397 #endif