| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <gobject/gvaluecollector.h> |
| 24 |
|
| 25 |
#include "dex-block-private.h" |
| 26 |
#include "dex-future-private.h" |
| 27 |
#include "dex-promise.h" |
| 28 |
#include "dex-thread.h" |
| 29 |
#include "dex-thread-storage-private.h" |
| 30 |
#include "dex-waiter-private.h" |
| 31 |
|
| 32 |
typedef struct _ThreadSpawn |
| 33 |
{ |
| 34 |
DexScheduler *scheduler; |
| 35 |
DexPromise *promise; |
| 36 |
DexThreadFunc thread_func; |
| 37 |
gpointer thread_func_data; |
| 38 |
} ThreadSpawn; |
| 39 |
|
| 40 |
static DexFuture * |
| 41 |
propagate_future (DexFuture *completed, |
| 42 |
gpointer user_data) |
| 43 |
{ |
| 44 |
DexPromise *promise = user_data; |
| 45 |
const GValue *value; |
| 46 |
GError *error = NULL; |
| 47 |
|
| 48 |
if ((value = dex_future_get_value (completed, &error))) |
| 49 |
dex_promise_resolve (promise, value); |
| 50 |
else |
| 51 |
dex_promise_reject (promise, g_steal_pointer (&error)); |
| 52 |
|
| 53 |
return dex_future_new_true (); |
| 54 |
} |
| 55 |
|
| 56 |
static DexFuture * |
| 57 |
do_nothing (DexFuture *completed, |
| 58 |
gpointer user_data) |
| 59 |
{ |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
return dex_ref (completed); |
| 64 |
} |
| 65 |
|
| 66 |
static gpointer |
| 67 |
dex_trampoline_thread (gpointer data) |
| 68 |
{ |
| 69 |
ThreadSpawn *state = data; |
| 70 |
DexFuture *future; |
| 71 |
|
| 72 |
g_assert (state != NULL); |
| 73 |
g_assert (state->thread_func != NULL); |
| 74 |
g_assert (DEX_IS_SCHEDULER (state->scheduler)); |
| 75 |
g_assert (DEX_IS_PROMISE (state->promise)); |
| 76 |
|
| 77 |
future = state->thread_func (state->thread_func_data); |
| 78 |
|
| 79 |
dex_future_disown_full (dex_block_new (g_steal_pointer (&future), |
| 80 |
state->scheduler, |
| 81 |
DEX_BLOCK_KIND_FINALLY, |
| 82 |
propagate_future, |
| 83 |
dex_ref (state->promise), |
| 84 |
dex_unref), |
| 85 |
state->scheduler); |
| 86 |
|
| 87 |
dex_clear (&state->scheduler); |
| 88 |
dex_clear (&state->promise); |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
state->thread_func = NULL; |
| 94 |
state->thread_func_data = NULL; |
| 95 |
|
| 96 |
g_free (state); |
| 97 |
|
| 98 |
return NULL; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
DexFuture * |
| 128 |
dex_thread_spawn (const char *thread_name, |
| 129 |
DexThreadFunc thread_func, |
| 130 |
gpointer user_data, |
| 131 |
GDestroyNotify user_data_destroy) |
| 132 |
{ |
| 133 |
DexThreadStorage *storage; |
| 134 |
DexScheduler *scheduler; |
| 135 |
ThreadSpawn *state; |
| 136 |
DexFuture *ret; |
| 137 |
|
| 138 |
dex_return_error_if_fail (thread_func != NULL); |
| 139 |
|
| 140 |
if (thread_name == NULL) |
| 141 |
thread_name = "[dex-thread]"; |
| 142 |
|
| 143 |
storage = dex_thread_storage_peek (); |
| 144 |
|
| 145 |
if (storage == NULL) |
| 146 |
scheduler = dex_scheduler_get_default (); |
| 147 |
else |
| 148 |
scheduler = storage->scheduler; |
| 149 |
|
| 150 |
g_assert (DEX_IS_SCHEDULER (scheduler)); |
| 151 |
|
| 152 |
state = g_new0 (ThreadSpawn, 1); |
| 153 |
state->scheduler = dex_ref (scheduler); |
| 154 |
state->thread_func = thread_func; |
| 155 |
state->thread_func_data = user_data; |
| 156 |
state->promise = dex_promise_new (); |
| 157 |
|
| 158 |
dex_future_set_static_name (DEX_FUTURE (state->promise), |
| 159 |
g_intern_string (thread_name)); |
| 160 |
|
| 161 |
ret = dex_block_new (dex_ref (state->promise), |
| 162 |
scheduler, |
| 163 |
DEX_BLOCK_KIND_FINALLY, |
| 164 |
do_nothing, |
| 165 |
user_data, |
| 166 |
user_data_destroy); |
| 167 |
|
| 168 |
g_thread_unref (g_thread_new (thread_name, dex_trampoline_thread, state)); |
| 169 |
|
| 170 |
return DEX_FUTURE (ret); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
gboolean |
| 187 |
dex_thread_wait_for (DexFuture *future, |
| 188 |
GError **error) |
| 189 |
{ |
| 190 |
DexThreadStorage *storage; |
| 191 |
gboolean ret; |
| 192 |
|
| 193 |
g_return_val_if_fail (DEX_IS_FUTURE (future), FALSE); |
| 194 |
|
| 195 |
if ((storage = dex_thread_storage_peek ()) && storage->scheduler != NULL) |
| 196 |
{ |
| 197 |
g_set_error_literal (error, |
| 198 |
G_IO_ERROR, |
| 199 |
G_IO_ERROR_FAILED, |
| 200 |
"Attempt to wait for future on scheduler controlled thread. This is not allowed."); |
| 201 |
return FALSE; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
if (!dex_future_is_pending (future)) |
| 206 |
{ |
| 207 |
ret = !!dex_future_get_value (future, error); |
| 208 |
dex_unref (future); |
| 209 |
} |
| 210 |
else |
| 211 |
{ |
| 212 |
DexWaiter *waiter; |
| 213 |
|
| 214 |
|
| 215 |
waiter = dex_waiter_new (future); |
| 216 |
dex_waiter_wait (waiter); |
| 217 |
ret = !!dex_future_get_value (DEX_FUTURE (waiter), error); |
| 218 |
dex_unref (waiter); |
| 219 |
dex_unref (future); |
| 220 |
} |
| 221 |
|
| 222 |
return ret; |
| 223 |
} |