| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#pragma once |
| 22 |
|
| 23 |
#include <errno.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include <glib.h> |
| 27 |
|
| 28 |
#include "dex-compat-private.h" |
| 29 |
#include "dex-platform.h" |
| 30 |
#include "dex-stack-private.h" |
| 31 |
|
| 32 |
#ifdef G_OS_UNIX |
| 33 |
# include "dex-ucontext-private.h" |
| 34 |
#endif |
| 35 |
|
| 36 |
#ifdef G_OS_WIN32 |
| 37 |
# include <windows.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
G_BEGIN_DECLS |
| 41 |
|
| 42 |
typedef struct _DexFiberContextStart |
| 43 |
{ |
| 44 |
GHookFunc func; |
| 45 |
gpointer data; |
| 46 |
} DexFiberContextStart; |
| 47 |
|
| 48 |
#ifdef G_OS_UNIX |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
#if ALIGN_OF_UCONTEXT > GLIB_SIZEOF_VOID_P |
| 59 |
typedef ucontext_t *DexFiberContext; |
| 60 |
#else |
| 61 |
typedef ucontext_t DexFiberContext; |
| 62 |
#endif |
| 63 |
|
| 64 |
static inline void |
| 65 |
dex_fiber_context_switch (DexFiberContext *old_context, |
| 66 |
DexFiberContext *new_context); |
| 67 |
|
| 68 |
static inline void |
| 69 |
dex_fiber_context_start (guint start_lo, |
| 70 |
guint start_hi) |
| 71 |
{ |
| 72 |
union { |
| 73 |
guintptr uval; |
| 74 |
DexFiberContextStart *ptr; |
| 75 |
} u; |
| 76 |
|
| 77 |
u.uval = 0; |
| 78 |
#if GLIB_SIZEOF_VOID_P == 8 |
| 79 |
u.uval |= start_hi; |
| 80 |
u.uval <<= 32; |
| 81 |
#endif |
| 82 |
u.uval |= start_lo; |
| 83 |
|
| 84 |
u.ptr->func (u.ptr->data); |
| 85 |
} |
| 86 |
|
| 87 |
static inline void |
| 88 |
_dex_fiber_context_makecontext (ucontext_t *ucontext, |
| 89 |
DexStack *stack, |
| 90 |
DexFiberContextStart *start) |
| 91 |
{ |
| 92 |
guint start_lo; |
| 93 |
guint start_hi = 0; |
| 94 |
|
| 95 |
ucontext->uc_stack.ss_size = stack->size; |
| 96 |
ucontext->uc_stack.ss_sp = stack->ptr; |
| 97 |
ucontext->uc_link = 0; |
| 98 |
|
| 99 |
#if GLIB_SIZEOF_VOID_P == 8 |
| 100 |
start_lo = GPOINTER_TO_SIZE (start) & 0xFFFFFFFFF; |
| 101 |
start_hi = (GPOINTER_TO_SIZE (start) >> 32) & 0xFFFFFFFFF; |
| 102 |
#else |
| 103 |
start_lo = GPOINTER_TO_SIZE (start); |
| 104 |
start_hi = 0; |
| 105 |
#endif |
| 106 |
|
| 107 |
G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
| 108 |
makecontext (ucontext, |
| 109 |
G_CALLBACK (dex_fiber_context_start), |
| 110 |
2, start_lo, start_hi); |
| 111 |
G_GNUC_END_IGNORE_DEPRECATIONS |
| 112 |
} |
| 113 |
|
| 114 |
static inline void |
| 115 |
dex_fiber_context_init (DexFiberContext *context, |
| 116 |
DexStack *stack, |
| 117 |
DexFiberContextStart *start) |
| 118 |
{ |
| 119 |
ucontext_t *ucontext; |
| 120 |
|
| 121 |
#if ALIGN_OF_UCONTEXT > GLIB_SIZEOF_VOID_P |
| 122 |
*context = g_aligned_alloc (1, sizeof (ucontext_t), ALIGN_OF_UCONTEXT); |
| 123 |
ucontext = *context; |
| 124 |
#else |
| 125 |
ucontext = context; |
| 126 |
#endif |
| 127 |
|
| 128 |
memset (ucontext, 0, sizeof *ucontext); |
| 129 |
|
| 130 |
G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
| 131 |
getcontext (ucontext); |
| 132 |
G_GNUC_END_IGNORE_DEPRECATIONS |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
if (stack != NULL) |
| 138 |
_dex_fiber_context_makecontext (ucontext, stack, start); |
| 139 |
} |
| 140 |
|
| 141 |
static inline void |
| 142 |
dex_fiber_context_clear (DexFiberContext *context) |
| 143 |
{ |
| 144 |
#if ALIGN_OF_UCONTEXT > GLIB_SIZEOF_VOID_P |
| 145 |
g_aligned_free (*context); |
| 146 |
#endif |
| 147 |
} |
| 148 |
|
| 149 |
static inline void |
| 150 |
dex_fiber_context_init_main (DexFiberContext *context) |
| 151 |
{ |
| 152 |
dex_fiber_context_init (context, NULL, NULL); |
| 153 |
} |
| 154 |
|
| 155 |
static inline void |
| 156 |
dex_fiber_context_clear_main (DexFiberContext *context) |
| 157 |
{ |
| 158 |
dex_fiber_context_clear (context); |
| 159 |
} |
| 160 |
|
| 161 |
static inline void |
| 162 |
dex_fiber_context_switch (DexFiberContext *old_context, |
| 163 |
DexFiberContext *new_context) |
| 164 |
{ |
| 165 |
int r; |
| 166 |
|
| 167 |
G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
| 168 |
#if ALIGN_OF_UCONTEXT > GLIB_SIZEOF_VOID_P |
| 169 |
r = swapcontext (*old_context, *new_context); |
| 170 |
#else |
| 171 |
r = swapcontext (old_context, new_context); |
| 172 |
#endif |
| 173 |
G_GNUC_END_IGNORE_DEPRECATIONS |
| 174 |
|
| 175 |
#ifndef G_DISABLE_ASSERT |
| 176 |
if G_UNLIKELY (r != 0) |
| 177 |
g_error ("swapcontext(): %s", g_strerror (errno)); |
| 178 |
#else |
| 179 |
(void)r; |
| 180 |
#endif |
| 181 |
} |
| 182 |
#endif |
| 183 |
|
| 184 |
#ifdef G_OS_WIN32 |
| 185 |
typedef LPVOID DexFiberContext; |
| 186 |
|
| 187 |
static inline void |
| 188 |
dex_fiber_context_init_main (DexFiberContext *context) |
| 189 |
{ |
| 190 |
*context = ConvertThreadToFiber (0); |
| 191 |
} |
| 192 |
|
| 193 |
static inline void |
| 194 |
dex_fiber_context_clear_main (DexFiberContext *context) |
| 195 |
{ |
| 196 |
*context = NULL; |
| 197 |
ConvertFiberToThread (); |
| 198 |
} |
| 199 |
|
| 200 |
static inline void |
| 201 |
dex_fiber_context_init (DexFiberContext *context, |
| 202 |
DexStack *stack, |
| 203 |
DexFiberContextStart *start) |
| 204 |
{ |
| 205 |
*context = CreateFiberEx (dex_get_min_stack_size (), |
| 206 |
stack->size, |
| 207 |
FIBER_FLAG_FLOAT_SWITCH, |
| 208 |
(LPFIBER_START_ROUTINE)start->func, |
| 209 |
start->data); |
| 210 |
|
| 211 |
if (*context == NULL) |
| 212 |
g_printerr ("Failed to create fiber (stack %u) (start_func %p): %s", |
| 213 |
(guint)stack->size, start->func, |
| 214 |
g_strerror (GetLastError ())); |
| 215 |
} |
| 216 |
|
| 217 |
static inline void |
| 218 |
dex_fiber_context_clear (DexFiberContext *context) |
| 219 |
{ |
| 220 |
DeleteFiber (*context); |
| 221 |
} |
| 222 |
|
| 223 |
static inline void |
| 224 |
dex_fiber_context_switch (DexFiberContext *old_context, |
| 225 |
DexFiberContext *new_context) |
| 226 |
{ |
| 227 |
SwitchToFiber (*new_context); |
| 228 |
} |
| 229 |
#endif |
| 230 |
|
| 231 |
G_END_DECLS |