| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
r |
| 23 |
|
| 24 |
|
| 25 |
p |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
#pragma once |
| 53 |
|
| 54 |
#include <stdatomic.h> |
| 55 |
|
| 56 |
#include <glib.h> |
| 57 |
|
| 58 |
#include "dex-scheduler-private.h" |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
G_BEGIN_DECLS |
| 74 |
|
| 75 |
#ifndef DEX_CACHELINE_SIZE |
| 76 |
# define DEX_CACHELINE_SIZE 64 |
| 77 |
#endif |
| 78 |
|
| 79 |
typedef struct _DexWorkStealingArray |
| 80 |
{ |
| 81 |
gint64 C; |
| 82 |
gint64 M; |
| 83 |
_Atomic(DexWorkItem) S[]; |
| 84 |
} DexWorkStealingArray; |
| 85 |
|
| 86 |
typedef struct _DexWorkStealingQueue |
| 87 |
{ |
| 88 |
_Alignas(DEX_CACHELINE_SIZE) _Atomic(gint64) top; |
| 89 |
_Alignas(DEX_CACHELINE_SIZE) _Atomic(gint64) bottom; |
| 90 |
_Alignas(DEX_CACHELINE_SIZE) _Atomic(DexWorkStealingArray*) array; |
| 91 |
GPtrArray *garbage; |
| 92 |
gatomicrefcount ref_count; |
| 93 |
} DexWorkStealingQueue; |
| 94 |
|
| 95 |
DexWorkStealingQueue *dex_work_stealing_queue_new (gint64 capacity); |
| 96 |
DexWorkStealingQueue *dex_work_stealing_queue_ref (DexWorkStealingQueue *work_stealing_queue); |
| 97 |
void dex_work_stealing_queue_unref (DexWorkStealingQueue *work_stealing_queue); |
| 98 |
GSource *dex_work_stealing_queue_create_source (DexWorkStealingQueue *work_stealing_queue); |
| 99 |
|
| 100 |
static inline DexWorkStealingArray * |
| 101 |
dex_work_stealing_array_new (gint64 c) |
| 102 |
{ |
| 103 |
DexWorkStealingArray *work_stealing_array; |
| 104 |
|
| 105 |
work_stealing_array = g_aligned_alloc0 (1, |
| 106 |
sizeof (DexWorkStealingArray) + (c * sizeof (DexWorkItem)), |
| 107 |
G_ALIGNOF (DexWorkStealingArray)); |
| 108 |
work_stealing_array->C = c; |
| 109 |
work_stealing_array->M = c-1; |
| 110 |
|
| 111 |
return work_stealing_array; |
| 112 |
} |
| 113 |
|
| 114 |
static inline gint64 |
| 115 |
dex_work_stealing_array_capacity (DexWorkStealingArray *work_stealing_array) |
| 116 |
{ |
| 117 |
return work_stealing_array->C; |
| 118 |
} |
| 119 |
|
| 120 |
static inline void |
| 121 |
dex_work_stealing_array_push (DexWorkStealingArray *work_stealing_array, |
| 122 |
gint64 i, |
| 123 |
DexWorkItem work_item) |
| 124 |
{ |
| 125 |
atomic_store_explicit (&work_stealing_array->S[i & work_stealing_array->M], |
| 126 |
work_item, |
| 127 |
memory_order_relaxed); |
| 128 |
} |
| 129 |
|
| 130 |
static inline DexWorkItem |
| 131 |
dex_work_stealing_array_pop (DexWorkStealingArray *work_stealing_array, |
| 132 |
gint64 i) |
| 133 |
{ |
| 134 |
return atomic_load_explicit (&work_stealing_array->S[i & work_stealing_array->M], |
| 135 |
memory_order_relaxed); |
| 136 |
} |
| 137 |
|
| 138 |
static inline DexWorkStealingArray * |
| 139 |
dex_work_stealing_array_resize (DexWorkStealingArray *work_stealing_array, |
| 140 |
gint64 b, |
| 141 |
gint64 t) |
| 142 |
{ |
| 143 |
DexWorkStealingArray *ptr = dex_work_stealing_array_new (work_stealing_array->C * 2); |
| 144 |
for (gint64 i = t; i != b; i++) |
| 145 |
dex_work_stealing_array_push (ptr, i, dex_work_stealing_array_pop (work_stealing_array, i)); |
| 146 |
return ptr; |
| 147 |
} |
| 148 |
|
| 149 |
static inline void |
| 150 |
dex_work_stealing_array_free (DexWorkStealingArray *work_stealing_array) |
| 151 |
{ |
| 152 |
g_aligned_free (work_stealing_array); |
| 153 |
} |
| 154 |
|
| 155 |
static inline gboolean |
| 156 |
dex_work_stealing_queue_empty (DexWorkStealingQueue *work_stealing_queue) |
| 157 |
{ |
| 158 |
gint64 b = atomic_load_explicit (&work_stealing_queue->bottom, memory_order_relaxed); |
| 159 |
gint64 t = atomic_load_explicit (&work_stealing_queue->top, memory_order_relaxed); |
| 160 |
return b <= t; |
| 161 |
} |
| 162 |
|
| 163 |
static inline gsize |
| 164 |
dex_work_stealing_queue_size (DexWorkStealingQueue *work_stealing_queue) |
| 165 |
{ |
| 166 |
gint64 b = atomic_load_explicit (&work_stealing_queue->bottom, memory_order_relaxed); |
| 167 |
gint64 t = atomic_load_explicit (&work_stealing_queue->top, memory_order_relaxed); |
| 168 |
return b >= t ? b - t : 0; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
static inline void |
| 186 |
dex_work_stealing_queue_push (DexWorkStealingQueue *work_stealing_queue, |
| 187 |
DexWorkItem work_item) |
| 188 |
{ |
| 189 |
gint64 b = atomic_load_explicit (&work_stealing_queue->bottom, memory_order_relaxed); |
| 190 |
gint64 t = atomic_load_explicit (&work_stealing_queue->top, memory_order_acquire); |
| 191 |
DexWorkStealingArray *a = atomic_load_explicit (&work_stealing_queue->array, memory_order_relaxed); |
| 192 |
|
| 193 |
|
| 194 |
if (dex_work_stealing_array_capacity (a) - 1 < (b - t)) |
| 195 |
{ |
| 196 |
DexWorkStealingArray *tmp = dex_work_stealing_array_resize (a, b, t); |
| 197 |
g_ptr_array_add (work_stealing_queue->garbage, a); |
| 198 |
a = tmp; |
| 199 |
atomic_store_explicit (&work_stealing_queue->array, a, memory_order_relaxed); |
| 200 |
} |
| 201 |
|
| 202 |
dex_work_stealing_array_push (a, b, work_item); |
| 203 |
atomic_thread_fence (memory_order_release); |
| 204 |
atomic_store_explicit (&work_stealing_queue->bottom, b + 1, memory_order_relaxed); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
static inline gboolean |
| 220 |
dex_work_stealing_queue_pop (DexWorkStealingQueue *work_stealing_queue, |
| 221 |
DexWorkItem *out_work_item) |
| 222 |
{ |
| 223 |
DexWorkStealingArray *a; |
| 224 |
gboolean ret; |
| 225 |
gint64 b; |
| 226 |
gint64 t; |
| 227 |
|
| 228 |
b = atomic_load_explicit (&work_stealing_queue->bottom, memory_order_relaxed) - 1; |
| 229 |
a = atomic_load_explicit (&work_stealing_queue->array, memory_order_relaxed); |
| 230 |
atomic_store_explicit (&work_stealing_queue->bottom, b, memory_order_relaxed); |
| 231 |
atomic_thread_fence (memory_order_seq_cst); |
| 232 |
t = atomic_load_explicit (&work_stealing_queue->top, memory_order_relaxed); |
| 233 |
|
| 234 |
ret = t <= b; |
| 235 |
|
| 236 |
if (ret) |
| 237 |
{ |
| 238 |
*out_work_item = dex_work_stealing_array_pop (a, b); |
| 239 |
|
| 240 |
if (t == b) |
| 241 |
{ |
| 242 |
|
| 243 |
if (!atomic_compare_exchange_strong_explicit (&work_stealing_queue->top, |
| 244 |
&t, |
| 245 |
t + 1, |
| 246 |
memory_order_seq_cst, |
| 247 |
memory_order_relaxed)) |
| 248 |
ret = FALSE; |
| 249 |
|
| 250 |
atomic_store_explicit (&work_stealing_queue->bottom, b + 1, memory_order_relaxed); |
| 251 |
} |
| 252 |
} |
| 253 |
else |
| 254 |
{ |
| 255 |
atomic_store_explicit (&work_stealing_queue->bottom, b + 1, memory_order_relaxed); |
| 256 |
} |
| 257 |
|
| 258 |
return ret; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
static inline gboolean |
| 275 |
dex_work_stealing_queue_steal (DexWorkStealingQueue *work_stealing_queue, |
| 276 |
DexWorkItem *out_work_item) |
| 277 |
{ |
| 278 |
gint64 t; |
| 279 |
gint64 b; |
| 280 |
|
| 281 |
t = atomic_load_explicit (&work_stealing_queue->top, memory_order_acquire); |
| 282 |
atomic_thread_fence (memory_order_seq_cst); |
| 283 |
b = atomic_load_explicit (&work_stealing_queue->bottom, memory_order_acquire); |
| 284 |
|
| 285 |
if (t < b) |
| 286 |
{ |
| 287 |
DexWorkStealingArray *a = atomic_load_explicit (&work_stealing_queue->array, memory_order_consume); |
| 288 |
|
| 289 |
*out_work_item = dex_work_stealing_array_pop (a, t); |
| 290 |
|
| 291 |
if (atomic_compare_exchange_strong_explicit (&work_stealing_queue->top, |
| 292 |
&t, |
| 293 |
t + 1, |
| 294 |
memory_order_seq_cst, |
| 295 |
memory_order_relaxed)) |
| 296 |
return TRUE; |
| 297 |
} |
| 298 |
|
| 299 |
return FALSE; |
| 300 |
} |
| 301 |
|
| 302 |
static inline gint64 |
| 303 |
dex_work_stealing_queue_capacity (DexWorkStealingQueue *work_stealing_queue) |
| 304 |
{ |
| 305 |
return dex_work_stealing_array_capacity ( |
| 306 |
atomic_load_explicit (&work_stealing_queue->array, memory_order_relaxed)); |
| 307 |
} |
| 308 |
|
| 309 |
G_END_DECLS |