| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include <gio/gio.h> |
| 25 |
|
| 26 |
#include "dex-compat-private.h" |
| 27 |
#include "dex-error.h" |
| 28 |
#include "dex-future-private.h" |
| 29 |
#include "dex-scheduler.h" |
| 30 |
#include "dex-timeout.h" |
| 31 |
#include "dex-timeout-private.h" |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
typedef struct _DexTimeout |
| 41 |
{ |
| 42 |
DexFuture parent_instance; |
| 43 |
GSource *source; |
| 44 |
DexFuture *future; |
| 45 |
} DexTimeout; |
| 46 |
|
| 47 |
typedef struct _DexTimeoutClass |
| 48 |
{ |
| 49 |
DexFutureClass parent_class; |
| 50 |
} DexTimeoutClass; |
| 51 |
|
| 52 |
DEX_DEFINE_FINAL_TYPE (DexTimeout, dex_timeout, DEX_TYPE_FUTURE) |
| 53 |
|
| 54 |
static void |
| 55 |
dex_timeout_clear_source (DexTimeout *timeout) |
| 56 |
{ |
| 57 |
GSource *source = NULL; |
| 58 |
|
| 59 |
g_assert (DEX_IS_TIMEOUT (timeout)); |
| 60 |
|
| 61 |
dex_object_lock (timeout); |
| 62 |
source = g_steal_pointer (&timeout->source); |
| 63 |
dex_object_unlock (timeout); |
| 64 |
|
| 65 |
if (source != NULL) |
| 66 |
{ |
| 67 |
if (!g_source_is_destroyed (source)) |
| 68 |
g_source_destroy (source); |
| 69 |
|
| 70 |
g_source_unref (source); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
static void |
| 75 |
dex_timeout_discard (DexFuture *future) |
| 76 |
{ |
| 77 |
DexTimeout *timeout = DEX_TIMEOUT (future); |
| 78 |
|
| 79 |
dex_timeout_clear_source (timeout); |
| 80 |
|
| 81 |
if (timeout->future != NULL) |
| 82 |
dex_future_discard (timeout->future, future); |
| 83 |
} |
| 84 |
|
| 85 |
static gboolean |
| 86 |
dex_timeout_propagate (DexFuture *future, |
| 87 |
DexFuture *completed) |
| 88 |
{ |
| 89 |
DexTimeout *timeout = DEX_TIMEOUT (future); |
| 90 |
|
| 91 |
g_assert (DEX_IS_TIMEOUT (timeout)); |
| 92 |
g_assert (DEX_IS_FUTURE (completed)); |
| 93 |
|
| 94 |
if (completed == timeout->future) |
| 95 |
{ |
| 96 |
dex_timeout_clear_source (timeout); |
| 97 |
dex_future_complete_from (future, completed); |
| 98 |
return TRUE; |
| 99 |
} |
| 100 |
|
| 101 |
return FALSE; |
| 102 |
} |
| 103 |
|
| 104 |
static void |
| 105 |
dex_timeout_finalize (DexObject *object) |
| 106 |
{ |
| 107 |
DexTimeout *timeout = DEX_TIMEOUT (object); |
| 108 |
|
| 109 |
if (timeout->source != NULL) |
| 110 |
{ |
| 111 |
if (!g_source_is_destroyed (timeout->source)) |
| 112 |
{ |
| 113 |
g_critical ("%s destroyed while timer was active. " |
| 114 |
"This is likely a bug as no future is holding a reference to %p", |
| 115 |
DEX_OBJECT_TYPE_NAME (object), object); |
| 116 |
g_source_destroy (timeout->source); |
| 117 |
} |
| 118 |
|
| 119 |
g_clear_pointer (&timeout->source, g_source_unref); |
| 120 |
} |
| 121 |
|
| 122 |
dex_clear (&timeout->future); |
| 123 |
|
| 124 |
DEX_OBJECT_CLASS (dex_timeout_parent_class)->finalize (object); |
| 125 |
} |
| 126 |
|
| 127 |
static void |
| 128 |
dex_timeout_class_init (DexTimeoutClass *timeout_class) |
| 129 |
{ |
| 130 |
DexObjectClass *object_class = DEX_OBJECT_CLASS (timeout_class); |
| 131 |
DexFutureClass *future_class = DEX_FUTURE_CLASS (timeout_class); |
| 132 |
|
| 133 |
object_class->finalize = dex_timeout_finalize; |
| 134 |
|
| 135 |
future_class->propagate = dex_timeout_propagate; |
| 136 |
future_class->discard = dex_timeout_discard; |
| 137 |
} |
| 138 |
|
| 139 |
static void |
| 140 |
dex_timeout_init (DexTimeout *timeout) |
| 141 |
{ |
| 142 |
} |
| 143 |
|
| 144 |
static void |
| 145 |
clear_weak_ref (gpointer data) |
| 146 |
{ |
| 147 |
dex_weak_ref_clear (data); |
| 148 |
g_free (data); |
| 149 |
} |
| 150 |
|
| 151 |
static gboolean |
| 152 |
dex_timeout_source_func (gpointer data) |
| 153 |
{ |
| 154 |
DexWeakRef *wr = data; |
| 155 |
DexTimeout *timeout = dex_weak_ref_get (wr); |
| 156 |
|
| 157 |
g_assert (!timeout || DEX_IS_TIMEOUT (timeout)); |
| 158 |
|
| 159 |
if (timeout != NULL) |
| 160 |
{ |
| 161 |
dex_future_complete (DEX_FUTURE (timeout), |
| 162 |
NULL, |
| 163 |
g_error_new_literal (DEX_ERROR, |
| 164 |
DEX_ERROR_TIMED_OUT, |
| 165 |
"Operation timed out")); |
| 166 |
|
| 167 |
if (timeout->future != NULL) |
| 168 |
dex_future_discard (timeout->future, DEX_FUTURE (timeout)); |
| 169 |
|
| 170 |
dex_object_lock (timeout); |
| 171 |
g_clear_pointer (&timeout->source, g_source_unref); |
| 172 |
dex_object_unlock (timeout); |
| 173 |
|
| 174 |
dex_unref (timeout); |
| 175 |
} |
| 176 |
|
| 177 |
return G_SOURCE_REMOVE; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
DexFuture * |
| 189 |
dex_timeout_new_deadline (gint64 deadline) |
| 190 |
{ |
| 191 |
static const char *name; |
| 192 |
DexScheduler *scheduler; |
| 193 |
DexTimeout *timeout; |
| 194 |
DexWeakRef *wr; |
| 195 |
|
| 196 |
if G_UNLIKELY (name == NULL) |
| 197 |
name = g_intern_static_string ("[dex-timeout]"); |
| 198 |
|
| 199 |
timeout = (DexTimeout *)dex_object_create_instance (DEX_TYPE_TIMEOUT); |
| 200 |
|
| 201 |
wr = g_new0 (DexWeakRef, 1); |
| 202 |
dex_weak_ref_init (wr, timeout); |
| 203 |
|
| 204 |
timeout->source = g_timeout_source_new (0); |
| 205 |
g_source_set_ready_time (timeout->source, deadline); |
| 206 |
_g_source_set_static_name (timeout->source, name); |
| 207 |
g_source_set_priority (timeout->source, G_PRIORITY_DEFAULT); |
| 208 |
g_source_set_callback (timeout->source, |
| 209 |
dex_timeout_source_func, |
| 210 |
wr, clear_weak_ref); |
| 211 |
|
| 212 |
if (!(scheduler = dex_scheduler_get_thread_default ())) |
| 213 |
scheduler = dex_scheduler_get_default (); |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
g_source_attach (timeout->source, dex_scheduler_get_main_context (scheduler)); |
| 229 |
|
| 230 |
return DEX_FUTURE (timeout); |
| 231 |
} |
| 232 |
|
| 233 |
DexFuture * |
| 234 |
_dex_timeout_new_for_future_deadline (DexFuture *future, |
| 235 |
gint64 deadline) |
| 236 |
{ |
| 237 |
DexTimeout *timeout; |
| 238 |
|
| 239 |
g_return_val_if_fail (DEX_IS_FUTURE (future), NULL); |
| 240 |
|
| 241 |
timeout = DEX_TIMEOUT (dex_timeout_new_deadline (deadline)); |
| 242 |
timeout->future = g_steal_pointer (&future); |
| 243 |
dex_future_chain (timeout->future, DEX_FUTURE (timeout)); |
| 244 |
|
| 245 |
return DEX_FUTURE (timeout); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
DexFuture * |
| 257 |
dex_timeout_new_seconds (int seconds) |
| 258 |
{ |
| 259 |
gint64 usec = (gint64) G_USEC_PER_SEC * seconds; |
| 260 |
return dex_timeout_new_deadline (g_get_monotonic_time () + usec); |
| 261 |
} |
| 262 |
|
| 263 |
DexFuture * |
| 264 |
_dex_timeout_new_for_future_seconds (DexFuture *future, |
| 265 |
int seconds) |
| 266 |
{ |
| 267 |
gint64 usec = (gint64) G_USEC_PER_SEC * seconds; |
| 268 |
|
| 269 |
g_return_val_if_fail (DEX_IS_FUTURE (future), NULL); |
| 270 |
|
| 271 |
return _dex_timeout_new_for_future_deadline (future, g_get_monotonic_time () + usec); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
DexFuture * |
| 283 |
dex_timeout_new_msec (int msec) |
| 284 |
{ |
| 285 |
gint64 usec = (G_USEC_PER_SEC/1000L) * msec; |
| 286 |
return dex_timeout_new_deadline (g_get_monotonic_time () + usec); |
| 287 |
} |
| 288 |
|
| 289 |
DexFuture * |
| 290 |
_dex_timeout_new_for_future_msec (DexFuture *future, |
| 291 |
int msec) |
| 292 |
{ |
| 293 |
gint64 usec = (G_USEC_PER_SEC/1000L) * msec; |
| 294 |
|
| 295 |
g_return_val_if_fail (DEX_IS_FUTURE (future), NULL); |
| 296 |
|
| 297 |
return _dex_timeout_new_for_future_deadline (future, g_get_monotonic_time () + usec); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
DexFuture * |
| 309 |
dex_timeout_new_usec (gint64 usec) |
| 310 |
{ |
| 311 |
return dex_timeout_new_deadline (g_get_monotonic_time () + usec); |
| 312 |
} |
| 313 |
|
| 314 |
DexFuture * |
| 315 |
_dex_timeout_new_for_future (DexFuture *future, |
| 316 |
gint64 usec) |
| 317 |
{ |
| 318 |
g_return_val_if_fail (DEX_IS_FUTURE (future), NULL); |
| 319 |
|
| 320 |
return _dex_timeout_new_for_future_deadline (future, g_get_monotonic_time () + usec); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
void |
| 333 |
dex_timeout_postpone_until (DexTimeout *timeout, |
| 334 |
gint64 deadline) |
| 335 |
{ |
| 336 |
g_return_if_fail (DEX_IS_TIMEOUT (timeout)); |
| 337 |
|
| 338 |
dex_object_lock (timeout); |
| 339 |
if (timeout->source != NULL) |
| 340 |
g_source_set_ready_time (timeout->source, deadline); |
| 341 |
dex_object_unlock (timeout); |
| 342 |
} |