Debian Codegraph @

src/dex-thread-pool-worker.c

raw symbols SCIP
1 /*
2 * dex-thread-pool-worker.c
3 *
4 * Copyright 2022 Christian Hergert <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 #include "config.h"
23
24 #include <stdatomic.h>
25
26 #include "dex-aio-backend-private.h"
27 #include "dex-compat-private.h"
28 #include "dex-fiber-private.h"
29 #include "dex-coroutine-private.h"
30 #include "dex-posix-aio-backend-private.h"
31 #include "dex-thread-pool-worker-private.h"
32 #include "dex-thread-storage-private.h"
33 #include "dex-work-stealing-queue-private.h"
34 #include "dex-work-queue-private.h"
35
36 typedef enum _DexThreadPoolWorkerStatus
37 {
38 DEX_THREAD_POOL_WORKER_INITIAL,
39 DEX_THREAD_POOL_WORKER_RUNNING,
40 DEX_THREAD_POOL_WORKER_STOPPING,
41 DEX_THREAD_POOL_WORKER_FINISHED,
42 } DexThreadPoolWorkerStatus;
43
44 struct _DexThreadPoolWorker
45 {
46 DexScheduler parent_instance;
47
48 GList set_link;
49 DexThreadPoolWorkerSet *set;
50
51 GThread *thread;
52 GMainContext *main_context;
53 GMainLoop *main_loop;
54 DexAioContext *aio_context;
55 DexWorkQueue *global_work_queue;
56 DexWorkStealingQueue *work_stealing_queue;
57 GSource *set_source;
58 GSource *local_source;
59 GSource *fiber_scheduler;
60 GSource *coroutine_scheduler;
61
62 GMutex setup_mutex;
63 GCond setup_cond;
64
65 _Atomic int steal_requested;
66 guint steal_cursor;
67
68 DexThreadPoolWorkerStatus status : 2;
69 guint force_create : 1;
70 };
71
72 typedef struct _DexThreadPoolWorkerClass
73 {
74 DexSchedulerClass parent_class;
75 } DexThreadPoolWorkerClass;
76
77 DEX_DEFINE_FINAL_TYPE (DexThreadPoolWorker, dex_thread_pool_worker, DEX_TYPE_SCHEDULER)
78
79 #undef DEX_TYPE_THREAD_POOL_WORKER
80 #define DEX_TYPE_THREAD_POOL_WORKER dex_thread_pool_worker_type
81
82 static void dex_thread_pool_worker_set_add (DexThreadPoolWorkerSet *set,
83 DexThreadPoolWorker *thread_pool_worker);
84 static void dex_thread_pool_worker_set_remove (DexThreadPoolWorkerSet *set,
85 DexThreadPoolWorker *thread_pool_worker);
86 static gboolean dex_thread_pool_worker_set_request_one (DexThreadPoolWorkerSet *set,
87 DexThreadPoolWorker *producer);
88 static GSource *dex_thread_pool_worker_set_create_source (DexThreadPoolWorkerSet *set,
89 DexThreadPoolWorker *thread_pool_worker);
90
91 static gboolean
92 dex_thread_pool_worker_work_item_cb (gpointer user_data)
93 {
94 DexWorkItem *work_item = user_data;
95 dex_work_item_invoke (work_item);
96 return G_SOURCE_REMOVE;
97 }
98
99 static void
100 dex_thread_pool_worker_push (DexScheduler *scheduler,
101 DexWorkItem work_item)
102 {
103 DexThreadPoolWorker *thread_pool_worker = DEX_THREAD_POOL_WORKER (scheduler);
104
105 g_assert (DEX_IS_THREAD_POOL_WORKER (thread_pool_worker));
106
107 if G_LIKELY (g_thread_self () == thread_pool_worker->thread &&
108 thread_pool_worker->status == DEX_THREAD_POOL_WORKER_RUNNING)
109 {
110 dex_work_stealing_queue_push (thread_pool_worker->work_stealing_queue, work_item);
111
112 if (dex_work_stealing_queue_size (thread_pool_worker->work_stealing_queue) > 1)
113 dex_thread_pool_worker_set_request_one (thread_pool_worker->set, thread_pool_worker);
114 }
115 else
116 {
117 GSource *source;
118
119 /* Pushing a work item directly onto a worker is generally going
120 * to be related to completing work items. Treat those as extremely
121 * high priority as they will delay further processing of futures.
122 *
123 * This is currently a workaround to improve the situation with
124 * issue #17 when we're on fallback configurations. If we want
125 * to improve that situation further, we can reduce some overhead
126 * here by creating a dedicated GSource like DexMainScheduler does
127 * for work items instead of a 1:1 GSource creation.
128 *
129 * But this at least improves the situation immediately that is
130 * causing potential lock-ups.
131 */
132
133 source = g_idle_source_new ();
134 g_source_set_priority (source, G_MININT);
135 g_source_set_callback (source,
136 dex_thread_pool_worker_work_item_cb,
137 g_memdup2 (&work_item, sizeof work_item),
138 g_free);
139 g_source_attach (source, thread_pool_worker->main_context);
140 g_source_unref (source);
141 }
142 }
143
144 static gboolean
145 dex_thread_pool_worker_finalize_cb (gpointer data)
146 {
147 DexThreadPoolWorker *thread_pool_worker = DEX_THREAD_POOL_WORKER (data);
148 DexWorkItem work_item;
149
150 /* First set the status to ensure that our check/dispatch will bail */
151 thread_pool_worker->status = DEX_THREAD_POOL_WORKER_STOPPING;
152 g_main_loop_quit (thread_pool_worker->main_loop);
153
154 /* Now flush out the rest of the work items if there are any */
155 while (dex_work_stealing_queue_pop (thread_pool_worker->work_stealing_queue, &work_item))
156 dex_work_item_invoke (&work_item);
157
158 return G_SOURCE_REMOVE;
159 }
160
161 static void
162 dex_thread_pool_worker_finalize (DexObject *object)
163 {
164 DexThreadPoolWorker *thread_pool_worker = DEX_THREAD_POOL_WORKER (object);
165 GSource *idle_source;
166
167 g_assert (thread_pool_worker->thread != g_thread_self ());
168
169 /* To finalize the worker, we need to push a work item to the thread
170 * that will cause it to stop processing and then wait for the thread
171 * to exit after processing the rest of the queue. We change the finalizing
172 * bit on the worker thread so the common case (it's FALSE) doesn't require
173 * any sort of synchronization/atomic operations every loop.
174 */
175 idle_source = g_idle_source_new ();
176 _g_source_set_static_name (idle_source, "[dex-thread-pool-worker-finalize]");
177 g_source_set_priority (idle_source, G_MININT);
178 g_source_set_callback (idle_source,
179 dex_thread_pool_worker_finalize_cb,
180 thread_pool_worker, NULL);
181 g_source_attach (idle_source, thread_pool_worker->main_context);
182 g_source_unref (idle_source);
183
184 /* Now wait for the thread to process items and exit the thread */
185 g_thread_join (thread_pool_worker->thread);
186
187 #ifdef G_ENABLE_DEBUG
188 atomic_thread_fence (memory_order_seq_cst);
189
190 g_assert (thread_pool_worker->status == DEX_THREAD_POOL_WORKER_FINISHED);
191 g_assert (dex_work_stealing_queue_empty (thread_pool_worker->work_stealing_queue));
192 #endif
193
194 /* These are all destroyed during thread shutdown */
195 g_clear_pointer (&thread_pool_worker->set_source, g_source_unref);
196 g_clear_pointer (&thread_pool_worker->local_source, g_source_unref);
197 g_clear_pointer (&thread_pool_worker->fiber_scheduler, g_source_unref);
198 g_clear_pointer (&thread_pool_worker->coroutine_scheduler, g_source_unref);
199
200 g_clear_pointer (&thread_pool_worker->thread, g_thread_unref);
201 g_clear_pointer (&thread_pool_worker->main_context, g_main_context_unref);
202 g_clear_pointer (&thread_pool_worker->main_loop, g_main_loop_unref);
203 g_clear_pointer (&thread_pool_worker->work_stealing_queue, dex_work_stealing_queue_unref);
204
205 dex_clear (&thread_pool_worker->global_work_queue);
206 g_clear_pointer (&thread_pool_worker->set, dex_thread_pool_worker_set_unref);
207
208 g_assert (thread_pool_worker->set_link.prev == NULL);
209 g_assert (thread_pool_worker->set_link.next == NULL);
210
211 g_mutex_clear (&thread_pool_worker->setup_mutex);
212 g_cond_clear (&thread_pool_worker->setup_cond);
213
214 DEX_OBJECT_CLASS (dex_thread_pool_worker_parent_class)->finalize (object);
215 }
216
217 static GMainContext *
218 dex_thread_pool_worker_get_main_context (DexScheduler *scheduler)
219 {
220 DexThreadPoolWorker *thread_pool_worker = DEX_THREAD_POOL_WORKER (scheduler);
221
222 g_assert (DEX_IS_THREAD_POOL_WORKER (thread_pool_worker));
223
224 return thread_pool_worker->main_context;
225 }
226
227 static DexAioContext *
228 dex_thread_pool_worker_get_aio_context (DexScheduler *scheduler)
229 {
230 DexThreadPoolWorker *thread_pool_worker = DEX_THREAD_POOL_WORKER (scheduler);
231
232 g_assert (DEX_IS_THREAD_POOL_WORKER (thread_pool_worker));
233
234 return thread_pool_worker->aio_context;
235 }
236
237 static void
238 dex_thread_pool_worker_spawn (DexScheduler *scheduler,
239 DexFiber *fiber)
240 {
241 DexThreadPoolWorker *thread_pool_worker = DEX_THREAD_POOL_WORKER (scheduler);
242
243 g_assert (DEX_IS_THREAD_POOL_WORKER (thread_pool_worker));
244
245 dex_fiber_scheduler_register ((DexFiberScheduler *)thread_pool_worker->fiber_scheduler, fiber);
246 }
247
248 static void
249 dex_thread_pool_worker_spawn_coroutine (DexScheduler *scheduler,
250 DexCoroutine *coroutine)
251 {
252 DexThreadPoolWorker *thread_pool_worker = DEX_THREAD_POOL_WORKER (scheduler);
253
254 g_assert (DEX_IS_THREAD_POOL_WORKER (thread_pool_worker));
255
256 dex_coroutine_scheduler_register ((DexCoroutineScheduler *)thread_pool_worker->coroutine_scheduler,
257 coroutine);
258 }
259
260 static void
261 dex_thread_pool_worker_class_init (DexThreadPoolWorkerClass *thread_pool_worker_class)
262 {
263 DexObjectClass *object_class = DEX_OBJECT_CLASS (thread_pool_worker_class);
264 DexSchedulerClass *scheduler_class = DEX_SCHEDULER_CLASS (thread_pool_worker_class);
265
266 object_class->finalize = dex_thread_pool_worker_finalize;
267
268 scheduler_class->push = dex_thread_pool_worker_push;
269 scheduler_class->get_main_context = dex_thread_pool_worker_get_main_context;
270 scheduler_class->spawn = dex_thread_pool_worker_spawn;
271 scheduler_class->spawn_coroutine = dex_thread_pool_worker_spawn_coroutine;
272 scheduler_class->get_aio_context = dex_thread_pool_worker_get_aio_context;
273 }
274
275 static void
276 dex_thread_pool_worker_init (DexThreadPoolWorker *thread_pool_worker)
277 {
278 thread_pool_worker->set_link.data = thread_pool_worker;
279
280 g_mutex_init (&thread_pool_worker->setup_mutex);
281 g_cond_init (&thread_pool_worker->setup_cond);
282 }
283
284 static gpointer
285 dex_thread_pool_worker_thread_func (gpointer data)
286 {
287 DexThreadPoolWorker *thread_pool_worker = DEX_THREAD_POOL_WORKER (data);
288 DexThreadStorage *storage = dex_thread_storage_get ();
289 DexAioBackend *aio_backend;
290 DexAioContext *aio_context;
291 DexFuture *global_work_queue_loop;
292 GSource *source;
293
294 g_mutex_lock (&thread_pool_worker->setup_mutex);
295
296 aio_backend = dex_aio_backend_get_default ();
297 aio_context = dex_aio_backend_create_context (aio_backend);
298
299 /* Try a fallback aio-backend (POSIX) if we _MUST_ create a
300 * worker and our preferred failed for some reason.
301 */
302 if (aio_context == NULL && thread_pool_worker->force_create)
303 {
304 aio_backend = dex_posix_aio_backend_get_fallback ();
305 aio_context = dex_aio_backend_create_context (aio_backend);
306 }
307
308 /* If we fail to setup an AIO context, then there is no point in
309 * adding this thread pool worker. Just bail immediately and notify
310 * the creator of the issue.
311 */
312 if (aio_context == NULL)
313 {
314 thread_pool_worker->status = DEX_THREAD_POOL_WORKER_FINISHED;
315 g_cond_signal (&thread_pool_worker->setup_cond);
316 g_mutex_unlock (&thread_pool_worker->setup_mutex);
317 return NULL;
318 }
319
320 thread_pool_worker->aio_context = g_steal_pointer (&aio_context);
321
322 /* Attach our AIO source to complete AIO work */
323 g_source_attach ((GSource *)thread_pool_worker->aio_context,
324 thread_pool_worker->main_context);
325
326 /* Attach a GSource that will process items from the worker threads
327 * work queue.
328 */
329 source = dex_work_stealing_queue_create_source (thread_pool_worker->work_stealing_queue);
330 g_source_set_priority (source, G_PRIORITY_DEFAULT);
331 g_source_attach (source, thread_pool_worker->main_context);
332 thread_pool_worker->local_source = g_steal_pointer (&source);
333
334 /* Create a source to steal work items from other thread pool threads.
335 * This is slightly higher priority than the global queue because we
336 * want to steal items from the peers before the global queue.
337 */
338 source = dex_thread_pool_worker_set_create_source (thread_pool_worker->set, thread_pool_worker);
339 g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE-1);
340 g_source_attach (source, thread_pool_worker->main_context);
341 thread_pool_worker->set_source = g_steal_pointer (&source);
342
343 /* Setup fiber scheduler source */
344 source = (GSource *)dex_fiber_scheduler_new ();
345 g_source_attach (source, thread_pool_worker->main_context);
346 thread_pool_worker->fiber_scheduler = g_steal_pointer (&source);
347
348 /* Setup coroutine scheduler source */
349 source = (GSource *)dex_coroutine_scheduler_new ();
350 g_source_attach (source, thread_pool_worker->main_context);
351 thread_pool_worker->coroutine_scheduler = g_steal_pointer (&source);
352
353 storage->scheduler = DEX_SCHEDULER (thread_pool_worker);
354 storage->worker = thread_pool_worker;
355 storage->aio_context = thread_pool_worker->aio_context;
356
357 g_main_context_push_thread_default (thread_pool_worker->main_context);
358 thread_pool_worker->status = DEX_THREAD_POOL_WORKER_RUNNING;
359
360 /* Add to set so others may steal work items from us */
361 dex_thread_pool_worker_set_add (thread_pool_worker->set, thread_pool_worker);
362
363 /* Async processing global work-queue items until we're told to shutdown */
364 global_work_queue_loop = dex_work_queue_run (thread_pool_worker->global_work_queue);
365
366 /* Notify the caller that we are all setup */
367 g_cond_signal (&thread_pool_worker->setup_cond);
368 g_mutex_unlock (&thread_pool_worker->setup_mutex);
369
370 /* Process main context until we are told to shutdown */
371 g_main_loop_run (thread_pool_worker->main_loop);
372
373 /* Discard our work queue loop */
374 dex_clear (&global_work_queue_loop);
375
376 /* Flush out any pending operations */
377 while (g_main_context_pending (thread_pool_worker->main_context))
378 g_main_context_iteration (thread_pool_worker->main_context, FALSE);
379
380 /* Remove from set now so others will not try to steal from us */
381 dex_thread_pool_worker_set_remove (thread_pool_worker->set, thread_pool_worker);
382
383 /* Ensure our sources will not continue on */
384 g_source_destroy (thread_pool_worker->set_source);
385 g_source_destroy (thread_pool_worker->local_source);
386 g_source_destroy (thread_pool_worker->fiber_scheduler);
387 g_source_destroy (thread_pool_worker->coroutine_scheduler);
388
389 thread_pool_worker->status = DEX_THREAD_POOL_WORKER_FINISHED;
390 g_main_context_pop_thread_default (thread_pool_worker->main_context);
391
392 storage->worker = NULL;
393 storage->scheduler = NULL;
394 storage->aio_context = NULL;
395
396 return NULL;
397 }
398
399 typedef struct _DexThreadPoolWorkerSet
400 {
401 GQueue queue;
402 GRWLock rwlock;
403 _Atomic guint wake_cursor;
404 } DexThreadPoolWorkerSet;
405
406 DexThreadPoolWorkerSet *
407 dex_thread_pool_worker_set_new (void)
408 {
409 DexThreadPoolWorkerSet *set;
410
411 set = g_atomic_rc_box_new0 (DexThreadPoolWorkerSet);
412 g_rw_lock_init (&set->rwlock);
413
414 return set;
415 }
416
417 static void
418 dex_thread_pool_worker_set_add (DexThreadPoolWorkerSet *set,
419 DexThreadPoolWorker *thread_pool_worker)
420 {
421 g_return_if_fail (set != NULL);
422 g_return_if_fail (DEX_IS_THREAD_POOL_WORKER (thread_pool_worker));
423 g_return_if_fail (thread_pool_worker->set_link.prev == NULL);
424 g_return_if_fail (thread_pool_worker->set_link.next == NULL);
425
426 g_rw_lock_writer_lock (&set->rwlock);
427 g_queue_push_tail_link (&set->queue, &thread_pool_worker->set_link);
428 g_rw_lock_writer_unlock (&set->rwlock);
429 }
430
431 static void
432 dex_thread_pool_worker_set_remove (DexThreadPoolWorkerSet *set,
433 DexThreadPoolWorker *thread_pool_worker)
434 {
435 g_return_if_fail (set != NULL);
436 g_return_if_fail (DEX_IS_THREAD_POOL_WORKER (thread_pool_worker));
437
438 g_rw_lock_writer_lock (&set->rwlock);
439 g_queue_unlink (&set->queue, &thread_pool_worker->set_link);
440 g_rw_lock_writer_unlock (&set->rwlock);
441 }
442
443 DexThreadPoolWorkerSet *
444 dex_thread_pool_worker_set_ref (DexThreadPoolWorkerSet *set)
445 {
446 g_atomic_rc_box_acquire (set);
447 return set;
448 }
449
450 static void
451 dex_thread_pool_worker_set_finalize (gpointer data)
452 {
453 DexThreadPoolWorkerSet *set = data;
454
455 g_assert (set->queue.length == 0);
456
457 g_rw_lock_clear (&set->rwlock);
458 }
459
460 void
461 dex_thread_pool_worker_set_unref (DexThreadPoolWorkerSet *set)
462 {
463 g_atomic_rc_box_release_full (set, dex_thread_pool_worker_set_finalize);
464 }
465
466 static gboolean
467 dex_thread_pool_worker_set_request_one (DexThreadPoolWorkerSet *set,
468 DexThreadPoolWorker *producer)
469 {
470 GMainContext *context = NULL;
471 DexThreadPoolWorker *fallback = NULL;
472 const GList *iter;
473 guint start;
474 guint length;
475
476 g_assert (set != NULL);
477 g_assert (DEX_IS_THREAD_POOL_WORKER (producer));
478
479 g_rw_lock_reader_lock (&set->rwlock);
480
481 length = set->queue.length;
482
483 if (length < 2)
484 goto unlock;
485
486 start = atomic_fetch_add_explicit (&set->wake_cursor, 1, memory_order_relaxed) % length;
487 iter = g_queue_peek_nth_link (&set->queue, start);
488
489 for (guint i = 0; i < length; i++)
490 {
491 DexThreadPoolWorker *candidate = iter->data;
492 int expected = FALSE;
493
494 iter = iter->next != NULL ? iter->next : set->queue.head;
495
496 if (candidate == producer)
497 continue;
498
499 if (!dex_work_stealing_queue_empty (candidate->work_stealing_queue))
500 {
501 if (fallback == NULL &&
502 !atomic_load_explicit (&candidate->steal_requested,
503 memory_order_relaxed))
504 fallback = candidate;
505
506 continue;
507 }
508
509 if (atomic_compare_exchange_strong_explicit (&candidate->steal_requested,
510 &expected,
511 TRUE,
512 memory_order_release,
513 memory_order_relaxed))
514 {
515 context = g_main_context_ref (candidate->main_context);
516 break;
517 }
518 }
519
520 if (context == NULL && fallback != NULL)
521 {
522 int expected = FALSE;
523
524 if (atomic_compare_exchange_strong_explicit (&fallback->steal_requested,
525 &expected,
526 TRUE,
527 memory_order_release,
528 memory_order_relaxed))
529 context = g_main_context_ref (fallback->main_context);
530 }
531
532 unlock:
533 g_rw_lock_reader_unlock (&set->rwlock);
534
535 if (context != NULL)
536 {
537 g_main_context_wakeup (context);
538 g_main_context_unref (context);
539 return TRUE;
540 }
541
542 return FALSE;
543 }
544
545 static gboolean
546 dex_thread_pool_worker_set_try_steal (DexThreadPoolWorkerSet *set,
547 DexThreadPoolWorker *thief,
548 DexWorkItem *out_work_item,
549 gboolean *out_more_work)
550 {
551 const GList *iter;
552 gboolean stolen = FALSE;
553 guint start;
554 guint length;
555
556 g_assert (set != NULL);
557 g_assert (DEX_IS_THREAD_POOL_WORKER (thief));
558 g_assert (out_work_item != NULL);
559 g_assert (out_more_work != NULL);
560
561 *out_more_work = FALSE;
562
563 g_rw_lock_reader_lock (&set->rwlock);
564
565 length = set->queue.length;
566
567 if (length < 2)
568 goto unlock;
569
570 start = thief->steal_cursor % length;
571 iter = g_queue_peek_nth_link (&set->queue, start);
572
573 for (guint i = 0; i < length; i++)
574 {
575 DexThreadPoolWorker *victim = iter->data;
576
577 iter = iter->next != NULL ? iter->next : set->queue.head;
578
579 if (victim == thief)
580 continue;
581
582 if (!stolen &&
583 dex_work_stealing_queue_steal (victim->work_stealing_queue, out_work_item))
584 {
585 thief->steal_cursor = (start + i + 1) % length;
586 stolen = TRUE;
587 }
588
589 if (stolen &&
590 !dex_work_stealing_queue_empty (victim->work_stealing_queue))
591 *out_more_work = TRUE;
592 }
593
594 unlock:
595 g_rw_lock_reader_unlock (&set->rwlock);
596
597 return stolen;
598 }
599
600 typedef struct _DexThreadPoolWorkerSetSource
601 {
602 GSource parent_source;
603 DexThreadPoolWorkerSet *set;
604 DexThreadPoolWorker *thread_pool_worker;
605 } DexThreadPoolWorkerSetSource;
606
607 static gboolean
608 dex_thread_pool_worker_set_source_prepare (GSource *source,
609 int *timeout)
610 {
611 DexThreadPoolWorkerSetSource *real_source = (DexThreadPoolWorkerSetSource *)source;
612
613 *timeout = -1;
614
615 return atomic_load_explicit (&real_source->thread_pool_worker->steal_requested,
616 memory_order_acquire);
617 }
618
619 static gboolean
620 dex_thread_pool_worker_set_source_check (GSource *source)
621 {
622 DexThreadPoolWorkerSetSource *real_source = (DexThreadPoolWorkerSetSource *)source;
623
624 return atomic_load_explicit (&real_source->thread_pool_worker->steal_requested,
625 memory_order_acquire);
626 }
627
628 static gboolean
629 dex_thread_pool_worker_set_source_dispatch (GSource *source,
630 GSourceFunc callback,
631 gpointer callback_data)
632 {
633 DexThreadPoolWorkerSetSource *real_source = (DexThreadPoolWorkerSetSource *)source;
634 DexThreadPoolWorker *thread_pool_worker = real_source->thread_pool_worker;
635 DexWorkItem work_item;
636 gboolean more_work = FALSE;
637
638 if (!atomic_exchange_explicit (&thread_pool_worker->steal_requested,
639 FALSE,
640 memory_order_acq_rel))
641 return G_SOURCE_CONTINUE;
642
643 if (thread_pool_worker->status != DEX_THREAD_POOL_WORKER_RUNNING)
644 return G_SOURCE_CONTINUE;
645
646 if (dex_thread_pool_worker_set_try_steal (real_source->set,
647 thread_pool_worker,
648 &work_item,
649 &more_work))
650 {
651 dex_work_item_invoke (&work_item);
652
653 if (more_work)
654 {
655 int expected = FALSE;
656
657 atomic_compare_exchange_strong_explicit (&thread_pool_worker->steal_requested,
658 &expected,
659 TRUE,
660 memory_order_release,
661 memory_order_relaxed);
662 dex_thread_pool_worker_set_request_one (real_source->set, thread_pool_worker);
663 }
664 }
665
666 return G_SOURCE_CONTINUE;
667 }
668
669 static GSourceFuncs dex_thread_pool_worker_set_source_funcs = {
670 .prepare = dex_thread_pool_worker_set_source_prepare,
671 .check = dex_thread_pool_worker_set_source_check,
672 .dispatch = dex_thread_pool_worker_set_source_dispatch,
673 };
674
675 static GSource *
676 dex_thread_pool_worker_set_create_source (DexThreadPoolWorkerSet *set,
677 DexThreadPoolWorker *thread_pool_worker)
678 {
679 DexThreadPoolWorkerSetSource *source;
680
681 g_return_val_if_fail (set != NULL, NULL);
682 g_return_val_if_fail (DEX_IS_THREAD_POOL_WORKER (thread_pool_worker), NULL);
683
684 source = (DexThreadPoolWorkerSetSource *)
685 g_source_new (&dex_thread_pool_worker_set_source_funcs, sizeof *source);
686 _g_source_set_static_name ((GSource *)source, "[dex-thread-pool-worker-set]");
687 source->set = set;
688 source->thread_pool_worker = thread_pool_worker;
689
690 return (GSource *)source;
691 }
692
693 DexThreadPoolWorker *
694 dex_thread_pool_worker_new (DexWorkQueue *work_queue,
695 DexThreadPoolWorkerSet *set,
696 gboolean force_create)
697 {
698 DexThreadPoolWorker *thread_pool_worker;
699 gboolean failed;
700
701 g_return_val_if_fail (work_queue != NULL, NULL);
702 g_return_val_if_fail (set != NULL, NULL);
703
704 thread_pool_worker = (DexThreadPoolWorker *)dex_object_create_instance (DEX_TYPE_THREAD_POOL_WORKER);
705 thread_pool_worker->main_context = g_main_context_new ();
706 thread_pool_worker->main_loop = g_main_loop_new (thread_pool_worker->main_context, FALSE);
707 thread_pool_worker->global_work_queue = dex_ref (work_queue);
708 thread_pool_worker->work_stealing_queue = dex_work_stealing_queue_new (256);
709 thread_pool_worker->set = dex_thread_pool_worker_set_ref (set);
710 thread_pool_worker->force_create = !!force_create;
711
712 /* Now spawn our thread to process events via GSource */
713 g_mutex_lock (&thread_pool_worker->setup_mutex);
714 thread_pool_worker->thread = g_thread_new ("dex-thread-pool-worker",
715 dex_thread_pool_worker_thread_func,
716 thread_pool_worker);
717 g_cond_wait (&thread_pool_worker->setup_cond, &thread_pool_worker->setup_mutex);
718 failed = thread_pool_worker->status == DEX_THREAD_POOL_WORKER_FINISHED;
719 g_mutex_unlock (&thread_pool_worker->setup_mutex);
720
721 if (failed)
722 dex_clear (&thread_pool_worker);
723
724 return thread_pool_worker;
725 }