| 1 | /* dex-fiber-private.h |
| 2 | * |
| 3 | * Copyright 2022 Christian Hergert <[email protected]> |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU Lesser General Public License as |
| 7 | * published by the Free Software Foundation; either version 2.1 of the |
| 8 | * License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * SPDX-License-Identifier: LGPL-2.1-or-later |
| 19 | */ |
| 20 | |
| 21 | #pragma once |
| 22 | |
| 23 | #ifndef PACKAGE_VERSION |
| 24 | # error "config.h must be included before dex-fiber-private.h" |
| 25 | #endif |
| 26 | |
| 27 | #include <glib.h> |
| 28 | |
| 29 | #ifdef G_OS_UNIX |
| 30 | # include "dex-ucontext-private.h" |
| 31 | #endif |
| 32 | |
| 33 | #include "dex-fiber.h" |
| 34 | #include "dex-fiber-context-private.h" |
| 35 | #include "dex-future-private.h" |
| 36 | #include "dex-scheduler.h" |
| 37 | #include "dex-stack-private.h" |
| 38 | |
| 39 | G_BEGIN_DECLS |
| 40 | |
| 41 | typedef struct _DexFiberScheduler DexFiberScheduler; |
| 42 | |
| 43 | enum { |
| 44 | QUEUE_NONE, |
| 45 | QUEUE_RUNNABLE, |
| 46 | QUEUE_BLOCKED, |
| 47 | LAST_QUEUE |
| 48 | }; |
| 49 | |
| 50 | struct _DexFiber |
| 51 | { |
| 52 | DexFuture parent_instance; |
| 53 | |
| 54 | /* Augmented link placed in either runnable or waiting queue |
| 55 | * of the DexFiberScheduler. |
| 56 | */ |
| 57 | GList link; |
| 58 | |
| 59 | /* Various flags for the fiber */ |
| 60 | guint running : 1; |
| 61 | guint runnable : 1; |
| 62 | guint exited : 1; |
| 63 | guint released : 1; |
| 64 | guint cancelled : 1; |
| 65 | guint queue : 2; |
| 66 | |
| 67 | /* The requested stack size */ |
| 68 | gsize stack_size; |
| 69 | |
| 70 | #ifdef HAVE_SYSPROF |
| 71 | /* Entry time used to emit a duration mark when the fiber exits. */ |
| 72 | gint64 begin_time; |
| 73 | #endif |
| 74 | |
| 75 | /* The assigned stack */ |
| 76 | DexStack *stack; |
| 77 | |
| 78 | /* The scheduler affinity */ |
| 79 | DexFiberScheduler *fiber_scheduler; |
| 80 | |
| 81 | /* Origin function/data for the fiber */ |
| 82 | DexFiberFunc func; |
| 83 | gpointer func_data; |
| 84 | GDestroyNotify func_data_destroy; |
| 85 | |
| 86 | /* Used to hook into the fiber during creation */ |
| 87 | DexFiberContextStart hook; |
| 88 | |
| 89 | /* The saved context for switching. This is abstracted in |
| 90 | * dex-fiber-context-private.h for the particular platform |
| 91 | * and alignment constraints. |
| 92 | */ |
| 93 | DexFiberContext context; |
| 94 | }; |
| 95 | |
| 96 | struct _DexFiberScheduler |
| 97 | { |
| 98 | GSource source; |
| 99 | |
| 100 | /* All fibers are in either runnable or blocked. A pointer to the |
| 101 | * running fiber (which is also in runnable until it yields) is |
| 102 | * provided to allow getting the current fiber from internal code. |
| 103 | */ |
| 104 | GMutex mutex; |
| 105 | DexFiber *running; |
| 106 | GQueue runnable; |
| 107 | GQueue blocked; |
| 108 | |
| 109 | /* Pooling of unused thread stacks */ |
| 110 | DexStackPool *stack_pool; |
| 111 | |
| 112 | /* The saved context for the thread, which we return to when a |
| 113 | * fiber yields back to the scheduler. |
| 114 | */ |
| 115 | DexFiberContext context; |
| 116 | |
| 117 | /* We delay initialization until first iteration so that we don't |
| 118 | * have to call ConvertThreadToFiber() from _start() (as that fails |
| 119 | * on wine64). |
| 120 | */ |
| 121 | guint has_initialized : 1; |
| 122 | }; |
| 123 | |
| 124 | DexFiberScheduler *dex_fiber_scheduler_new (void); |
| 125 | DexFiber *dex_fiber_new (DexFiberFunc func, |
| 126 | gpointer func_data, |
| 127 | GDestroyNotify func_data_destroy, |
| 128 | gsize stack_size); |
| 129 | void dex_fiber_scheduler_register (DexFiberScheduler *fiber_scheduler, |
| 130 | DexFiber *fiber); |
| 131 | |
| 132 | G_END_DECLS |